SKY-D205 · Security in code
Unsafe pickle.loads deserialization
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
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
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
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
- Resolves call names through imports and assignments, including
pickle.loadsstored in another name, and matchespickle.loadsandcloudpickle.loads. - Skips a
pickle.loads(x)whosexis the result ofpickle.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. - 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. 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.
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 whenobjis 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
# noqadoes not hide a CRITICAL security finding.Python return pickle.loads(blob) # skylos: ignore[SKY-D205] blob is HMAC-checked aboveAccept existing findings with
skylos baseline ., then scan with--baseline.[tool.skylos] ignore = ["SKY-D205"]turns the rule off for the whole project.