SKY-D204 · Security in code
Unsafe pickle deserialization
Python's pickle format stores instructions for rebuilding objects, and loading a pickle carries them out, including calls to any importable function. SKY-D204 reports code that loads a pickle from a file or stream, and the matching object loaders in Java, Kotlin and PHP.
- Languages
- Python, Java, Kotlin, PHP
- Quality gate
- counted toward the gate limits; a CRITICAL finding fails the gate on its own (full and diff scans)
- Finding type
- review
Why it matters
A crafted pickle file runs os.system(...) the moment it is loaded, with no bug in your code needed. An "import your settings" upload or a model file downloaded from the internet is enough to take over the server.
What Skylos flags
import pickle from flask import Flask, request app = Flask(__name__) @app.post("/import")def import_session(): upload = request.files["session"] state = pickle.load(upload.stream) return {"keys": sorted(state)}What Skylos leaves alone
import builtinsimport ioimport pickle SAFE_BUILTINS = {"dict", "list", "str", "int", "float", "bool", "set", "tuple"} class RestrictedUnpickler(pickle.Unpickler): def find_class(self, module, name): if module == "builtins" and name in SAFE_BUILTINS: return getattr(builtins, name) raise pickle.UnpicklingError(f"global '{module}.{name}' is forbidden") def load_session(blob: bytes) -> dict: return RestrictedUnpickler(io.BytesIO(blob)).load()Why Skylos leaves it alone
The subclass overrides find_class, the documented hook that decides which functions and classes a pickle may load. Skylos does not report a .load() on an unpickler subclass that defines find_class.
More languages
PHP
Flagged
<?php $cart = unserialize($_COOKIE['cart'] ?? 'a:0:{}');echo count($cart);Not flagged
<?php $cart = unserialize($_COOKIE['cart'] ?? 'a:0:{}', ['allowed_classes' => false]);echo count($cart);Why Skylos leaves it alone
With allowed_classes => false, PHP turns every object in the data into an inert __PHP_Incomplete_Class, so no class's magic methods run. An array of allowed class names is accepted too.
How Skylos decides
- Python: resolves call names through imports and assignments and matches
pickle.load,cloudpickle.load,pandas.read_pickleandpickle.Unpickler(...).load(). A.load()on a module class that subclassespickle.Unpicklerwithout definingfind_classcounts aspickle.Unpickler.load. - Skips an unpickler subclass that defines
find_class. It does not check what that method allows. - Reports every other match as CRITICAL. There is no constant or trusted-path exception: the file's origin is not traced.
pickle.loads()on bytes is a separate rule, SKY-D205, and model loaders such astorch.loadare SKY-D265.- Java and Kotlin: reports
new ObjectInputStream(...)as CRITICAL when request or network data feeds it, HIGH otherwise, and MEDIUM when the stream has anObjectInputFilter. - PHP: reports
unserialize()of request data (CRITICAL) unless the second argument setsallowed_classestofalseor to an array.
How to fix it
Store user-supplied state in a data-only format such as JSON. If you must read pickles, only read ones your own service wrote and signed, or restrict find_class to plain built-in types.
Removed: import pickleAdded: import json from flask import Flask, request app = Flask(__name__) @app.post("/import")def import_session(): upload = request.files["session"]Removed: state = pickle.load(upload.stream)Added: state = json.load(upload.stream) return {"keys": sorted(state)}When it can be wrong
- Loading a pickle your own process wrote to a private cache directory is reported too; Skylos does not prove where a file came from.
- An unpickler subclass whose
find_classallows everything is not reported, because only the method's presence is checked.
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 state = pickle.load(handle) # skylos: ignore[SKY-D204] file written by our own workerAccept existing findings with
skylos baseline ., then scan with--baseline.[tool.skylos] ignore = ["SKY-D204"]turns the rule off for the whole project.