Skip to content

Commit 2e12c25

Browse files
authored
feat(jsonrpc): validate jsonrpc version, method and null id (#7032)
1 parent 37a681a commit 2e12c25

2 files changed

Lines changed: 234 additions & 70 deletions

File tree

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

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class JsonRpcServlet extends RateLimiterServlet {
3232

3333
private static final ObjectMapper MAPPER = JsonRpcMapper.create();
3434
private static final ObjectMapper OUTBOUND_MAPPER = new ObjectMapper();
35+
private static final String INVALID_REQUEST_MESSAGE = "Invalid Request";
3536

3637
private enum JsonRpcError {
3738
PARSE_ERROR(-32700),
@@ -112,21 +113,22 @@ private void doPostInternal(HttpServletRequest req, HttpServletResponse resp) th
112113
}
113114

114115
if (!rootNode.isObject() && !rootNode.isArray()) {
115-
writeJsonRpcError(resp, JsonRpcError.INVALID_REQUEST, "Invalid Request", null, false);
116+
writeJsonRpcError(resp, JsonRpcError.INVALID_REQUEST, INVALID_REQUEST_MESSAGE, null, false);
116117
return;
117118
}
118119

119120
boolean isBatch = rootNode.isArray();
120121
if (isBatch && rootNode.isEmpty()) {
121-
writeJsonRpcError(resp, JsonRpcError.INVALID_REQUEST, "Invalid Request", null, false);
122+
writeJsonRpcError(resp, JsonRpcError.INVALID_REQUEST, INVALID_REQUEST_MESSAGE, null, false);
122123
return;
123124
}
124-
if (!isBatch && hasInvalidRequestId(rootNode)) {
125-
writeJsonRpcError(resp, JsonRpcError.INVALID_REQUEST, "Invalid Request", null, false);
125+
if (!isBatch && hasInvalidVersion(rootNode)) {
126+
writeJsonRpcError(resp, JsonRpcError.INVALID_REQUEST,
127+
INVALID_REQUEST_MESSAGE + ": jsonrpc must be \"2.0\"", rootNode.get("id"), false);
126128
return;
127129
}
128-
if (!isBatch && hasScalarParams(rootNode)) {
129-
writeJsonRpcError(resp, JsonRpcError.INVALID_REQUEST, "Invalid Request",
130+
if (!isBatch && isInvalidRequest(rootNode)) {
131+
writeJsonRpcError(resp, JsonRpcError.INVALID_REQUEST, INVALID_REQUEST_MESSAGE,
130132
rootNode.get("id"), false);
131133
return;
132134
}
@@ -189,9 +191,14 @@ private void handleBatch(HttpServletResponse resp, JsonNode rootNode, int maxRes
189191
for (int i = 0; i < rootNode.size(); i++) {
190192
JsonNode subRequest = rootNode.get(i);
191193

192-
if (!subRequest.isObject() || hasInvalidRequestId(subRequest)
193-
|| hasScalarParams(subRequest)) {
194-
ObjectNode errNode = buildErrorNode(JsonRpcError.INVALID_REQUEST, "Invalid Request",
194+
String invalidRequest = null;
195+
if (subRequest.isObject() && hasInvalidVersion(subRequest)) {
196+
invalidRequest = INVALID_REQUEST_MESSAGE + ": jsonrpc must be \"2.0\"";
197+
} else if (isInvalidRequest(subRequest)) {
198+
invalidRequest = INVALID_REQUEST_MESSAGE;
199+
}
200+
if (invalidRequest != null) {
201+
ObjectNode errNode = buildErrorNode(JsonRpcError.INVALID_REQUEST, invalidRequest,
195202
subRequest.get("id"));
196203
if (!overflow) {
197204
byte[] errBytes = MAPPER.writeValueAsBytes(errNode);
@@ -340,6 +347,29 @@ private ObjectNode buildErrorNode(JsonRpcError error, String message, JsonNode i
340347
return errorObj;
341348
}
342349

350+
private boolean hasInvalidVersion(JsonNode request) {
351+
JsonNode version = request.get("jsonrpc");
352+
return version == null || !version.isTextual() || !"2.0".equals(version.asText());
353+
}
354+
355+
private boolean isInvalidRequest(JsonNode request) {
356+
return !request.isObject() || hasInvalidRequestId(request) || hasNullRequestId(request)
357+
|| hasScalarParams(request) || hasInvalidMethod(request);
358+
}
359+
360+
// Stricter than the spec and geth, which both answer a null id normally. jsonrpc4j invokes
361+
// the method but treats a null id as a notification and discards a successful result, so the
362+
// caller never sees the outcome of a call that took effect. Reject it before dispatch instead;
363+
// a request without an id is still dispatched as a notification.
364+
private boolean hasNullRequestId(JsonNode request) {
365+
return request.has("id") && request.get("id").isNull();
366+
}
367+
368+
private boolean hasInvalidMethod(JsonNode request) {
369+
JsonNode method = request.get("method");
370+
return method == null || !method.isTextual() || method.asText().isEmpty();
371+
}
372+
343373
private static boolean hasInvalidRequestId(JsonNode request) {
344374
return request.has("id") && !isValidRequestId(request.get("id"));
345375
}

0 commit comments

Comments
 (0)