Video in progress
How does the Java deserialization vulnerability work, and how does ObjectInputFilter protect against it?
readObject creates any serializable class named in the bytes and runs its readObject/readResolve code. An attacker builds a chain out of "gadget" classes on the classpath to execute commands. ObjectInputFilter (Java 9+) restricts up front which classes, how much depth and how much size are allowed.
📖 Answer
The key point: the vulnerability is not in your class. During deserialization ObjectInputStream reads the class name from the bytes and creates it; the type you expect, (Order), is not checked until the cast. By the time of the cast, the malicious code has already run.
The protection is an allowlist:
ObjectInputFilter filter = ObjectInputFilter.Config.createFilter(
"com.myapp.dto.*;java.util.*;java.lang.*;maxdepth=10;maxbytes=1048576;!*");
try (var in = new ObjectInputStream(input)) {
in.setObjectInputFilter(filter);
Order o = (Order) in.readObject();
}
The !* at the end means everything not on the list is rejected. You can also set it for the whole JVM with -Djdk.serialFilter=...; Java 17 also added a context-specific filter factory.
The maxdepth, maxarray and maxrefs limits protect against DoS too: deeply nested HashSets inside a small payload can keep the CPU busy for hours.
But the most reliable protection is not reading untrusted data with Java serialization at all. In an audit I look for: where ObjectInputStream is created, whether the data comes from the network, a cookie or a queue, and which format the Redis/JMS serializers use. Jackson has a similar risk: don't leave polymorphic types open with activateDefaultTyping.
Sign up to mark studied questions and save your progress.