-
-
Notifications
You must be signed in to change notification settings - Fork 818
StreamReadConstraints
StreamReadConstraints were added in Jackson 2.15 to provide configurable limits on streaming input.
They act as guards against malicious or overly large JSON input by preventing processing of "too big" values or structures.
Constraints are registered with a TokenStreamFactory (such as JsonFactory).
If nothing is explicitly specified, default constraints are used.
Constraints can be configured in different ways:
// Option 1: (preferred) use builder directly when constructing JsonFactory
JsonFactory f = JsonFactory.builder()
.streamReadConstraints(
StreamReadConstraints.builder()
.maxNestingDepth(500)
.maxStringLength(10_000_000)
.maxDocumentLength(5_000_000)
.build()
)
.build();
// Option 2: (discouraged) override defaults globally (use with caution!)
StreamReadConstraints.overrideDefaultStreamReadConstraints(
StreamReadConstraints.builder()
.maxNestingDepth(200)
.maxStringLength(5_000_000)
.build()
);Note: Option 2 changes the default constraints used by all new
JsonFactoryinstances unless explicitly overridden.
Currently constrained aspects:
-
Maximum nesting depth
-
Default:
1000 -
Accessor:
getMaxNestingDepth() -
Builder method:
builder().maxNestingDepth(int) - Depth is the number of open objects
{and arrays[that have not yet been closed. - Setting a negative value throws
IllegalArgumentException.
-
Default:
-
Validation helper:
validateNestingDepth(int depth)ThrowsStreamConstraintsExceptionif exceeded.
-
Maximum document length
-
Default: unlimited (
-1) -
Accessor:
getMaxDocumentLength() -
Builder method:
builder().maxDocumentLength(long) - Checked when reading new chunks of input.
-
Default: unlimited (
-
Validation helper:
validateDocumentLength(long len)
-
Maximum token count
-
Default: unlimited (
-1) -
Accessor:
getMaxTokenCount() -
Builder method:
builder().maxTokenCount(long)
-
Default: unlimited (
-
Validation helper:
validateTokenCount(long count)
-
Maximum number length
-
Default:
1000 -
Accessor:
getMaxNumberLength() -
Builder method:
builder().maxNumberLength(int)
-
Default:
-
Validation helpers:
validateIntegerLength(int),validateFPLength(int)
-
Maximum string length
-
Default:
20_000_000 -
Accessor:
getMaxStringLength() -
Builder method:
builder().maxStringLength(int)
-
Default:
-
Validation helper:
validateStringLength(int)
-
Maximum name length
-
Default:
50_000 -
Accessor:
getMaxNameLength() -
Builder method:
builder().maxNameLength(int)
-
Default:
-
Validation helper:
validateNameLength(int)
-
Maximum magnitude of BigDecimal scale
-
Default:
100_000
-
Default:
-
Validation helper:
validateBigIntegerScale(int)
DEFAULT_MAX_DEPTH = 1000DEFAULT_MAX_DOC_LEN = -1DEFAULT_MAX_TOKEN_COUNT = -1DEFAULT_MAX_NUM_LEN = 1000DEFAULT_MAX_STRING_LEN = 20_000_000DEFAULT_MAX_NAME_LEN = 50_000
Fetch the current defaults:
StreamReadConstraints defaults = StreamReadConstraints.defaults();- Use
overrideDefaultStreamReadConstraints(...)only in application code (never in libraries) to avoid interfering with other Jackson usage. - For libraries, configure
ObjectMapperorJsonFactoryinstances individually instead.