Safeguard
Security Guides•

Does a restricted unpickler make pickle safe? Myth versus fact

Restricting globals in pickle's find_class helps, but the Python docs warn allow listing needs care. Myth versus fact on hardening pickle for data jobs.

Safeguard Security Team
3 min read

A restricted unpickler lowers risk but does not make pickle safe for untrusted data. The Python docs show how to override find_class to allow only a few classes, and also warn that allow listing needs care. If the data is untrusted, use a format like JSON instead.

Myth and fact

Myth: "If I restrict which classes can load, pickle is safe."

Fact: It narrows the attack surface when done carefully, but the Python docs still open with a warning that the module is not secure and that you should only unpickle data you trust.

What the mechanism does

By default, the docs say, unpickling imports any class or function found in the pickle data. That lets a crafted stream call things like a shell command function. The documented fix is to subclass the unpickler and override find_class, which is called whenever a global is requested. Your override returns allowed items and raises an error for the rest.

The docs include an example that allows only a handful of harmless built in types, and shows a malicious stream being rejected with an unpickling error naming the forbidden global.

Why it still is not a guarantee

The docs warn that allow listing needs care, and use an example involving eval reachable through a permitted looking path to make the point. Every allowed callable is a chance to be misused, and data science libraries expose many callables. A list broad enough to load a real model or DataFrame can be broad enough to be abused.

It also guards only the loading step. It does not tell you the data inside is correct or unmodified.

What the docs suggest instead

NeedOption from the docs
Untrusted inputJSON, which does not in itself create an arbitrary code execution vulnerability when deserialized
Tamper detectionSign data with HMAC. This addresses tampering only, not untrusted sources
Looking inside a suspect fileUse pickletools, which does not execute bytecode

For data pipelines

  1. Move cross team exchange to Parquet, CSV or JSON where the data is plain data. This is our recommendation rather than a quoted doc claim.
  2. Keep pickle for files only your own job can write and read.
  3. If you must load pickle from elsewhere, restrict globals with an allow list, test it against malicious samples, and run the load in a sandbox with no credentials.
  4. Sign and verify the file before loading, so tampering is caught first.

See also what to do instead of pickle and whether joblib model files are safe.

Quick check

Does HMAC signing fix untrusted pickle?

No. It detects tampering by someone without the key. It does not make a file from an untrusted source safe to load.

What is the safest way to look at a pickle file?

Use pickletools to read it without executing it, as the Python docs describe.

Safeguard scanning flags vulnerable Python libraries in your data projects, with a free scan at gold.safeguard.sh/scan.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.