Designing Data to Prevent Insecure Deserialization
Learn why restoring untrusted serialized data can cause object tampering, unintended behavior, or resource exhaustion, and how to design safer data flows.
What deserialization does
Serialization converts program data into a form that can be stored or sent; deserialization reconstructs a usable structure from that form. A simple parser may produce strings, numbers, arrays, and maps. Some native object formats or polymorphic library settings can also let input choose classes and invoke reconstruction methods. That turns data handling into object creation and behavior.
An attacker may connect existing application or dependency features into a gadget chain. Depending on the system, consequences include remote code execution, changes to application state or authorization decisions, and denial of service through excessive CPU or memory use. A valid signature does not make old permissions or business rules current.
Is JSON automatically safe?
No. JSON is generally a simpler interchange format than language-native object formats, but parser configuration and post-parse behavior still matter. Type metadata such as $type or @class can select classes, and merging arbitrary JSON into sensitive domain objects can still be unsafe. Prefer fixed data-transfer objects (DTOs), validate schema, types, ranges, lengths, and nesting, then check authorization and business rules separately.
For an illustrative example, suppose a web service restores “user preferences” from a cookie. If it turns the cookie bytes into an object with Python pickle or PHP unserialize(), client-controlled bytes can influence reconstruction. A safer design uses an opaque session ID to look up server-side state, or parses only a few required fields into a fixed JSON DTO and validates them. This is a design example, not a test of a real system. OWASP says never to unpickle untrusted data and recommends safe YAML loaders for untrusted YAML.
Choose safer controls
Remove general-purpose object reconstruction from untrusted inputs. Treat files, queues, databases, and third-party messages as untrusted unless their writer and integrity are established.
Do not let input select arbitrary types. Restrict polymorphic formats to specific required types and verify safe defaults; review user-controlled type tags such as `$type` and `@class`.
Parse into a new DTO and validate required fields, types, ranges, lengths, collection sizes, and nesting. Then check ownership, authorization, and allowed state transitions. Successful parsing does not prove safety or permission.
If object serialization cannot be removed, authenticate data before deserializing it. A signature or HMAC can detect changes to data produced by a trusted key holder, but does not help if the key is exposed or the client can sign. Restrict classes and also cap graph depth, references, array lengths, and input bytes; an allowlist alone is not a complete defense.
Limit input size and processing resources, remove unnecessary gadget-bearing libraries, update vulnerable dependencies, and run parsers with least privilege. Signing does not replace authorization or resource limits.
| Option | Best fit | Trade-off |
|---|---|---|
| Server-side state with opaque session IDs | Browser sessions or sensitive state | Clients cannot alter the stored object, but session storage, expiry, and revocation need management. |
| Simple format plus fixed DTO | API requests and settings | Reduces type selection, but schema, merge behavior, and authorization still need validation. |
| Object deserialization with class and resource filters | Legacy internal data that cannot yet change | Can reduce risk but cannot eliminate filter gaps, bypasses, or resource exhaustion. |
Review and verify
Inventory serialized inputs in request bodies, cookies, headers, hidden fields, uploads, queues, and stored data. Trace each source to the deserialization call and see whether the resulting object affects authorization, files, processes, or sensitive state. Examples to review include Python pickle.load()/loads() and unsafe YAML loaders, PHP unserialize(), Java ObjectInputStream.readObject(), and .NET BinaryFormatter or type-selecting settings. Check current official documentation because safe APIs and defaults vary by version.
In an authorized test environment, use non-destructive malformed input, disallowed fields or types, oversized input, excessive nesting, and boundary values. Do not create payloads that execute code or change real data. Confirm that valid input produces only the expected DTO; invalid input is rejected without exposing internal classes, stack traces, or sensitive values; state remains unchanged; and resource use is bounded. If a control fails, remove unsafe object deserialization or replace it with a fixed-schema parser and DTO rather than relying only on a later signature check.
The key question is not just whether data can be parsed. Ask what types and behavior the input can select, how the restored values affect authorization, and how much resource they can consume.
References
[OWASP Deserialization Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Deserialization_Cheat_Sheet.html)
[MITRE CWE-502: Deserialization of Untrusted Data](https://cwe.mitre.org/data/definitions/502)