CWE-502OWASP A08:2021

CWE-502: Insecure Deserialization

Untrusted data is deserialized into objects, letting an attacker trigger code execution or unexpected behavior.

Also known as: Insecure deserialization, Deserialization of untrusted data, Unsafe object deserialization

Risk

Typically critical. Unsafe deserialization of attacker-controlled data commonly results in remote code execution.

Definition

What it is

CWE-502 occurs when untrusted data is deserialized with a format that can instantiate arbitrary types or run code during parsing, such as pickle, native Java serialization, or an unsafe YAML load. A crafted payload can trigger code execution before the application ever inspects the data. Because the attack happens inside the parser, ordinary input validation downstream does not help. The fix is to deserialize untrusted input only with data-only formats into known structures.

Use-cases

How it shows up in real code

Pickle from a request

A service unpickles data sent by a client. A crafted payload runs code during deserialization, taking over the process.

Unsafe YAML load

Config or input is parsed with a YAML loader that instantiates types. A malicious document executes code as it loads.

Session cookie object

A framework deserializes a rich object from a cookie. A tampered cookie triggers unexpected object creation and code paths.

Message queue payload

A worker deserializes native objects off a queue. An attacker who can post a message achieves remote code execution.

AI risk

Why AI-generated code has increased the exposure

  • Asked to load or restore an object from input, assistants often reach for pickle, native serialization, or an unsafe YAML load, because it is concise and works in the demo.
  • Models learned from examples that deserialize data with powerful, type-resolving formats rather than a safe data-only parser.
  • Generated code trusts that the serialized blob is well-formed app data, because the prompt described caching or messaging and not an attacker crafting a payload.
  • When scaffolding sessions, queues, caches, or inter-service messages, the easiest generated approach serializes and deserializes rich objects directly.
  • Each generated deserialization point that accepts outside data is an independent exposure, so more generated integrations means more sinks for a malicious payload.

Vibe coding

How vibe coding amplifies the risk

  • Vibe coding ships the generated deserialization unread, so an unsafe loader on untrusted data reaches production unnoticed.
  • Prompt-and-ship skips the review where pickle or an unsafe YAML load on request data would be rejected.
  • Iterating by prompt extends the existing object-serialization approach rather than switching to a data-only format.
  • Trusted payloads deserialize correctly in testing, so the flaw stays invisible until a crafted object arrives.
  • Rapid growth adds message and cache integrations faster than review can confine each deserializer.

Detection

How to find it

  • Look for native deserializers such as pickle, Java readObject, or unsafe YAML load that receive external data.
  • Trace every byte stream from a request, queue, cache, or cookie to the point where it is deserialized.
  • Watch for object formats that can instantiate arbitrary types during parsing.
  • Test with known gadget-style payloads and confirm they cannot trigger execution or object creation.

Remediation

How to fix it

  • Deserialize untrusted input only with a data-only format such as JSON into known structures.
  • Avoid native object serialization for anything that crosses a trust boundary.
  • If a rich format is unavoidable, restrict allowed types with an allow-list and verify integrity first.
  • Sign or MAC serialized data so tampered payloads are rejected before parsing.
  • Add a CI rule that flags unsafe deserialization calls on external data.

Frequently asked questions

What is CWE-502 (insecure deserialization)?
CWE-502, insecure deserialization, is when untrusted data is deserialized with a format that can instantiate arbitrary types or run code during parsing, such as pickle or an unsafe YAML load, letting an attacker achieve code execution before the data is ever validated.
How have AI assistants and vibe coding increased insecure deserialization risk?
Assistants reach for pickle, native serialization, or unsafe YAML load because they are concise and work in the demo, learned from examples that deserialize rich objects. Vibe coding ships that loader unread, so an unsafe deserializer on untrusted data reaches production, and trusted payloads in testing hide the flaw until a crafted object arrives.
How do I fix insecure deserialization?
Deserialize untrusted input only with a data-only format such as JSON into known structures, avoid native object serialization across trust boundaries, restrict allowed types with an allow-list and verify integrity if a rich format is unavoidable, sign or MAC serialized data, and add a CI rule flagging unsafe deserialization.

Related weaknesses

Catch CWE-502 before you ship

Tethlyn Audit scans your codebase and its git history for CWE-502 and the rest of the injection & code vulnerabilities class, then ranks each finding by real exploitability and data sensitivity.

See Tethlyn Audit