Insecure deserialization: when an object is rebuilt from bytes nobody checked

Serialization turns an object in memory into bytes or text that can be stored or sent. Deserialization does the reverse. Insecure deserialization is what happens when those bytes come from someone who can change them and the application rebuilds the object without checking: the attacker then decides which objects exist inside your process, and some languages will run code while building them.

Section Web attacks Updated Weakness CWE-502

Quick facts

Weakness
CWE-502: deserialization of untrusted data
Core condition
Data the client can change is turned back into objects before it is verified
Possible outcomes
Denial of service, broken access control, remote code execution
Java stream marker
A Java serialization stream begins with the bytes ac ed 00 05 (Java Object Serialization Specification)
Main fix
Do not deserialize untrusted input with native object formats; verify integrity before parsing anything else

Definitions and the list of outcomes follow the OWASP Deserialization Cheat Sheet; the weakness description follows CWE-502.

How the attack works

Applications serialize objects to keep a session, cache a result, pass a message between services or hand state to the browser. When the data comes back, a deserializer reads it and recreates the objects. OWASP defines the step as "taking data structured in some format, and rebuilding it into an object". CWE-502 describes the flaw in one sentence: the product deserializes untrusted data without sufficiently ensuring that the resulting data will be valid.

The trouble is that a native format describes more than values. It names classes. A deserializer in Java, Python or PHP will look up whatever class the stream asks for and, depending on the language, call code on it while the object is assembled. The attacker never has to upload anything: the program's own classes, already loaded, do the work.

Gadgets

A gadget is an existing class whose ordinary behaviour, triggered during reconstruction, does something helpful to an attacker. CWE-502 lists limiting the available types and gadgets as a mitigation. This page does not show gadget chains; they are tied to particular library versions, and the defence does not depend on knowing them. If the application never deserializes attacker-controlled native data, no gadget matters.

Signed does not mean safe, unsigned does not mean broken

A signature proves the bytes were produced by someone holding the key. That makes it a control for data your server sends out and later receives back, such as client-side state. It does nothing for data from parties who legitimately hold no key, it fails if the key leaks, and it fails if the code parses the data before checking the signature. The order matters: verify first.

Where untrusted bytes come from

Any place a client can write to and the server later reads is a candidate. Typical ones:

  • Cookies and hidden form fields that hold session or view state as a serialized object.
  • API request bodies in a binary or language-specific format rather than plain JSON.
  • Message queues and caches that other, less trusted systems can write to.
  • Uploaded files that the application loads as saved objects, including some configuration and model formats.
  • Session stores and tokens that carry an object instead of a reference to one.

OWASP puts the same idea under integrity: A08:2025 covers objects or data converted into a structure that an attacker can view and change. The category has a wider scope than this page. Updates and build pipelines belong to it too, and software or data integrity failures covers them.

What it can lead to

The OWASP cheat sheet says native deserialization features can be repurposed for denial-of-service, access control or remote code execution attacks. The three are different problems.

Denial of service. A small input can describe a very large or deeply nested structure. The process spends its memory or processor time on it, with no code execution at all. Resource limits on size and depth are part of the fix.

Access control. If the serialized object holds a role, a price or a user identifier and the server trusts it, changing the field changes the outcome. This is close to IDOR, with the identifier hidden inside an object.

Code execution. This is the serious outcome. When the deserializer reaches a class that does something unintended while being rebuilt, the attacker's input becomes the program's behaviour. The consequences are the same as in any remote code execution flaw, and the route differs from command injection: no shell command is assembled from a string, an object is assembled from bytes.

Languages and formats that carry the risk

The cheat sheet gives language-specific guidance. Four statements from it are worth keeping:

  • Java. Configure serialization filters with an application-specific class allowlist and resource limits. The cheat sheet adds that filtering alone does not make untrusted deserialization safe, and that its look-ahead ObjectInputStream example is an illustrative class check rather than a complete defence.
  • Python. pickle can run arbitrary code, so untrusted data should never be unpickled. For untrusted YAML, use yaml.safe_load().
  • PHP. Review every call to unserialize() and how external input reaches it. When serialized data has to reach users, use json_encode() and json_decode().
  • .NET. Microsoft states that the BinaryFormatter type is dangerous and cannot be secured, so it should be avoided. In JSON.NET, keep TypeNameHandling at None, or use a custom binder with an allowlist.

The last point is why "use JSON" is a direction and not a guarantee. A parser that reads a type name from the input behaves like a native deserializer, even though the text looks harmless.

Where a WAF helps and where it does not

CWE-502 lists an application firewall among its mitigations and rates it as moderately effective, defence in depth rather than a fix. That matches what a filter can actually see.

What a filter can see

A request is bytes in a body, a cookie or a header. A rule can recognise a known serialized-object format where the application expects plain text: a Java stream marker inside a cookie that is normally a short token, a binary blob posted to an endpoint that takes JSON. It can also flag unusually large or deeply nested bodies. When a vulnerable library is announced, a rule written for the announcement works as virtual patching, a stopgap that gives the team time to update.

What it cannot see

A filter does not know which classes your application has loaded or which ones are dangerous. It cannot read a payload that is encrypted, compressed in a custom way or wrapped in an internal format. It cannot tell a legitimate serialized object from a forged one when the application also sends them to itself. And it does not change the code: the unsafe call is still there, waiting for the next route around the rule.

Off, monitor, block

Bridge WAF has three modes. In off nothing is inspected. In monitor matches are recorded and every request is still served, so the activity log shows which requests carry serialized-object markers and which endpoints receive them. Start there: some applications legitimately exchange serialized data between their own services, and blocking that traffic breaks them. After the log is read and exceptions are made, block refuses matching requests and still records them. The step-by-step is in WAF monitor mode.

The same limit applies across injection classes, including command injection: the filter reduces exposure, and the code has to be correct.

How to prevent it in code

  1. Avoid native deserialization of untrusted input. OWASP recommends a pure data format such as JSON or XML. Parse it into plain data, then build your own objects from validated fields.
  2. Verify integrity before parsing. When state must travel through the client, sign it, for example with an HMAC, and refuse any message without a valid signature. OWASP A08:2025 adds that unsigned or unencrypted serialized data should not be accepted from untrusted clients.
  3. Restrict types. Use the allowlist and filter facilities of your runtime. Do not let the data stream choose the type to instantiate.
  4. Set limits. Cap size, depth and object count, so a denial-of-service payload fails early.
  5. Remove dangerous formats. Take BinaryFormatter, pickle on external input and unrestricted unserialize() out of the code paths that touch the network.
  6. Reduce what a successful attack can do. Run the application with least privilege and limit its outbound connections, as remote code execution advice also says.
  7. Keep libraries current. Many deserialization incidents are bugs in a library that was never updated.

Questions

What is insecure deserialization?

Insecure deserialization is a flaw in which an application rebuilds objects from serialized data that an attacker can change, without checking the data first. Depending on the language and the classes available, the result can be a crash, bypassed access checks or remote code execution.

Is deserializing JSON safe?

Plain JSON parsed into plain data is far less risky than native object serialization, which is why OWASP suggests switching to a pure data format. It is not a guarantee: some JSON libraries can be configured to read a type name from the input and create that type, and that setting reopens the problem.

Can a WAF stop insecure deserialization?

A WAF can flag requests that carry recognisable serialized-object formats in places that should hold plain values, and it can buy time while code is fixed. It cannot see inside encrypted or custom formats, and it does not replace removing unsafe deserialization from the application.

How do you prevent insecure deserialization?

Do not deserialize untrusted data with native object formats. Accept plain data such as JSON and build objects yourself, sign data that must round-trip through the client and verify the signature before parsing, and restrict which types can be created with an allowlist.

What is a gadget in deserialization?

A gadget is an existing class in the application or its libraries whose normal behaviour, triggered while an object is being rebuilt, does something useful to an attacker. Restricting which classes can be deserialized shrinks the set of gadgets available.