Skip to main content

SKY-D206 · Security in code

Unsafe YAML load

Ruby findings with a proven input source are HIGH. Other resolved nonliteral Ruby unsafe loads are MEDIUM review findings.

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
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

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

deploy/config.py · PythonReported on line 8
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

deploy/config.py · Python
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

  1. 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_all and ruamel's YAML(typ="unsafe").
  2. For yaml.load and yaml.load_all, skips the call when the Loader (keyword or second argument) is PyYAML's own SafeLoader, CSafeLoader, BaseLoader or CBaseLoader, imported or reached as yaml.SafeLoader, or a local subclass Skylos can prove only adds data-only constructors. A call with no Loader is reported.
  3. A name such as SafeLoader only counts when nothing in the module rebinds it: SafeLoader = yaml.UnsafeLoader keeps the finding, while getattr(yaml, "CSafeLoader", yaml.SafeLoader) is accepted.
  4. yaml.unsafe_load, yaml.full_load and their _all forms are always reported, because their loaders can build Python objects.
  5. 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.
  6. In Ruby, reports a resolved unsafe_load call 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.

deploy/config.py · Python · the fix, against the flagged file
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 # noqa does not hide a HIGH security finding.

    Python
    return yaml.load(handle, Loader=yaml.Loader)  # skylos: ignore[SKY-D206] own repo
  • Accept existing findings with skylos baseline ., then scan with --baseline.

  • [tool.skylos] ignore = ["SKY-D206"] turns the rule off for the whole project.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.