Insecure deserialisation is when an app rebuilds saved data it should not trust, letting an attacker control the result.
This guide is for students, new developers and anyone who works with apps or AI models. You do not need to write code to be affected: if your team downloads AI models or uses apps that pass saved data around, this risk can apply to you.
In short
- Apps often pack data up to save or send it, then unpack it later. Unpacking is called deserialisation.
- Some unpacking tools rebuild whatever the data describes, and that can include running code.
- If an attacker can change the packed data, they may be able to make the app do things it should not.
- Use plain data formats, never unpack untrusted data with risky tools, and treat downloaded AI model files with care.
What you will learn
- Explain serialisation and deserialisation in plain words.
- Spot where an app might be unpacking data it should not trust.
- Explain why some AI model files carry the same risk.
- Ask your team which formats and tools they use to load data.
- Packing data (serialisation)
- turning something an app is working with into a file or text so it can be saved or sent.
- Unpacking data (deserialisation)
- reading that file or text back and rebuilding the original thing from it.
- Running someone else's code (remote code execution)
- when an attacker makes a system run instructions they chose.
What is insecure deserialisation?
Think of a flat-pack furniture kit. The box holds the parts and an instruction sheet. The builder follows the sheet without question, step by step.
Apps do something similar with data. An object is a bundle of data the app is working with. To save or send one, the app packs it into a file or a line of text. This is called serialisation. Later, another part of the app unpacks it and rebuilds the object. That is called deserialisation.
Some unpacking tools are like a builder who follows any sheet in the box. The packed data says what to build, and the tool builds it. If an attacker can swap in their own sheet, the app may build something harmful. In the worst cases, it runs the attacker's code. This is called remote code execution.
Where the comparison stops working: a builder might notice an odd step, like "drill into the wall". An unpacking tool runs at computer speed and does not stop to ask whether a step makes sense.
Why does it matter?
Microsoft warns that attackers who exploit these flaws can crash an app, steal information or run code inside it.[4] The risk now reaches AI too. Many AI model files use a Python packing format called pickle, and loading an untrusted one can run code.[3]
If you are learning to code
What could happen: you use a handy save-and-load tool on data that came from users.
Check this: read the security warning in your tool's documentation before you load outside data.
If you work with AI models
What could happen: a model downloaded from a sharing site runs hidden code when you load it.
Check this: prefer models from trusted sources, in safer formats such as safetensors.
If you manage a software team
What could happen: an old part of an app still uses an unpacking tool its maker says is unsafe.
Check this: ask which tools the team uses to load saved data, and whether any are flagged as unsafe.
What does it look like?
Here is a made-up scenario card showing where risky unpacking can hide in a small online shop.
Example only. All values are made up.
App: EXAMPLE shop basket
Saved basket cookie: basket=PACKED-OBJECT-0000-FAKE (1)
On each visit: app unpacks the cookie with a tool
that rebuilds any object it names (2)
Nightly job: loads model.pkl from a shared folder (3)
Settings: loaded from JSON with fixed fields (4)
- In a real file, this line would contain a packed object stored in the visitor's browser. The visitor can change it.
- The risky part. The data decides what gets built.
- A pickle file. Anyone who can write to that folder may be able to plant code.
- The safer pattern. The app decides the shape, and the data only fills it in.
Why this is risky: the cookie and the shared folder are both places someone else can change. The app then trusts what it unpacks.
The safer way: keep only an ID in the cookie and the basket on the server. Load models in safer formats, from trusted places.
How does it work?
- Packing. An app packs an object into bytes or text, so it can be saved or sent somewhere.[1]
- Travelling. The packed data goes somewhere outside the app's control, such as a cookie, a file upload or a shared folder.
- Tampering. An attacker replaces it with packed data they made themselves.
- Unpacking. The app unpacks it. Some tools rebuild whatever types the data names and run set-up code as they do.
- Harm. The rebuilt object triggers actions the app never meant to take. Python's own guide warns that harmful pickle data can run any code it likes while being unpacked.[2]


The good news is that safer choices exist. The OWASP cheat sheet, a widely used free security guide, says avoiding native packing formats greatly reduces the risk. It suggests plain data formats such as JSON instead.[1] Python's guide adds that unpacking untrusted JSON does not, on its own, let code run.[2] Microsoft goes further for one old .NET tool, BinaryFormatter. It says the tool is insecure and cannot be made secure, and from .NET 9 the built-in version throws an error when used.[4]
A worked example
Umar Abara is a developer at Rowan Pharmacy, a fictional chain of high street chemists. He looks after a small tool that predicts which medicines each branch will need.
A colleague finds a free model online that might make the predictions better. It comes as a single file ending in .pkl. She asks Umar to load it and try it out.
Umar remembers the flat-pack box. A .pkl file is a pickle, and loading a pickle follows the instructions inside it. He checks the Python guide, which warns never to unpickle data from an untrusted source.[2]
He does not know the person who shared the file. So he looks for the same model on a sharing site that scans pickle files. The site lists what each file would load, while warning that its scanner is not foolproof.[3] He also finds a version of the model in the safetensors format, which stores the model's numbers without the code-running risk of pickle.
He uses that version, from the publisher's own account, on a test machine first. The results look good, so he writes a short team note.
The note says: no pickle files from unknown sources, prefer safetensors, and test new models away from live systems. His manager adds it to the team's checklist.
Myths and mistakes
| What people think | What is true |
|---|---|
| Data is just data. It cannot run code. | Some unpacking tools run code as part of rebuilding objects. Python warns that pickle data can do this.[2] |
| Our own code is fine, so we are safe. | Attacks can use ordinary parts of the libraries an app already includes. |
| A virus scanner will catch a bad model file. | Hugging Face notes that ClamAV antivirus scanning does not cover pickle exploits.[3] |
| Hiding the data in a cookie keeps it safe. | Visitors can see and change their own cookies. Keep the real data on the server. |
| A model from a big sharing site must be safe. | Anyone can upload. Check who published it and prefer safer formats.[3] |
What can I do this week?
- List the places your app loads saved or sent data, such as cookies, uploads and shared folders.
- Check which tool each place uses, and read that tool's security warning.
- Swap risky tools for plain data formats, such as JSON, where you can.
- Agree a rule for AI models: trusted sources, safer formats and a test machine first.
Checklist
- We know every place our apps unpack saved or sent data.
- Data from users or the internet is loaded only as plain data with fixed types.
- No app uses an unpacking tool its maker says is unsafe for untrusted data.
- Data we must trust is signed, and the signature is checked before unpacking.
- Cookies hold IDs, not packed objects.
- AI models come from known publishers, in safer formats where available.
- New models are tested away from live systems before use.
Common questions
Is JSON safe from this problem?
Mostly, yes. Python's guide says unpacking untrusted JSON does not, on its own, let code run. Some JSON tools have optional settings that read type names from the data, and those can bring the risk back. Keep such settings off for outside data.
What is a pickle file?
A pickle file is Python's own way of saving objects. It is handy and widely used, including for many AI model files. Python warns that it is not secure, because unpacking a harmful pickle can run code. Only load pickle files you trust.
What is safetensors?
Safetensors is a file format for storing AI model data. It was designed as a simpler alternative to pickle, so loading it does not run code in the same way. Check whether a model is offered in this format. Check the publisher too, because a safe format is not the same as a trusted source.
Can signing the data fix it?
It helps. OWASP suggests signing data when it is packed and refusing anything without a valid signature. That proves the data came from you and was not changed. It only works while the signing key stays secret, so the key needs the same care as a password.
Why is it spelt deserialisation and deserialization?
Both mean the same thing. Deserialisation is the British spelling and deserialization the American one. Most tools, documents and security reports use the American spelling, so search for both when you look for help.
Where next?
Ready for the detail? Read the practitioner guide: Insecure Deserialisation Guide. It goes further into how the attacks work in each language, the platform settings that limit them, and how to find and test risky code.
Also useful:
- Secure Deserialisation Guide
- Downloading AI Models for Beginners
- AI Model File Formats Compared for Security
- CWE for Beginners
- Secure Code Review for Beginners
Related NHI Mgmt Group resources: Insecure Deserialisation Guide · Web Application Attacks Guide · Fuzzing Guide
Key terms
- Serialisation
- Turning an object into a format that can be saved or sent and rebuilt later.
- Deserialisation
- Rebuilding an object from saved or sent data.
- Remote code execution
- When an attacker makes a system run code of their choosing.
- Gadget chain
- A series of ordinary pieces of code that an attacker links together during unpacking to cause harm.
- Pickle
- Python's built-in format for saving objects, which can run code when loaded.
- Safetensors
- A format for storing AI model data that is designed not to run code when loaded.
- Model supply chain
- All the sources, files and tools an AI model passes through before you use it.
Sources and further reading
- Deserialization Cheat Sheet. OWASP Cheat Sheet Series, OWASP Foundation. https://cheatsheetseries.owasp.org/cheatsheets/Deserialization_Cheat_Sheet.html, accessed 28 Nov 2026.
- pickle: Python object serialization. Python documentation, Python Software Foundation. https://docs.python.org/3/library/pickle.html, accessed 28 Nov 2026.
- Pickle Scanning. Hugging Face Hub documentation, Hugging Face. https://huggingface.co/docs/hub/security-pickle, accessed 28 Nov 2026.
- Deserialization risks in use of BinaryFormatter and related types. .NET documentation, Microsoft Learn. https://learn.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide, accessed 28 Nov 2026.