Skip to content

Commit 74c9ee2

Browse files
authored
Backport #1570 into 3.1 (for 3.1.1) (#1574)
1 parent 4fc50ff commit 74c9ee2

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

release-notes/VERSION

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ No changes since 3.1
2323

2424
#1562: Move `module-info.class` to jar root level (`/module-info.class`)
2525
from under `META-INF/versions/17`
26+
#1570: Fail parsing from `DataInput` if
27+
`StreamReadConstraints.getMaxDocumentLength()` set
28+
(fix by @cowtowncoder, w/ Claude code)
2629

2730
3.1.0 (23-Feb-2026)
2831

src/main/java/tools/jackson/core/json/JsonFactory.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import tools.jackson.core.*;
1212
import tools.jackson.core.base.TextualTSFactory;
13+
import tools.jackson.core.exc.StreamConstraintsException;
1314
import tools.jackson.core.io.*;
1415
import tools.jackson.core.json.async.NonBlockingByteArrayJsonParser;
1516
import tools.jackson.core.json.async.NonBlockingByteBufferJsonParser;
@@ -452,6 +453,13 @@ protected JsonParser _createParser(ObjectReadContext readCtxt, IOContext ioCtxt,
452453
DataInput input)
453454
throws JacksonException
454455
{
456+
// [core#1570] DataInput reads byte-by-byte so we cannot efficiently enforce
457+
// maxDocumentLength. Fail fast if the limit has been configured.
458+
if (_streamReadConstraints.hasMaxDocumentLength()) {
459+
throw new StreamConstraintsException(
460+
"Can not enforce `StreamReadConstraints.getMaxDocumentLength()` limit with `DataInput`-backed parser: "
461+
+"use other input source types, or remove the max-document-length limit");
462+
}
455463
// Also: while we can't do full bootstrapping (due to read-ahead limitations), should
456464
// at least handle possible UTF-8 BOM
457465
int firstByte = ByteSourceJsonBootstrapper.skipUTF8BOM(input);

src/test/java/tools/jackson/core/unittest/constraints/LargeDocReadTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import tools.jackson.core.json.JsonFactory;
1212
import tools.jackson.core.unittest.async.AsyncTestBase;
1313
import tools.jackson.core.unittest.testutil.AsyncReaderWrapper;
14+
import tools.jackson.core.unittest.testutil.MockDataInput;
1415

1516
import static org.junit.jupiter.api.Assertions.assertEquals;
1617
import static org.junit.jupiter.api.Assertions.fail;
@@ -107,6 +108,31 @@ void largeNameWithSmallLimitAsync() throws Exception
107108
}
108109
}
109110

111+
// [core#1570] Should fail fast when DataInput used with maxDocumentLength set
112+
@Test
113+
void dataInputWithDocLengthLimitFails() throws Exception
114+
{
115+
final String doc = generateJSON(100);
116+
try (JsonParser p = JSON_F_DOC_10K.createParser(ObjectReadContext.empty(),
117+
new MockDataInput(doc))) {
118+
fail("expected StreamConstraintsException");
119+
} catch (StreamConstraintsException e) {
120+
verifyException(e, "DataInput");
121+
verifyException(e, "maxDocumentLength");
122+
}
123+
}
124+
125+
// [core#1570] DataInput without maxDocumentLength should still work
126+
@Test
127+
void dataInputWithoutDocLengthLimitWorks() throws Exception
128+
{
129+
final String doc = generateJSON(100);
130+
try (JsonParser p = JSON_F_DEFAULT.createParser(ObjectReadContext.empty(),
131+
new MockDataInput(doc))) {
132+
consumeTokens(p);
133+
}
134+
}
135+
110136
@Test
111137
void tokenLimitBytes() throws Exception {
112138
final String doc = generateJSON(StreamReadConstraints.defaults().getMaxNameLength() - 100);

0 commit comments

Comments
 (0)