Fix it in your stack

Tested fixes for CWE-502 in Express, Flask, Spring Boot, ASP.NET Core, FastAPI, Rails and Laravel: the vulnerable code, the fix, a plausible fix that still fails, and the regression tests.

Definition

MITRE’s description of CWE-502: The product deserializes untrusted data without sufficiently ensuring that the resulting data will be valid.

Common consequences MITRE lists

  • Integrity: Modify Application Data; Unexpected State
  • Availability: DoS: Resource Consumption (CPU)
  • Other: Varies by Context

Why it matters

Native serialization formats can create arbitrary objects, so a crafted payload can trigger gadget chains that run code; many Java, .NET, PHP and Python vulnerabilities work this way.

How to prevent it

  • Do not deserialize untrusted data with native object formats such as Java serialization, .NET BinaryFormatter, PHP unserialize or Python pickle.
  • Exchange plain data such as JSON and parse it into an explicit schema of allowed types and fields.
  • If a native format is unavoidable, allowlist the classes that may be created and authenticate the data with a MAC before parsing it.
  • Bound input size and nesting depth.

Guides on this site

Practice

References

  1. CWE-502 in the MITRE CWE list (version 4.20)
  2. 2025 CWE Top 25 Most Dangerous Software Weaknesses (MITRE CWE Program)
  3. OWASP Deserialization Cheat Sheet

List data from MITRE; the name, definition and consequences are from MITRE’s CWE 4.20 catalogue. A guide is listed as the main subject when the weakness is what the guide is about, and as covering it when a dedicated section or worked example addresses it.

CWE content: Copyright © 2006–2026, The MITRE Corporation. CWE, CWSS, CWRAF, and the CWE logo are trademarks of The MITRE Corporation. CWE™ is free to use by any organization or individual for any research, development, and/or commercial purposes, per these CWE Terms of Use. Accordingly, The MITRE Corporation hereby grants you a non-exclusive, royalty-free license to use CWE for research, development, and commercial purposes. Any copy you make for such purposes is authorized on the condition that you reproduce MITRE’s copyright designation and this license in any such copy. CWE is a trademark of The MITRE Corporation. CWE Terms of Use.