Skip to main content

SKY-D205 · Security in code

Unsafe pickle.loads deserialization

CriticalCWE-502

pickle.loads() rebuilds Python objects from bytes, and the bytes can tell it to call any importable function while doing so. SKY-D205 reports pickle.loads() and cloudpickle.loads() calls on bytes that are not proven to be plain data this code just pickled.

Languages
Python
Quality gate
counted toward the gate limits; a CRITICAL finding fails the gate on its own (full and diff scans). A pickle.loads() of fixed bytes 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

Bytes from a cache, a message queue or a cookie are only as safe as everyone who can write there. Anyone who can put a crafted value into Redis can run commands on every worker that loads it.

What Skylos flags

app/reports.py · PythonReported on line 12
import pickle import redis cache = redis.Redis()  def get_report(report_id: str):    blob = cache.get(f"report:{report_id}")    if blob is None:        return None    return pickle.loads(blob)

What Skylos leaves alone

app/health.py · Python
import pickle  def pickle_self_check() -> bool:    """Startup check: this worker's pickle protocol round-trips plain data."""    blob = pickle.dumps({"job": "ping", "attempt": 1}, protocol=5)    return pickle.loads(blob) == {"job": "ping", "attempt": 1}

Why Skylos leaves it alone

blob was produced a line earlier by pickle.dumps() of a dict made only of literals, so it cannot hold anything but that data. Skylos proves such local round-trips and does not report them.

How Skylos decides

  1. Resolves call names through imports and assignments, including pickle.loads stored in another name, and matches pickle.loads and cloudpickle.loads.
  2. Skips a pickle.loads(x) whose x is the result of pickle.dumps() on built-in literal data (strings, numbers, lists, dicts, tuples of those) in the same function, with no extra arguments. A mutable container stored in a variable first, a parameter, an attribute or a loop breaks the proof.
  3. Reports everything else as CRITICAL. When the bytes passed to pickle.loads() are fixed literals, the finding is marked low-risk: hidden from the default list, still in JSON output and counted by the gate.
  4. pickle.load() on a file or stream is SKY-D204.

How to fix it

Cache data in a format that cannot run code, such as JSON. If you need pickle for speed, sign the bytes with an HMAC when writing and check the signature before loading.

app/reports.py · Python · the fix, against the flagged file
Removed: import pickleAdded: import json import redis cache = redis.Redis()  def get_report(report_id: str):    blob = cache.get(f"report:{report_id}")    if blob is None:        return NoneRemoved:     return pickle.loads(blob)Added:     return json.loads(blob)

When it can be wrong

  • A cache written and read only by your own service is still reported: Skylos cannot see who else can write to Redis or disk.
  • pickle.loads(pickle.dumps(obj)) used as a deep copy is reported when obj is a parameter or an object of unknown type, because such objects can define their own pickling hooks.

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 CRITICAL security finding.

    Python
    return pickle.loads(blob)  # skylos: ignore[SKY-D205] blob is HMAC-checked above
  • Accept existing findings with skylos baseline ., then scan with --baseline.

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

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