Skip to content

Commit bf6d23a

Browse files
committed
fix(jsonrpc): accept only UTF-8 request bodies
Also reject a request body that contains a NUL byte, as the admin endpoint already does. A NUL byte cannot appear in a UTF-8 JSON text that the parser accepts, but Jackson takes one in the first four bytes as a sign of UTF-16 or UTF-32 and decodes the body that way. Compatibility: UTF-8 request bodies, with or without a byte order mark, are unchanged, and the JSON escape \u0000 still works. A UTF-16 or UTF-32 body without a byte order mark used to be decoded as such when its raw bytes were also well-formed UTF-8; it now gets a -32700 parse error with a null id.
1 parent 1d841d4 commit bf6d23a

2 files changed

Lines changed: 39 additions & 25 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.fasterxml.jackson.databind.node.ObjectNode;
1111
import com.google.common.annotations.VisibleForTesting;
1212
import com.google.common.base.Utf8;
13+
import com.google.common.primitives.Bytes;
1314
import com.googlecode.jsonrpc4j.ErrorResolver.JsonError;
1415
import com.googlecode.jsonrpc4j.JsonRpcInterceptor;
1516
import com.googlecode.jsonrpc4j.JsonRpcMethod;
@@ -124,8 +125,9 @@ private void doPostInternal(HttpServletRequest req, HttpServletResponse resp) th
124125

125126
// Transport IOException from readBody propagates as HTTP 500 (genuine IO failure).
126127
byte[] body = readBody(req.getInputStream());
127-
// Jackson accepts overlong and surrogate UTF-8 sequences, so check the bytes first.
128-
if (!Utf8.isWellFormed(body)) {
128+
// Jackson accepts overlong, surrogate and above-U+10FFFF UTF-8 sequences and treats NUL
129+
// bytes as a sign of UTF-16 or UTF-32, so check the bytes first.
130+
if (!Utf8.isWellFormed(body) || Bytes.contains(body, (byte) 0)) {
129131
writeJsonRpcError(resp, JsonRpcError.PARSE_ERROR, "JSON parse error", null, false);
130132
return;
131133
}

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

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -165,24 +165,47 @@ public void utf8ByteOrderMark_isAccepted() throws Exception {
165165
}
166166

167167
@Test
168-
public void utf16WithoutByteOrderMark_keepsEncodingDetection() throws Exception {
169-
// Characterizes the current behavior: these UTF-16 bytes are also well-formed UTF-8 byte-wise
170-
// (U+4E2D encodes as 4E 2D) and Jackson detects the encoding.
168+
public void utf16AndUtf32WithoutByteOrderMark_areRejected() throws Exception {
171169
TronJsonRpc rpc = useRealRpcServer();
172-
when(rpc.web3Sha3(any())).thenReturn("0x1");
173170
String request = "{\"jsonrpc\":\"2.0\",\"method\":\"eth_blockNumber\",\"id\":1}";
174171
String chinese = "{\"jsonrpc\":\"2.0\",\"method\":\"web3_sha3\",\"params\":[\"中\"],\"id\":1}";
175-
for (Charset charset
176-
: Arrays.asList(StandardCharsets.UTF_16LE, StandardCharsets.UTF_16BE)) {
172+
for (String charset : Arrays.asList("UTF-16LE", "UTF-16BE", "UTF-32LE", "UTF-32BE")) {
177173
for (String body : Arrays.asList(request, chinese)) {
178-
MockHttpServletResponse resp = doPost(body.getBytes(charset));
179-
assertEquals(charset.name() + " " + body,
180-
MAPPER.readTree("{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":\"0x1\"}"),
181-
MAPPER.readTree(resp.getContentAsByteArray()));
174+
assertParseError(charset + " " + body, doPost(body.getBytes(Charset.forName(charset))));
182175
}
183176
}
184-
verify(rpc, times(2)).getLatestBlockNum();
185-
verify(rpc, times(2)).web3Sha3("中");
177+
verify(realRpcServer, never()).handleRequest(any(InputStream.class), any(OutputStream.class));
178+
verify(rpc, never()).getLatestBlockNum();
179+
verify(rpc, never()).web3Sha3(any());
180+
}
181+
182+
@Test
183+
public void rawNulByte_isRejected() throws Exception {
184+
TronJsonRpc rpc = useRealRpcServer();
185+
String prefix = "{\"jsonrpc\":\"2.0\",\"method\":\"web3_sha3\",\"params\":[\"a";
186+
String suffix = "b\"],\"id\":1}";
187+
List<byte[]> bodies = Arrays.asList(
188+
concat(utf8(prefix), new byte[] {0}, utf8(suffix)),
189+
concat(utf8(prefix + suffix), new byte[] {0}),
190+
concat(new byte[] {0}, utf8(prefix + suffix)));
191+
for (byte[] body : bodies) {
192+
assertParseError(Arrays.toString(body), doPost(body));
193+
}
194+
verify(realRpcServer, never()).handleRequest(any(InputStream.class), any(OutputStream.class));
195+
verify(rpc, never()).web3Sha3(any());
196+
}
197+
198+
@Test
199+
public void escapedNulCharacter_isDispatched() throws Exception {
200+
TronJsonRpc rpc = useRealRpcServer();
201+
when(rpc.web3Sha3(any())).thenReturn("0x1");
202+
203+
MockHttpServletResponse resp = doPost("{\"jsonrpc\":\"2.0\",\"method\":\"web3_sha3\","
204+
+ "\"params\":[\"a\\u0000b\"],\"id\":1}");
205+
206+
assertEquals(MAPPER.readTree("{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":\"0x1\"}"),
207+
MAPPER.readTree(resp.getContentAsByteArray()));
208+
verify(rpc).web3Sha3("a" + (char) 0 + "b");
186209
}
187210

188211
@Test
@@ -269,17 +292,6 @@ public void undecodableUtf32Body_returnsParseError() throws Exception {
269292
verify(rpc, never()).getLatestBlockNum();
270293
}
271294

272-
@Test
273-
public void utf16WithoutByteOrderMark_rejectsTrailingContent() throws Exception {
274-
TronJsonRpc rpc = useRealRpcServer();
275-
String request = "{\"jsonrpc\":\"2.0\",\"method\":\"eth_blockNumber\",\"id\":1}";
276-
for (Charset charset : Arrays.asList(StandardCharsets.UTF_16LE, StandardCharsets.UTF_16BE)) {
277-
assertParseError(charset.name(), doPost((request + " " + request).getBytes(charset)));
278-
}
279-
verify(realRpcServer, never()).handleRequest(any(InputStream.class), any(OutputStream.class));
280-
verify(rpc, never()).getLatestBlockNum();
281-
}
282-
283295
// --- batch size limit ---
284296

285297
@Test

0 commit comments

Comments
 (0)