16
16
* </li>
17
17
* <li>Maximum String value length: default 20_000_000 (see {@link #DEFAULT_MAX_STRING_LEN})
18
18
* </li>
19
+ * <li>Maximum Property name length: default 50_000 (see {@link #DEFAULT_MAX_NAME_LEN})
20
+ * </li>
19
21
* <li>Maximum Nesting depth: default 1000 (see {@link #DEFAULT_MAX_DEPTH})
20
22
* </li>
21
23
* </ul>
@@ -45,6 +47,14 @@ public class StreamReadConstraints
45
47
*/
46
48
public static final int DEFAULT_MAX_STRING_LEN = 20_000_000 ;
47
49
50
+ /**
51
+ * Default setting for maximum name length: see {@link Builder#maxNameLength(int)}
52
+ * for details.
53
+ *
54
+ * @since 2.16
55
+ */
56
+ public static final int DEFAULT_MAX_NAME_LEN = 50_000 ;
57
+
48
58
/**
49
59
* Limit for the maximum magnitude of Scale of {@link java.math.BigDecimal} that can be
50
60
* converted to {@link java.math.BigInteger}.
@@ -56,9 +66,11 @@ public class StreamReadConstraints
56
66
protected final int _maxNestingDepth ;
57
67
protected final int _maxNumLen ;
58
68
protected final int _maxStringLen ;
69
+ protected final int _maxNameLen ;
59
70
60
71
private static StreamReadConstraints DEFAULT =
61
- new StreamReadConstraints (DEFAULT_MAX_DEPTH , DEFAULT_MAX_NUM_LEN , DEFAULT_MAX_STRING_LEN );
72
+ new StreamReadConstraints (DEFAULT_MAX_DEPTH , DEFAULT_MAX_NUM_LEN ,
73
+ DEFAULT_MAX_STRING_LEN , DEFAULT_MAX_NAME_LEN );
62
74
63
75
/**
64
76
* Override the default StreamReadConstraints. These defaults are only used when {@link JsonFactory}
@@ -89,6 +101,7 @@ public static final class Builder {
89
101
private int maxNestingDepth ;
90
102
private int maxNumLen ;
91
103
private int maxStringLen ;
104
+ private int maxNameLen ;
92
105
93
106
/**
94
107
* Sets the maximum nesting depth. The depth is a count of objects and arrays that have not
@@ -149,24 +162,47 @@ public Builder maxStringLength(final int maxStringLen) {
149
162
return this ;
150
163
}
151
164
165
+ /**
166
+ * Sets the maximum name length (in chars or bytes, depending on input context).
167
+ * The default is 50,000. This limit is not exact, the limit is applied when we increase
168
+ * internal buffer sizes and an exception will happen at sizes greater than this limit. Some
169
+ * text values that are a little bigger than the limit may be treated as valid but no text
170
+ * values with sizes less than or equal to this limit will be treated as invalid.
171
+ *
172
+ * @param maxNameLen the maximum string length (in chars or bytes, depending on input context)
173
+ *
174
+ * @return this builder
175
+ * @throws IllegalArgumentException if the maxStringLen is set to a negative value
176
+ * @since 2.16.0
177
+ */
178
+ public Builder maxNameLength (final int maxNameLen ) {
179
+ if (maxNameLen < 0 ) {
180
+ throw new IllegalArgumentException ("Cannot set maxNameLen to a negative value" );
181
+ }
182
+ this .maxNameLen = maxNameLen ;
183
+ return this ;
184
+ }
185
+
152
186
Builder () {
153
- this (DEFAULT_MAX_DEPTH , DEFAULT_MAX_NUM_LEN , DEFAULT_MAX_STRING_LEN );
187
+ this (DEFAULT_MAX_DEPTH , DEFAULT_MAX_NUM_LEN , DEFAULT_MAX_STRING_LEN , DEFAULT_MAX_NAME_LEN );
154
188
}
155
189
156
- Builder (final int maxNestingDepth , final int maxNumLen , final int maxStringLen ) {
190
+ Builder (final int maxNestingDepth , final int maxNumLen , final int maxStringLen , final int maxNameLen ) {
157
191
this .maxNestingDepth = maxNestingDepth ;
158
192
this .maxNumLen = maxNumLen ;
159
193
this .maxStringLen = maxStringLen ;
194
+ this .maxNameLen = maxNameLen ;
160
195
}
161
196
162
197
Builder (StreamReadConstraints src ) {
163
198
maxNestingDepth = src ._maxNestingDepth ;
164
199
maxNumLen = src ._maxNumLen ;
165
200
maxStringLen = src ._maxStringLen ;
201
+ maxNameLen = src ._maxNameLen ;
166
202
}
167
203
168
204
public StreamReadConstraints build () {
169
- return new StreamReadConstraints (maxNestingDepth , maxNumLen , maxStringLen );
205
+ return new StreamReadConstraints (maxNestingDepth , maxNumLen , maxStringLen , maxNameLen );
170
206
}
171
207
}
172
208
@@ -176,10 +212,17 @@ public StreamReadConstraints build() {
176
212
/**********************************************************************
177
213
*/
178
214
215
+ @ Deprecated // since 2.16
179
216
protected StreamReadConstraints (final int maxNestingDepth , final int maxNumLen , final int maxStringLen ) {
217
+ this (maxNestingDepth , maxNumLen , maxStringLen , DEFAULT_MAX_NAME_LEN );
218
+ }
219
+
220
+ protected StreamReadConstraints (final int maxNestingDepth , final int maxNumLen ,
221
+ final int maxStringLen , final int maxNameLen ) {
180
222
_maxNestingDepth = maxNestingDepth ;
181
223
_maxNumLen = maxNumLen ;
182
224
_maxStringLen = maxStringLen ;
225
+ _maxNameLen = maxNameLen ;
183
226
}
184
227
185
228
public static Builder builder () {
@@ -238,6 +281,16 @@ public int getMaxStringLength() {
238
281
return _maxStringLen ;
239
282
}
240
283
284
+ /**
285
+ * Accessor for maximum length of names to decode.
286
+ * see {@link Builder#maxNameLength(int)} for details.
287
+ *
288
+ * @return Maximum allowed name length
289
+ */
290
+ public int getMaxNameLength () {
291
+ return _maxNameLen ;
292
+ }
293
+
241
294
/*
242
295
/**********************************************************************
243
296
/* Convenience methods for validation, document limits
@@ -334,6 +387,27 @@ public void validateStringLength(int length) throws StreamConstraintsException
334
387
}
335
388
}
336
389
390
+ /**
391
+ * Convenience method that can be used to verify that a name
392
+ * of specified length does not exceed maximum specific by this
393
+ * constraints object: if it does, a
394
+ * {@link StreamConstraintsException}
395
+ * is thrown.
396
+ *
397
+ * @param length Length of name in input units
398
+ *
399
+ * @throws StreamConstraintsException If length exceeds maximum
400
+ */
401
+ public void validateNameLength (int length ) throws StreamConstraintsException
402
+ {
403
+ if (length > _maxNameLen ) {
404
+ throw _constructException (
405
+ "Name value length (%d) exceeds the maximum allowed (%d, from %s)" ,
406
+ length , _maxNameLen ,
407
+ _constrainRef ("getMaxNameLength" ));
408
+ }
409
+ }
410
+
337
411
/*
338
412
/**********************************************************************
339
413
/* Convenience methods for validation, other
0 commit comments