Skip to content

Commit da2e833

Browse files
committed
add flag to drop the limit of json depth
1 parent 2a7ba6e commit da2e833

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

json-smart/src/main/java/net/minidev/json/parser/JSONParser.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ public class JSONParser {
9393
* @since 2.4
9494
*/
9595
public final static int BIG_DIGIT_UNRESTRICTED = 2048;
96+
97+
/**
98+
* If limit the max depth of json size
99+
*
100+
* @since 2.5
101+
*/
102+
public static final int FINITE_JSON_DEPTH = 4096;
96103

97104

98105
/**
@@ -132,7 +139,7 @@ public class JSONParser {
132139
/*
133140
* internal fields
134141
*/
135-
private int mode;
142+
private final int mode;
136143

137144
private JSONParserInputStream pBinStream;
138145
private JSONParserByteArray pBytes;

json-smart/src/main/java/net/minidev/json/parser/JSONParserBase.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ abstract class JSONParserBase {
9292
protected final boolean reject127;
9393
protected final boolean unrestictBigDigit;
9494

95+
protected final boolean finiteJsonDepth;
96+
9597
public JSONParserBase(int permissiveMode) {
9698
this.acceptNaN = (permissiveMode & JSONParser.ACCEPT_NAN) > 0;
9799
this.acceptNonQuote = (permissiveMode & JSONParser.ACCEPT_NON_QUOTE) > 0;
@@ -107,6 +109,7 @@ public JSONParserBase(int permissiveMode) {
107109
this.checkTaillingSpace = (permissiveMode & JSONParser.ACCEPT_TAILLING_SPACE) == 0;
108110
this.reject127 = (permissiveMode & JSONParser.REJECT_127_CHAR) > 0;
109111
this.unrestictBigDigit = (permissiveMode & JSONParser.BIG_DIGIT_UNRESTRICTED) > 0;
112+
this.finiteJsonDepth = (permissiveMode & JSONParser.FINITE_JSON_DEPTH) > 0;
110113
}
111114

112115
public void checkControleChar() throws ParseException {
@@ -296,7 +299,7 @@ protected Number parseNumber(String s) throws ParseException {
296299
protected <T> T readArray(JsonReaderI<T> mapper) throws ParseException, IOException {
297300
if (c != '[')
298301
throw new RuntimeException("Internal Error");
299-
if (++this.depth > MAX_DEPTH) {
302+
if (finiteJsonDepth && ++this.depth > MAX_DEPTH) {
300303
throw new ParseException(pos, ERROR_UNEXPECTED_JSON_DEPTH, c);
301304
}
302305
Object current = mapper.createArray();
@@ -553,7 +556,7 @@ protected <T> T readObject(JsonReaderI<T> mapper) throws ParseException, IOExcep
553556
//
554557
if (c != '{')
555558
throw new RuntimeException("Internal Error");
556-
if (++this.depth > MAX_DEPTH) {
559+
if (finiteJsonDepth && ++this.depth > MAX_DEPTH) {
557560
throw new ParseException(pos, ERROR_UNEXPECTED_JSON_DEPTH, c);
558561
}
559562
Object current = mapper.createObject();

json-smart/src/test/java/net/minidev/json/test/TestOverflow.java

+24-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import net.minidev.json.JSONArray;
44
import net.minidev.json.JSONValue;
5+
import net.minidev.json.parser.JSONParser;
56
import net.minidev.json.parser.ParseException;
67

8+
import static net.minidev.json.parser.JSONParser.DEFAULT_PERMISSIVE_MODE;
79
import static org.junit.jupiter.api.Assertions.assertEquals;
8-
import static org.junit.jupiter.api.Assertions.assertTrue;
10+
import static org.junit.jupiter.api.Assertions.fail;
911

1012
import org.junit.jupiter.api.Test;
1113

@@ -28,7 +30,27 @@ public void stressTest() throws Exception {
2830
assertEquals(e.getErrorType(), ParseException.ERROR_UNEXPECTED_JSON_DEPTH);
2931
return;
3032
}
31-
assertTrue(false);
33+
fail();
34+
}
35+
36+
@Test
37+
public void shouldNotFailWhenInfiniteJsonDepth() throws Exception {
38+
int size = 500;
39+
StringBuilder sb = new StringBuilder(10 + size*4);
40+
for (int i=0; i < size; i++) {
41+
sb.append("{a:");
42+
}
43+
sb.append("true");
44+
for (int i=0; i < size; i++) {
45+
sb.append("}");
46+
}
47+
String s = sb.toString();
48+
try {
49+
JSONParser parser = new JSONParser(DEFAULT_PERMISSIVE_MODE & ~JSONParser.FINITE_JSON_DEPTH);
50+
parser.parse(s, JSONValue.defaultReader.DEFAULT);
51+
} catch (ParseException e) {
52+
fail();
53+
}
3254
}
3355

3456
@Test

0 commit comments

Comments
 (0)