OOmnistu

Blog / Engineering Playbooks

Engineering Playbooks

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.

OOmnistu
Reviewed 2026-11-16 · 3 min read
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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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; 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

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.

Sources

Previous
Input validation: what your app should check before it trusts a form or API request