Pickle is code, not data: scanning your Python and ML models for unsafe deserialization
The problem in six lines This is code execution, even though it does not look like it: import pickle with open("model.pkl", "rb") as f: model = pickle.load(f) # runs whatever the file's __reduce__ says A
The problem in six lines
This is code execution, even though it does not look like it:
import pickle
with open("model.pkl", "rb") as f:
model = pickle.load(f) # runs whatever the file's __reduce__ says
A pickle file is a program for a small stack-based virtual machine. When you unpickle a file you did not create yourself, you run whatever opcodes the author put in it, including a REDUCE to os.system. This is documented behaviour, not a bug in pickle, which is exactly why it keeps biting people: the risk sits in every shared checkpoint, dataset, and joblib file, and in the ordinary-looking loader calls scattered through a codebase.
It is not theoretical. CVE-2026-57845 (published 23 Oct 2026, CVSS 8.8): Hugging Face datasets through 5.1.0, the latest release, routes load_dataset("pandas", data_files=...) to pandas.read_pickle(). A crafted pickle runs arbitrary Python with the victim's privileges the moment the dataset loads. The upstream fix (huggingface/datasets#8879) disables the pandas builder, but no patched release exists yet.
I built pickle-sentry to find both sides of this: the unsafe loaders in your code, and the payloads in the files you are about to load.
What it catches
In code (an AST scan; your code is parsed, never imported):
-
pickle.load/loads,dill,cloudpickle,marshal,shelve,joblib.load pandas.read_pickle(...)-
torch.load(...)withweights_only=False, or with no explicitweights_onlyat all -
yaml.load(...)without a SafeLoader numpy.load(..., allow_pickle=True)-
load_dataset("pandas", ...), flagged critical as CVE-2026-57845
In data files, the distinction that matters is invoked versus referenced. A pickle that mentions os.system in a global is suspicious; a pickle whose REDUCE opcode actually calls it is a live payload. pickle-sentry runs an abstract interpreter over the pickletools disassembly, tracking a symbolic stack through REDUCE, NEWOBJ, the memo, and nested pickle streams embedded in bytes constants. That is how it separates the two:
-
PICKLE-DANGEROUS-REDUCE(critical): the stream actually invokesos.system,subprocess.*,eval,exec, and similar. -
PICKLE-DANGEROUS-GLOBAL(high/medium): a dangerous global is referenced but never invoked. -
PICKLE-CONTAINER-OPAQUE(high): a 7-Zip archive, which the standard library cannot inspect. Compressed wrappers (gzip, bzip2, xz, zlib) and zip/tar members are peeled and scanned first.
It also covers .npy object-array payloads and .npz members, .keras Lambda layers (marshalled bytecode that runs at load time), and header checks for safetensors and GGUF. And it can scan a Hugging Face Hub repo remotely before you download anything for real:
pickle-sentry hub user/repo --repo-type model
One property I was not willing to compromise on: the scanner never executes the files it inspects. The test suite builds a real malicious pickle (__reduce__ to os.system) and asserts two things: it is flagged critical, and the payload's canary file never appears on disk.
Using it
pip install pickle-sentry
pickle-sentry . # scan code + pickle files in a project
pickle-sentry . --fail-on high # CI gate: exit 1 on high or above
Zero dependencies, Python 3.9+, 60 tests. Output formats are text, JSON, and SARIF for GitHub code scanning.
As a GitHub Action:
- uses: Sun-Wukhan/[email protected]
with:
path: .
fail-on: high
It is also listed on the GitHub Marketplace. As a pre-commit hook:
- repo: https://github.com/Sun-Wukhan/pickle-sentry
rev: v0.3.1
hooks:
- id: pickle-sentry
Where it fits
Use pickle-sentry alongside a model-file scanner, not instead of one. Fickling decompiles pickles back to readable Python; pickle-sentry does not. What it adds is the loader and supply-chain surface: your code, the packages you are about to install, and the Hub repo you are about to load.
Two real runs are written up in the repo docs: scanning huggingface/datasets produced exactly one production finding, the CVE sink itself at packaged_modules/pandas/pandas.py:56; scanning scikit-learn found its findings concentrated in dataset loaders that unpickle cached copies of remotely fetched datasets. That second pattern is the one I see everywhere, and it is invisible if you only scan model files.
The longer-term fix is migration: Parquet, CSV, JSON, or Arrow for datasets, weights_only=True for torch, safetensors where you can. Until a codebase gets there, a scanner that fails CI on a new unsafe loader is a reasonable seatbelt.
- Repo: https://github.com/Sun-Wukhan/pickle-sentry
- PyPI: https://pypi.org/project/pickle-sentry/
- Marketplace: https://github.com/marketplace/actions/pickle-sentry
False positives and missed loaders are the feedback I most want. Issues are open.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.