AI Hints
Home›MLOps Tutorial›Security and Privacy Deep Dive
MLOps · Lesson 64 of 80

The Danger in Model Files

Why pickle is unsafe.

⏱ 2 min read▶ Video at 2:37:15🎓 Free
Watch this lesson in the MLOps courseWatch on YouTube ↗Full MLOps course

A model file looks like harmless data. But some formats are really programs. Loading one can run code, on your machine, with your permissions. This is one of the most overlooked risks in machine learning, because sharing models from the internet is so common. In this lesson, we demonstrate the problem safely, and see the cure.

The Danger in Model Files
From the video: The Danger in Model Files
  • Model files can be programs
  • Loading can run code
  • Safer formats exist

Code that runs while loading

Here is the problem, in a harmless form. A class defines a special method, that tells Python how to rebuild the object. Instead of rebuilding data, it says, call this function, with these arguments. Here the function is just print, so nothing bad happens. We save the object into bytes, as a model file would be. Then we load it. The message prints, during loading. An attacker would not call print. They would call a function that steals files, or installs malware. And all they need is for you to load their file.

pickle_risk.pypython
import pickle

class Harmless:
    def __reduce__(self):
        return (print, ("this code ran just by loading the file",))

blob = pickle.dumps(Harmless())
print("loading a model file...")
pickle.loads(blob)
print("...and it already executed code.")
Output
loading a model file...
this code ran just by loading the file
...and it already executed code.

Safer ways to store models

What should you do instead? Prefer formats that store only data, never code. Safe tensors, for neural network weights. O N N X, for portable models, which contains a graph of operations, and no arbitrary code. Plain text formats, like J S O N, for simple models, such as linear coefficients. And, if you must use pickle, for scikit learn models, only load files that you created yourself, or that come from a source you fully trust, and verify them with a checksum, or a signature, before loading.

FormatRuns code when loaded?Use for
pickle, joblibyes, possiblyyour own files only
safetensorsnoneural network weights
ONNXnoportable models
JSON, plain textnosimple models and settings

Verify what you load

Treat every model file like downloaded software. Get it from a trusted registry, not from a random link. Check its checksum against the published one. Where possible, verify a digital signature. Scan it, with tools that detect suspicious model files. Load it first in an isolated sandbox, with no secrets, and no network. And keep a record, in your registry, of where each model came from, and who approved it. Provenance is your defence.

Trusted sourcesa registry, not a random link
Checksum and signatureprove it is the real file
Scan and sandboxisolate before trusting
Record provenancewhere, and who approved

Summary

Good. Pickle based model files can execute code when loaded. Prefer safe formats, verify checksums and signatures, and load unknown files only in a sandbox. Next, the same suspicion, applied to the libraries your project depends on, the supply chain.

Key takeaways
  • Pickle can run code on load
  • Prefer safetensors and ONNX
  • Verify, scan, sandbox
Next up: Supply Chain Security — trusting your dependencies