Cybersecurity Glossary

What is Insecure Deserialization?

Serialization converts data or an object into a representation that can be stored or transmitted. Deserialization later reconstructs values or objects. Insecure deserialization occurs when an application processes untrusted serialized data while accepting types, properties or side effects that an attacker can control.

What happens during serialization?

Applications serialize sessions, cache entries, queue messages, cookies, API payloads and stored objects. A simple format contains primitive values such as text and numbers. Native object formats can also carry class names, type information, references and object state. During reconstruction, the runtime creates classes and may invoke constructors, setters, destructors or special “magic” methods.

Why is deserialization dangerous?

The data stream appears passive but can determine which objects are created and which methods run indirectly. Public cookies or hidden fields, messages from less-trusted queues, modified cache entries and imported files are risky. Base64 or encryption without verified integrity does not make data trustworthy. A signature helps only when checked before deserialization, keys are protected and no other path admits unsigned objects.

What are gadget and POP chains?

A gadget is existing code in the application or a library with a useful side effect during object processing, such as a file operation, process launch or dynamic call. Attackers chain gadgets by serializing suitable objects and properties. PHP discussions often use POP chain, while Java and .NET commonly use gadget chain. No new code needs to be uploaded; existing classes are abused. Adding a library can therefore make a previously non-exploitable deserialization dangerous.

What impact exists besides code execution?

ImpactExample
Business logicPrice, role, balance or approval state is changed in the object.
AuthenticationA session identity or permission attribute is modified.
File or network accessExisting methods read files, send requests or write to unexpected locations.
Denial of serviceDeep, large or cyclic structures exhaust CPU, memory or recursion.
Code executionA gadget chain ends in process launch or dynamic code evaluation.

Is JSON automatically safe?

Plain JSON describes values and has no executable classes, preventing many classic native gadget chains. Unsafe polymorphic type binding can nevertheless use a JSON type field to instantiate arbitrary classes. Mass assignment, unexpected properties, prototype manipulation, and missing schema or authorization checks remain possible. What matters is how parser and downstream code interpret data, not the filename extension.

How is the risk prevented?

  1. Avoid native objects: Use simple data transfer objects with an explicit schema across trust boundaries.
  2. Restrict types: Disable polymorphic or dynamic resolution and tightly allowlist required types.
  3. Validate fields: Check type, range, size, depth and unknown properties before business logic.
  4. Protect integrity: Prefer server-side state or authenticate data cryptographically, verifying before any side-effecting parse.
  5. Reduce dependencies: Keep libraries current and remove unnecessary classes or dangerous gadgets.
  6. Isolate: Process unavoidable input with low privileges, limits and no unnecessary network access.

Code example: replace native objects with a validated data schema

Vulnerable: A cookie controls which PHP objects are created.

$state = unserialize(
    base64_decode($_COOKIE['checkout_state'])
);
processCheckout($state);

Safer approach: Only expected primitive values are accepted.

$data = json_decode(
    base64_decode($_COOKIE['checkout_state']),
    true,
    16,
    JSON_THROW_ON_ERROR
);

$validated = validator($data, [
    'cart_id' => ['required', 'uuid'],
    'currency' => ['required', Rule::in(['EUR', 'USD'])],
])->validate();

// Reload price, ownership and permissions server-side
$cart = $request->user()->carts()->findOrFail($validated['cart_id']);

allowed_classes => false can further constrain legacy PHP, but does not replace an explicit data schema. Security-relevant state should preferably not come from the client at all.

How is the vulnerability tested?

Testers recognize formats in cookies, binary data, Base64, type fields, errors and framework markers. Harmless property changes or type errors first prove server-side deserialization. Gadget discovery and code execution are controlled and require approval because parsing itself can cause side effects. Size limits, signature verification, alternative input channels and dependency versions are assessed as well.

Penetration Tests

Uncover Security Vulnerabilities

Professional penetration testing for your business

Web Apps
Networks
Mobile Apps
10% New Customer Discount
Plan Now

Analysis tools

  • ysoserial – examine Java gadget chains in authorised tests
  • ysoserial.net – investigate known .NET deserialisation paths

These tools can generate dangerous payloads. Use them only in isolation, under control and with explicit authorisation.

Thank you for your feedback! We will review it and optimize this content.

Do you have feedback on Insecure Deserialization? Tell us!