Insecure deserialization
| Class | Injection |
|---|---|
| Also known as | Deserialization of untrusted data; PHP object injection |
| CWE | CWE-502: Deserialization of Untrusted Data |
| OWASP Top 10 | A08:2025 Software or Data Integrity Failures |
| Typical severity | Critical when it leads to code execution |
| Affects | Python pickle and full YAML loading, PHP unserialize, Java ObjectInputStream, .NET BinaryFormatter, and framework protocols that rebuild objects from requests |
A program that stores an object or sends it to another program first writes it out as text or bytes, a step called serialization; the receiving side reads those bytes and rebuilds the object, which is deserialization.1 Insecure deserialization is the vulnerability that arises when the bytes come from someone the application does not trust and the format lets them decide which objects get built. What the attacker gains depends on what those objects do when they are created: a denial of service, a bypassed access check, or in the worst case their own code running on the server.12
JSON is the common format for this today, and XML before it. Most languages also have a native format of their own, such as Python's pickle, PHP's serialize and Java's object streams, which can record far more than plain values.1 A safe loader limits itself to simple values such as integers and lists.3
Why native formats run code
A native format records the type of each object, and the reader looks that type up and creates an instance of it, so the incoming bytes choose the classes, the values of their fields and how the objects refer to each other.4 Many types run code as part of being rebuilt. PHP, for example, calls an object's __wakeup() or __unserialize() method as soon as it is reconstructed.5
In Python's pickle, the data can name any importable function and the arguments to call it with, and unpickling imports and calls it.6 The following object, once pickled, runs a shell command on whichever machine unpickles it:
import os, pickle
class Payload:
def __reduce__(self):
# unpickling calls os.system("echo hello world")
return (os.system, ("echo hello world",))
data = pickle.dumps(Payload())
pickle.loads(data) # the command runs hereJava and .NET payloads are usually built from classes the application or its libraries already contain. Each one does something small while it is rebuilt, and in the right order the steps open a shell for the attacker; such a sequence is a gadget chain.2 For .NET, Microsoft compares calling BinaryFormatter.Deserialize on a payload to launching that payload as a program.7 Checking the type of the result afterwards does not help, because the code has already run by the time the call returns.1
Where it appears
A YAML document passed to PyYAML's yaml.load with an unsafe loader can call any Python function, as pickle can.31 Model files are another route. CVE-2024-37052 gave code execution on a model management platform through a crafted pickled object inside a model file.2 Data often crosses a trust boundary without the developer noticing. Microsoft's examples include a save file that users later share by email, and an internal server that trusts its desktop clients until malware on one employee's machine starts sending it payloads.7
| Language | Unsafe with data from outside | Replacement |
|---|---|---|
| Python | pickle.loads, jsonpickle.decode | json.loads |
| Python, PyYAML | yaml.load with Loader=yaml.Loader or UnsafeLoader, yaml.unsafe_load | yaml.safe_load |
| PHP | unserialize, whatever allowed_classes is set to | json_decode |
| Java | ObjectInputStream.readObject with no filter | JSON into plain data classes, or a serialization filter |
| .NET | BinaryFormatter, SoapFormatter, NetDataContractSerializer, ObjectStateFormatter; JSON.NET with TypeNameHandling other than None | System.Text.Json |
Sources for the table: Python and PyYAML1, PHP5, Java4, .NET71. Since .NET 9, the built-in BinaryFormatter throws an exception whenever it is used.7
React Server Components, 2025
Frameworks deserialize too. React Server Functions let browser code call a function on the server: React turns the call into an HTTP request, and the server decodes the request back into arguments. On 29 November 2025 the researcher Lachlan Davidson reported that a crafted request to that decoder could run code on the server without logging in. The React team published fixed versions 19.0.1, 19.1.2 and 19.2.1 of the react-server-dom-* packages on 3 December as CVE-2025-55182, rated CVSS 10.0. Next.js, React Router, Waku and other frameworks depended on the affected packages, and an app could be exposed even if it defined no Server Functions of its own.8
The fault was in how Flight, the protocol React uses between server and client, parsed object references during deserialization. Amazon saw exploitation attempts within hours of disclosure, CISA added the CVE to the KEV catalog on 5 December, and attackers installed cryptocurrency miners and tried to read AWS credential files. Shadowserver counted 77,664 vulnerable IP addresses on 5 December and 28,964 two days later.9 A Laravel case from 2018, where a decrypted cookie went straight into unserialize, is described in Securing Rails, Django and Laravel applications.10
Classification
The attacker's data ends up driving code, yet the OWASP Top 10 files this weakness under integrity failures (A08), next to unsigned software updates, and not under Injection. The fault it points to is trust in serialized data the application could not verify. Its example has Spring Boot services that pass serialized user state back and forth with every request from a React front end; an attacker spots the rO0 Java signature in the Base64 and gets code execution on the server.11
Prevention
Keep native formats away from any data that arrives from outside the server. Exchange data as JSON, parse it into plain values, and check those values with Input validation before building any object from them.1
# Python
import json, yaml
state = json.loads(request_body) # not pickle.loads
config = yaml.safe_load(uploaded_file) # not yaml.loadWhen serialized data has to leave the server and come back, as in a cookie or a hidden field, sign it with an HMAC under a server-side key and refuse to deserialize anything whose signature does not match.6511 A signature only proves the server wrote the data, so the signing key needs the same care as any other secret: the Laravel flaw above needed the application key.
Where Java serialization cannot be removed, a serialization filter limits what the stream may contain. A filter is set in code, or for one application with the jdk.serialFilter property on the command line, and can name allowed classes on an allowlist as well as cap sizes. The quotes keep a Unix shell from treating the semicolons as command separators:4
java "-Djdk.serialFilter=maxarray=100000;maxdepth=20;maxrefs=500" com.example.test.ApplicationA flaw inside a framework's own protocol, as with React, is fixed only by upgrading; the React team warned that the temporary mitigations hosting providers added were no substitute for the update.8 Tracking Vulnerable dependencies catches that case.
Finding it
In source code, search for the calls in Table 1. In captured traffic, Java serialized data starts with the bytes AC ED 00 05, which appear as rO0 in Base64, and Base64 pickles often start with gASV.1
Find every place in this project that deserializes data with a native format: pickle, jsonpickle, yaml.load or yaml.unsafe_load, PHP unserialize, Java ObjectInputStream, .NET BinaryFormatter or JSON.NET TypeNameHandling. For each one, say where the data comes from. Where it can come from a request, a cookie, an upload, a cache or another service, replace the call with JSON parsing plus validation, or yaml.safe_load for YAML. Where serialized data must round-trip through the client, add an HMAC signature with a server-side key and reject data that fails it. List each file and line you changed.
In SecFlash
SecFlash reports pickle and full YAML loading in the code as SF-SEC-705 on the Security page, together with code run from strings. A JSF or ASP.NET view state kept on the client without encryption and signing is reported as SF-EXP-407 on the Exposure page.
See also
- Command injection
- Input validation
- Vulnerable dependencies
- Known Exploited Vulnerabilities catalog
- Securing Rails, Django and Laravel applications
- Category: Injection
References
- a b c d e f g h i "Deserialization Cheat Sheet". OWASP Cheat Sheet Series.
- a b c "CWE-502: Deserialization of Untrusted Data". MITRE.
- a b "PyYAML Documentation". pyyaml.org.
- a b c Oracle. "Serialization Filtering". Java SE 17 Core Libraries.
- a b c "unserialize". PHP Manual.
- a b "pickle: Python object serialization". Python documentation.
- a b c d Microsoft. "Deserialization risks in use of BinaryFormatter and related types". Microsoft Learn.
- a b The React Team (3 December 2025). "Critical Security Vulnerability in React Server Components". React Blog.
- back Lakshmanan, Ravie (6 December 2025). "Critical React2Shell Flaw Added to CISA KEV After Confirmed Active Exploitation". The Hacker News.
- back NIST (9 August 2018). "CVE-2018-15133". National Vulnerability Database.
- a b "A08:2025 Software or Data Integrity Failures". OWASP Top 10:2025.
External links
- Restricting Globals, Python documentation, on limiting what an unpickler may import