Skip to content

harden: use yaml.safe_load in daily_crawl_arxiv.py (gitlab.bandit.B506) - #2

Open
anupamme wants to merge 1 commit into
leelige:masterfrom
anupamme:fix-repo-article-yaml-safe-load-b506
Open

harden: use yaml.safe_load in daily_crawl_arxiv.py (gitlab.bandit.B506)#2
anupamme wants to merge 1 commit into
leelige:masterfrom
anupamme:fix-repo-article-yaml-safe-load-b506

Conversation

@anupamme

Copy link
Copy Markdown

Summary

Harden input handling in daily_crawl_arxiv.py (flagged by semgrep).

Vulnerability

Field Value
ID gitlab.bandit.B506
Severity HIGH
Scanner semgrep
Rule gitlab.bandit.B506
File daily_crawl_arxiv.py:40
Assessment Defensive hardening

Description: The application was found using an unsafe version of yaml load which is vulnerable to
deserialization attacks. Deserialization attacks exploit the process of reading serialized
data and turning it back
into an object. By constructing malicious objects and serializing them, an adversary may
attempt to:

  • Inject code that is executed upon object construction, which occurs during the
    deserialization process.
  • Exploit mass assignment by including fields that are not normally a part of the serialized
    data but are read in during deserialization.

To remediate this issue, use safe_load() or call yaml.load() with the Loader argument
set to
yaml.SafeLoader.

Example loading YAML using safe_load:

import yaml

# Use safe_load to load data into an intermediary object
intermediary_object = yaml.safe_load("""user:
    name: 'test user'"""
)
# Create our real object, copying over only the necessary fields
user_object = {'user': {
        # Assign the deserialized data from intermediary object
        'name': intermediary_object['user']['name'],
        # Add in protected data in object definition (or set it from a class constructor)
        'is_admin': False,
    }
}
# Work with user_object
# ...

For more details on deserialization attacks in general, see OWASP's guide:

Threat Model Context

This is a CLI tool - exploitation requires the attacker to control arguments or input files passed to the tool.

Changes

  • daily_crawl_arxiv.py

Behavior Preservation

The change is scoped to 1 file on the vulnerable path; it only tightens handling of untrusted input and leaves valid inputs unaffected.


This patch removes an exploit primitive — a code pattern that, while not independently exploitable today, could be chained with other weaknesses by automated exploit-development tooling. Proactive removal of such primitives raises the bar against increasingly capable automated attack tools.


Automated security fix by OrbisAI Security

Automated security fix generated by OrbisAI Security
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant