Insecure deserialization: why you should not rebuild objects from untrusted data
Understand the risks of rebuilding objects from untrusted data and discuss safer formats and parsing boundaries with your team.
- Never unpickle untrusted data
- Avoid native object formats for untrusted input; OWASP says a plain format such as JSON or XML greatly reduces the risk, though mistakes remain possible.
- Sign what you must trust
- Deserialize only signed data when you know in advance which messages you will process; refuse anything without a valid signature.
- Some libraries cannot be secured
- Microsoft says BinaryFormatter is dangerous and cannot be secured, so it should not be used.
All wording checked against the OWASP Deserialization Cheat Sheet linked in Sources; version guidance changes - check current library docs.
Serialization turns an object into data that can be stored or sent. Deserialization rebuilds the object. OWASP's Deserialization Cheat Sheet says many languages have native formats with features beyond JSON or XML, and those features can be repurposed against you when the data is untrusted, leading to denial of service, access control bypass or remote code execution. We read the whole sheet on 15 November 2026 and not the Oracle, Microsoft and Python pages it links. Library and version statements below are the sheet's as of that date; check current documentation before relying on them.
The language-agnostic advice
- Avoid native formats for untrusted data. Use a plain data format such as JSON or XML. OWASP says this greatly reduces the risk, although mistakes remain possible after a plain data object is parsed. It mentions the data-transfer-object pattern.
- Deserialize only signed data when you know in advance which messages you will process: sign when serializing and refuse anything without a valid signature from an authorized source. A signature does not make inherently dangerous object deserialization safe; avoid it for untrusted data and still apply schema, replay and resource controls.
- Do not let the data choose the type. This comes up repeatedly in the .NET section: do not let the stream, or a value an attacker can change (the sheet's example reads a type name from a database), decide what gets instantiated.
- Checking after the fact is too late. The sheet's .NET example: code may already run during deserialization, so testing the object's type afterwards does not help.
- Keep "gadget" code away from internet-facing code, says the sheet, since risky classes in the same project can be abused; it gives a Windows desktop UI class that can call arbitrary methods as an example not to reference in a REST service.
What the sheet says per language
- PHP: review uses of
unserialize()with external input and use JSON for data you pass to users. - Python: never unpickle untrusted data, because it can execute arbitrary code. For YAML use the safe loader, not the unsafe ones, and be wary of
jsonpicklewith untrusted input. Base64 data starting withgASVmay indicate pickle. - Java: review
ObjectInputStream,readObjectand friends,XMLDecoderandXStream. Configure serialization filters with an application-specific class allowlist and resource limits before reading objects. A customresolveClass()check does not cover everything and does not limit resource use. Filtering does not make arbitrary untrusted deserialization safe. Mark sensitive fieldstransient, and for classes that must never be deserialized, add areadObject()that always throws. The sheet also describes a JVM agent for code you cannot change, with a denylist, as a stopgap, and says it expects new gadget classes to be found. Traffic starting with hexAC ED 00 05or Base64rO0may be Java serialization. - Other Java libraries: it sorts them into usable with default configuration (with conditions), needing configuration first, and not safe or unmaintained. Examples are fastjson, jackson-databind, Kryo, SnakeYAML and XStream, each with version and option conditions we do not repeat here, because they change. The sheet calls the JDK
XMLDecoderclose to impossible to use securely with untrusted input. - .NET: Microsoft says
BinaryFormatteris dangerous and cannot be secured, so it should not be used. Where Json.NET is used,TypeNameHandlingshould beNone. Restricting allowed types is still risky because some native types are dangerous themselves.
PHP
Review uses of unserialize() with external input; use JSON for data you pass to users.
Python
Never unpickle untrusted data - it can execute arbitrary code. Use the safe YAML loader; be wary of jsonpickle. Base64 starting with gASV may indicate pickle.
Java
Review ObjectInputStream, readObject, XMLDecoder, XStream. Configure serialization filters with an application-specific allowlist and resource limits before reading objects.
.NET
BinaryFormatter should not be used. With Json.NET, TypeNameHandling should be None; restricting allowed types is still risky.
Questions for your team
- Where do we deserialize data that came from outside (requests, queues, cookies, files, caches, databases others can write)?
- Is any of it a native object format, such as pickle, Java serialization or BinaryFormatter?
- Can the data pick the type to create?
- Could we switch to JSON with a fixed schema, and sign what we must trust?
- Do we keep library versions current?
Related: input validation basics, mass assignment explained, SQL injection prevention.
Limits
One OWASP page, read in full. Library version guidance changes; check the library's current documentation. No exploit details or code are given. We did not test anything.
By Omnistu.