ValidateObjectInputStream and Input Filtering
What you'll learn: How to protect your application from malicious deserialization by filtering which classes can be instantiated during readObject().
The Problem: Uncontrolled Deserialization
You've learned that deserialization is an attack vector—malicious byte streams can trick your application into instantiating dangerous classes, triggering gadget chains, or causing resource exhaustion. The default ObjectInputStream trusts whatever class names appear in the stream. This is like accepting packages from strangers without checking what's inside.
The Solution: Object Input Filtering (Java 9+)
Starting with Java 9, the ObjectInputFilter interface lets you intercept deserialization attempts and decide which classes are allowed. Think of it as a security checkpoint: every class trying to deserialize must pass through your filter first.
You can:
- Whitelist specific classes (only allow these)
- Blacklist dangerous classes (block these)
- Limit array sizes, object depth, or stream size
How It Works
You create a filter that examines each class before deserialization proceeds. The filter returns:
ALLOWED– proceed with deserializationREJECTED– throwInvalidClassExceptionUNDECIDED– defer to next filter or default policy
Filters are registered either per-stream or globally using ObjectInputFilter.Config.
Practical Approach
Whitelist your known-safe classes (your domain objects), reject everything else. This prevents attackers from slipping in harmful classes like Runtime or obscure JDK classes used in gadget chains. For extra safety, limit object graph depth and array sizes to prevent resource exhaustion attacks.
Key Takeaway:
ObjectInputFilter is your first line of defense against deserialization attacks—always whitelist expected classes and reject unknowns, treating untrusted byte streams as hostile until proven safe.