Scrivio Interview library
ConceptIntermediate7 min read · updated 12 November 2026

Which Jackson protections exist against malicious JSON such as deeply nested arrays or very long numbers?

Since Jackson 2.15, StreamReadConstraints limit how deep JSON may nest, how long a number may be and how long a string may be, and later 2.x versions added limits such as name length and document size. The defaults are generous, so we check them for our version and lower them where our payloads are small. On top of that we limit request size at the server or proxy, never enable default typing or Id.CLASS for untrusted input, and keep Jackson up to date for security fixes.

Why this question is asked

A JSON parser sits directly behind every public endpoint, so it is the first code an attacker's input reaches. A request does not need to be valid for our business rules to hurt us; it only needs to make the parser spend a lot of memory or CPU. Interviewers ask this question to see whether we know the parser itself is part of our attack surface and which knobs Jackson gives us. In this answer, we will recap the kinds of malicious JSON, see how StreamReadConstraints work and how to configure them, then look at request size limits, the polymorphic typing risk and why updates matter.

1A Quick Recap of Malicious JSON

Most attacks on a JSON parser aim at denial of service, making the server busy or out of memory so that real users cannot be served. Three shapes are common. Deep nesting, such as a hundred thousand opening brackets, can exhaust the thread stack, because a recursive parser or deserializer uses one stack frame per level. Very long numbers, a number with a million digits, are cheap to send but expensive to turn into a BigInteger or BigDecimal, and converting such values can take time that grows faster than the input. Very long strings, hundreds of megabytes in one value, exhaust heap memory.

A fourth risk is different in kind: input that chooses which Java class to create. That is the polymorphic deserialization problem, and it can lead to remote code execution rather than only denial of service. We come back to it below.

Java
String deep = "[".repeat(100_000) + "]".repeat(100_000); // about 200 KB of brackets
String longNum = "{\"amount\": " + "9".repeat(1_000_000) + "}"; // a 1 MB number
String bigStr = "{\"note\": \"" + "a".repeat(50_000_000) + "\"}"; // a 50 MB string
mapper.readTree(deep);
// Jackson 2.15+: StreamConstraintsException, nesting depth exceeds the maximum allowed
// Older versions: StackOverflowError in the deserializer

2How StreamReadConstraints Work

StreamReadConstraints is a small settings object held by the JsonFactory, the part of Jackson that creates parsers. Every parser checks the limits while it reads tokens, so a violation is detected early, while scanning, before Jackson builds objects or numbers from the input. When a limit is exceeded, the parser throws StreamConstraintsException, which in Spring MVC ends up as a 400 Bad Request.

The limits introduced in 2.15 are maximum nesting depth (default 1,000 levels), maximum number length (default 1,000 characters) and maximum string length (20 million characters in recent 2.x versions; 2.15.0 used a smaller value). Later 2.x releases added a maximum property name length, a maximum document length and a maximum token count, which are off or generous by default. The defaults have changed between minor versions, so check your version's documentation rather than assuming the numbers above.

Step throughWhere each protection applies to a request
  1. Proxy or load balancerRejects bodies over the size limit, e.g. 1 MB
  2. Servlet containerReads the body as a stream
  3. Jackson parserStreamReadConstraints: depth, number and string length
  4. Jackson databindBuilds objects; no default typing, allow-listed subtypes only
  5. Bean ValidationBusiness limits such as @Size(max = 500)

5 steps in order. Play them, or step through them one at a time.

Cheap checks run first, so most hostile input never reaches object creation.

Notice what the defaults are designed for: they stop extreme abuse without breaking any reasonable application. A 20-million-character string is still 40 MB of heap for one value. If our API never accepts strings longer than a few kilobytes, we should set a much lower limit ourselves.

3Configuring Tighter Limits

We set the constraints on the JsonFactory and build the mapper from it. In a Spring Boot 3 application we can do it with a customizer on the mapper that Spring Boot creates, so every controller uses the same limits.

Java
StreamReadConstraints limits = StreamReadConstraints.builder()
.maxNestingDepth(64) // our payloads nest 5 levels at most
.maxNumberLength(40) // enough for any amount or id we accept
.maxStringLength(200_000) // 200 thousand characters per value
.build();
JsonFactory factory = JsonFactory.builder().streamReadConstraints(limits).build();
ObjectMapper mapper = JsonMapper.builder(factory).build();
// Spring Boot 3 with Jackson 2: apply the limits to the auto-configured mapper
@Bean
Jackson2ObjectMapperBuilderCustomizer jsonLimits() {
return builder -> builder.postConfigurer(m -> m.getFactory().setStreamReadConstraints(limits));
}
mapper.readTree("[".repeat(65) + "]".repeat(65));
// StreamConstraintsException: nesting depth (65) exceeds the maximum allowed (64)

In Jackson 3, the same ideas exist but configuration goes through the immutable builders, and Spring Boot 4 uses a different customizer type, so check your version for the exact names. Choose limits from real payloads: look at the deepest and largest valid requests in our logs and leave a comfortable margin.

4Limiting the Request Size Before Jackson

Parser limits work per value; they do not stop a 2 GB body made of many small values. So we also limit the size of the whole request in front of the application. A reverse proxy such as nginx has client_max_body_size, which defaults to 1 MB, and cloud load balancers and API gateways have similar settings. For Spring WebFlux, spring.codec.max-in-memory-size limits how much of a body the codecs buffer, 256 KB by default. For Spring MVC on Tomcat, the body of a JSON request is not limited by the form-post setting, which is another reason to limit it at the proxy or with a small filter that checks Content-Length and counts bytes.

The reason to do this early is cost. Rejecting a request at the proxy costs almost nothing; rejecting it after the application has read 500 MB into memory has already done the damage.

5Avoiding Default Typing and Id.CLASS

The most serious Jackson advisories have come from polymorphic deserialization, where the JSON names the class to create. With default typing enabled, or with @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS) on a broad base type such as Object, an attacker can send the name of a class already on our classpath whose constructor or setters do something dangerous, a so-called gadget. Jackson spent years blocking such classes one by one, and since 2.10 requires a PolymorphicTypeValidator when we activate default typing.

Java
// Dangerous for untrusted input: the JSON chooses the class
mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,
ObjectMapper.DefaultTyping.NON_FINAL);
// Safe pattern: logical names, a closed set of subtypes
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = CardPayment.class, name = "card"),
@JsonSubTypes.Type(value = BankPayment.class, name = "bank")
})
sealed interface Payment permits CardPayment, BankPayment {}

With Id.NAME the input can only pick among the two classes we listed, so there is nothing for a gadget to exploit. Finally, keep Jackson updated. The StreamReadConstraints themselves arrived because of denial-of-service advisories, and fixes only protect us once we upgrade. Dependency scanning, such as Dependabot or OWASP Dependency-Check, should flag old jackson-databind versions in every service.

6Follow-up questions

Why does deep nesting cause a StackOverflowError rather than an OutOfMemoryError?

Because both the parser's handling of containers and Jackson's deserializers for nested objects and collections are recursive: each nesting level adds frames to the thread's stack. The stack is small, often 512 KB to 1 MB per thread, so tens of thousands of levels exhaust it long before the heap fills. The nesting limit stops reading at a fixed depth, before recursion gets that deep.

Can a huge number really slow the server down if it is never used?

It can, because Jackson may convert it while binding, for example into a BigDecimal field, and some conversions such as BigDecimal to BigInteger with a large exponent are expensive. The maxNumberLength check stops the number while it is still being scanned as characters, so the expensive conversion never starts.

Are these limits needed for internal services too?

Yes, with perhaps more generous values. Internal services receive data that originally came from users, and a bug in another service can send huge payloads just as well as an attacker. Consistent limits from a shared configuration are cheap insurance.

7Conclusion

In this answer, we saw the shapes of malicious JSON, deep nesting, huge numbers and huge strings, and how StreamReadConstraints since Jackson 2.15 stop them while the parser reads. We checked the defaults for our version and lowered them to fit our payloads, limited the request size at the proxy, avoided default typing and Id.CLASS in favour of logical type names, and kept Jackson current so that security fixes reach us.

What to say in the interviewAbout 80 seconds aloud

Open with

Since Jackson 2.15 there are StreamReadConstraints on the JsonFactory. They limit nesting depth, by default 1,000 levels, number length, by default 1,000 characters, and string length, around 20 million characters in recent versions, and later 2.x versions added name length, document length and token count limits.

Then, if they want more

The parser checks them while reading tokens and throws a StreamConstraintsException, which becomes a 400, so a deeply nested array no longer overflows the stack and a million-digit number never gets converted.

The defaults are deliberately generous and have changed between minor versions, so I check them for my version and lower them to fit my payloads, for example a nesting depth of 64 and strings of a few hundred kilobytes, through a customizer on Spring Boot's mapper.

Those limits are per value, so I also cap the whole request size at the proxy or gateway, and in WebFlux with the codec max in-memory size. Then the bigger risk: I never enable default typing or Id.CLASS for untrusted input; polymorphism uses Id.NAME with a closed list of subtypes.

And I keep Jackson updated, with dependency scanning, because the constraints themselves came out of security advisories.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 3, 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.
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 "Which Jackson protections exist against malicious JSON such as deeply nested arrays or...": https://scrivio-interview-library.netlify.app/spring-boot/json-serialization/e6bd026754/