Skip to content

Commit 852caf6

Browse files
authored
Merge pull request #233 from ccudennec-otto/fix-CVE-2024-57699
fix CVE-2024-57699 for predefined parsers
2 parents d1f4645 + c21d854 commit 852caf6

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ So I do not use my json-smart anymore. I had fun with this project. If you want
1919

2020
# Changelog
2121

22+
## *V 2.5.2* (2025-02-07)
23+
24+
* Fix CVE-2024-57699 for predefined parsers. [PR 233](https://github.com/netplex/json-smart-v2/pull/233)
25+
2226
### *V 2.5.1* (2024-03-14)
2327

2428
* Bump all dependencies.
@@ -122,4 +126,4 @@ So I do not use my json-smart anymore. I had fun with this project. If you want
122126

123127
### *V 2.0-RC1* (2012-02-18)
124128
* speed improvement in POJO manipulation
125-
* add JSONStyle.LT_COMPRESS predefined generate strct json, but ignoring / escapement.
129+
* add JSONStyle.LT_COMPRESS predefined generate strct json, but ignoring / escapement.

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,21 @@ public class JSONParser {
115115
*
116116
* @since 1.0.6
117117
*/
118-
public final static int MODE_RFC4627 = USE_INTEGER_STORAGE | USE_HI_PRECISION_FLOAT | ACCEPT_TAILLING_SPACE;
118+
public final static int MODE_RFC4627 = USE_INTEGER_STORAGE | USE_HI_PRECISION_FLOAT | ACCEPT_TAILLING_SPACE | LIMIT_JSON_DEPTH;
119119
/**
120120
* Parse Object like json-simple
121121
*
122122
* Best for an iso-bug json-simple API port.
123123
*
124124
* @since 1.0.7
125125
*/
126-
public final static int MODE_JSON_SIMPLE = ACCEPT_USELESS_COMMA | USE_HI_PRECISION_FLOAT | ACCEPT_TAILLING_DATA | ACCEPT_TAILLING_SPACE | REJECT_127_CHAR | BIG_DIGIT_UNRESTRICTED;
126+
public final static int MODE_JSON_SIMPLE = ACCEPT_USELESS_COMMA | USE_HI_PRECISION_FLOAT | ACCEPT_TAILLING_DATA | ACCEPT_TAILLING_SPACE | REJECT_127_CHAR | BIG_DIGIT_UNRESTRICTED | LIMIT_JSON_DEPTH;
127127
/**
128128
* Strictest parsing mode
129129
*
130130
* @since 2.0.1
131131
*/
132-
public final static int MODE_STRICTEST = USE_INTEGER_STORAGE | USE_HI_PRECISION_FLOAT | REJECT_127_CHAR;
132+
public final static int MODE_STRICTEST = USE_INTEGER_STORAGE | USE_HI_PRECISION_FLOAT | REJECT_127_CHAR | LIMIT_JSON_DEPTH;
133133
/**
134134
* Default json-smart processing mode
135135
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package net.minidev.json.test;
2+
3+
import net.minidev.json.parser.JSONParser;
4+
import net.minidev.json.parser.ParseException;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertThrows;
8+
9+
public class TestCVE202457699 {
10+
11+
private static final String MALICIOUS_STRING = createMaliciousString();
12+
13+
@Test
14+
public void jsonSimpleParserShouldRestrictDepth() {
15+
JSONParser p = new JSONParser(JSONParser.MODE_JSON_SIMPLE);
16+
assertThrows(ParseException.class,
17+
() -> p.parse(MALICIOUS_STRING),
18+
"Malicious payload, having non natural depths");
19+
}
20+
21+
@Test
22+
public void strictestParserShouldRestrictDepth() {
23+
JSONParser p = new JSONParser(JSONParser.MODE_STRICTEST);
24+
assertThrows(ParseException.class,
25+
() -> p.parse(MALICIOUS_STRING),
26+
"Malicious payload, having non natural depths");
27+
}
28+
29+
@Test
30+
public void rfc4627ParserShouldRestrictDepth() {
31+
JSONParser p = new JSONParser(JSONParser.MODE_RFC4627);
32+
assertThrows(ParseException.class,
33+
() -> p.parse(MALICIOUS_STRING),
34+
"Malicious payload, having non natural depths");
35+
}
36+
37+
@Test
38+
public void permissiveParserShouldRestrictDepth() {
39+
JSONParser p = new JSONParser(JSONParser.MODE_PERMISSIVE);
40+
assertThrows(ParseException.class,
41+
() -> p.parse(MALICIOUS_STRING),
42+
"Malicious payload, having non natural depths");
43+
}
44+
45+
private static String createMaliciousString() {
46+
StringBuilder sb = new StringBuilder();
47+
for (int i = 0; i < 10000 ; i++) {
48+
sb.append("{\"a\":");
49+
}
50+
sb.append("1");
51+
for (int i = 0; i < 10000 ; i++) {
52+
sb.append("}");
53+
}
54+
return sb.toString();
55+
}
56+
}

0 commit comments

Comments
 (0)