Deserializing untrusted data (pickle, unserialize or Java objects)

Last updated November 9, 2026
Steps
- Search the project for native deserializers on data you did not create yourself: pickle.loads, pickle.load, cPickle, cloudpickle, PHP unserialize, Java ObjectInputStream.readObject, Ruby Marshal.load, YAML load (the unsafe kind), node-serialize and similar helpers, and any “restore session from cookie” or “load this uploaded model file” path.
- For each match, ask whether the bytes can come from a user upload, a request, a cookie, a query string or a file on disk that someone else can replace. If yes, do not run the native deserializer on it.
- Replace those loads with a data-only format. JSON (or another text format you parse into plain fields) plus a strict schema (Zod, Joi, Ajv, Pydantic-style) is the usual path. Rebuild a new object from allowlisted fields instead of resurrecting whatever class name arrived in the payload.
- If you must accept a serialized blob you produced yourself (for example a signed job payload), HMAC or otherwise sign it before it leaves the server and reject anything with a bad signature—CWE-502 and the OWASP Deserialization Cheat Sheet both describe signing as a way to detect tampering. Signing does not make pickle safe against a stolen key.
- Do not accept uploaded .pkl, serialized Java, or similar model/session files from end users into the same process that serves the website. If a feature needs those files, isolate the loader and treat the file as untrusted.
- Trim libraries you do not need. CWE-502 notes that “gadget” classes sitting unused in dependencies can still be reached during unsafe deserialization; an allowlist of expected types is a backstop, not a complete fix.
- Retest by confirming the old deserializer is gone from request, cookie and upload paths, and that unexpected JSON fields are rejected by your schema.
Why it matters
CWE-502 describes deserializing untrusted data without making sure the result is valid. Consequences range from changed application data to resource-exhaustion and, depending on the objects involved, running unintended code (MITRE’s examples include Python pickle and PHP unserialize). Python’s pickle documentation warns that malicious pickle data can execute arbitrary code during unpickling, and recommends HMAC if you need to detect tampering plus JSON when you are processing untrusted data. The OWASP Deserialization Cheat Sheet’s language-agnostic guidance is to prefer alternative data formats and, if you deserialize, only deserialize signed data. This page is about finding and removing that pattern in your own app—not about attacking anyone else’s.
Sources
MITRE CWE-502: Deserialization of Untrusted Data — https://cwe.mitre.org/data/definitions/502.html
OWASP Cheat Sheet: Deserialization — https://cheatsheetseries.owasp.org/cheatsheets/Deserialization_Cheat_Sheet.html
Python documentation: pickle — https://docs.python.org/3/library/pickle.html
Checked at the source on November 8, 2026.
Related patterns
- Running AI or user output with eval, exec or new Function (code injection)
- File uploads with no type, size or name checks
- Prompt injection in your app's AI chatbot or AI features
- Outdated packages with known vulnerabilities
- AI-suggested packages that don't exist (slopsquatting)
- Rendering user or AI output as raw HTML (XSS)
- Server-side template injection from unsanitized template strings
- XML parsers that resolve external entities (XXE)
Found a problem you'd rather not fix yourself?
Tell us what's wrong. A StorenTech engineer reviews your app and replies with a fixed quote, so you know the cost before any work starts.
Get it fixed by StorenTechVibe Security Weekly
One email a week with new failure patterns and how to fix them.
By joining you agree we may email you this newsletter. See our privacy policy. Free. Unsubscribe anytime. We'll never sell your email.