Skip to main content

SKY-D204 · Security in code

Unsafe pickle deserialization

CriticalCWE-502

Python findings are always CRITICAL. Java and Kotlin ObjectInputStream is CRITICAL for request or network data, HIGH otherwise and MEDIUM when a class filter is set. PHP unserialize() is reported (CRITICAL) only for request data without an allowed_classes restriction.

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

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

app/sessions.py · PythonReported on line 11
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

app/sessions.py · Python
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

public/cart.php · PHPReported on line 3
<?php $cart = unserialize($_COOKIE['cart'] ?? 'a:0:{}');echo count($cart);

Not flagged

public/cart.php · PHP
<?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

  1. Python: resolves call names through imports and assignments and matches pickle.load, cloudpickle.load, pandas.read_pickle and pickle.Unpickler(...).load(). A .load() on a module class that subclasses pickle.Unpickler without defining find_class counts as pickle.Unpickler.load.
  2. Skips an unpickler subclass that defines find_class. It does not check what that method allows.
  3. Reports every other match as CRITICAL. There is no constant or trusted-path exception: the file's origin is not traced.
  4. pickle.loads() on bytes is a separate rule, SKY-D205, and model loaders such as torch.load are SKY-D265.
  5. Java and Kotlin: reports new ObjectInputStream(...) as CRITICAL when request or network data feeds it, HIGH otherwise, and MEDIUM when the stream has an ObjectInputFilter.
  6. PHP: reports unserialize() of request data (CRITICAL) unless the second argument sets allowed_classes to false or 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.

app/sessions.py · Python · the fix, against the flagged file
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_class allows 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 # noqa does not hide a CRITICAL security finding.

    Python
    state = pickle.load(handle)  # skylos: ignore[SKY-D204] file written by our own worker
  • Accept existing findings with skylos baseline ., then scan with --baseline.

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

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