Skip to content

Commit 7ce3622

Browse files
authored
Backport #1570 fix (#1573)
1 parent aaea198 commit 7ce3622

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

release-notes/VERSION-2.x

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ a pure JSON library.
1414
=== Releases ===
1515
------------------------------------------------------------------------
1616

17+
2.18.7 (not yet released)
18+
19+
#1570: Fail parsing from `DataInput` if
20+
`StreamReadConstraints.getMaxDocumentLength()` set [GHSA-2m67-wjpj-xhg9]
21+
(fix by @cowtowncoder, w/ Claude code)
22+
1723
2.18.6 (22-Feb-2026)
1824

1925
#1512: Number-parsing fix for `UTF8DataInputJsonParser`

src/main/java/com/fasterxml/jackson/core/JsonFactory.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.List;
1212
import java.util.Objects;
1313

14+
import com.fasterxml.jackson.core.exc.StreamConstraintsException;
1415
import com.fasterxml.jackson.core.format.InputAccessor;
1516
import com.fasterxml.jackson.core.format.MatchStrength;
1617
import com.fasterxml.jackson.core.io.*;
@@ -1981,6 +1982,13 @@ protected JsonParser _createParser(DataInput input, IOContext ctxt) throws IOExc
19811982
// 13-May-2016, tatu: Need to take care not to accidentally create JSON parser for
19821983
// non-JSON input.
19831984
_requireJSONFactory("InputData source not (yet?) supported for this format (%s)");
1985+
// [core#1570] DataInput reads byte-by-byte so we cannot efficiently enforce
1986+
// maxDocumentLength. Fail fast if the limit has been configured.
1987+
if (_streamReadConstraints.hasMaxDocumentLength()) {
1988+
throw new StreamConstraintsException(
1989+
"Can not enforce `StreamReadConstraints.getMaxDocumentLength()` limit with `DataInput`-backed parser: "
1990+
+"use other input source types, or remove the max-document-length limit");
1991+
}
19841992
// Also: while we can't do full bootstrapping (due to read-ahead limitations), should
19851993
// at least handle possible UTF-8 BOM
19861994
int firstByte = ByteSourceJsonBootstrapper.skipUTF8BOM(input);

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.fasterxml.jackson.core.async.AsyncTestBase;
99
import com.fasterxml.jackson.core.exc.StreamConstraintsException;
1010
import com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper;
11+
import com.fasterxml.jackson.core.testsupport.MockDataInput;
1112

1213
import static org.junit.jupiter.api.Assertions.assertEquals;
1314
import static org.junit.jupiter.api.Assertions.fail;
@@ -102,6 +103,29 @@ void largeNameWithSmallLimitAsync() throws Exception
102103
}
103104
}
104105

106+
// [core#1570] Should fail fast when DataInput used with maxDocumentLength set
107+
@Test
108+
void dataInputWithDocLengthLimitFails() throws Exception
109+
{
110+
final String doc = generateJSON(100);
111+
try (JsonParser p = JSON_F_DOC_10K.createParser(new MockDataInput(doc))) {
112+
fail("expected StreamConstraintsException");
113+
} catch (StreamConstraintsException e) {
114+
verifyException(e, "DataInput");
115+
verifyException(e, "maxDocumentLength");
116+
}
117+
}
118+
119+
// [core#1570] DataInput without maxDocumentLength should still work
120+
@Test
121+
void dataInputWithoutDocLengthLimitWorks() throws Exception
122+
{
123+
final String doc = generateJSON(100);
124+
try (JsonParser p = JSON_F_DEFAULT.createParser(new MockDataInput(doc))) {
125+
consumeTokens(p);
126+
}
127+
}
128+
105129
@Test
106130
void tokenLimitBytes() throws Exception {
107131
final String doc = generateJSON(StreamReadConstraints.defaults().getMaxNameLength() - 100);

0 commit comments

Comments
 (0)