Use Safe YAML Loading for External Configuration
Parsing untrusted YAML with a full loader can execute arbitrary code. Use safe_load for external input and validate the result afterward.
15 Oct 2025, 20:06 UTC

The problem: a config file that runs code
YAML is not just a data format. A full YAML loader can honor tags that tell the parser to instantiate application objects, call constructors, or invoke functions. If your service accepts YAML from a user, a webhook, or an uploaded file and passes it to an unrestricted loader, a malicious document can trigger remote code execution before your application logic ever runs.
This behavior is part of YAML’s parsing model, not an optional extension. The safer default is to treat every external YAML document as untrusted and parse it with a safe loader.
What a safe loader blocks
A safe loader restricts construction to basic YAML types: mappings, sequences, strings, integers, floats, and booleans. It rejects custom tags and application-specific object instantiation. Many libraries expose this as a dedicated entry point—commonly safe_load—alongside a full loader meant only for trusted internal schemas. Exact function names vary by language and library, so check your runtime’s documentation.
Use the full loader only when you control both the producer and consumer of the YAML, such as internal feature flags stored in a private repository. For anything arriving over the network or from a user, choose the safe entry point.
Worked example: tagged YAML under two loaders
Run this in a Python REPL or a local script. No special permissions are required to parse, but do not process untrusted files with a privileged account.
import yaml
untrusted_yaml = """
server:
host: example.com
port: 8080
!!custom/connection: secret
"""
# Full loader: may honor the custom tag depending on registered constructors.
# Do not use this path for input you do not trust.
try:
full_result = yaml.load(untrusted_yaml, Loader=yaml.FullLoader)
print("Full loader parsed:", full_result)
except Exception as exc:
print("Full loader rejected:", type(exc).__name__)
# Safe loader: permits basic types only; custom tags cause an error.
try:
safe_result = yaml.safe_load(untrusted_yaml)
print("Safe loader parsed:", safe_result)
except Exception as exc:
print("Safe loader rejected:", type(exc).__name__)
Expected check: the safe loader should reject the document when it encounters !!custom/connection, while the full loader may construct an object or raise depending on registered tags. Never assume the safe loader silently drops unknown tags; it rejects them.
Risk: running the full-loader branch against attacker-controlled input can execute code if a dangerous constructor is registered. Remove or comment that branch before deploying code that handles external files.
Limitations: safe loading is not validation
Safe loading prevents object graph construction, but it does not enforce your schema. After parsing, you still need to check required keys, value types, allowed ranges, and nesting depth. A safe-loaded document can still contain extremely deep mappings or unexpected keys that crash downstream logic.
YAML anchors and aliases also work under safe loading. They create shared references, so mutating one aliased structure can mutate another. Invisible whitespace changes can reorder or collapse mappings because YAML is indentation-sensitive.
Practical verification after parsing
- Parse the external document with
safe_load. - Confirm the result is a mapping or sequence as expected.
- Check required keys and coerce or reject unexpected types.
- Reject documents that exceed a maximum key count or nesting depth.
If the parser raises, log the rejection and return an error to the caller. Do not fall back to a full loader because the safe loader failed.
Closing
Treat YAML as executable configuration until proven otherwise. Parse external documents with a safe loader, then validate the resulting data structures against your schema. Reserve the full loader for trusted, internal-only schemas where you control every producer.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.