Skip to content

Commit 7c8b6d5

Browse files
pjfanningcowtowncoder
authored andcommitted
Add test for nesting for DataInput-backed JsonParser (#1550)
1 parent 97a647b commit 7c8b6d5

2 files changed

Lines changed: 75 additions & 3 deletions

File tree

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010

1111
/**
1212
* Unit test(s) for verifying handling of new (in 2.15) StreamReadConstraints
13-
* wrt maximum nesting depth.
13+
* wrt maximum nesting depth, for all streaming (InputStream,
14+
* Reader; regular/throttled) inputs and {@link DataInput}.
15+
*
1416
*/
1517
class DeeplyNestedContentReadTest
16-
extends com.fasterxml.jackson.core.JUnit5TestBase
18+
extends com.fasterxml.jackson.core.JUnit5TestBase
1719
{
1820
private final JsonFactory JSON_F = newStreamFactory();
1921

@@ -32,6 +34,15 @@ void deepNestingStreaming() throws Exception
3234
}
3335
}
3436

37+
@Test
38+
void deepNestingDataInput() throws Exception
39+
{
40+
final String DOC = createDeepNestedDoc(TESTED_NESTING);
41+
try (JsonParser p = createParser(JSON_F, MODE_DATA_INPUT, DOC)) {
42+
_testDeepNesting(p);
43+
}
44+
}
45+
3546
private void _testDeepNesting(JsonParser p) throws Exception
3647
{
3748
try {
@@ -44,7 +55,7 @@ private void _testDeepNesting(JsonParser p) throws Exception
4455
}
4556

4657
@Test
47-
void legacyConstraintSettingTest() throws Exception
58+
void legacyConstraintSettingStreaming() throws Exception
4859
{
4960
final int LOWER_MAX = 40;
5061

@@ -59,6 +70,20 @@ void legacyConstraintSettingTest() throws Exception
5970
}
6071
}
6172

73+
@Test
74+
void legacyConstraintSettingDataInput() throws Exception
75+
{
76+
final int LOWER_MAX = 40;
77+
78+
final String DOC = createDeepNestedDoc(LOWER_MAX + 10);
79+
JsonFactory f = new JsonFactory();
80+
f.setStreamReadConstraints(StreamReadConstraints.builder()
81+
.maxNestingDepth(LOWER_MAX).build());
82+
try (JsonParser p = createParser(f, MODE_DATA_INPUT, DOC)) {
83+
_testLegacyConstraintSettingTest(p, LOWER_MAX);
84+
}
85+
}
86+
6287
private void _testLegacyConstraintSettingTest(JsonParser p, int maxNesting) throws Exception
6388
{
6489
try {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.fasterxml.jackson.core.constraints;
2+
3+
import java.io.*;
4+
import java.nio.charset.StandardCharsets;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
import com.fasterxml.jackson.core.*;
9+
import com.fasterxml.jackson.core.exc.StreamConstraintsException;
10+
11+
import static org.junit.jupiter.api.Assertions.*;
12+
13+
/**
14+
* Nesting Depth Constraint Bypass in UTF8DataInputJsonParser
15+
*/
16+
class DeeplyNestedContentViaDataInputTest {
17+
18+
private static final int TEST_NESTING_DEPTH = 5000;
19+
20+
private final JsonFactory factory = new JsonFactory();
21+
22+
@Test
23+
void dataInputParserBypassesNestingDepth() throws Exception {
24+
byte[] data = buildNestedArrays(TEST_NESTING_DEPTH);
25+
DataInput di = new DataInputStream(new ByteArrayInputStream(data));
26+
27+
int maxDepth = 0;
28+
try (JsonParser p = factory.createParser(di)) {
29+
while (p.nextToken() != null) {
30+
if (p.currentToken() == JsonToken.START_ARRAY) {
31+
maxDepth++;
32+
}
33+
}
34+
fail("DataInput parser must reject nesting depth " + TEST_NESTING_DEPTH+", got "+maxDepth);
35+
} catch (StreamConstraintsException e) {
36+
assertTrue(e.getMessage().contains("Document nesting depth"),
37+
"Unexpected exception message: " + e.getMessage());
38+
}
39+
}
40+
41+
private byte[] buildNestedArrays(int depth) {
42+
StringBuilder sb = new StringBuilder(depth * 2);
43+
for (int i = 0; i < depth; i++) sb.append('[');
44+
for (int i = 0; i < depth; i++) sb.append(']');
45+
return sb.toString().getBytes(StandardCharsets.UTF_8);
46+
}
47+
}

0 commit comments

Comments
 (0)