Security weaknessCWE Top 25 · #15
CWE-502 Insecure deserialization
Your application rebuilds objects from data an attacker can modify, which can lead to tampering or code execution.
- Senior security engineers
- 540+ projects delivered
- Reply within one business day
Quick answer
What is CWE-502?
Deserialization of Untrusted Data: The product deserializes untrusted data without sufficiently ensuring that the resulting data will be valid.
Business impact
What CWE-502 could cost your business
MITRE documents what this weakness can do. Here is what that means for a website, store or app.
Integrity
Data, pages or code changed
Prices, orders, content or the code your site runs can be altered without your approval.
- Modify Application Data
- Unexpected State
Availability
Downtime and lost sales
Crashes, slowdowns or exhausted resources can take your site, store or app offline.
- DoS: Resource Consumption (CPU)
In plain English
What Insecure deserialization means for your business
- 540+projects delivered
- 9+years in business
- 23+certified experts
A senior engineer replies within one business day.
Serialization turns an object in memory into a string or file so it can be stored or sent. Deserialization turns it back. The weakness appears when an application deserializes data that came from outside, such as a cookie, a form field, an API request or an uploaded file, and trusts the result. Some formats can describe more than plain values. They can name which classes to create, and creating them can trigger code.
An attacker who can change that data can change what your application builds from it. In milder cases they alter values the code assumed were safe, such as a user ID or a price. In serious cases, existing classes in your application or its libraries can be chained together so that loading the data runs commands. In PHP this is often called object injection. The result can be a full server compromise from a single request.
Where it shows up
- PHP applications and WordPress plugins that call unserialize on cookies, form fields, imported settings or values a user could have stored in the database.
- Java and .NET services that accept native serialized objects over HTTP, message queues or remote method calls from clients.
- Python services that load pickle files, cached objects or machine learning model files obtained from users or untrusted sources.
- Session, cart or view-state data sent to the browser in serialized form and read back without a signature check.
Free security check
Is CWE-502 hiding in your site?
A senior engineer checks your site, store or app for this weakness and the rest of the CWE Top 25, then sends a prioritized fix plan within one business day. No obligation.
Official definition
How MITRE defines CWE-502
Deserialization of Untrusted Data
The product deserializes untrusted data without sufficiently ensuring that the resulting data will be valid.
Also known as: Marshaling/Marshalling, Unmarshaling/Unmarshalling, Pickling, Unpickling, PHP Object Injection
Fix it
How to fix Insecure deserialization
Practical steps in priority order. Share them with your developer or agency.
Use plain data formats
Exchange data as JSON or another format that carries only values, and parse it with a standard parser. In PHP, replace unserialize with json_decode for anything that originates outside your application.
Never deserialize untrusted input
Treat cookies, request bodies, uploads and user-editable database fields as untrusted. Keep session and cart state on the server and send the browser only a random identifier, so there is nothing to tamper with.
Sign data you must round-trip
If serialized data has to leave your server, attach an HMAC, which is a keyed integrity check, and verify it before deserializing. Reject anything that fails. Keep the signing key secret and rotate it periodically.
Restrict which classes can load
Configure the deserializer to allow only a short list of expected types. In PHP, set the allowed_classes option to false or a specific list. In Java, use serialization filters. Block everything else by default.
Validate, update and isolate
Copy deserialized values into fresh objects through normal validation instead of using them directly. Keep libraries updated to remove known gadget classes. Run the service with least privilege so a successful attack reaches less.
Signs you may be affected
- Cookies or hidden fields contain long encoded blobs that the server turns back into objects.
- Your code calls unserialize, pickle.load or a native object reader on outside input.
- Import or restore features accept files in a language-specific serialized format.
Questions to ask your developer
- Does our code deserialize anything that comes from a browser, upload or API?
- Is session and cart data stored on the server or in cookies?
- Which plugins or libraries use PHP unserialize or similar functions?
Prevention
What MITRE recommends, phase by phase
Every mitigation from the CWE entry, in the order a project runs: plan, build, test, operate.
- 01
Architecture and Design
- If available, use the signing/sealing features of the programming language to assure that deserialized data has not been tainted. For example, a hash-based message authentication code (HMAC) could be used to ensure that data has not been modified.
- Make fields transient to protect them from deserialization. An attempt to serialize and then deserialize a class containing transient fields will result in NULLs where the transient data should be. This is an excellent way to prevent time, environment-based, or sensitive variables from being carried over and used improperly.
- Employ cryptography of the data or code for protection. However, it’s important to note that it would still be client-side security. This is risky because if the client is compromised then the security implemented on the client (the cryptography) can be bypassed.
- 02
Implementation
- When deserializing data, populate a new object rather than just deserializing. The result is that the data flows through safe input validation and that the functions are safe.
- Explicitly define a final object() to prevent deserialization.
- Avoid having unnecessary types or gadgets (a sequence of instances and method invocations that can self-execute during the deserialization process, often found in libraries) available that can be leveraged for malicious ends. This limits the potential for unintended or unauthorized types and gadgets to be leveraged by the attacker. Add only acceptable classes to an allowlist.
- 03
Operation
- Use an application firewall that can detect attacks against this weakness. It can be beneficial in cases in which the code cannot be fixed (because it is controlled by a third party), as an emergency prevention measure while more comprehensive software assurance measures are applied, or to provide defense in depth [REF-1481].
Detection and evidence
How CWE-502 is found, and where it has hit
The testing methods MITRE recommends, and real vulnerabilities this weakness has caused.
How it is detected
- Automated Static AnalysisAutomated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then.
Real vulnerabilities it caused
- CVE-2024-37052insecure deserialization in platform for managing AI/ML applications and models allows code execution via a crafted pickled object in a model file
- CVE-2024-37288deserialization of untrusted YAML data in dashboard for data query and visualization of Elasticsearch data
- CVE-2024-9314PHP object injection in WordPress plugin for AI-based SEO
- CVE-2019-12799Chain: bypass of untrusted deserialization issue (CWE-502) by using an assumed-trusted class (CWE-183)
- CVE-2015-8103Deserialization issue in commonly-used Java library allows remote execution.
- CVE-2015-4852Deserialization issue in commonly-used Java library allows remote execution.
How we fix it
From finding to fixed: how Codefreex closes CWE-502
- 01
Scan and confirm
We test your site, store or app for this weakness and the rest of the CWE Top 25 with automated scanning and manual review, and confirm what is actually exploitable.
- 02
Fix at the source
We change the code, configuration or dependency that causes it, not just the symptom, and plan the changes to avoid downtime.
- 03
Verify the fix
We retest every finding, check that nothing else broke and hand you a short report you can share with your team, client or auditor.
- 04
Keep it fixed
Updates, monitoring and maintenance so the weakness does not return with the next plugin, theme, app or code change.
Website SecurityPenetration TestingMalware Removal & Hack RepairWordPress Maintenance
Related weaknesses
Weaknesses related to CWE-502
FAQ
CWE-502: FAQs
Short answers for owners and developers. Not sure whether your site is affected? Ask us to check.
How serious is insecure deserialization?
It can be critical. The outcome depends on which classes are available to the application, and ranges from changed data to denial of service to running code on the server. MITRE rates the likelihood of exploitation as medium. Because the worst case is full compromise, it deserves prompt attention wherever untrusted data reaches a deserializer.
Is JSON safe from this problem?
Plain JSON parsing is much safer, because JSON describes only values such as text, numbers, lists and maps. It does not instruct the application to create arbitrary classes. Risk returns if a JSON library is configured to embed type names and build objects from them. Keep that feature off, and validate the parsed values like any other input.
How do I prevent insecure deserialization?
Avoid language-specific serialization for anything that crosses a trust boundary, and use plain data formats instead. Keep state on the server. Where serialized data must travel, sign it and verify the signature first. Limit the classes a deserializer may create. Keep dependencies current, and review plugins and libraries that deserialize on your behalf.
What is CWE-502?
CWE-502, “Deserialization of Untrusted Data”, is a software weakness catalogued by MITRE in the Common Weakness Enumeration. The product deserializes untrusted data without sufficiently ensuring that the resulting data will be valid.
How serious is CWE-502?
MITRE rates the likelihood of exploit as Medium. It can affect integrity, availability, other, with impacts such as modify application data, unexpected state, dos: resource consumption (cpu). It ranks number 15 on the 2025 CWE Top 25 Most Dangerous Software Weaknesses. The real risk depends on where the weakness sits and what data or functions it exposes.
How do you prevent CWE-502?
If available, use the signing/sealing features of the programming language to assure that deserialized data has not been tainted. For example, a hash-based message authentication code (HMAC) could be used to ensure that data has not been modified. When deserializing data, populate a new object rather than just deserializing. The result is that the data flows through safe input validation and that the functions are safe.
Can Codefreex check my site for CWE-502?
Yes. A senior engineer reviews your site, store or app for CWE-502 and the rest of the CWE Top 25, confirms what is exploitable and sends a prioritized fix plan within one business day. Start with the free security check form on this page.
Free security check
Find out if CWE-502 affects your site
Send your URL. A senior engineer checks for this weakness and the rest of the CWE Top 25, and replies within one business day with what to fix first.
- A senior strategist reviews your request, not a sales bot
- Clear scope, timeline and estimate within one business day
- Free 30-minute discovery call, no obligation
- NDA available on request
- Emailhello@codefreex.com
- CallUSA +1 (209) 375-3475 · India +91 790 3375 530
- Prefer to talk?Book a 30-minute call
Weakness data from the Common Weakness Enumeration (CWE®) version 5.0, Copyright © 2006–2026, The MITRE Corporation. CWE is free to use by any organization or individual for any research, development, and/or commercial purposes under the CWE Terms of Use. CWE is a registered trademark of The MITRE Corporation. The business impact, risk profile and plain-English guidance on this page are written by Codefreex and are general information, not a security assessment of your system.