Featured on Product HuntReview on Product Hunt
← All posts
Web Application Security

Insecure Deserialization Explained

NovaSafe Team·

Applications often need to turn data into objects and later reconstruct those objects. That process is called deserialization. It becomes a security concern when an application accepts serialized d…

Share

Applications often need to turn data into objects and later reconstruct those objects. That process is called deserialization. It becomes a security concern when an application accepts serialized data it does not trust and reconstructs it without adequate controls.

Insecure deserialization is not limited to one language or file format. The risk depends on the serialization mechanism, the data source, the classes or behaviors available during reconstruction, and how the application uses the resulting objects. This guide provides a practical workflow for evaluating the issue in an authorized development or test environment, then validating and remediating findings.

What is deserialization?

Serialization converts an in-memory value or object into a representation that can be stored or transmitted. Deserialization reads that representation and reconstructs a value or object. Formats range from plain JSON to language-specific binary formats such as Java serialization or Python pickle.

For example, an application might serialize a shopping cart to store it between requests. If the application later accepts a client-controlled representation and reconstructs it using a powerful, general-purpose object deserializer, an attacker may be able to influence more than the expected cart fields. Depending on the technology and application, that could lead to unexpected behavior, denial of service, data exposure, or—in some circumstances—code execution.

The key distinction is data versus behavior. A parser that maps input into a small, explicitly defined data structure has a different risk profile from a deserializer that can instantiate arbitrary classes or trigger behavior while reconstructing objects.

Why insecure deserialization matters

A deserialization vulnerability usually requires a reachable input path and a susceptible processing step. Risk assessment should consider both, rather than treating every use of serialization as equally dangerous.

Potential consequences include:

  • Unexpected application behavior: Inputs may create object states the application does not expect.
  • Denial of service: Excessively large, deeply nested, or specially structured data may consume significant CPU, memory, or stack space.
  • Authorization or integrity problems: If reconstructed values influence identity, roles, prices, or workflow decisions, tampering may affect application behavior.
  • Code execution: Some deserialization mechanisms and environments can invoke dangerous behavior during reconstruction. This outcome is context-dependent; it should not be assumed for every serializer.

A digital signature or message authentication code (MAC) can help detect tampering when implemented and managed correctly. It does not make an unsafe deserializer safe if untrusted parties can still supply data, keys are exposed, or the signed content can be replayed or abused. Prefer a safer data format and constrained input model where practical.

A practical assessment workflow

Perform testing only on systems you own or are explicitly authorized to assess. Use a local instance, test environment, or approved staging system, and avoid submitting crafted inputs to production. The goal is to identify data flows and demonstrate risk with the least intrusive evidence needed—not to execute commands or use weaponized payloads.

1. Inventory serialization and deserialization paths

Search the codebase for serialization APIs, framework bindings, and places where data crosses a trust boundary. Include request bodies, cookies, headers, queues, caches, files, and database fields that may contain serialized values.

Useful initial searches include:

```sh

rg -n -i 'deserialize|objectinputstream|readobject|pickle\.loads|yaml\.load|binaryformatter' .

```

This is a starting point, not proof of vulnerability. Search results need review: names vary by language and library, wrappers may hide the operation, and some matches may be harmless. Also inspect dependencies and framework configuration for automatic binding or object reconstruction.

For each path, document:

  • The input source and who can influence it.
  • The format and deserialization library.
  • Whether the application selects a concrete type or allows types to be chosen from input.
  • What happens immediately after reconstruction.
  • Any authentication, integrity, size, or schema checks performed before use.

2. Trace trust boundaries and data flow

Follow the value from its source to the deserialization call. A value read from a server-controlled file may have a different exposure from a cookie or API request that a remote user can modify. But server storage is not automatically trustworthy: another application component, compromised account, import process, or prior injection flaw might influence it.

Record whether input is authenticated, integrity-protected, access-controlled, and bounded in size. Then check whether any of those protections can be bypassed or applied only after deserialization. Authentication and authorization checks performed after a dangerous reconstruction may come too late to prevent side effects during that process.

3. Review the format and type controls

Determine whether the parser accepts only simple data or can instantiate application and library classes. Look for polymorphic type handling, type names embedded in input, custom constructors, callbacks, and object hooks. Review configured allowlists and deny lists closely; a broad package rule or an incomplete deny list can leave unexpected types available.

Prefer a format and API that produce simple values—such as strings, numbers, arrays, and maps—and explicitly map those values into application types. Do not enable automatic type discovery merely to avoid writing a small mapping layer.

4. Test safely in an authorized environment

Build a focused test around the same parsing path used by the application. Use benign, controlled inputs to verify expected and rejected cases. For example, test a valid object, a missing required field, an unknown field, an invalid type, an oversized input, and a deeply nested input if the parser supports nesting.

Do not use public exploit payloads, gadget chains, or command-execution tests against a live service. If you need to establish whether a potentially dangerous class can be reached, prefer static analysis, a test-only instrumented build, or a harmless sentinel type in an isolated test harness. Capture logs and resource usage, and stop if the test produces unexpected side effects.

A useful test matrix is:

| Case | Expected result |

| --- | --- |

| Valid, schema-conforming data | Accepted and mapped to the intended application model |

| Unknown or unexpected type | Rejected before application logic uses it |

| Missing, malformed, or wrong-type field | Rejected with a controlled validation error |

| Oversized or excessively nested input | Rejected within configured resource limits |

| Tampered integrity-protected data | Rejected before deserialization when possible |

5. Assess impact and prioritize remediation

A finding is more concerning when untrusted users can reach the deserializer, input can select or influence types, and reconstruction can trigger dangerous behavior. Consider the privileges of the application process, the sensitivity of affected data, the exposure of the endpoint, and any compensating controls.

Document evidence without overstating it. Distinguish confirmed facts—such as an externally reachable unsafe deserialization call—from potential consequences that depend on runtime configuration or available classes. This makes the risk assessment useful to developers and reviewers.

Remediation and security best practices

Prefer simple, constrained data formats

For new interfaces, use a format that represents data rather than arbitrary language objects. Parse into primitive values or a small schema, validate types and bounds, and then construct application objects explicitly. A JSON parser is not automatically safe in every configuration: disable polymorphic type resolution unless there is a specific, reviewed need for it.

Avoid unsafe native deserialization for untrusted data

Do not deserialize attacker-controlled input with APIs that can reconstruct arbitrary objects. In Python, for example, `pickle.loads` is not appropriate for untrusted data. Replacing unsafe deserialization with a safer format is generally stronger than trying to make arbitrary object reconstruction safe through filtering alone.

Constrain types when a legacy format must remain

If migration cannot happen immediately, use the strictest type allowlist the platform supports, reject unrecognized classes, and set limits on depth, references, and input size where available. Keep the allowed set small and review changes to it. A filter is a defense in depth measure; it does not replace input trust analysis or a migration plan.

For example, Java applications using native object serialization can apply an `ObjectInputFilter` to constrain classes and resource use. The following is illustrative: replace the example class with the exact types required by your application and test the filter against legitimate data. A deny-by-default policy is preferable to broad package allowances.

```java

ObjectInputFilter filter = ObjectInputFilter.Config.createFilter(

"maxdepth=10;maxrefs=1000;maxbytes=100000;com.example.dto.Cart;!*"

);

try (ObjectInputStream in = new ObjectInputStream(inputStream)) {

in.setObjectInputFilter(filter);

Object value = in.readObject();

// Validate the expected type and fields before use.

}

```

The limits shown are examples, not universal safe values. Choose bounds based on the legitimate data your application needs. Also verify the behavior and configuration of the specific Java version and serialization path in use.

Validate reconstructed data

Validation should cover the application’s actual rules, not only whether parsing succeeded. Check required fields, allowed values, lengths, numeric ranges, relationships between fields, and authorization-sensitive properties. Do not trust a serialized role, user ID, price, or permission merely because it was successfully parsed.

Where clients need to submit data, accept only the fields they are allowed to set. Derive sensitive values on the server and perform authorization checks at the point of use.

Protect integrity when serialized data must cross a boundary

If a design requires clients to hold serialized state, authenticate it with a well-reviewed MAC or signature scheme and protect keys appropriately. Verify integrity before deserializing whenever the format and design permit. Include expiry or versioning where appropriate, and consider replay risks. Integrity protection reduces tampering risk, but it does not replace safer parsing, access control, or resource limits.

Limit resource consumption and monitor failures

Set maximum input sizes and parser limits for nesting, references, and processing time where supported. Return controlled errors to callers and avoid exposing stack traces or internal class details. Log enough context to investigate repeated invalid input, but do not log secrets or entire sensitive payloads.

Validation after remediation

A fix is not complete until the relevant behavior is tested again. Add regression tests that demonstrate both acceptance of legitimate data and rejection of invalid or disallowed input. Then:

1. Confirm the production code path uses the updated parser or filter—not only the test helper.

2. Verify that unexpected types and malformed fields fail safely before sensitive application logic runs.

3. Exercise configured size and depth limits with bounded test inputs.

4. Check that authentication, integrity verification, and authorization occur in the intended order.

5. Review logs and error responses to ensure failures do not leak sensitive implementation details.

6. Repeat dependency and code searches to find parallel deserialization paths.

If behavior differs between local and staging environments, compare runtime versions, serializer settings, dependency versions, and framework defaults. Configuration drift can leave a vulnerable path in place even after code changes.

Troubleshooting common assessment issues

  • Search finds no deserialization API: Check framework data binding, middleware, message consumers, and wrappers. Trace input to object construction rather than relying only on API names.
  • A test input is rejected before reaching the parser: Confirm the route, content type, authentication, and request schema in the isolated environment. Do not weaken production controls just to make a test work.
  • A filter blocks legitimate data: Identify the exact required types and add only those types after review. Avoid solving the problem with a broad package wildcard.
  • A signed value still appears risky: Verify that signature validation happens before reconstruction, keys are not exposed to the client, and replay or overly privileged signed data is addressed. Consider replacing the format.
  • The vulnerability is hard to reproduce: Use static data-flow analysis and a minimal test harness. A lack of a demonstrated exploit does not by itself prove that an unsafe reachable deserialization path is safe.

FAQ

Is every use of deserialization a vulnerability?

No. Deserialization is a normal programming operation. The concern is whether untrusted or insufficiently constrained input can influence a risky reconstruction process or create invalid application state. Assess the source, format, type controls, and downstream use.

Is JSON always safe to deserialize?

No format is automatically safe in every configuration. Basic JSON parsing into primitive data is generally easier to constrain than arbitrary object reconstruction, but polymorphic type features, excessive input sizes, weak validation, and unsafe use of parsed values can still create security problems.

Does signing serialized data eliminate the risk?

Signing or authenticating data can help detect unauthorized modification if verification is correct and happens before deserialization. It does not make a dangerous parser safe in every design, and it does not address exposed keys, replay, unsafe signed content, or resource exhaustion.

How should developers test this issue?

Use an authorized, isolated environment and test the real application path with benign valid and invalid inputs. Prefer code review, static analysis, and harmless test sentinels over exploit payloads. Record the observed behavior and avoid testing command execution against production.

What is the best long-term fix?

Avoid accepting arbitrary serialized objects from untrusted sources. Use a constrained data format, validate a narrow schema, construct application objects explicitly, and enforce authorization and resource limits. For legacy formats, apply strict filters as a temporary or defense-in-depth measure while planning migration.

Conclusion

Insecure deserialization is best evaluated as a data-flow and trust-boundary problem. Find every reconstruction path, identify who can influence its input, understand what types and behaviors the parser permits, and validate the result in an authorized environment. The strongest remediation is usually to replace arbitrary object reconstruction with simple, constrained data parsing and explicit validation. Where legacy serialization remains, strict type and resource controls, careful integrity checks, and regression testing help reduce risk without overstating what any single safeguard can guarantee.