Skip to content

Commit 95d84d3

Browse files
authored
fix(api): tighten json parsing limits (#6844)
1 parent f40f188 commit 95d84d3

4 files changed

Lines changed: 27 additions & 7 deletions

File tree

‎common/src/main/java/org/tron/core/Constant.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class Constant {
6565
public static final String LOCAL_HOST = "127.0.0.1";
6666

6767
// JSON parsing (DoS protection)
68-
public static final int MAX_NESTING_DEPTH = 100;
68+
public static final int MAX_NESTING_DEPTH = 20;
6969
public static final int MAX_TOKEN_COUNT = 100_000;
7070

7171
}

‎framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcServlet.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.core.JsonFactory;
44
import com.fasterxml.jackson.core.JsonProcessingException;
55
import com.fasterxml.jackson.core.StreamReadConstraints;
6+
import com.fasterxml.jackson.core.exc.StreamConstraintsException;
67
import com.fasterxml.jackson.databind.JsonNode;
78
import com.fasterxml.jackson.databind.ObjectMapper;
89
import com.fasterxml.jackson.databind.node.ArrayNode;
@@ -114,7 +115,11 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
114115
return;
115116
}
116117
} catch (JsonProcessingException e) {
117-
writeJsonRpcError(resp, JsonRpcError.PARSE_ERROR, "JSON parse error", null, false);
118+
if (e instanceof StreamConstraintsException) {
119+
writeJsonRpcError(resp, JsonRpcError.PARSE_ERROR, e.getMessage(), null, false);
120+
} else {
121+
writeJsonRpcError(resp, JsonRpcError.PARSE_ERROR, "JSON parse error", null, false);
122+
}
118123
return;
119124
}
120125

‎framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,7 @@ private TransactionJson buildCreateSmartContractTransaction(byte[] ownerAddress,
11431143
String abiStr = "{" + "\"entrys\":" + args.getAbi() + "}";
11441144
try {
11451145
JsonFormat.merge(abiStr, abiBuilder, args.isVisible());
1146-
} catch (JsonFormat.ParseException | StackOverflowError e) {
1146+
} catch (Exception e) {
11471147
throw new JsonRpcInvalidParamsException("invalid abi");
11481148
}
11491149
}

‎framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcServletTest.java‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.Assert.assertArrayEquals;
44
import static org.junit.Assert.assertEquals;
55
import static org.junit.Assert.assertFalse;
6+
import static org.junit.Assert.assertNotEquals;
67
import static org.junit.Assert.assertTrue;
78
import static org.mockito.ArgumentMatchers.any;
89
import static org.mockito.Mockito.doAnswer;
@@ -62,6 +63,8 @@ public void invalidJson_returnsParseError() throws Exception {
6263
JsonNode body = MAPPER.readTree(resp.getContentAsString());
6364
assertFalse(body.isArray());
6465
assertEquals(-32700, body.get("error").get("code").asInt());
66+
// A non-constraint JsonProcessingException keeps the generic message (else branch).
67+
assertEquals("JSON parse error", body.get("error").get("message").asText());
6568
assertEquals("2.0", body.get("jsonrpc").asText());
6669
assertTrue(body.get("id").isNull());
6770
}
@@ -378,8 +381,14 @@ public void excessivelyNestedRequest_returnsParseError() throws Exception {
378381

379382
MockHttpServletResponse resp = doPost(sb.toString());
380383
assertEquals(200, resp.getStatus());
381-
assertEquals(-32700,
382-
MAPPER.readTree(resp.getContentAsString()).get("error").get("code").asInt());
384+
JsonNode error = MAPPER.readTree(resp.getContentAsString()).get("error");
385+
assertEquals(-32700, error.get("code").asInt());
386+
// StreamConstraintsException message must be surfaced verbatim, not the generic text,
387+
// so callers can tell which constraint (nesting depth) was hit.
388+
String message = error.get("message").asText();
389+
assertNotEquals("JSON parse error", message);
390+
assertTrue("expected a nesting-depth constraint message, got: " + message,
391+
message.contains("nesting depth") && message.contains("exceeds the maximum allowed"));
383392
}
384393

385394
@Test
@@ -396,8 +405,14 @@ public void tooManyTokens_returnsParseError() throws Exception {
396405

397406
MockHttpServletResponse resp = doPost(sb.toString());
398407
assertEquals(200, resp.getStatus());
399-
assertEquals(-32700,
400-
MAPPER.readTree(resp.getContentAsString()).get("error").get("code").asInt());
408+
JsonNode error = MAPPER.readTree(resp.getContentAsString()).get("error");
409+
assertEquals(-32700, error.get("code").asInt());
410+
// StreamConstraintsException message must be surfaced verbatim, not the generic text,
411+
// so callers can tell which constraint (token count) was hit.
412+
String message = error.get("message").asText();
413+
assertNotEquals("JSON parse error", message);
414+
assertTrue("expected a token-count constraint message, got: " + message,
415+
message.contains("Token count") && message.contains("exceeds the maximum allowed"));
401416
}
402417

403418
// --- helpers ---

0 commit comments

Comments
 (0)