Skip to content

Commit bc5c4ac

Browse files
authored
feat(api): reject request strings that cannot be encoded as UTF-8 (#7102)
1 parent 6ed1154 commit bc5c4ac

5 files changed

Lines changed: 119 additions & 2 deletions

File tree

‎framework/src/main/java/org/tron/core/services/http/servlets/DeployContractServlet.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.springframework.stereotype.Component;
1414
import org.tron.common.utils.ByteArray;
1515
import org.tron.core.Wallet;
16+
import org.tron.core.exception.InvalidHttpParameterException;
1617
import org.tron.core.services.http.HttpApi;
1718
import org.tron.core.services.http.HttpApi.Access;
1819
import org.tron.core.services.http.HttpApi.Surface;
@@ -78,6 +79,9 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
7879
}
7980
String name = jsonObject.getString("name");
8081
if (!Strings.isNullOrEmpty(name)) {
82+
if (JsonFormat.hasUnpairedSurrogate(name)) {
83+
throw new InvalidHttpParameterException("Invalid name: contains an unpaired surrogate");
84+
}
8185
smartBuilder.setName(name);
8286
}
8387

‎framework/src/main/java/org/tron/core/services/http/servlets/JsonFormat.java‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2929
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030
*/
3131

32+
import com.google.common.base.Utf8;
3233
import com.google.common.collect.ImmutableSet;
3334
import com.google.protobuf.ByteString;
3435
import com.google.protobuf.Descriptors;
@@ -767,7 +768,7 @@ private static Object handlePrimitive(Tokenizer tokenizer, FieldDescriptor field
767768
break;
768769

769770
case STRING:
770-
value = tokenizer.consumeString();
771+
value = consumeUtf8String(tokenizer, field);
771772
break;
772773

773774
case BYTES:
@@ -1079,6 +1080,28 @@ private static String replaceMalformedSurrogates(String value) {
10791080
return new String(value.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);
10801081
}
10811082

1083+
/**
1084+
* Returns whether the value cannot be encoded as UTF-8, which a protobuf string field requires.
1085+
*/
1086+
public static boolean hasUnpairedSurrogate(String value) {
1087+
try {
1088+
Utf8.encodedLength(value);
1089+
return false;
1090+
} catch (IllegalArgumentException e) {
1091+
return true;
1092+
}
1093+
}
1094+
1095+
private static String consumeUtf8String(Tokenizer tokenizer, FieldDescriptor field)
1096+
throws ParseException {
1097+
String value = tokenizer.consumeString();
1098+
if (hasUnpairedSurrogate(value)) {
1099+
throw tokenizer.parseExceptionPreviousToken(
1100+
"String field " + field.getFullName() + " contains an unpaired surrogate");
1101+
}
1102+
return value;
1103+
}
1104+
10821105
/**
10831106
* Is this an octal digit.
10841107
*/

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,9 @@ private TransactionJson buildCreateSmartContractTransaction(byte[] ownerAddress,
12451245
ByteString.copyFrom(ByteArray.fromHexString(args.resolveData())));
12461246

12471247
if (StringUtils.isNotEmpty(args.getName())) {
1248+
if (JsonFormat.hasUnpairedSurrogate(args.getName())) {
1249+
throw new JsonRpcInvalidParamsException("invalid name");
1250+
}
12481251
smartBuilder.setName(args.getName());
12491252
}
12501253

‎framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2011,6 +2011,30 @@ public void testBuildTransactionRejectsDeeplyNestedAbi() {
20112011
Assert.assertEquals("invalid abi", e.getMessage());
20122012
}
20132013

2014+
@Test
2015+
public void testBuildTransactionRejectsUnpairedSurrogateName() {
2016+
BuildArguments args = new BuildArguments();
2017+
args.setFrom("0xabd4b9367799eaa3197fecb144eb71de1e049abc");
2018+
args.setData("60806040");
2019+
args.setName("a" + (char) 0xD800);
2020+
2021+
JsonRpcInvalidParamsException e = Assert.assertThrows(JsonRpcInvalidParamsException.class,
2022+
() -> tronJsonRpc.buildTransaction(args));
2023+
Assert.assertEquals("invalid name", e.getMessage());
2024+
}
2025+
2026+
@Test
2027+
public void testBuildTransactionRejectsUnpairedSurrogateInAbi() {
2028+
BuildArguments args = new BuildArguments();
2029+
args.setFrom("0xabd4b9367799eaa3197fecb144eb71de1e049abc");
2030+
args.setData("60806040");
2031+
args.setAbi("[{\"name\":\"\\ud800\"}]");
2032+
2033+
JsonRpcInvalidParamsException e = Assert.assertThrows(JsonRpcInvalidParamsException.class,
2034+
() -> tronJsonRpc.buildTransaction(args));
2035+
Assert.assertEquals("invalid abi", e.getMessage());
2036+
}
2037+
20142038
@Test
20152039
public void testWeb3ClientVersion() {
20162040
try {

‎framework/src/test/java/org/tron/core/services/http/servlets/JsonFormatUnicodeErrorTest.java‎

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.tron.core.services.http.servlets;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
45
import static org.junit.Assert.assertThrows;
56
import static org.junit.Assert.assertTrue;
67

@@ -19,10 +20,60 @@
1920
import org.tron.core.services.ratelimiter.RateLimiterContainer;
2021
import org.tron.json.JSONObject;
2122
import org.tron.protos.Protocol.Account;
23+
import org.tron.protos.contract.SmartContractOuterClass.SmartContract;
2224

2325
public class JsonFormatUnicodeErrorTest {
2426

2527
private static final String EMOJI = new String(Character.toChars(0x1F600));
28+
private static final String NAME_SURROGATE_ERROR =
29+
"1:9: String field protocol.SmartContract.name contains an unpaired surrogate";
30+
31+
@Test
32+
public void testUnpairedSurrogateDetection() {
33+
assertTrue(JsonFormat.hasUnpairedSurrogate(String.valueOf((char) 0xD800)));
34+
assertTrue(JsonFormat.hasUnpairedSurrogate(String.valueOf((char) 0xDC00)));
35+
assertTrue(JsonFormat.hasUnpairedSurrogate("" + (char) 0xDC00 + (char) 0xD800));
36+
assertTrue(JsonFormat.hasUnpairedSurrogate("a" + (char) 0xD83D));
37+
assertFalse(JsonFormat.hasUnpairedSurrogate(""));
38+
assertFalse(JsonFormat.hasUnpairedSurrogate(EMOJI));
39+
assertFalse(JsonFormat.hasUnpairedSurrogate("" + (char) 0x4E2D + (char) 0xFFFF));
40+
}
41+
42+
@Test
43+
public void testStringFieldRejectsUnpairedSurrogates() {
44+
assertNameRejected("\\ud800");
45+
assertNameRejected("\\udc00");
46+
assertNameRejected("a\\udc00\\ud800");
47+
assertNameRejected("x\\uD83D");
48+
assertNameRejected(String.valueOf((char) 0xD800));
49+
}
50+
51+
@Test
52+
public void testStringFieldKeepsWellFormedText() throws Exception {
53+
SmartContract.Builder builder = SmartContract.newBuilder();
54+
JsonFormat.merge("{\"name\":\"\\ud83d\\ude00\\u4e2d\\uffff" + EMOJI + "\"}", builder, false);
55+
56+
assertEquals(EMOJI + (char) 0x4E2D + (char) 0xFFFF + EMOJI, builder.getName());
57+
}
58+
59+
@Test
60+
public void testDeployContractRejectsUnpairedSurrogateInAbi() throws Exception {
61+
String body = "{\"owner_address\":\"\",\"abi\":\"[{\\\"name\\\":\\\"\\\\ud800\\\"}]\"}";
62+
63+
String error = requestError(new DeployContractServlet(), "/wallet/deploycontract", body);
64+
65+
assertEquals("1:20: String field protocol.SmartContract.ABI.Entry.name"
66+
+ " contains an unpaired surrogate", error);
67+
}
68+
69+
@Test
70+
public void testDeployContractRejectsUnpairedSurrogateInName() throws Exception {
71+
String body = "{\"owner_address\":\"\",\"bytecode\":\"00\",\"name\":\"\\ud800\"}";
72+
73+
String error = requestError(new DeployContractServlet(), "/wallet/deploycontract", body);
74+
75+
assertEquals("Invalid name: contains an unpaired surrogate", error);
76+
}
2677

2778
@Test
2879
public void testInvalidSupplementaryEscapeKeepsLegacyReplacement() {
@@ -129,6 +180,11 @@ public void testOrdinaryIntegerErrorsKeepTheirMessages() {
129180

130181
private static String requestError(RateLimiterServlet servlet, String path, JSONObject input)
131182
throws Exception {
183+
return requestError(servlet, path, input.toJSONString());
184+
}
185+
186+
private static String requestError(RateLimiterServlet servlet, String path, String requestBody)
187+
throws Exception {
132188
CommonParameter args = Args.getInstance();
133189
long originalMaxSize = args.getHttpMaxMessageSize();
134190
boolean originalNonBlocking = args.isRateLimiterApiNonBlocking();
@@ -145,7 +201,7 @@ private static String requestError(RateLimiterServlet servlet, String path, JSON
145201
args.setHttpMaxMessageSize(1_000_000L);
146202
args.setRateLimiterApiNonBlocking(false);
147203
server.start();
148-
byte[] body = input.toJSONString().getBytes(StandardCharsets.UTF_8);
204+
byte[] body = requestBody.getBytes(StandardCharsets.UTF_8);
149205
byte[] headers = ("POST " + path + " HTTP/1.1\r\n"
150206
+ "Host: localhost\r\n"
151207
+ "Connection: close\r\n"
@@ -177,6 +233,13 @@ private static String requestError(RateLimiterServlet servlet, String path, JSON
177233
}
178234
}
179235

236+
private static void assertNameRejected(String text) {
237+
JsonFormat.ParseException error = assertThrows(JsonFormat.ParseException.class,
238+
() -> JsonFormat.merge("{\"name\":\"" + text + "\"}", SmartContract.newBuilder(), false));
239+
240+
assertEquals(NAME_SURROGATE_ERROR, error.getMessage());
241+
}
242+
180243
private static void assertEscapeError(String input, String expectedCharacter) {
181244
JsonFormat.InvalidEscapeSequence error = assertThrows(JsonFormat.InvalidEscapeSequence.class,
182245
() -> JsonFormat.unescapeText(input));

0 commit comments

Comments
 (0)