Skip to main content
CriticalCodeOWASP A08:2021CWE-502

How to fix: PHP unserialize on user input

unserialize($_COOKIE['x']) or unserialize($request->body) can trigger __wakeup, __destruct, or other magic methods on attacker-chosen classes, leading to RCE via POP gadget chains.

Why it matters

Object injection via PHP unserialize enables property-oriented programming chains and remote code execution.

The `code-deser-php-unserialize` check verifies that the server does not expose the `deser-php-unserialize` weakness in the code category. The detection runs against the response headers, body, or auxiliary probes (DNS, TLS, async fetch) as appropriate.

How to fix it

  1. 1Use json_decode for any untrusted data instead of unserialize.
  2. 2If serialization is required, pass ['allowed_classes' => false] as the second argument.
  3. 3Audit any class with magic methods (__wakeup, __destruct) for side effects.

Code examples

Avoid unserialize with untrusted data in PHP

PHP
<?php
// BAD: unserialize() with user input can lead to object injection / RCE
$data = unserialize($userInput);

// GOOD: use json_decode() for data from users
$data = json_decode($userInput, true);
// Validate the parsed array before using it
if (!isset($data["id"]) || !is_int($data["id"])) {
    throw new InvalidArgumentException("Invalid input");
}

References

Detected automatically by VulnRadar. The check id is code-deser-php-unserialize, stable across every scan so you can filter or diff on it.

See every code check

Questions

How severe is PHP unserialize on user input?
VulnRadar rates this critical severity in the code category. The severity is fixed per check, not scored per site, so the same finding is the same severity on every scan.
Does VulnRadar check for this automatically?
Yes. It runs as part of every scan under the check id code-deser-php-unserialize, which stays the same between runs so you can filter, diff, or gate CI on it.
Which standards does PHP unserialize on user input map to?
It maps to CWE-502. In the OWASP Top 10 it falls under A08:2021.

Missing a check?

The detection set is open. If you know a misconfiguration this does not catch, propose it: the issue template asks for the threat model, how to detect it, the false-positive risk, and a test fixture, which is the same thing a maintainer would have to work out anyway.

Propose a checkHow a check is built

Scan your own site for php unserialize on user input

VulnRadar runs this check and hundreds more against a URL in seconds. No agent to install, just paste a URL.

Or scan this site as a demoRead the docs

How to fix: PHP unserialize on user input | VulnRadar