Skip to content

Commit 2970bc2

Browse files
authored
fix(http): bound JsonFormat integer and enum token parsing (#7031)
1 parent 075049f commit 2970bc2

7 files changed

Lines changed: 513 additions & 27 deletions

File tree

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

Lines changed: 76 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,21 @@ public class JsonFormat {
8484
private static final String EXPECTED_STRING = "Expected string.";
8585
private static final String MISSING_END_QUOTE = "String missing ending quote.";
8686

87+
// Bounds all work and error messages performed by parseInteger after tokenization.
88+
static final int MAX_INTEGER_TOKEN_LENGTH = 256;
89+
90+
// Enum identifiers are schema-defined and small. This is an input-contract and performance
91+
// limit, not the error-response bound; abbreviateIdentifier() bounds errors independently.
92+
// Keep it separate from generic identifiers, strings and bytes so large values remain valid.
93+
static final int MAX_ENUM_TOKEN_LENGTH = 256;
94+
95+
private static final int MAX_IDENTIFIER_ECHO_PREFIX_LENGTH = 64;
96+
97+
// Caps only the JDK NumberFormatException detail retained for integer syntax errors. The detail
98+
// may contain an input prefix. Supported JDKs emit shorter messages in tested cases, but their
99+
// message format is not a stable contract, so keep this limit as a defensive boundary.
100+
private static final int MAX_INTEGER_ERROR_DETAIL_PREFIX_LENGTH = 256;
101+
87102
public static final boolean ALWAYS_OUTPUT_DEFAULT_VALUE_FIELDS = true;
88103
public static final Set<Class<? extends Message>> MESSAGES = ImmutableSet.of(
89104
BalanceContract.AccountBalanceResponse.class,
@@ -768,6 +783,13 @@ private static Object handlePrimitive(Tokenizer tokenizer, FieldDescriptor field
768783
+ number + ".");
769784
}
770785
} else {
786+
String enumToken = tokenizer.currentToken();
787+
// Reject before case normalization and descriptor lookup. Error echo is bounded
788+
// independently by abbreviateIdentifier().
789+
if (enumToken.length() > MAX_ENUM_TOKEN_LENGTH) {
790+
throw tokenizer.parseException("Enum token is too long: length "
791+
+ enumToken.length() + ", max " + MAX_ENUM_TOKEN_LENGTH);
792+
}
771793
String id = tokenizer.consumeIdentifier();
772794
if (StringUtils.isAllLowerCase(id)) {
773795
char b = id.charAt(0);
@@ -780,7 +802,7 @@ private static Object handlePrimitive(Tokenizer tokenizer, FieldDescriptor field
780802
throw tokenizer.parseExceptionPreviousToken("Enum type \""
781803
+ enumType.getFullName()
782804
+ "\" has no value named \""
783-
+ id + "\".");
805+
+ abbreviateIdentifier(id) + "\".");
784806
}
785807
}
786808

@@ -1140,14 +1162,50 @@ static long parseUInt64(String text) throws NumberFormatException {
11401162
return parseInteger(text, false, true);
11411163
}
11421164

1165+
private static String truncate(String text, int maxPrefixLength) {
1166+
if (text == null) {
1167+
return "null";
1168+
}
1169+
if (text.length() <= maxPrefixLength) {
1170+
return text;
1171+
}
1172+
int prefixEnd = maxPrefixLength;
1173+
if (prefixEnd > 0
1174+
&& Character.isHighSurrogate(text.charAt(prefixEnd - 1))
1175+
&& Character.isLowSurrogate(text.charAt(prefixEnd))) {
1176+
--prefixEnd;
1177+
}
1178+
return text.substring(0, prefixEnd) + "...(truncated)";
1179+
}
1180+
1181+
private static String integerOutOfRangeMessage(boolean isSigned, boolean isLong) {
1182+
return "Number out of range for " + (isLong ? "64" : "32") + "-bit "
1183+
+ (isSigned ? "signed" : "unsigned") + " integer.";
1184+
}
1185+
1186+
private static String abbreviateIdentifier(String text) {
1187+
return truncate(text, MAX_IDENTIFIER_ECHO_PREFIX_LENGTH);
1188+
}
1189+
1190+
private static String abbreviateIntegerError(String text) {
1191+
return truncate(text, MAX_INTEGER_ERROR_DETAIL_PREFIX_LENGTH);
1192+
}
1193+
11431194
private static long parseInteger(String text, boolean isSigned, boolean isLong)
11441195
throws NumberFormatException {
1196+
// This must remain the first operation: no substring, large error message or BigInteger
1197+
// construction may happen before the raw token is bounded.
1198+
if (text.length() > MAX_INTEGER_TOKEN_LENGTH) {
1199+
throw new NumberFormatException("Integer token is too long: length " + text.length()
1200+
+ ", max " + MAX_INTEGER_TOKEN_LENGTH);
1201+
}
1202+
11451203
int pos = 0;
11461204

11471205
boolean negative = false;
11481206
if (text.startsWith("-", pos)) {
11491207
if (!isSigned) {
1150-
throw new NumberFormatException("Number must be positive: " + text);
1208+
throw new NumberFormatException("Number must be positive.");
11511209
}
11521210
++pos;
11531211
negative = true;
@@ -1177,17 +1235,17 @@ private static long parseInteger(String text, boolean isSigned, boolean isLong)
11771235
if (!isLong) {
11781236
if (isSigned) {
11791237
if ((result > Integer.MAX_VALUE) || (result < Integer.MIN_VALUE)) {
1180-
throw new NumberFormatException("Number out of range for 32-bit signed integer: "
1181-
+ text);
1238+
throw new NumberFormatException(integerOutOfRangeMessage(isSigned, isLong));
11821239
}
11831240
} else {
11841241
if ((result >= (1L << 32)) || (result < 0)) {
1185-
throw new NumberFormatException("Number out of range for 32-bit unsigned integer: "
1186-
+ text);
1242+
throw new NumberFormatException(integerOutOfRangeMessage(isSigned, isLong));
11871243
}
11881244
}
11891245
}
11901246
} else {
1247+
// Preserve existing syntax behavior by passing numberText to BigInteger without additional
1248+
// normalization.
11911249
BigInteger bigValue = new BigInteger(numberText, radix);
11921250
if (negative) {
11931251
bigValue = bigValue.negate();
@@ -1197,25 +1255,21 @@ private static long parseInteger(String text, boolean isSigned, boolean isLong)
11971255
if (!isLong) {
11981256
if (isSigned) {
11991257
if (bigValue.bitLength() > 31) {
1200-
throw new NumberFormatException("Number out of range for 32-bit signed integer: "
1201-
+ text);
1258+
throw new NumberFormatException(integerOutOfRangeMessage(isSigned, isLong));
12021259
}
12031260
} else {
12041261
if (bigValue.bitLength() > 32) {
1205-
throw new NumberFormatException("Number out of range for 32-bit unsigned integer: "
1206-
+ text);
1262+
throw new NumberFormatException(integerOutOfRangeMessage(isSigned, isLong));
12071263
}
12081264
}
12091265
} else {
12101266
if (isSigned) {
12111267
if (bigValue.bitLength() > 63) {
1212-
throw new NumberFormatException("Number out of range for 64-bit signed integer: "
1213-
+ text);
1268+
throw new NumberFormatException(integerOutOfRangeMessage(isSigned, isLong));
12141269
}
12151270
} else {
12161271
if (bigValue.bitLength() > 64) {
1217-
throw new NumberFormatException("Number out of range for 64-bit unsigned integer: "
1218-
+ text);
1272+
throw new NumberFormatException(integerOutOfRangeMessage(isSigned, isLong));
12191273
}
12201274
}
12211275
}
@@ -1606,8 +1660,8 @@ public double consumeDouble() throws ParseException {
16061660
double result = Double.parseDouble(currentToken);
16071661
nextToken();
16081662
return result;
1609-
} catch (NumberFormatException e) {
1610-
throw floatParseException(e);
1663+
} catch (NumberFormatException ignored) {
1664+
throw floatParseException();
16111665
}
16121666
}
16131667

@@ -1631,8 +1685,8 @@ public float consumeFloat() throws ParseException {
16311685
float result = Float.parseFloat(currentToken);
16321686
nextToken();
16331687
return result;
1634-
} catch (NumberFormatException e) {
1635-
throw floatParseException(e);
1688+
} catch (NumberFormatException ignored) {
1689+
throw floatParseException();
16361690
}
16371691
}
16381692

@@ -1752,15 +1806,12 @@ public ParseException parseExceptionPreviousToken(String description) {
17521806
* when trying to parse an integer.
17531807
*/
17541808
private ParseException integerParseException(NumberFormatException e) {
1755-
return parseException("Couldn't parse integer: " + e.getMessage());
1809+
return parseException("Couldn't parse integer: " + abbreviateIntegerError(e.getMessage()));
17561810
}
17571811

1758-
/**
1759-
* Constructs an appropriate {@link ParseException} for the given {@code NumberFormatException}
1760-
* when trying to parse a float or double.
1761-
*/
1762-
private ParseException floatParseException(NumberFormatException e) {
1763-
return parseException("Couldn't parse number: " + e.getMessage());
1812+
/** Constructs a bounded {@link ParseException} without forwarding the JDK parse error text. */
1813+
private ParseException floatParseException() {
1814+
return parseException("Couldn't parse number: token length " + currentToken.length());
17641815
}
17651816
}
17661817

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
package org.tron.core.services.http.servlets;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertNotNull;
6+
import static org.junit.Assert.assertTrue;
47
import static org.mockito.ArgumentMatchers.any;
58
import static org.mockito.ArgumentMatchers.argThat;
69
import static org.mockito.ArgumentMatchers.eq;
710
import static org.mockito.Mockito.verify;
11+
import static org.mockito.Mockito.verifyNoInteractions;
812
import static org.mockito.Mockito.when;
913

14+
import com.google.common.base.Strings;
1015
import org.junit.Test;
1116
import org.springframework.mock.web.MockHttpServletRequest;
1217
import org.springframework.mock.web.MockHttpServletResponse;
1318
import org.tron.core.capsule.TransactionCapsule;
19+
import org.tron.json.JSONObject;
1420
import org.tron.protos.Protocol;
1521
import org.tron.protos.contract.SmartContractOuterClass.CreateSmartContract;
1622

@@ -82,4 +88,52 @@ public void testDeployContractOmitsNullAbiOutputs() throws Exception {
8288
eq(Protocol.Transaction.Contract.ContractType.CreateSmartContract));
8389
assertTransactionResponse(response);
8490
}
91+
92+
@Test
93+
public void testRejectsOversizedAbiIntegerWithBoundedResponse() throws Exception {
94+
// This test scopes the response bound to the ABI parser used by this endpoint. Numeric
95+
// request fields use a separate conversion path and are outside this test's scope.
96+
String oversizedValue = Strings.repeat("9", 100_000);
97+
String jsonParam = "{"
98+
+ "\"owner_address\":\"4199357684BC659F5166046B56C95A0E99F1265CD1\","
99+
+ "\"abi\":\"[{\\\"name\\\":\\\"x\\\",\\\"type\\\":"
100+
+ oversizedValue + "}]\""
101+
+ "}";
102+
MockHttpServletResponse response = newResponse();
103+
104+
servlet.doPost(postRequest(jsonParam), response);
105+
106+
String content = response.getContentAsString();
107+
JSONObject result = JSONObject.parseObject(content);
108+
String error = result.getString("Error");
109+
assertNotNull("Missing Error field in response: " + content, error);
110+
assertTrue(error.contains("Integer token is too long"));
111+
assertTrue("Unexpectedly large response", response.getContentAsByteArray().length < 512);
112+
assertFalse("Response echoes the request token", content.contains(Strings.repeat("9", 8)));
113+
verifyNoInteractions(wallet);
114+
}
115+
116+
@Test
117+
public void testRejectsOversizedAbiEnumIdentifierWithBoundedResponse() throws Exception {
118+
// This test scopes the response bound to the ABI parser used by this endpoint. Numeric
119+
// request fields use a separate conversion path and are outside this test's scope.
120+
String oversizedValue = Strings.repeat("9", 100_000);
121+
String jsonParam = "{"
122+
+ "\"owner_address\":\"4199357684BC659F5166046B56C95A0E99F1265CD1\","
123+
+ "\"abi\":\"[{\\\"name\\\":\\\"x\\\",\\\"type\\\":\\\""
124+
+ oversizedValue + "\\\"}]\""
125+
+ "}";
126+
MockHttpServletResponse response = newResponse();
127+
128+
servlet.doPost(postRequest(jsonParam), response);
129+
130+
String content = response.getContentAsString();
131+
JSONObject result = JSONObject.parseObject(content);
132+
String error = result.getString("Error");
133+
assertNotNull("Missing Error field in response: " + content, error);
134+
assertTrue(error.contains("Enum token is too long"));
135+
assertTrue("Unexpectedly large response", response.getContentAsByteArray().length < 512);
136+
assertFalse("Response echoes the request token", content.contains(Strings.repeat("9", 8)));
137+
verifyNoInteractions(wallet);
138+
}
85139
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package org.tron.core.services.http.servlets;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertThrows;
6+
import static org.junit.Assert.assertTrue;
7+
import static org.tron.core.services.http.servlets.JsonFormatTestSupport.assertBoundedWithoutRepeatedInput;
8+
import static org.tron.core.services.http.servlets.JsonFormatTestSupport.mergeAbi;
9+
10+
import com.google.common.base.Strings;
11+
import org.junit.Test;
12+
import org.tron.protos.contract.SmartContractOuterClass.SmartContract.ABI;
13+
import org.tron.protos.contract.SmartContractOuterClass.SmartContract.ABI.Entry;
14+
import org.tron.protos.contract.SmartContractOuterClass.TriggerSmartContract;
15+
16+
public class JsonFormatErrorBoundaryTest {
17+
18+
private static final int LARGE_TOKEN_LENGTH = 65_536;
19+
20+
@Test
21+
public void oversizedEnumIdentifierIsRejectedWithoutEcho() {
22+
String value = Strings.repeat("9", 100_000);
23+
24+
JsonFormat.ParseException error = assertThrows(JsonFormat.ParseException.class,
25+
() -> mergeAbi("[{\"name\":\"x\",\"type\":\"" + value + "\"}]"));
26+
27+
assertTrue(error.getMessage().contains("Enum token is too long"));
28+
assertBoundedWithoutRepeatedInput(error.getMessage(), '9');
29+
}
30+
31+
@Test
32+
public void enumRawLimitIncludesQuotes() {
33+
String atLimit = Strings.repeat("A", JsonFormat.MAX_ENUM_TOKEN_LENGTH - 2);
34+
String overLimit = Strings.repeat("A", JsonFormat.MAX_ENUM_TOKEN_LENGTH - 1);
35+
36+
JsonFormat.ParseException atLimitError = assertThrows(JsonFormat.ParseException.class,
37+
() -> mergeAbi("[{\"name\":\"x\",\"type\":\"" + atLimit + "\"}]"));
38+
JsonFormat.ParseException overLimitError = assertThrows(JsonFormat.ParseException.class,
39+
() -> mergeAbi("[{\"name\":\"x\",\"type\":\"" + overLimit + "\"}]"));
40+
41+
// Values at the enum-token limit retain a bounded identifier prefix for diagnostics.
42+
// Oversized enum tokens take the fixed no-echo rejection path.
43+
String expectedPrefix = Strings.repeat("A", 64) + "...(truncated)";
44+
assertFalse(atLimitError.getMessage().contains("Enum token is too long"));
45+
assertFalse(atLimitError.getMessage().contains(atLimit));
46+
assertTrue(atLimitError.getMessage().contains(expectedPrefix));
47+
assertTrue("Unexpectedly long error message", atLimitError.getMessage().length() < 512);
48+
assertTrue(overLimitError.getMessage().contains("Enum token is too long"));
49+
assertBoundedWithoutRepeatedInput(overLimitError.getMessage(), 'A');
50+
}
51+
52+
@Test
53+
public void oversizedUnknownFieldNameRemainsIgnored() throws Exception {
54+
String unknownName = Strings.repeat("a", 100_000);
55+
56+
ABI abi = mergeAbi("[{\"name\":\"x\",\"" + unknownName + "\":1}]");
57+
58+
assertEquals(1, abi.getEntrysCount());
59+
assertEquals("x", abi.getEntrys(0).getName());
60+
}
61+
62+
@Test
63+
public void largeStringTokenRemainsAccepted() throws Exception {
64+
String value = Strings.repeat("A", LARGE_TOKEN_LENGTH);
65+
Entry.Builder builder = Entry.newBuilder();
66+
67+
JsonFormat.merge("{\"name\":\"" + value + "\"}", builder, false);
68+
69+
assertEquals(value, builder.getName());
70+
}
71+
72+
@Test
73+
public void largeBytesTokenRemainsAccepted() throws Exception {
74+
String value = Strings.repeat("ab", LARGE_TOKEN_LENGTH / 2);
75+
TriggerSmartContract.Builder builder = TriggerSmartContract.newBuilder();
76+
77+
JsonFormat.merge("{\"data\":\"" + value + "\"}", builder, false);
78+
79+
assertEquals(LARGE_TOKEN_LENGTH / 2, builder.getData().size());
80+
assertEquals((byte) 0xab, builder.getData().byteAt(0));
81+
assertEquals((byte) 0xab, builder.getData().byteAt(builder.getData().size() - 1));
82+
}
83+
}

0 commit comments

Comments
 (0)