Skip to main content
Insecure deserialization vulnerabilities occur when applications deserialize untrusted data without proper validation or protection. Serialization is the process of converting complex data structures into a format that can be stored or transmitted, while deserialization is the reverse process of reconstructing these data structures.When an application deserializes untrusted data, attackers can manipulate the serialized data to achieve various attacks, including remote code execution, denial of service, authentication bypass, or privilege escalation.Preventing insecure deserialization requires implementing proper input validation, using secure deserialization libraries, and adopting safer alternatives to native serialization mechanisms.
Java’s native serialization mechanism is particularly vulnerable to deserialization attacks, as it allows for arbitrary code execution during the deserialization process.To prevent unsafe Java deserialization:
  • Avoid using Java’s native serialization (ObjectInputStream) for untrusted data
  • Use safer alternatives like JSON, XML, or YAML with appropriate libraries
  • If native serialization is necessary, implement a whitelist of allowed classes
  • Consider using serialization filtering in Java 9+ (ObjectInputFilter)
  • Keep all libraries and dependencies updated
  • Implement proper exception handling
  • Consider using the OWASP SerialKiller or other serialization security libraries
PHP’s unserialize() function can lead to object injection vulnerabilities if used with untrusted data, potentially allowing attackers to instantiate arbitrary classes and trigger unexpected code execution.To prevent unsafe PHP deserialization:
  • Avoid using unserialize() with untrusted data
  • Use JSON or other safer formats instead
  • If serialization is necessary, use the allowed_classes option in PHP 7+
  • Implement proper input validation
  • Consider using cryptographic signatures to verify serialized data integrity
  • Keep PHP and all libraries updated
  • Implement proper exception handling
Python’s pickle module is notoriously dangerous for deserializing untrusted data, as it can execute arbitrary code during deserialization.To prevent unsafe Python deserialization:
  • Never use pickle, marshal, or shelve modules with untrusted data
  • Use safer alternatives like JSON, YAML, or MessagePack
  • Implement proper input validation
  • Consider using schema validation libraries like marshmallow or pydantic
  • If complex object serialization is needed, consider libraries like jsonpickle with safe_mode enabled
  • Implement proper exception handling
  • Keep all dependencies updated
Node.js applications can be vulnerable to deserialization attacks, particularly when using libraries like node-serialize that support serializing functions.To prevent unsafe Node.js deserialization:
  • Avoid using libraries that support serializing functions (like node-serialize)
  • Use JSON.parse for deserialization of untrusted data
  • Implement proper input validation
  • Consider using schema validation libraries like Joi or Yup
  • Implement proper error handling
  • Keep all dependencies updated
  • Consider using Object.freeze() to prevent modification of deserialized objects
APIs that accept serialized data from clients are particularly vulnerable to deserialization attacks, as they often process untrusted data from various sources.To prevent deserialization vulnerabilities in APIs:
  • Use safe formats like JSON for data exchange
  • Implement proper input validation and schema validation
  • Set strict content type requirements
  • Implement proper error handling without exposing sensitive details
  • Use rate limiting to prevent DoS attacks via deserialization
  • Implement proper logging and monitoring
  • Keep all dependencies updated
  • Consider implementing API gateways with additional security controls
Gadget chains are a particularly dangerous aspect of deserialization vulnerabilities, where attackers chain together multiple classes to achieve malicious outcomes during deserialization.To prevent gadget chain attacks:
  • Avoid using native serialization mechanisms with untrusted data
  • Implement class whitelisting for deserialization
  • Use serialization filters (e.g., ObjectInputFilter in Java 9+)
  • Keep all libraries and dependencies updated
  • Consider using deserialization security libraries
  • Implement proper exception handling
  • Consider using runtime application self-protection (RASP) solutions
Deserialization can be exploited for denial of service attacks by crafting serialized data that consumes excessive resources during deserialization.To prevent deserialization denial of service:
  • Implement size limits for serialized data
  • Use timeouts for deserialization operations
  • Implement resource limits (memory, CPU)
  • Consider processing deserialization in a separate process or container
  • Implement proper monitoring and alerting
  • Use rate limiting for endpoints that accept serialized data
  • Consider using more efficient serialization formats
  • Implement circuit breakers for critical systems
Implementing secure deserialization patterns can help mitigate the risks associated with deserializing untrusted data.Key secure deserialization patterns:
  • Use Data Transfer Objects (DTOs) with manual deserialization
  • Implement the Builder pattern with validation
  • Use immutable objects to prevent post-deserialization manipulation
  • Implement factory methods for controlled object creation
  • Use schema validation libraries
  • Implement proper input validation
  • Consider using safer serialization formats
  • Implement proper exception handling
Preventing insecure deserialization requires a comprehensive approach that addresses multiple aspects of the deserialization process.Key prevention strategies:
  • Use safer alternatives to native serialization
  • Implement proper input validation and integrity verification
  • Restrict which classes can be deserialized
  • Protect against resource exhaustion
  • Keep all dependencies updated
  • Implement proper monitoring and logging
  • Follow the principle of defense in depth