Interactive Playground
Experiment with the vulnerable code and security rule below. Edit the code to see how the rule detects different vulnerability patterns.
pathfinder scan --ruleset python/PYTHON-LANG-SEC-041 --project .About This Rule
Understanding the vulnerability and how it is detected
PyYAML's yaml.load() function, when called without an explicit Loader argument or with Loader=yaml.Loader/yaml.UnsafeLoader, can instantiate arbitrary Python objects during parsing using YAML's !!python/object and !!python/object/apply tags. This enables remote code execution when processing YAML from untrusted sources.
The vulnerability is triggered by YAML content such as: !!python/object/apply:os.system ["id"] or more sophisticated payloads using subprocess or socket. PyYAML versions before 5.1 used the unsafe loader by default; since 5.1 a warning is issued unless an explicit Loader is provided.
The safe alternative is yaml.safe_load() or yaml.load(data, Loader=yaml.SafeLoader), which only processes YAML scalars, sequences, and mappings without instantiating Python objects.
Security Implications
Potential attack scenarios if this vulnerability is exploited
Remote Code Execution via !!python Tags
The !!python/object/apply tag in YAML invokes arbitrary Python callables. An attacker who can control YAML input can execute os.system(), subprocess.Popen(), or any other callable, achieving full RCE with a single YAML document.
Configuration File Injection
Applications that load YAML configuration files and process them with yaml.load() are vulnerable if an attacker can modify the configuration file, inject content through environment variable expansion, or write to the configuration directory.
API and Webhook Payload Injection
REST APIs, CI/CD pipelines, and infrastructure-as-code tools that accept YAML input from users and parse it with yaml.load() are directly exploitable. This is a common vector in DevOps tooling.
Kubernetes and Helm Chart Injection
Tools that process Kubernetes manifests or Helm chart values using PyYAML's unsafe loader can be exploited through crafted chart values or manifest files submitted by unprivileged users.
How to Fix
Recommended remediation steps
- 1Replace all yaml.load() calls with yaml.safe_load() or yaml.load(data, Loader=yaml.SafeLoader).
- 2Never use yaml.unsafe_load() or yaml.load() with yaml.Loader/yaml.UnsafeLoader on external input.
- 3If custom Python objects must be serialized to YAML, use explicit schema validation rather than relying on YAML's !!python tags.
- 4Audit all YAML parsing in CI/CD pipelines, configuration loaders, and API endpoints that accept YAML input.
- 5Consider restricting YAML features to a safe subset (scalars, sequences, mappings) by using yaml.safe_load() universally.
Detection Scope
How Code Pathfinder analyzes your code for this vulnerability
This rule detects calls to yaml.load() without an explicit Loader argument or with unsafe Loader values, and calls to yaml.unsafe_load(). The rule flags these patterns as they enable Python object instantiation during YAML parsing.
Compliance & Standards
Industry frameworks and regulations that require detection of this vulnerability
References
External resources and documentation
Similar Rules
Explore related security rules for Python
Pickle Deserialization of Untrusted Data
pickle.loads() and pickle.load() execute arbitrary Python code during deserialization. Never unpickle data from untrusted sources.
ruamel.yaml Unsafe Loader Configuration
ruamel.yaml configured with typ='unsafe' can instantiate arbitrary Python objects during YAML parsing. Use typ='safe' or the default round-trip loader.
jsonpickle Deserialization Detected
jsonpickle.decode() can execute arbitrary Python code during deserialization. Use the standard json module for untrusted data.
Frequently Asked Questions
Common questions about PyYAML Unsafe Load Function
New feature
Get these findings posted directly on your GitHub pull requests
The PyYAML Unsafe Load Function rule runs in CI and posts inline review comments on the exact lines — no dashboard, no SARIF viewer.