Course contentsShow
← Security
Lesson 1190 of 3,05224. Application Security - Input ValidationFree lesson

Safe Deserialization Practices

Implementing allowlists, integrity checks, isolated deserialization contexts, and secure parser configurations.

Safe Deserialization Practices

You'll learn how to defend against insecure deserialization by controlling what gets processed, verifying data integrity, and isolating risky operations.

Now that you understand how deserialization attacks work (from gadget chains to type confusion), let's talk defense. Safe deserialization is about treating incoming serialized data like a suspicious package at the airport—you don't just open it blindly.

Core Defense Strategies

Allowlists for Types

Just like you learned with input validation, only permit known-safe classes to be deserialized. Reject anything else by default. In Java, this might mean configuring your ObjectInputStream to check class names before instantiating. In Python, never use pickle.loads() on untrusted data—use safer alternatives like JSON that can't execute arbitrary code.

Integrity Checks

Sign or MAC your serialized data before sending it. When deserializing, verify the signature first. If the data has been tampered with (injected with gadget chains), the signature won't match and you reject it—like a tamper-evident seal on medication bottles.

Isolated Deserialization Contexts

Run deserialization in low-privilege environments or sandboxes. If an attacker manages to trigger a gadget chain, limit the damage by restricting what the process can access—filesystem, network, memory. Think of it like handling unknown chemicals in a fume hood instead of your living room.

Secure Parser Configuration

For XML/JSON parsers, disable dangerous features: turn off external entity resolution (XXE protection), disable type metadata in JSON.NET, restrict maximum nesting depth. Use the safest mode your parser offers and only relax settings when absolutely necessary.

Key Takeaway:
Safe deserialization combines allowlists for permitted types, cryptographic integrity checks, process isolation, and secure parser defaults—never trust serialized data from untrusted sources without verification.