Skip to content

Commit d108e30

Browse files
committed
Fix #1046: implement max-doc-len limits
1 parent 99c3d2b commit d108e30

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ a pure JSON library.
3131
#1041: Start using AssertJ in unit tests
3232
#1042: Allow configuring spaces before and/or after the colon in `DefaultPrettyPrinter`
3333
(contributed by @digulla)
34+
#1046: Add configurable limit for the maximum number of bytes/chars
35+
of content to parse before failing
3436
#1047: Add configurable limit for the maximum length of Object property names
3537
to parse before failing (default max: 50,000 chars)
3638
(contributed by @pjfanning)

src/main/java/com/fasterxml/jackson/core/json/ReaderBasedJsonParser.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,14 @@ protected void _loadMoreGuaranteed() throws IOException {
273273
protected boolean _loadMore() throws IOException
274274
{
275275
if (_reader != null) {
276+
final int bufSize = _inputEnd;
277+
_currInputProcessed += bufSize;
278+
_currInputRowStart -= bufSize;
279+
// 06-Sep-2023, tatu: [core#1046] Enforce max doc length limit
280+
streamReadConstraints().validateDocumentLength(_currInputProcessed);
281+
276282
int count = _reader.read(_inputBuffer, 0, _inputBuffer.length);
277283
if (count > 0) {
278-
final int bufSize = _inputEnd;
279-
_currInputProcessed += bufSize;
280-
_currInputRowStart -= bufSize;
281284

282285
// 26-Nov-2015, tatu: Since name-offset requires it too, must offset
283286
// this increase to avoid "moving" name-offset, resulting most likely
@@ -289,6 +292,7 @@ protected boolean _loadMore() throws IOException
289292

290293
return true;
291294
}
295+
_inputPtr = _inputEnd = 0;
292296
// End of input
293297
_closeInput();
294298
// Should never return 0, so let's fail

src/main/java/com/fasterxml/jackson/core/json/UTF8StreamJsonParser.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,14 @@ protected final boolean _loadMore() throws IOException
255255
return false;
256256
}
257257

258+
final int bufSize = _inputEnd;
259+
_currInputProcessed += bufSize;
260+
_currInputRowStart -= bufSize;
261+
// 06-Sep-2023, tatu: [core#1046] Enforce max doc length limit
262+
streamReadConstraints().validateDocumentLength(_currInputProcessed);
263+
258264
int count = _inputStream.read(_inputBuffer, 0, space);
259265
if (count > 0) {
260-
final int bufSize = _inputEnd;
261-
262-
_currInputProcessed += bufSize;
263-
_currInputRowStart -= bufSize;
264266

265267
// 26-Nov-2015, tatu: Since name-offset requires it too, must offset
266268
// this increase to avoid "moving" name-offset, resulting most likely
@@ -272,6 +274,7 @@ protected final boolean _loadMore() throws IOException
272274

273275
return true;
274276
}
277+
_inputPtr = _inputEnd = 0;
275278
// End of input
276279
_closeInput();
277280
// Should never return 0, so let's fail

src/test/java/com/fasterxml/jackson/core/constraints/LargeDocReadTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void testLargeNameChars() throws Exception {
3333

3434
public void testLargeNameWithSmallLimitBytes() throws Exception
3535
{
36-
final String doc = generateJSON(15_000);
36+
final String doc = generateJSON(12_000);
3737
try (JsonParser p = createParserUsingStream(JSON_F_DOC_10K, doc, "UTF-8")) {
3838
consumeTokens(p);
3939
fail("expected StreamConstraintsException");
@@ -44,7 +44,7 @@ public void testLargeNameWithSmallLimitBytes() throws Exception
4444

4545
public void testLargeNameWithSmallLimitChars() throws Exception
4646
{
47-
final String doc = generateJSON(15_000);
47+
final String doc = generateJSON(12_000);
4848
try (JsonParser p = createParserUsingReader(JSON_F_DOC_10K, doc)) {
4949
consumeTokens(p);
5050
fail("expected StreamConstraintsException");
@@ -55,7 +55,7 @@ public void testLargeNameWithSmallLimitChars() throws Exception
5555

5656
public void testLargeNameWithSmallLimitAsync() throws Exception
5757
{
58-
final byte[] doc = utf8Bytes(generateJSON(25_000));
58+
final byte[] doc = utf8Bytes(generateJSON(12_000));
5959

6060
// first with byte[] backend
6161
try (AsyncReaderWrapper p = asyncForBytes(JSON_F_DOC_10K, 1000, doc, 1)) {

0 commit comments

Comments
 (0)