SKY-D206 · Security in code
Unsafe YAML load
SKY-D206 reports Python YAML loads that use or may use a loader outside the supported data-only set. In Ruby, it recognizes explicit YAML.unsafe_load and Psych.unsafe_load calls on resolved library receivers.
- Languages
- Python, Ruby
- Quality gate
- counted toward the gate limits (full and diff scans). A load of fixed YAML text is hidden from the default list but still counted.
- Finding type
- review
Why it matters
An unsafe object loader can instantiate objects and invoke application code while reading YAML. Files, request bodies, and command-line input can come from people who should only be able to supply data.
What Skylos flags
from pathlib import Path import yaml def read_deploy_config(checkout: Path) -> dict: with open(checkout / ".deploy.yml") as handle: return yaml.load(handle, Loader=yaml.Loader)What Skylos leaves alone
from pathlib import Path import yaml def read_deploy_config(checkout: Path) -> dict: with open(checkout / ".deploy.yml") as handle: return yaml.load(handle, Loader=yaml.SafeLoader)Why Skylos leaves it alone
yaml.SafeLoader only builds strings, numbers, lists and dicts; a !!python/... tag makes it raise an error instead of calling anything. Skylos accepts PyYAML's own SafeLoader, CSafeLoader, BaseLoader and CBaseLoader.
How Skylos decides
- Resolves call names through imports and matches
yaml.load,yaml.load_all,yaml.unsafe_load,yaml.unsafe_load_all,yaml.full_load,yaml.full_load_alland ruamel'sYAML(typ="unsafe"). - For
yaml.loadandyaml.load_all, skips the call when theLoader(keyword or second argument) is PyYAML's ownSafeLoader,CSafeLoader,BaseLoaderorCBaseLoader, imported or reached asyaml.SafeLoader, or a local subclass Skylos can prove only adds data-only constructors. A call with noLoaderis reported. - A name such as
SafeLoaderonly counts when nothing in the module rebinds it:SafeLoader = yaml.UnsafeLoaderkeeps the finding, whilegetattr(yaml, "CSafeLoader", yaml.SafeLoader)is accepted. yaml.unsafe_load,yaml.full_loadand their_allforms are always reported, because their loaders can build Python objects.- Reports a match as HIGH. When the YAML text itself is a fixed literal, the finding is marked low-risk: hidden from the default list, still in JSON output and counted by the gate.
- In Ruby, reports a resolved
unsafe_loadcall with proven input as HIGH and a nonliteral value with unknown provenance as a MEDIUM review. Literal data and lookalike receivers stay quiet.
How to fix it
Use yaml.safe_load() (or Loader=yaml.SafeLoader). If the file needs custom types, register constructors on a SafeLoader subclass that build plain data. In Ruby, use YAML.safe_load or Psych.safe_load and permit additional classes only when required.
from pathlib import Path import yaml def read_deploy_config(checkout: Path) -> dict: with open(checkout / ".deploy.yml") as handle:Removed: return yaml.load(handle, Loader=yaml.Loader)Added: return yaml.safe_load(handle)When it can be wrong
- A config file that only your own team edits is reported too; Skylos does not trace where the file came from.
- A loader passed in through a variable or parameter is reported even when callers always pass
SafeLoader, because the loader cannot be proved at the call.
Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.
How to silence it
Mark a reviewed call on its own line. A bare
# noqadoes not hide a HIGH security finding.Python return yaml.load(handle, Loader=yaml.Loader) # skylos: ignore[SKY-D206] own repoAccept existing findings with
skylos baseline ., then scan with--baseline.[tool.skylos] ignore = ["SKY-D206"]turns the rule off for the whole project.