Insecure Deserialization

Deserialization of Untrusted Data

CWE-502 · Critical severity · Updated November 6, 2026

What it is

CWE-502 exists when an application deserializes data from an untrusted source using a deserializer that can instantiate arbitrary objects or trigger code paths during reconstruction. Many serialization formats (Python pickle, Java Serializable, PHP unserialize, .NET BinaryFormatter, Ruby Marshal) allow gadget chains that execute attacker-controlled code during deserialization.

Why it matters

Insecure deserialization typically grants remote code execution with the privileges of the deserializing service. Gadget-chain research has produced reliable exploits for most major serialization libraries. The Apache Commons Collections gadget chain alone has been used in dozens of major breaches over the past decade.

Common patterns

  • Python pickle.load or pickle.loads on data from HTTP requests, message queues, or cache backends.
  • Java ObjectInputStream.readObject on data from network sources.
  • PHP unserialize on cookie or query-string values.
  • .NET BinaryFormatter (deprecated in .NET 5, removed by default in .NET 9).
  • Ruby Marshal.load on session data or cache entries.
  • YAML.load (Python) or YAML.load_file without safe_load on user-supplied YAML.

Languages affected

Python, Java, Ruby, PHP, C#

What Deva detects

Deva flags pickle.load, pickle.loads, unsafe yaml.load, Marshal.load, and BinaryFormatter or NetDataContractSerializer use regardless of the data source, and ObjectInputStream.readObject and PHP unserialize when input traces from a request.

Example

Vulnerable

import pickle
import base64
from flask import request

@app.route('/restore', methods=['POST'])
def restore():
    data = base64.b64decode(request.form['state'])
    state = pickle.loads(data)
    return restore_session(state)

Fixed

import json
import os
import hmac, hashlib
from flask import request, abort

SECRET = os.environ['SESSION_SIGNING_KEY'].encode()

@app.route('/restore', methods=['POST'])
def restore():
    payload, sep, sig = request.form.get('state', '').rpartition('.')
    expected = hmac.new(SECRET, payload.encode(), hashlib.sha256).hexdigest()
    if not sep or not hmac.compare_digest(sig.encode(), expected.encode()):
        abort(403)
    state = json.loads(payload)  # JSON cannot instantiate arbitrary classes
    return restore_session(state)

The vulnerable version pickle.loads data straight from a form parameter. Any attacker producing a pickle bytestream achieves remote code execution. The fix uses JSON (which deserializes only built-in types) and an HMAC signature to enforce integrity. The two changes together are the standard defense pattern.

Where this fits in OWASP Top 10

Compliance framework references

These references explain how the weakness relates to control topics. They do not mean Deva offers evaluation for every framework listed. CMMC evaluation and SPRS scoring are currently unavailable. See the FAQ for offered frameworks and preview status. Deva provides code-level evidence for your assessment, without certifying compliance.

FrameworkControls
OWASP Top 10:2025A08:2025 Software or Data Integrity Failures
NIST 800-53 Rev 5SI-10 Information Input Validation, SC-8 Transmission Confidentiality and Integrity
PCI-DSS v4.0.16.2.4 Software engineering techniques

Related CWEs

  • CWE-20 · Improper input validation.
  • CWE-915 · Improperly controlled modification of dynamically-determined object attributes.
  • CWE-470 · Use of externally-controlled input to select classes or code (unsafe reflection).

Related glossary terms