Skip to content

Commit 54eb6f0

Browse files
authored
Merge pull request #156 from netplex/add-flag
add flag to drop the limit of json depth
2 parents 2a7ba6e + 0a35821 commit 54eb6f0

File tree

6 files changed

+41
-10
lines changed

6 files changed

+41
-10
lines changed

accessors-smart/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
<modelVersion>4.0.0</modelVersion>
1818
<groupId>net.minidev</groupId>
1919
<artifactId>accessors-smart</artifactId>
20-
<version>2.4.11</version>
20+
<version>2.5.0</version>
2121
<name>ASM based accessors helper used by json-smart</name>
2222
<description>Java reflect give poor performance on getter setter an constructor calls, accessors-smart use ASM to speed up those calls.</description>
2323
<packaging>bundle</packaging>

json-smart-action/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>net.minidev</groupId>
44
<artifactId>json-smart-action</artifactId>
5-
<version>2.4.11</version>
5+
<version>2.5.0</version>
66
<name>JSON-smart-action Small and Fast Parser</name>
77
<description>JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.</description>
88
<packaging>bundle</packaging>
@@ -245,7 +245,7 @@
245245
<dependency>
246246
<groupId>net.minidev</groupId>
247247
<artifactId>json-smart</artifactId>
248-
<version>2.4.11</version>
248+
<version>2.5.0</version>
249249
</dependency>
250250
</dependencies>
251251
</project>

json-smart/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
<modelVersion>4.0.0</modelVersion>
1818
<groupId>net.minidev</groupId>
1919
<artifactId>json-smart</artifactId>
20-
<version>2.4.11</version>
20+
<version>2.5.0</version>
2121
<name>JSON Small and Fast Parser</name>
2222
<description>JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.</description>
2323
<packaging>bundle</packaging>
@@ -260,7 +260,7 @@ limitations under the License.
260260
<dependency>
261261
<groupId>net.minidev</groupId>
262262
<artifactId>accessors-smart</artifactId>
263-
<version>2.4.11</version>
263+
<version>2.5.0</version>
264264
</dependency>
265265
</dependencies>
266266
</project>

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 LIMIT_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

+4-2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ abstract class JSONParserBase {
9191
protected final boolean useIntegerStorage;
9292
protected final boolean reject127;
9393
protected final boolean unrestictBigDigit;
94+
protected final boolean limitJsonDepth;
9495

9596
public JSONParserBase(int permissiveMode) {
9697
this.acceptNaN = (permissiveMode & JSONParser.ACCEPT_NAN) > 0;
@@ -107,6 +108,7 @@ public JSONParserBase(int permissiveMode) {
107108
this.checkTaillingSpace = (permissiveMode & JSONParser.ACCEPT_TAILLING_SPACE) == 0;
108109
this.reject127 = (permissiveMode & JSONParser.REJECT_127_CHAR) > 0;
109110
this.unrestictBigDigit = (permissiveMode & JSONParser.BIG_DIGIT_UNRESTRICTED) > 0;
111+
this.limitJsonDepth = (permissiveMode & JSONParser.LIMIT_JSON_DEPTH) > 0;
110112
}
111113

112114
public void checkControleChar() throws ParseException {
@@ -296,7 +298,7 @@ protected Number parseNumber(String s) throws ParseException {
296298
protected <T> T readArray(JsonReaderI<T> mapper) throws ParseException, IOException {
297299
if (c != '[')
298300
throw new RuntimeException("Internal Error");
299-
if (++this.depth > MAX_DEPTH) {
301+
if (limitJsonDepth && ++this.depth > MAX_DEPTH) {
300302
throw new ParseException(pos, ERROR_UNEXPECTED_JSON_DEPTH, c);
301303
}
302304
Object current = mapper.createArray();
@@ -553,7 +555,7 @@ protected <T> T readObject(JsonReaderI<T> mapper) throws ParseException, IOExcep
553555
//
554556
if (c != '{')
555557
throw new RuntimeException("Internal Error");
556-
if (++this.depth > MAX_DEPTH) {
558+
if (limitJsonDepth && ++this.depth > MAX_DEPTH) {
557559
throw new ParseException(pos, ERROR_UNEXPECTED_JSON_DEPTH, c);
558560
}
559561
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.LIMIT_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)