Pickle vs Safetensors: Why Loading a Model Can Run Code
Loading a pickle file, a format model files can use, ran code I had hidden inside it, four files out of four.
Video #99 ยท AI security and governance
Steps in the video
Files
- attacks.py (26 lines)
- compare.py (53 lines)
- guards.py (26 lines)
- payload.py (6 lines)
Tested with
Python 3.12.15 | safetensors==0.8.0, numpy==2.5.4
What this does not show
Four harmless payloads written by the author. The bad-module scan only reads the older pickle format used here (protocol 2); files saved with the default protocol show it no module names. A real model file may need more allow-list globals. Safetensors holds tensors, not everything in a model. No scanner service and no real model file were tested.
Sources
Learning IT by Example.