Insecure Deserialization in Java and Python Applications

Deserialization runs attacker code before validation, and only format replacement truly fixes it.

· 8 min read · Updated
Cover illustration for “Insecure Deserialization in Java and Python Applications”
OWASP Top 10 · September 30, 2026 · 8 min read · 1,719 words

Why deserialization is a pre-authentication, pre-validation code execution primitive

  • Role: Opens the argument by establishing the single mechanical fact that makes deserialization uniquely dangerous compared to most other vulnerability classes — execution fires before application logic can inspect or reject it.
  • Insecure deserialization is dangerous not because the data itself is executable but because reconstructing an object from a byte stream triggers class methods automatically, before any application-level check can run.
    • A cast such as (Greeting) in.readObject() is not a guard — by the time Java evaluates the cast, the named class has already been loaded and its side-effecting methods have already run
    • Python's pickle.loads() makes method dispatch (via __reduce__) the deserialization process itself; PHP's unserialize() differs, it first reconstructs the object, then triggers magic methods like __wakeup() and __destruct() as a consequence of deserialization
    • This is why the vulnerability lives in data, not in code — the sink (readObject, pickle.loads, unserialize) looks correct in isolation; the bug is the trust boundary around what reaches it
    • Static analyzers flag the call but cannot reason about the bytes arriving — a limitation with direct consequences for detection strategy
  • The impact ceiling is remote code execution at the process's privilege level, but the same primitive supports authentication bypass, privilege escalation, and denial of service depending on what the deserialized object does
    • Authentication bypass: a manipulated session or user object can grant access without credentials
    • Denial of service: recursive object graphs, hash collisions, and billion-laughs payloads exhaust memory or CPU during reconstruction
    • These are not lesser variants — they follow from the same root condition
  • Note: Keep this section tight and mechanical. Do not survey all languages yet — the goal is one shared mental model the Java and Python sections will build on. Introduce the attacker's framing: "I don't need the application to run my code; I just need it to deserialize my data."

Where serialized data crosses trust boundaries: the attack surface developers underestimate

  • Role: Bridges the "why dangerous" opener to the "how it works by language" sections by showing that the real problem begins before any gadget chain — at the moment an application accepts serialized bytes from an untrusted source.
  • Most deserialization vulnerabilities are exploited not through obvious API endpoints but through channels developers do not think of as deserialization surfaces at all.
    • Serialized blobs arrive in HTTP cookies, URL parameters, binary protocol messages, message queue payloads, API responses, and cache entries
    • Java serialized payloads are identifiable by the magic bytes 0xACED0005 — a reliable fingerprint in HTTP traffic, cookies, and binary protocols that can be used during code review and traffic inspection
    • The Jenkins CLI protocol is a concrete example: attackers sent crafted objects over a channel not designed to carry user-controlled data, gaining full control of build servers
  • The supply-chain entry point is a distinct and growing trust-boundary problem, especially in Python and ML pipelines
    • An AI/ML team that runs torch.load() on a community-contributed checkpoint is deserializing untrusted data — whether or not they frame it that way
  • Note: Use this section to shift the developer's frame from "I don't call pickle on user input" to "let me enumerate every path where serialized bytes reach a sink." That reframe is what the detection section later depends on.

How Java gadget chains turn legitimate classpath libraries into a weapon

  • Role: First of two deep-mechanics sections. Establishes the gadget chain concept in Java — the core intellectual challenge of the vulnerability class — so the Python section can be understood as a parallel but distinct model.
  • A Java deserialization exploit does not require the application to ship malicious code — it requires only that a gadget-bearing library is on the classpath.
    • Every class implementing java.io.Serializable that defines readObject() or readResolve() can execute custom logic during deserialization
    • A gadget chain is a sequence of such classes, already present on the classpath, whose deserialization methods cascade until a dangerous sink — typically Runtime.exec() — is reached
    • The attack requires only that these libraries are on the classpath, not that the application directly invokes their dangerous methods
  • The "dormant gadget" problem: a safe dependency update can silently activate a new chain
    • Gadget chains exploit the interaction between classes, not a bug in any single class — removing one library closes one chain but leaves others intact
    • Patching individual gadgets is therefore a losing strategy; the correct fix operates at the trust boundary, not the gadget inventory
    • The Russian-doll metaphor from the comprehensive guide: each gadget opens to reveal the next, and the attacker's payload is the outermost doll
  • Beyond ObjectInputStream: other Java sinks that require independent hardening
    • XMLDecoder.readObject(), XStream.fromXML(), SnakeYAML Constructor() (CVE-2022-1471), and Jackson ObjectMapper with enableDefaultTyping() are all distinct deserialization paths
  • Real instance: the Cisco Identity Services Engine flaw (2025) allowed authenticated users (including read-only admins) to send crafted serialized Java objects to an affected API, leading to arbitrary command execution with root-level privileges (CVE-2025-20124); later 2025 ISE flaws such as CVE-2025-20281 and CVE-2025-20337 enabled the same outcome without any authentication, risking network-wide credential exposure and disruption of access controls
  • Note: Use the CommonsCollections chain as the worked example — it is well-documented in the sources and concrete enough to make "gadget chain" a felt concept rather than a label. Do not turn this into a ysoserial tutorial; the goal is conceptual clarity about why patching library X doesn't close the problem.

How Python's pickle executes arbitrary code by design, and why yaml.load() is the same problem

  • Role: Shifts from Java's gadget-chain model to Python's fundamentally different — and in some ways more direct — execution model, showing that the same root condition manifests differently and requires different technical answers.
  • Python's pickle is not a data format with an execution side effect — it is a stack virtual machine that treats serialized bytes as instructions, making arbitrary code execution its intended design rather than an abuse of it.
  • yaml.load() with the default or FullLoader is a parallel sink: a YAML document embedding!!python/object/apply:os.system ["id"] achieves remote code execution on deserialization
    • This is a shadow vulnerability pattern: the sink is indirect (a dependency's dependency), making it invisible to developers who audit their own import statements
  • The ML pipeline dimension makes this a supply-chain problem, not just an application-security one
    • CVE-2025-32444 (vLLM, CVSS 10.0) is a concrete instance of a critical deserialization flaw in an ML serving framework
    • A further CVE against LightLLM is a further example from the same ecosystem
    • PickleScan, a commonly recommended mitigation, is fundamentally fragile — its detection can be evaded, making it an inadequate substitute for replacing pickle with a safe format
    • torch.load() migration to safer alternatives is incomplete across the ecosystem as of the sources
  • Note: The through-line for this section is "pickle is a VM, not a serializer" — say it early and let the rest of the section demonstrate it. The ML angle is important for the audience of technical founders and engineering leaders who may not think of model loading as a security event.

How to find deserialization sinks in a real codebase before an attacker does

  • Role: Pivots from mechanics to detection, translating the conceptual understanding of gadget chains and pickle's VM model into concrete, actionable audit steps — the bridge from "understanding the attack" to "finding it in my code."
  • Detection requires both static taint analysis of the codebase and dynamic testing of running applications, because not every deserialization call is reachable through code review alone.
    • Static approach: grep and taint-tracing from sources (HTTP parameters, cookies, queue payloads, file reads) to known sinks by language
    • Java sinks to grep for: ObjectInputStream.readObject(), XMLDecoder.readObject(), XStream.fromXML(), SnakeYAML Yaml.load(), Jackson ObjectMapper with enableDefaultTyping()
    • Python sinks to grep for: pickle.loads, pickle.load, cPickle, joblib.load, torch.load, numpy.load(allow_pickle=True), yaml.load (without safe_load), dill.loads, marshal.loads, jsonpickle.decode, shelve
    • CodeQL and Semgrep can express taint-source-to-sink patterns that scale across large codebases where manual grep is impractical
  • Dynamic detection looks for the runtime signatures of deserialization, not the source-code signatures
    • Java: the magic bytes 0xACED0005 in HTTP traffic, cookies, or binary protocols identify native Java serialization payloads in transit — a signal that belongs in WAF rules and traffic inspection
    • Runtime telemetry: logging and alerting on deserialization operations, especially those originating from external sources, surfaces exploitation attempts that bypass static analysis
    • The Markov-chain approach from Oracle Labs research (ICSE-NIER'22) uses the ordering and features of class sequences in object graphs to distinguish malicious from benign payloads at runtime — an example of probabilistic detection layered beneath filter-based controls
  • Offensive tooling used defensively: generating payloads with ysoserial (Java) and PHPGGC (PHP) against a staging environment confirms whether a suspected sink is actually reachable and exploitable — proof-of-exploitation, not guesswork
    • A finding that says "this sink exists" without a working exploit chain does not confirm real risk; a finding with a demonstrated payload does
    • This is the distinction between a vulnerability scan and a penetration test
  • Note: End this section on the limits of any single detection layer — static analysis misses runtime-constructed sinks, runtime filters miss novel chains, and scanning for magic bytes misses non-Java formats. That framing sets up the remediation hierarchy in the next section.

The remediation hierarchy: why format replacement is the fix and everything else is defense-in-depth

  • Role: Delivers the actionable resolution of the argument — not a list of mitigations but an ordered hierarchy where the root fix is eliminating the trust boundary problem entirely, and every other control is explicitly labeled as partial.
  • The only complete fix for insecure deserialization is replacing native serialization with a data-only format that cannot embed executable instructions; every other control reduces exploitability without eliminating the root condition.
    • For Python: replace pickle with JSON and strict schema validation; switch yaml.load() to yaml.safe_load() unconditionally — these are two lines of change with no architectural cost in most applications
    • For Java: replace ObjectInputStream-based serialization with Protocol Buffers, JSON with Jackson in safe mode, or another data-only format for any input crossing a trust boundary
    • For PHP: replace unserialize() with json_decode() for data exchange

Sources

  1. Runtime Prevention of Deserialization Attacks
  2. Sleeping Giants -- Activating Dormant Java Deserialization Gadget Chains through Stealthy Code Changes
  3. An In-depth Study of Java Deserialization Remote-Code Execution Exploits and Vulnerabilities | ACM Transactions on Software Engineering and Methodology
  4. Sleeping Giants - Activating Dormant Java Deserialization Gadget Chains through Stealthy Code Changes | Proceedings of the 2025 ACM SIGSAC Conference on Computer and Communications Security
  5. PyTorch Users at Risk: Unveiling 3 Zero-Day PickleScan Vulnerabilities
  6. From .pth to p0wned: Abuse of Pickle Files in AI Model Supply Chains
Filed underOWASP Top 10

More in OWASP Top 10