Prevent Insecure Deserialization
Never deserialize untrusted data using native serialization formats — use data-only formats (JSON, Protobuf) with schema validation, or enforce strict type allowlists when native serialization is unavoidable.
Why This Is Best Practice
Adopted by: OWASP Top 10 2021 A08 (Software and Data Integrity Failures) covers insecure deserialization. Oracle, Apache, Red Hat, and Cisco have all patched critical RCE vulnerabilities caused by Java deserialization (CVE-2015-4852, CVE-2016-0792, etc). Node.js node-serialize, Python pickle, PHP unserialize, and Ruby Marshal.load are all documented RCE vectors when given untrusted input.
Impact: Apache Commons Collections deserialization RCE (2015) affected WebLogic, JBoss, Jenkins, and dozens of Java application servers — enabling remote code execution with zero authentication. Python pickle.loads(user_input) is equivalent to eval(user_input) — it executes arbitrary code. GitHub's 2017 Enterprise Server RCE was caused by Ruby Marshal deserialization. Insecure deserialization is rated CVSS 9.8 (Critical) in most CVEs.
Why best: Denylisting dangerous classes (Java serialization filters, etc.) is the alternative — it requires knowing every gadget chain in advance and fails when new chains are discovered. Using data-only formats (JSON) eliminates the vulnerability class: JSON can represent data structures but cannot encode executable code.
Sources: OWASP Deserialization Cheat Sheet; Apache Commons Collections CVE-2015-4852; CWE-502; Java Serialization "AppSecCali 2015 — Marshalling Pickles" (Frohoff & Lawrence)
Steps
Prefer data-only serialization formats — JSON, XML (with XXE disabled), MessagePack, Protobuf, or Avro. These encode data, not executable objects.
# BAD — pickle executes arbitrary code on load
import pickle
obj = pickle.loads(user_input)
# GOOD — JSON only represents data
import json
data = json.loads(user_input) # safe; validate schema after
If native serialization is required, validate/authenticate the data before deserializing:
Sign the serialized blob with an HMAC and verify the signature before deserializing:
import hmac, hashlib, pickle
SECRET = b'server-secret-key-min-32-bytes-here'
def serialize_signed(obj):
payload = pickle.dumps(obj)
sig = hmac.new(SECRET, payload, hashlib.sha256).hexdigest()
return sig + ':' + payload.hex()
def deserialize_verified(signed_data):
sig, hex_payload = signed_data.split(':', 1)
payload = bytes.fromhex(hex_payload)
expected = hmac.new(SECRET, payload, hashlib.sha256).hexdigest()
if not hmac.compare_digest(sig, expected):
raise ValueError("Signature mismatch — data tampered")
return pickle.loads(payload)
Java — implement serialization filters (JEP 290):
// Java 9+ serialization filter — allowlist only expected classes
ObjectInputFilter filter = ObjectInputFilter.Config.createFilter(
"com.example.SafeClass;!*" // allow only SafeClass, reject all others
);
ObjectInputStream ois = new ObjectInputStream(inputStream);
ois.setObjectInputFilter(filter);
Object obj = ois.readObject();
Alternatively, use safer alternatives: Jackson (with @JsonTypeInfo restrictions), Kryo with registered-classes-only mode, or FST with class registration.
PHP — avoid unserialize() with untrusted input:
// BAD
$obj = unserialize($_COOKIE['data']);
// GOOD — use JSON
$data = json_decode($_COOKIE['data'], true);
// If unserialize is unavoidable, use allowed_classes:
$obj = unserialize($data, ['allowed_classes' => ['SafeClass']]);
Ruby — avoid Marshal.load with untrusted data:
# BAD
obj = Marshal.load(user_supplied_string)
# GOOD — JSON
require 'json'
data = JSON.parse(user_supplied_string)
Validate schema after deserialization — even with safe formats, verify the structure matches expectations before using the data:
import json
from jsonschema import validate
schema = {"type": "object", "properties": {"user_id": {"type": "integer"}}, "required": ["user_id"]}
data = json.loads(user_input)
validate(instance=data, schema=schema)
Log and alert on deserialization failures — unexpected deserialization errors often indicate exploit attempts:
try:
obj = deserialize(input_data)
except (ValueError, TypeError, pickle.UnpicklingError) as e:
logger.warning("Deserialization failure from %s: %s", request.remote_addr, e)
raise BadRequest("Invalid data format")
Rules
pickle.loads, yaml.load (without Loader=yaml.SafeLoader), Marshal.load, and Java's ObjectInputStream are unsafe on untrusted input — no exceptions.
- YAML
yaml.load() without SafeLoader allows arbitrary Python object construction — always use yaml.safe_load().
- Signing serialized data prevents tampering but does not prevent attacks if the signing key is compromised — defense-in-depth (type allowlists) is still needed.
- Deserialization gadget chains evolve — new chains are discovered in existing libraries regularly; allowlisting is more durable than denylisting.
Common Mistakes
- Using
yaml.load(input) instead of yaml.safe_load(input) — YAML's default loader supports Python object tags and executes arbitrary constructors.
- Base64-encoding the serialized blob and assuming it's safe — encoding != encryption != authentication. Attackers can decode, modify, and re-encode.
- Java
ObjectInputStream without filters — the default behavior accepts all classes, enabling gadget-chain RCE.
- Trusting signed JWTs for complex object reconstruction — JWT payload should contain simple claims, not serialized objects.
1---2name: prevent-insecure-deserialization3description: Use when deserializing data from untrusted sources — user-submitted cookies, API request bodies, message queue payloads, or any format that reconstructs objects (pickle, Java serialization, YAML, PHP serialize).4source: 'OWASP Deserialization Cheat Sheet (owasp.org/www-project-cheat-sheets); OWASP Top 10 2021 A08; CWE-502; NIST NVD deserialization CVE history'5tags: [security, owasp, deserialization, pickle, java-serialization, rce, developer]6metadata:7 source: https://skillmd.com/skills/jeffreytse/prevent-insecure-deserialization8---910# Prevent Insecure Deserialization1112Never deserialize untrusted data using native serialization formats — use data-only formats (JSON, Protobuf) with schema validation, or enforce strict type allowlists when native serialization is unavoidable.1314## Why This Is Best Practice1516**Adopted by:** OWASP Top 10 2021 A08 (Software and Data Integrity Failures) covers insecure deserialization. Oracle, Apache, Red Hat, and Cisco have all patched critical RCE vulnerabilities caused by Java deserialization (CVE-2015-4852, CVE-2016-0792, etc). Node.js `node-serialize`, Python `pickle`, PHP `unserialize`, and Ruby `Marshal.load` are all documented RCE vectors when given untrusted input.17**Impact:** Apache Commons Collections deserialization RCE (2015) affected WebLogic, JBoss, Jenkins, and dozens of Java application servers — enabling remote code execution with zero authentication. Python `pickle.loads(user_input)` is equivalent to `eval(user_input)` — it executes arbitrary code. GitHub's 2017 Enterprise Server RCE was caused by Ruby Marshal deserialization. Insecure deserialization is rated CVSS 9.8 (Critical) in most CVEs.18**Why best:** Denylisting dangerous classes (Java serialization filters, etc.) is the alternative — it requires knowing every gadget chain in advance and fails when new chains are discovered. Using data-only formats (JSON) eliminates the vulnerability class: JSON can represent data structures but cannot encode executable code.1920Sources: OWASP Deserialization Cheat Sheet; Apache Commons Collections CVE-2015-4852; CWE-502; Java Serialization "AppSecCali 2015 — Marshalling Pickles" (Frohoff & Lawrence)2122## Steps23241. **Prefer data-only serialization formats** — JSON, XML (with XXE disabled), MessagePack, Protobuf, or Avro. These encode data, not executable objects.2526 ```python27 # BAD — pickle executes arbitrary code on load28 import pickle29 obj = pickle.loads(user_input)3031 # GOOD — JSON only represents data32 import json33 data = json.loads(user_input) # safe; validate schema after34 ```35362. **If native serialization is required, validate/authenticate the data before deserializing**:3738 Sign the serialized blob with an HMAC and verify the signature before deserializing:3940 ```python41 import hmac, hashlib, pickle4243 SECRET = b'server-secret-key-min-32-bytes-here'4445 def serialize_signed(obj):46 payload = pickle.dumps(obj)47 sig = hmac.new(SECRET, payload, hashlib.sha256).hexdigest()48 return sig + ':' + payload.hex()4950 def deserialize_verified(signed_data):51 sig, hex_payload = signed_data.split(':', 1)52 payload = bytes.fromhex(hex_payload)53 expected = hmac.new(SECRET, payload, hashlib.sha256).hexdigest()54 if not hmac.compare_digest(sig, expected):55 raise ValueError("Signature mismatch — data tampered")56 return pickle.loads(payload)57 ```58593. **Java — implement serialization filters (JEP 290)**:6061 ```java62 // Java 9+ serialization filter — allowlist only expected classes63 ObjectInputFilter filter = ObjectInputFilter.Config.createFilter(64 "com.example.SafeClass;!*" // allow only SafeClass, reject all others65 );66 ObjectInputStream ois = new ObjectInputStream(inputStream);67 ois.setObjectInputFilter(filter);68 Object obj = ois.readObject();69 ```7071 Alternatively, use safer alternatives: Jackson (with `@JsonTypeInfo` restrictions), Kryo with registered-classes-only mode, or FST with class registration.72734. **PHP — avoid `unserialize()` with untrusted input**:7475 ```php76 // BAD77 $obj = unserialize($_COOKIE['data']);7879 // GOOD — use JSON80 $data = json_decode($_COOKIE['data'], true);8182 // If unserialize is unavoidable, use allowed_classes:83 $obj = unserialize($data, ['allowed_classes' => ['SafeClass']]);84 ```85865. **Ruby — avoid `Marshal.load` with untrusted data**:8788 ```ruby89 # BAD90 obj = Marshal.load(user_supplied_string)9192 # GOOD — JSON93 require 'json'94 data = JSON.parse(user_supplied_string)95 ```96976. **Validate schema after deserialization** — even with safe formats, verify the structure matches expectations before using the data:9899 ```python100 import json101 from jsonschema import validate102103 schema = {"type": "object", "properties": {"user_id": {"type": "integer"}}, "required": ["user_id"]}104 data = json.loads(user_input)105 validate(instance=data, schema=schema)106 ```1071087. **Log and alert on deserialization failures** — unexpected deserialization errors often indicate exploit attempts:109110 ```python111 try:112 obj = deserialize(input_data)113 except (ValueError, TypeError, pickle.UnpicklingError) as e:114 logger.warning("Deserialization failure from %s: %s", request.remote_addr, e)115 raise BadRequest("Invalid data format")116 ```117118## Rules119120- `pickle.loads`, `yaml.load` (without `Loader=yaml.SafeLoader`), `Marshal.load`, and Java's `ObjectInputStream` are unsafe on untrusted input — no exceptions.121- YAML `yaml.load()` without SafeLoader allows arbitrary Python object construction — always use `yaml.safe_load()`.122- Signing serialized data prevents tampering but does not prevent attacks if the signing key is compromised — defense-in-depth (type allowlists) is still needed.123- Deserialization gadget chains evolve — new chains are discovered in existing libraries regularly; allowlisting is more durable than denylisting.124125## Common Mistakes126127- **Using `yaml.load(input)` instead of `yaml.safe_load(input)`** — YAML's default loader supports Python object tags and executes arbitrary constructors.128- **Base64-encoding the serialized blob and assuming it's safe** — encoding != encryption != authentication. Attackers can decode, modify, and re-encode.129- **Java `ObjectInputStream` without filters** — the default behavior accepts all classes, enabling gadget-chain RCE.130- **Trusting signed JWTs for complex object reconstruction** — JWT payload should contain simple claims, not serialized objects.