CWE-502 — Deserialization of Untrusted Data
CWE-502: Deserialization of Untrusted Data
MITRE CWE weakness
| Kind | Weakness |
| Abstraction | Base |
| Status | Draft |
| Likelihood of exploit | Medium |
Description
The product deserializes untrusted data without sufficiently ensuring that the resulting data will be valid.
Common consequences
- Integrity: Modify Application Data, Unexpected State
- Availability: DoS: Resource Consumption (CPU)
- Other: Varies by Context
Mitigations
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.
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.
Implementation — Explicitly define a final object() to prevent deserialization.
Architecture and Design — 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.
Implementation — 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. Note: new gadgets are constantly being discovered, so this alone is not a sufficient mitigation.
References
- CWE page: https://cwe.mitre.org/data/definitions/502.html
- CWE list: https://cwe.mitre.org/data/index.html