Quick revision
The short answer
The marker interface is the wrong security boundary: it says which classes may be deserialized, but the attacker chooses which class appears in the stream, and any class carrying the marker, including library classes with dangerous side effects, can be chained into a gadget. We replace it with an explicit allow-list enforced by the deserializer (ObjectInputFilter for Java serialization, JEP 290 and JEP 415), move external input to JSON bound to specific DTO types, turn off class-name polymorphism in JSON libraries, and add scanning and tests so the hole cannot reopen.
What to remember
In this answer, we saw that a marker interface lets the attacker choose from every marked class, and that their fields and subclasses widen the choice further, which is how gadget chains get in. The fix moves the decision to the reader: an ObjectInputFilter allow-list with size and depth limits for Java serialization, JSON bound to specific DTOs for external input, logical type names instead of class names for polymorphism, and dependency scanning with rejection tests to keep it that way.
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.
The marker interface is the wrong security boundary: it says which classes may be deserialized, but the attacker chooses which class appears in the stream, and any class carrying the marker, including library classes with dangerous side effects, can be chained into a gadget. We replace it with an explicit allow-list enforced by the deserializer (ObjectInputFilter for Java serialization, JEP 290 and JEP 415), move external input to JSON bound to specific DTO types, turn off class-name polymorphism in JSON libraries, and add scanning and tests so the hole cannot reopen.
Background to this question
Deserialization of untrusted data has caused some of the most serious remote code execution vulnerabilities in the Java ecosystem, and the root cause is nearly always the same design: the incoming bytes decide which classes get created. When a security review flags a homegrown library that uses a marker interface as its safety check, we need a plan that fixes the design, not only the reported gadget. In this answer, we will recap what deserialization and gadget chains are, see exactly why the marker fails as a boundary, and then go through the changes in the order I would make them: an allow-list filter, a move to typed JSON, safe polymorphism settings, and the scanning and tests that keep it fixed.
1A Quick Recap: Deserialization and Gadget Chains
Deserialization turns bytes back into objects. With Java's built-in serialization, the stream itself names the class of every object it contains, and ObjectInputStream creates instances of those classes and runs their readObject methods. Many JSON libraries can do the same when told to read a class name from a field in the document.
A gadget is an ordinary class on our classpath whose code does something powerful when it is deserialized, compared or hashed: it calls a method by reflection, evaluates an expression, or opens a connection. None of these classes is a bug by itself. A gadget chain links several of them inside one crafted payload, so that the act of reading the object graph ends with the attacker's command being executed. The famous chains used classes from widely used libraries, which is why being careful in our own code is not enough.
2Why the Marker Interface Is the Wrong Gate
The library's rule is "if the class in the stream implements SafeToDeserialize, create it". It sounds like an allow-list, but it is really a deny-by-default rule with a door that anyone can walk through, for three reasons.
First, the attacker, not our code, chooses the class. The marker narrows the choice to every class that carries it, and in a large platform that is hundreds of classes across many modules and libraries, written by people who thought they were allowing data to travel, not approving constructors for hostile input. Second, a marked class can have fields typed as Object, List or Map, and the values inside them are read with the same permissive rule, so one marked class is enough to pull in a deep graph. Third, the marker spreads through inheritance: any subclass of a marked class is marked too, including subclasses added years later.
So the fix is not to remove the marker from the reported class. It is to change who decides what may be created: the code that reads the input, with a short, explicit list.
3Fix 1: Enforce an Explicit Allow-List With ObjectInputFilter
Java has a built-in mechanism for this. JEP 290, delivered in Java 9 and backported to Java 8 updates, added ObjectInputFilter: a filter that sees every class, array length and graph depth before an object is created and can reject it. JEP 415, in Java 17, added a filter factory so a different filter can be chosen for each context in the application. A pattern filter lists what is allowed and ends with !* to reject everything else.
- Stream names a classfor example
com.acme.payments.dto.RefundRequest - Filter checks class, depth, sizebefore any constructor or
readObjectruns - Allowed by the list?YesCreate the object and continueNoThrow InvalidClassException: REJECTED
- Fields are read the same wayevery nested class passes the same filter
4 steps in order. Play them, or step through them one at a time.
The decision belongs to the reader of the input, not to the classes being read.
Notice that the limits matter as much as the class list: maxdepth, maxrefs and maxbytes stop payloads that do no code execution at all but exhaust memory with deeply nested collections. In the library, we replace the marker check with a filter built from a list each consuming service declares. The marker can stay as documentation if teams find it useful, but it no longer grants anything.
4Fix 2: Prefer JSON Bound to Specific DTO Types
Filtering makes binary deserialization much safer, but the better long-term fix for external input is to stop letting input name classes at all. With JSON bound to a specific type, our code says which class to create, and the document only supplies values.
The reason this is safer is structural. The set of classes that can be created is the DTO and the types of its fields, all chosen by us and visible in the code. Records make good DTOs here: they are small, immutable and contain no logic that runs during binding. For internal service-to-service traffic, schema-based formats such as Protocol Buffers or Avro give the same property.
5Fix 3: Turn Off Class-Name Polymorphism in JSON Libraries
JSON is only safe if we keep it typed. Jackson can be configured to read the class name from the document, through default typing or through @JsonTypeInfo with Id.CLASS, and that brings the original problem straight back. Where we genuinely need polymorphism, we use logical type names, or a validator that restricts the allowed subtypes.
We should search the codebase for these settings as part of the same change, because a single shared ObjectMapper with default typing enabled undoes every other fix. Package names and some defaults changed in Jackson 3, so check your version when applying these settings.
6Fix 4: Keep It Fixed With Scanning and Tests
Gadgets come from dependencies, and dependencies change every week. We add dependency scanning (OWASP Dependency-Check, the build platform's own scanner or similar) so that known gadget-bearing versions are flagged, and we remove libraries we do not need, since every class on the classpath is a potential gadget.
We also add tests that prove the boundary holds. A test serializes an object of a class that is not on the allow-list, or uses a recorded payload for a known gadget, feeds it to each endpoint that accepts serialized input, and asserts that it is rejected with InvalidClassException or the JSON equivalent. Rolling out, I would first run the new filter in a logging mode on one service to see which classes real traffic uses, then switch to rejecting, then delete the marker-based path.
7Follow-up questions
Is a deny-list of known gadget classes good enough?
No. A deny-list only blocks the chains someone has already published, and new chains are found in popular libraries regularly. An allow-list inverts the burden: anything we did not name is rejected, so an unknown gadget is rejected too. Deny entries can still be added on top as defence in depth.
What is the difference between JEP 290 and JEP 415?
JEP 290 introduced ObjectInputFilter and a single process-wide filter that can be configured with the jdk.serialFilter property, plus per-stream filters. JEP 415, in Java 17, added a filter factory that chooses or combines filters per deserialization context, so a library and the application can each contribute rules without one overwriting the other.
Does this matter if the endpoint is only reachable inside our network?
Yes. Internal endpoints are reached by compromised services, by request forgery from other systems and by messages replayed from queues. Treating any bytes we did not create as untrusted is the safer default, and the allow-list costs very little on trusted traffic.
8Conclusion
In this answer, we saw that a marker interface lets the attacker choose from every marked class, and that their fields and subclasses widen the choice further, which is how gadget chains get in. The fix moves the decision to the reader: an ObjectInputFilter allow-list with size and depth limits for Java serialization, JSON bound to specific DTOs for external input, logical type names instead of class names for polymorphism, and dependency scanning with rejection tests to keep it that way.
Open with
The core problem is that the marker interface lets the incoming data choose the class. Any class that carries the marker can be created from the stream, and in a large platform that includes library classes and future subclasses, and their Object or collection fields pull in even more.
Then, if they want more
That is exactly the surface a gadget chain needs. So I would change who makes the decision.
For anything still using Java serialization, I would put an ObjectInputFilter on every stream, the mechanism from JEP 290 with the per-context filter factory from JEP 415, listing the exact packages each service accepts, ending in bang star to reject everything else, and setting maxdepth, maxrefs and maxbytes against memory attacks.
I would roll it out in logging mode first to learn what real traffic uses. For external input, I would move to JSON bound to specific DTO records, so the code names the class and the document only supplies values.
I would audit every ObjectMapper for default typing or Id.CLASS and replace them with logical type names or a polymorphic type validator. Finally, I would add dependency scanning and tests that send known gadget payloads to each endpoint and assert they are rejected, so the boundary stays closed.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 17, Java 8, 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 "Your platform's serialization library uses a marker interface to allow classes to be...": https://scrivio-interview-library.netlify.app/java/interfaces-and-abstract-classes/f2be5f7311/