Using yaml.unsafe_load() with Untrusted Data
Directly using yaml.unsafe_load() which explicitly allows arbitrary Python object construction.
Remote code execution (RCE) vulnerability where yaml.unsafe_load or yaml.load with unsafe loaders processes untrusted YAML constructors, allowing arbitrary code execution during deserialization. YAML's ability to represent complex Python objects makes it vulnerable to deserialization attacks where attackers can craft YAML documents containing malicious Python object constructors that execute when parsed.
import yaml
from flask import Flask, request
app = Flask(__name__)
# VULNERABLE: unsafe_load allows code execution
@app.route('/config', methods=['POST'])
def upload_config():
yaml_content = request.data.decode('utf-8')
# DANGEROUS: can execute arbitrary code
config = yaml.unsafe_load(yaml_content)
# Also dangerous:
# config = yaml.load(yaml_content, Loader=yaml.Loader)
return {'status': 'uploaded'}
# Example malicious YAML that executes code:
# !!python/object/apply:os.system
# - 'rm -rf /'import yaml
from flask import Flask, request
app = Flask(__name__)
# SECURE: safe_load prevents code execution
@app.route('/config', methods=['POST'])
def upload_config():
yaml_content = request.data.decode('utf-8')
# SAFE: only allows standard YAML tags
config = yaml.safe_load(yaml_content)
# Validate structure
if not isinstance(config, dict):
return {'error': 'Invalid format'}, 400
return {'status': 'uploaded'}The vulnerable code uses yaml.unsafe_load() which allows YAML to instantiate arbitrary Python objects, enabling remote code execution. The secure version uses yaml.safe_load() which only constructs standard YAML objects (strings, numbers, lists, dicts) and prevents code execution.
Directly using yaml.unsafe_load() which explicitly allows arbitrary Python object construction.
Sourcery automatically identifies remote code execution via unsafe yaml deserialization in pyyaml and many other security issues in your codebase.