Quick revision
The short answer
With JsonTypeInfo.Id.CLASS or default typing, the JSON itself names the Java class Jackson should create, so whoever writes the JSON chooses the class. Some classes on a typical classpath do dangerous things when they are created or their setters are called, such as looking up a remote JNDI resource or loading code, and chains of such classes, called gadget chains, can lead to remote code execution. Many jackson-databind CVEs came from exactly this. The safe approach is logical names with Id.NAME and a fixed list of subtypes, and if default typing is truly needed, a PolymorphicTypeValidator with a strict allow list.
What to remember
In this answer, we saw that Id.CLASS and default typing let the JSON name the class Jackson creates, so the author of the JSON chooses which constructors and setters run. Gadget classes on the classpath turn that into remote lookups, code loading or file access, and Jackson's block list could not keep up with new gadgets, which is where many jackson-databind CVEs came from. The safe design uses Id.NAME with a fixed list of subtypes, and where default typing is truly needed, activateDefaultTyping with a PolymorphicTypeValidator that allows only our own packages.
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.
With JsonTypeInfo.Id.CLASS or default typing, the JSON itself names the Java class Jackson should create, so whoever writes the JSON chooses the class. Some classes on a typical classpath do dangerous things when they are created or their setters are called, such as looking up a remote JNDI resource or loading code, and chains of such classes, called gadget chains, can lead to remote code execution. Many jackson-databind CVEs came from exactly this. The safe approach is logical names with Id.NAME and a fixed list of subtypes, and if default typing is truly needed, a PolymorphicTypeValidator with a strict allow list.
Why this question is asked
Deserialization looks like a passive operation: we read some text and get an object. But to build that object, Jackson calls constructors and setters, and if an attacker can choose the class, they choose which code runs. That is the whole problem, and it is why a few lines of convenient configuration became one of the most patched areas of the Java ecosystem. Interviewers ask this to see whether we understand the mechanism, not just the rule. In this answer, we will see how class-based type ids work, how a gadget turns object creation into an attack, why the CVEs kept coming, and how to configure polymorphism safely.
1A Quick Recap: Class Names in JSON
Jackson's polymorphic handling needs a type id to decide which class to create. With Id.NAME, the id is a logical name from a list we wrote, such as card. With Id.CLASS, the id is a fully qualified class name, such as com.acme.Card, and Jackson loads whatever class the string names, as long as it is assignable to the declared type. Default typing goes further: enabling it on a mapper adds class-based type ids to many or all properties automatically, often including those declared as Object.
Both are convenient for persisting arbitrary object graphs, for example in a cache, because no list of subtypes is needed. That convenience is the danger: if the declared type is Object, Serializable or a broad interface, almost any class on the classpath qualifies.
2How a Gadget Turns Creation Into an Attack
A gadget is a class that is harmless in normal use but performs a powerful action as a side effect of construction or of a setter. A well-known historical example is a JDBC row-set class in the JDK whose setters, when called in a certain order, make it connect to a data source by name through JNDI. If that name is an LDAP or RMI URL controlled by an attacker, older JVMs could be made to load and run remote code. Other gadgets live in popular libraries: connection pools, XML processors, template engines, logging and scripting libraries.
Jackson does exactly what it always does: creates the named class and calls its setters for the properties in the JSON. Nothing in Jackson is broken; it is simply following instructions, and with Id.CLASS on an open type the instructions come from the attacker. A chain of gadgets combines several such classes so that one object's side effect triggers the next, ending in code execution, file access or a request to an internal network.
5 messages in order. Play them, or step through them one at a time.
The service never calls the gadget; deserialization does it on the attacker's behalf.
3Why the CVEs Kept Coming
Jackson's first response, around 2017, was a block list: a list of known gadget classes that it refused to create even with Id.CLASS or default typing. That helped, but a block list can only list what is already known. Researchers kept finding new gadgets in other libraries, and each one produced a new CVE and a new patch release of jackson-databind. Over the following years there were dozens of them, which is why many organisations remember that period as a constant stream of urgent Jackson upgrades.
The lasting fix came in Jackson 2.10, which replaced enableDefaultTyping() with activateDefaultTyping(), a method that requires a PolymorphicTypeValidator: an object that decides which classes may be created. The burden moved from Jackson guessing what is dangerous to us declaring what is allowed. An allow list does not need updating when someone discovers a new gadget, because the gadget is not on it.
4The Safe Alternative: Logical Names and a Fixed List
For APIs and events, the answer is Id.NAME with @JsonSubTypes, or registered named types. The JSON carries a name such as order.paid, and only the classes we listed can ever be created. This also keeps our package names out of the wire format, so we can rename classes without breaking clients.
5If Default Typing Is Really Needed
Some uses genuinely need class names, such as a cache that stores arbitrary values with GenericJackson2JsonRedisSerializer from Spring Data Redis, which relies on default typing. Then we activate it only with a validator that allows our own packages and the few library types we store, and nothing else. The data should also come only from sources we control; a cache written by our own services is very different from a request body.
| Configuration | Who picks the class | Use it for |
|---|---|---|
Id.NAME + @JsonSubTypes | our fixed list | APIs, events, anything external |
| Default typing + strict allow list | validator, our packages only | internal caches we write ourselves |
Id.CLASS on a narrow sealed base | JSON, limited to subtypes | avoid: prefer Id.NAME |
Id.CLASS on Object / default typing, no validator | the JSON author | never |
Notice that the validator works by package prefix or by type, and the prefixes end with a dot so com.acme.cache cannot match com.acme.cachehack. Allowing a prefix as broad as java. or org. would defeat the purpose, because gadgets have lived in such packages. Jackson 3 keeps the validator approach with some renamed types and stricter defaults; check the migration notes for the exact API in your version.
6Follow-up questions
Is Id.CLASS safe if the base type is my own interface?
It is much less dangerous, because Jackson only creates classes assignable to that interface, and gadgets in third-party libraries do not implement it. But it still exposes package names and ties the format to class names, so Id.NAME remains the better choice. The real danger comes with broad base types such as Object, Serializable or Comparable, which almost every class satisfies.
Do I need to worry if I never use @JsonTypeInfo or default typing?
Much less. Plain Jackson data binding only creates the declared types of our classes, so the client cannot choose arbitrary classes. We should still keep jackson-databind up to date, check libraries that enable default typing internally, and look for @JsonTypeInfo on Object-typed fields in shared code.
How do I find risky configuration in a large codebase?
Search for enableDefaultTyping, activateDefaultTyping, Id.CLASS and Id.MINIMAL_CLASS, and for serializers in libraries that use default typing, such as generic Redis or cache serializers. Static analysis tools and dependency scanners also flag known patterns, and each hit should either move to Id.NAME or get a strict validator.
7Conclusion
In this answer, we saw that Id.CLASS and default typing let the JSON name the class Jackson creates, so the author of the JSON chooses which constructors and setters run. Gadget classes on the classpath turn that into remote lookups, code loading or file access, and Jackson's block list could not keep up with new gadgets, which is where many jackson-databind CVEs came from. The safe design uses Id.NAME with a fixed list of subtypes, and where default typing is truly needed, activateDefaultTyping with a PolymorphicTypeValidator that allows only our own packages.
Open with
The problem is that with Id.CLASS, or with default typing, the JSON contains the fully qualified class name, and Jackson creates whatever class it names. To build it, Jackson calls the constructor and the setters for the properties in the JSON.
Then, if they want more
So the person who writes the JSON decides which code runs during deserialization.
If the declared type is broad, like Object, almost any class on the classpath qualifies, and some classes have dangerous side effects when created or configured: there was a well-known JDK row-set class whose setters trigger a JNDI lookup to a URL the attacker controls, and many gadgets in popular libraries like connection pools and XML or template engines.
Chained together, these gadget chains can give remote code execution. Jackson first fought this with a block list of known gadgets, but every newly found gadget meant a new CVE and a patch, and there were dozens of them.
Since Jackson 2.10 the safe way to use default typing is activateDefaultTyping with a PolymorphicTypeValidator, which is an allow list, so unknown gadgets are rejected automatically. For APIs and events, I avoid class names altogether: Id.NAME with @JsonSubTypes, so the JSON carries a logical name and only my listed classes can be created.
Default typing with a strict validator is something I'd only accept for internal data like a cache we write ourselves.
About this answer
- Last updated
- 12 November 2026
- 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
- Spring Framework reference, Spring Web MVC
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 "Why is using JsonTypeInfo.Id.CLASS or enabling default typing dangerous on data from...": https://scrivio-interview-library.netlify.app/spring-boot/json-serialization/5cc428089b/