CWE-502 Base Draft Medium likelihood

Deserialization of Untrusted Data

This vulnerability occurs when an application accepts and processes serialized data from an untrusted source without proper validation, allowing an attacker to manipulate the data to execute…

Definition

What is CWE-502?

This vulnerability occurs when an application accepts and processes serialized data from an untrusted source without proper validation, allowing an attacker to manipulate the data to execute malicious code or cause unexpected behavior.
Serialization is a common technique for converting complex objects into a storable or transmittable format, like JSON or binary streams. The danger arises when an application blindly trusts this serialized data from external sources—such as user inputs, cookies, or network packets—and reconstructs (deserializes) it back into live objects. Without strict controls, an attacker can craft malicious payloads that, when deserialized, trigger dangerous actions like remote code execution, file system access, or denial-of-service attacks. To prevent this, developers should treat all serialized data from outside the application's trust boundary as inherently hostile. Effective defenses include using safe, data-only serialization formats (like plain JSON without code execution features), implementing integrity checks with digital signatures, employing strict type constraints during deserialization, and utilizing security tools that monitor and validate deserialization processes. Never rely on obscurity or client-side checks, as attackers can easily bypass them.
Vulnerability Diagram CWE-502
Deserialization of Untrusted Data Serialized payload { "@type": "Runtime" ".exec": "/bin/calc" } Server deserialize(input) # instantiates classes # calls setters / hooks → executes payload RCE code runs on server as the app user Object construction during deserialization triggers attacker-controlled gadgets.
Auswirkungen in der Praxis

Real-world CVEs caused by CWE-502

  • insecure deserialization in platform for managing AI/ML applications and models allows code execution via a crafted pickled object in a model file

  • deserialization of untrusted YAML data in dashboard for data query and visualization of Elasticsearch data

  • PHP object injection in WordPress plugin for AI-based SEO

  • chain: bypass of untrusted deserialization issue (CWE-502) by using an assumed-trusted class (CWE-183)

  • Deserialization issue in commonly-used Java library allows remote execution.

  • Deserialization issue in commonly-used Java library allows remote execution.

  • Use of PHP unserialize function on untrusted input allows attacker to modify application configuration.

  • Use of PHP unserialize function on untrusted input in content management system might allow code execution.

Wie Angreifer es ausnutzen

Angreiferpfad Schritt für Schritt

  1. 1

    This code snippet deserializes an object from a file and uses it as a UI button:

  2. 2

    This code does not attempt to verify the source or contents of the file before deserializing it. An attacker may be able to replace the intended file with a file that contains arbitrary malicious code which will be executed when the button is pressed.

  3. 3

    To mitigate this, explicitly define final readObject() to prevent deserialization. An example of this is:

  4. 4

    In Python, the Pickle library handles the serialization and deserialization processes. In this example derived from [REF-467], the code receives and parses data, and afterwards tries to authenticate a user based on validating a token.

  5. 5

    Unfortunately, the code does not verify that the incoming data is legitimate. An attacker can construct a illegitimate, serialized object "AuthToken" that instantiates one of Python's subprocesses to execute arbitrary commands. For instance,the attacker could construct a pickle that leverages Python's subprocess module, which spawns new processes and includes a number of arguments for various uses. Since Pickle allows objects to define the process for how they should be unpickled, the attacker can direct the unpickle process to call Popen in the subprocess module and execute /bin/sh.

Verwundbares Codebeispiel

Vulnerable Java

This code snippet deserializes an object from a file and uses it as a UI button:

Verwundbar Java
try {
  	File file = new File("object.obj");
  	ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
  	javax.swing.JButton button = (javax.swing.JButton) in.readObject();
  	in.close();
  }
Sicheres Codebeispiel

Secure Java

To mitigate this, explicitly define final readObject() to prevent deserialization. An example of this is:

Sicher Java
private final void readObject(ObjectInputStream in) throws java.io.IOException {
  throw new java.io.IOException("Cannot be deserialized"); }
What changed: the unsafe sink is replaced (or the input is validated/escaped) so the same payload no longer triggers the weakness.
Präventions-Checkliste

How to prevent CWE-502

  • Architecture and Design / Implementation If available, use the signing/sealing features of the programming language to assure that deserialized data has not been tainted. For example, a hash-based message authentication code (HMAC) could be used to ensure that data has not been modified.
  • Implementation When deserializing data, populate a new object rather than just deserializing. The result is that the data flows through safe input validation and that the functions are safe.
  • Implementation Explicitly define a final object() to prevent deserialization.
  • Architecture and Design / Implementation Make fields transient to protect them from deserialization. An attempt to serialize and then deserialize a class containing transient fields will result in NULLs where the transient data should be. This is an excellent way to prevent time, environment-based, or sensitive variables from being carried over and used improperly.
  • Implementation Avoid having unnecessary types or gadgets (a sequence of instances and method invocations that can self-execute during the deserialization process, often found in libraries) available that can be leveraged for malicious ends. This limits the potential for unintended or unauthorized types and gadgets to be leveraged by the attacker. Add only acceptable classes to an allowlist. Note: new gadgets are constantly being discovered, so this alone is not a sufficient mitigation.
  • Architecture and Design / Implementation Employ cryptography of the data or code for protection. However, it's important to note that it would still be client-side security. This is risky because if the client is compromised then the security implemented on the client (the cryptography) can be bypassed.
  • Operation Use an application firewall that can detect attacks against this weakness. It can be beneficial in cases in which the code cannot be fixed (because it is controlled by a third party), as an emergency prevention measure while more comprehensive software assurance measures are applied, or to provide defense in depth [REF-1481].
Erkennungssignale

How to detect CWE-502

Automated Static Analysis High

Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)

Plexicus Auto-Fix

Plexicus erkennt CWE-502 automatisch und öffnet in unter 60 Sekunden einen Fix-PR.

Codex Remedium scannt jeden Commit, identifiziert genau diese Schwachstelle und liefert einen reviewer-ready Pull Request mit dem Patch. Keine Tickets. Keine Hand-offs.

Häufig gestellte Fragen

Frequently asked questions

Was ist CWE-502?

This vulnerability occurs when an application accepts and processes serialized data from an untrusted source without proper validation, allowing an attacker to manipulate the data to execute malicious code or cause unexpected behavior.

Wie gravierend ist CWE-502?

MITRE stuft die Exploit-Wahrscheinlichkeit als mittel ein — eine Ausnutzung ist realistisch, erfordert aber meist bestimmte Bedingungen.

Welche Sprachen oder Plattformen sind von CWE-502 betroffen?

MITRE lists the following affected platforms: Java, Ruby, PHP, Python, JavaScript, ICS/OT.

Wie kann ich CWE-502 verhindern?

If available, use the signing/sealing features of the programming language to assure that deserialized data has not been tainted. For example, a hash-based message authentication code (HMAC) could be used to ensure that data has not been modified. When deserializing data, populate a new object rather than just deserializing. The result is that the data flows through safe input validation and that the functions are safe.

Wie erkennt und behebt Plexicus CWE-502?

Die SAST-Engine von Plexicus erkennt die Datenfluss-Signatur von CWE-502 bei jedem Commit. Bei einem Treffer öffnet unser Codex-Remedium-Agent einen Fix-PR mit korrigiertem Code, Tests und einer einzeiligen Zusammenfassung für den Reviewer.

Wo erfahre ich mehr über CWE-502?

MITRE veröffentlicht die kanonische Definition unter https://cwe.mitre.org/data/definitions/502.html. Für ergänzende Hinweise kannst du auch die OWASP- und NIST-Dokumentation heranziehen.

Bereit, wenn du es bist

Schluss mit dem Bezahlen pro Entwickler.
Schließ den Kreislauf.

Plexicus ist die KI-native ASPM, die scannt, filtert, fixt, pentestet und erklärt — autonom. Unbegrenzte Entwickler, unbegrenzte Repos, Fair-Use-KI-Aktionen. Echter kostenloser Tarif, €269/mo jährlich, wenn du bereit bist.