What Is Insecure Deserialization? CWE-502 Explained
Security Beginner 6 min read

What Is Insecure Deserialization? CWE-502 Explained

Insecure deserialization (CWE-502) lets attacker-controlled data become live objects and code. How it works, where it hides and how to prevent it.

Published: November 18, 2026 • Updated: November 18, 2026
deserializationcwe-502secure codingappsec

Insecure deserialization (CWE-502) is a flaw where an application rebuilds objects from data an attacker controls, and the rebuild itself does something dangerous. Serialization turns live objects into bytes for storage or transport. Deserialization reverses it. When the format can say "create an object of this type with these fields" and the receiver trusts that instruction, the sender gets to choose which code runs. The November 2026 Puppet Server bug, CVE-2026-115731, is the current headline example: a crafted agent report gave code execution as the Puppet service user.

What is insecure deserialization?

Insecure deserialization is when a program converts untrusted serialized data back into objects without restricting which types it will create or what those types do on creation. The attacker supplies the serialized blob, and the deserializer builds whatever the blob describes.

The problem is not the data. Reading a string or integer out of a message is harmless. The problem is that rich serialization formats carry type information, and many runtimes run code when they construct a type: constructors, property setters, magic methods, finalizers, hooks that fire when an object is loaded. If the attacker can name the type, they can pick which of that code runs and with which inputs.

Why does deserialization lead to code execution?

Deserialization leads to code execution because the attacker can chain existing classes in the application into a sequence that ends in a dangerous call. Each class does something small and legitimate on load. Linked together they can write a file, start a process or evaluate a string as code.

Security researchers call these sequences gadget chains. No single class is a vulnerability. A logging class that calls a method on a field, a collection class that compares elements by invoking them, a template class that renders a string: each is fine alone. A deserializer that will instantiate all of them from attacker input turns the application's own library into an exploit kit. This is why the fix is almost never "patch the gadget" and almost always "stop deserializing arbitrary types."

In some languages the chain is even shorter. Puppet's rich-data format carries a type tag on each value. Ostorlab's analysis of CVE-2026-115731 found that the report-processing code trusted that tag enough to construct and evaluate a Ruby class from it, so the attacker did not need a long chain at all. Our news coverage of the Puppet Enterprise CVE-2026-115731 KEV listing explains why that one request compromises the entire fleet.

Where insecure deserialization hides

Look for it anywhere one component sends structured state to another and the receiver is written in a language with a native object serializer.

  • Java: ObjectInputStream, and libraries that wrap it (RMI, JMX, old versions of many message brokers). The 2015 Apache Commons Collections research made this class famous.
  • .NET: BinaryFormatter, NetDataContractSerializer, and TypeNameHandling in JSON libraries when set to anything other than None. Microsoft has deprecated BinaryFormatter outright.
  • Python: pickle, yaml.load without a safe loader, and shelve or marshal on untrusted input.
  • Ruby: Marshal.load, YAML.load on older Psych versions, and custom formats like Puppet's rich data that map tags to classes.
  • PHP: unserialize() with magic methods such as __wakeup and __destruct.
  • Node.js: libraries that advertise "serialize functions" and rebuild them with eval.

The common thread is a format that can express types, not just values. JSON itself cannot name a class, so plain JSON parsing is safe. A JSON library configured to read a $type field and instantiate it is not.

How do you prevent insecure deserialization?

You prevent insecure deserialization by never letting untrusted input choose the type, and by treating any deserializer that can be told a type as a code-execution interface. Use data-only formats, enforce an allowlist of permitted classes, and verify integrity before parsing.

In order of preference:

  1. Use a data-only format. Plain JSON, Protocol Buffers, MessagePack without extension types, or CBOR with a fixed schema. Map the parsed data onto your own objects in code you wrote. The attacker gets to choose values, never types.
  2. Allowlist types. If you must use a native serializer, configure it to refuse anything not on a short list. Java's ObjectInputFilter, .NET's SerializationBinder, Python's restricted Unpickler.find_class, Ruby's Psych.safe_load with permitted_classes all exist for this. Denylists fail: researchers find new gadgets faster than vendors block them.
  3. Authenticate the blob. Sign serialized data with an HMAC and verify it before deserializing. This only helps when the sender is trusted and the key stays secret. It does nothing when the sender is the attacker, which is the Puppet case: every agent is a legitimate, authenticated sender.
  4. Reduce what the deserializing process can do. Run it as a low-privilege user, in a container with no outbound network, with the filesystem read-only. CVE-2026-115731 ran code as the Puppet service user, and that user could read the CA private key. Separate the CA from the catalog compiler and the damage from a successful attack shrinks.
  5. Log and alert on type names. If your deserializer logs the class it is about to construct, an unexpected name is an early signal. The Puppet attack arrived as a normal report upload and may leave nothing else in the logs.

The Puppet case also shows a trust mistake that makes this bug class worse. The report endpoint was authenticated, but authenticated meant "holds any certificate our CA signed," and the CA could be set to sign anything. Authentication does not make deserialization safe. It only limits who can send the payload, and in a fleet of thousands of nodes that is a large set. Our guide on disabling Puppet autosign and auditing agent certificates shows how to tighten who counts as authenticated.

How to find it in your own code

Grep for the serializer entry points above. For each hit, ask three questions: who produces the bytes, could that producer be an attacker or a compromised peer, and does the deserializer accept arbitrary types. If the answer to the last two is yes, you have a finding, regardless of whether a public gadget chain exists for that exact library today.

Static analysis tools flag most of the entry points. CodeQL has queries for Java, C#, Python, Ruby and JavaScript deserialization sinks. Semgrep's registry covers pickle, yaml.load, Marshal.load and unserialize. Dependency scanners will not help: the vulnerable code is usually your own call into a safe library with an unsafe setting.

For third-party products, the question is whether the vendor has restricted its deserializers. Puppet's fix in 8.22.2 and 9.1.3 does exactly that for report processing. When an advisory says "deserialization of untrusted data," the patch is mandatory and the mitigation is to shrink who can reach the endpoint until you apply it. A related bug class, unsafe reflection, lets input pick a method or class name directly rather than through a serializer; see what unsafe reflection (CWE-470) means.