Quick revision
The short answer
We create a pattern filter with ObjectInputFilter.Config.createFilter("maxdepth=10;maxarray=10000;com.acme.dto.*;java.lang.*;java.util.*;!*") and set it on the stream with in.setObjectInputFilter(filter) before the first readObject(). Class patterns are checked left to right, and the trailing !* rejects every class not allowed earlier. For the whole JVM we pass the same pattern as -Djdk.serialFilter=... or set the jdk.serialFilter security property, and since Java 17 a filter factory set with jdk.serialFilterFactory decides how JVM-wide and per-stream filters combine.
What to remember
In this answer, we built a pattern filter with ObjectInputFilter.Config.createFilter, combining limits on depth and array size with an allow-list of com.acme.dto, java.lang and java.util, and a trailing !* that rejects everything else. We set it with setObjectInputFilter before reading, set the same pattern JVM-wide with -Djdk.serialFilter or the java.security property, and used a Java 17 filter factory to merge stream filters with the JVM-wide one so libraries can only tighten the rules.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We create a pattern filter with ObjectInputFilter.Config.createFilter("maxdepth=10;maxarray=10000;com.acme.dto.*;java.lang.*;java.util.*;!*") and set it on the stream with in.setObjectInputFilter(filter) before the first readObject(). Class patterns are checked left to right, and the trailing !* rejects every class not allowed earlier. For the whole JVM we pass the same pattern as -Djdk.serialFilter=... or set the jdk.serialFilter security property, and since Java 17 a filter factory set with jdk.serialFilterFactory decides how JVM-wide and per-stream filters combine.
Why this question is asked
Deserialization filters are the standard mitigation when Java serialization cannot be removed straight away, for example in an internal protocol, a cache or a session store. Writing one looks easy, but the details matter: where the filter is set, what a pattern really matches, and what happens when a JVM-wide filter and a stream filter both exist. Interviewers ask for code to see whether we know those details. In this answer, we will recap what a filter is, write the per-stream filter and test it, explain the pattern rules, set the same rules for the whole JVM, combine them with a filter factory, and finally discuss the risk of allowing all of java.util.
1A Quick Recap of ObjectInputFilter
Since Java 9, ObjectInputStream can consult a filter while it reads. The filter is called for each class it is about to resolve, and also for array lengths, the graph depth, the number of references and the number of bytes read so far. For each call the filter answers ALLOWED, REJECTED or UNDECIDED. A REJECTED answer stops reading at once with InvalidClassException, before the class's readObject can run. That timing is what makes filters useful: they act during reading, unlike a cast after it.
2Writing the Per-Stream Filter
The simplest way to build a filter is from a pattern string. Limits and class patterns go into the same string, separated by semicolons.
Notice the two ways a stream is rejected: by its class, and by its shape. The limits stop denial-of-service streams such as a declared array of two billion elements or very deeply nested collections, even when every class is allowed. setObjectInputFilter must be called before reading starts; calling it after readObject() throws IllegalStateException.
3How the Patterns Are Matched
Class patterns are checked from left to right, and the first one that matches the class decides: a plain pattern allows, a pattern starting with ! rejects. If nothing matches, the answer is UNDECIDED. That is why the trailing !* matters so much: without it, any class we did not list would be UNDECIDED, and ObjectInputStream treats UNDECIDED as allowed. With it, our list becomes a true allow-list.
Each pattern form matches something specific. com.acme.dto.* matches classes directly in that package but not in sub-packages; com.acme.dto.** would include sub-packages. java.lang.* therefore does not include java.lang.reflect or java.lang.invoke. A pattern without a wildcard names one class, and a pattern ending in a plain * matches by prefix. For arrays the filter checks the element type, so CustomerDto[] is allowed because CustomerDto is; arrays of primitives are not matched by class patterns and are governed only by the limits. The limits are checked separately from the class patterns, whatever their position in the string.
- Over any limit?depth, array length, refs, bytesYesREJECTEDNocheck class patterns
- com.acme.dto.* matches?YesALLOWEDNonext pattern
- java.lang.* or java.util.* matches?YesALLOWEDNonext pattern
- !* matches?always trueYesREJECTEDNoUNDECIDED (never reached)
4 steps in order. Play them, or step through them one at a time.
Order matters for class patterns: the first match wins.
4Setting the Filter for the Whole JVM
A per-stream filter only protects the streams our code creates. Libraries such as caches or RMI create their own ObjectInputStreams, and we cannot always reach them. For those, Java has a JVM-wide filter that applies to every stream without its own filter. We can set it with a system property on the command line, or in the jdk.serialFilter security property in the JDK's conf/security/java.security file; the system property wins if both are set.
The pattern syntax is the same as for createFilter. In production we would test the JVM-wide filter carefully first, because it also applies to framework code we did not write, and an over-strict filter can break a cache or a management tool in ways that only show up under load.
5Combining Filters With a Filter Factory
Before Java 17, a stream filter simply replaced the JVM-wide filter for that stream, so a library that set a loose filter could weaken our global rules. Java 17 added filter factories (JEP 415). A factory is a BinaryOperator<ObjectInputFilter> that the JVM calls when a stream is created and again when setObjectInputFilter is called, passing the current filter and the requested one; whatever it returns becomes the stream's filter. We register it with -Djdk.serialFilterFactory=<class name> or ObjectInputFilter.Config.setSerialFilterFactory.
merge returns REJECTED if either filter rejects, so a library's stream filter can tighten our JVM-wide rules but never loosen them. rejectUndecidedClass turns UNDECIDED for a class into REJECTED, which protects us even when a library's pattern forgot the trailing !*. The factory class must be on the application class path.
6Follow-up questions
What is the risk of allow-listing java.util.* entirely?
java.util holds the collections that are the classic entry points of gadget chains: HashMap and HashSet call hashCode() on their elements while reading, TreeMap and PriorityQueue call comparators. If the element classes are restricted, a chain still needs a gadget class that the filter allows, so the main remaining risk is denial of service, for example nested sets with exponential hashing, which maxdepth limits. But a whole package also allows dozens of classes we never use, and new JDK versions add more. The safer pattern names the exact classes our DTOs use, such as java.util.ArrayList, java.util.HashMap and java.util.CollSer, the serial form that List.of and Map.of write; check the exact class names your data produces by logging what the filter sees.
How can we see what the filter rejects?
The JDK logs filter decisions through the java.io.serialization System.Logger, rejections at a debug level and other decisions at trace, so turning that logger up in a test environment shows every class a stream contains. Check your version's documentation for the exact levels.
Does a filter make Java serialization safe for untrusted input?
It makes it much safer, but an allow-list is only as good as the classes in it, and any allowed class with a careless readObject is still reachable. Filters are a mitigation; for untrusted input the real fix is a format such as JSON or Protobuf.
7Conclusion
In this answer, we built a pattern filter with ObjectInputFilter.Config.createFilter, combining limits on depth and array size with an allow-list of com.acme.dto, java.lang and java.util, and a trailing !* that rejects everything else. We set it with setObjectInputFilter before reading, set the same pattern JVM-wide with -Djdk.serialFilter or the java.security property, and used a Java 17 filter factory to merge stream filters with the JVM-wide one so libraries can only tighten the rules.
Open with
I would create the filter from a pattern string with ObjectInputFilter.Config.createFilter. The string holds limits first, maxdepth=10 and maxarray=10000, and I would usually add maxrefs and maxbytes too, then the allowed packages, com.acme.dto.*, java.lang.* and java.util.*, and finally !*.
Then, if they want more
Class patterns are checked left to right and the first match wins, so the trailing !* turns the list into a real allow-list; without it, unlisted classes would come back UNDECIDED and be accepted. Note that dto.* is only that package; ** would include sub-packages.
I set it with in.setObjectInputFilter(filter) right after creating the ObjectInputStream and before the first readObject, and a rejected class fails with InvalidClassException before its readObject can run. For streams I do not create myself, like those in a cache library, I set the same pattern JVM-wide with -Djdk.serialFilter on the command line or the jdk.serialFilter property in java.security.
Since Java 17 there is also a filter factory, set with jdk.serialFilterFactory, that decides how the JVM-wide and per-stream filters combine; I would merge them so either can reject and wrap the result with rejectUndecidedClass, so a library can tighten but never loosen the rules.
On allowing all of java.util: those collections are the usual gadget entry points and it allows far more than we need, so I would list the exact classes.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 17, Java 9
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Configure a deserialization filter so an ObjectInputStream only accepts classes in...": https://scrivio-interview-library.netlify.app/java/serialization/2a2de52006/