Skip to content

Commit 877fb62

Browse files
committed
fix(test): reverse jsonHexToInt length check
1 parent d72f880 commit 877fb62

3 files changed

Lines changed: 2 additions & 142 deletions

File tree

common/src/main/java/org/tron/common/utils/ByteArray.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,6 @@ public static BigInteger hexToBigInteger(String input) {
152152
}
153153

154154
public static long jsonHexToLong(String x) throws JsonRpcInvalidParamsException {
155-
// Constants for input length validation to prevent DDoS attacks
156-
int MAX_HEX_LONG_LENGTH = 20; // For 64-bit long values (18 chars for 0x7FFFFFFFFFFFFFFF) + safety buffer
157-
if (x == null || x.length() > MAX_HEX_LONG_LENGTH) {
158-
throw new JsonRpcInvalidParamsException("Input cannot be null or too long");
159-
}
160-
161155
if (!x.startsWith("0x")) {
162156
throw new JsonRpcInvalidParamsException("Incorrect hex syntax");
163157
}
@@ -166,12 +160,6 @@ public static long jsonHexToLong(String x) throws JsonRpcInvalidParamsException
166160
}
167161

168162
public static int jsonHexToInt(String x) throws Exception {
169-
// Constants for input length validation to prevent DDoS attacks
170-
int MAX_HEX_INT_LENGTH = 12; // For 32-bit int values (10 chars for 0x7FFFFFFF) + safety buffer
171-
if (x == null || x.length() > MAX_HEX_INT_LENGTH) {
172-
throw new Exception("Incorrect string length");
173-
}
174-
175163
if (!x.startsWith("0x")) {
176164
throw new Exception("Incorrect hex syntax");
177165
}

framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcApiUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@
5858
public class JsonRpcApiUtil {
5959
/**
6060
* Maximum allowed length for block identifiers to prevent DDoS attacks.
61-
* Supports block hashes (66 chars) + safety buffer.
61+
* Supports block hashes (66 chars) + safety enough buffer.
6262
*/
63-
private static final int MAX_BLOCK_IDENTIFIER_LENGTH = 128;
63+
private static final int MAX_BLOCK_IDENTIFIER_LENGTH = 512;
6464

6565
public static byte[] convertToTronAddress(byte[] address) {
6666
byte[] newAddress = new byte[21];

framework/src/test/java/org/tron/common/utils/ByteArrayTest.java

Lines changed: 0 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@
2323
import static org.junit.Assert.fail;
2424
import static org.tron.common.utils.ByteArray.fromHex;
2525
import static org.tron.common.utils.ByteArray.jsonHexToInt;
26-
import static org.tron.common.utils.ByteArray.jsonHexToLong;
2726

2827
import lombok.extern.slf4j.Slf4j;
2928
import org.bouncycastle.util.encoders.Hex;
3029
import org.junit.Test;
31-
import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException;
3230

3331
@Slf4j
3432
public class ByteArrayTest {
@@ -117,130 +115,4 @@ public void testFromHexWithPrefix() {
117115
String input1 = "1A3";
118116
assertEquals("01A3", fromHex(input1));
119117
}
120-
121-
@Test
122-
public void testJsonHexToLong_ValidInputs() {
123-
try {
124-
// Test basic hex conversion
125-
assertEquals(26L, jsonHexToLong("0x1A"));
126-
assertEquals(255L, jsonHexToLong("0xFF"));
127-
assertEquals(0L, jsonHexToLong("0x0"));
128-
assertEquals(1L, jsonHexToLong("0x1"));
129-
130-
// Test large values
131-
assertEquals(4294967295L, jsonHexToLong("0xFFFFFFFF"));
132-
133-
// Test maximum long value
134-
assertEquals(Long.MAX_VALUE, jsonHexToLong("0x7FFFFFFFFFFFFFFF"));
135-
} catch (JsonRpcInvalidParamsException e) {
136-
fail("Exception should not have been thrown for valid hex strings: " + e.getMessage());
137-
}
138-
}
139-
140-
@Test
141-
public void testJsonHexToLong_InvalidInputs() {
142-
// Test null input
143-
assertThrows(JsonRpcInvalidParamsException.class, () -> jsonHexToLong(null));
144-
145-
// Test missing 0x prefix
146-
assertThrows(JsonRpcInvalidParamsException.class, () -> jsonHexToLong("1A"));
147-
148-
// Test too long input (DDoS protection)
149-
StringBuilder tooLongStr = new StringBuilder("0x");
150-
for (int i = 0; i < 20; i++) {
151-
tooLongStr.append("F");
152-
}
153-
String tooLongHex = tooLongStr.toString(); // 22 characters total, exceeds MAX_HEX_LONG_LENGTH
154-
assertThrows(JsonRpcInvalidParamsException.class, () -> jsonHexToLong(tooLongHex));
155-
156-
// Test invalid hex characters
157-
assertThrows(NumberFormatException.class, () -> jsonHexToLong("0xGG"));
158-
}
159-
160-
@Test
161-
public void testJsonHexToInt_ValidInputs() {
162-
try {
163-
// Test basic hex conversion
164-
assertEquals(26, jsonHexToInt("0x1A"));
165-
assertEquals(255, jsonHexToInt("0xFF"));
166-
assertEquals(0, jsonHexToInt("0x0"));
167-
assertEquals(1, jsonHexToInt("0x1"));
168-
169-
// Test maximum int value
170-
assertEquals(Integer.MAX_VALUE, jsonHexToInt("0x7FFFFFFF"));
171-
172-
// Test large values
173-
assertEquals(65535, jsonHexToInt("0xFFFF"));
174-
} catch (Exception e) {
175-
fail("Exception should not have been thrown for valid hex strings: " + e.getMessage());
176-
}
177-
}
178-
179-
@Test
180-
public void testJsonHexToInt_InvalidInputs() {
181-
// Test null input
182-
assertThrows(Exception.class, () -> jsonHexToInt(null));
183-
184-
// Test missing 0x prefix
185-
assertThrows(Exception.class, () -> jsonHexToInt("1A"));
186-
187-
// Test too long input (DDoS protection)
188-
StringBuilder tooLongStr = new StringBuilder("0x");
189-
for (int i = 0; i < 12; i++) {
190-
tooLongStr.append("F");
191-
}
192-
String tooLongHex = tooLongStr.toString(); // 14 characters total, exceeds MAX_HEX_INT_LENGTH
193-
assertThrows(Exception.class, () -> jsonHexToInt(tooLongHex));
194-
195-
// Test invalid hex characters
196-
assertThrows(NumberFormatException.class, () -> jsonHexToInt("0xGG"));
197-
}
198-
199-
@Test
200-
public void testJsonHexToLong_EdgeCases() {
201-
try {
202-
// Test minimum length valid input
203-
assertEquals(0L, jsonHexToLong("0x0"));
204-
205-
// Test a long hex string that's within limits but doesn't overflow
206-
assertEquals(4095L, jsonHexToLong("0xFFF")); // 3 F's = 4095, safe value
207-
208-
// Test length validation - this should pass length check
209-
assertEquals(1048575L, jsonHexToLong("0xFFFFF")); // 5 F's = 1048575, safe value
210-
} catch (JsonRpcInvalidParamsException e) {
211-
fail("Exception should not have been thrown for edge case inputs: " + e.getMessage());
212-
}
213-
}
214-
215-
@Test
216-
public void testJsonHexToInt_EdgeCases() {
217-
try {
218-
// Test minimum length valid input
219-
assertEquals(0, jsonHexToInt("0x0"));
220-
221-
// Test a hex string that's within limits but doesn't overflow
222-
assertEquals(4095, jsonHexToInt("0xFFF")); // 3 F's = 4095, safe value
223-
224-
// Test length validation - this should pass length check
225-
assertEquals(1048575, jsonHexToInt("0xFFFFF")); // 5 F's = 1048575, safe value
226-
} catch (Exception e) {
227-
fail("Exception should not have been thrown for edge case inputs: " + e.getMessage());
228-
}
229-
}
230-
231-
@Test
232-
public void testJsonHexToLong_OverflowHandling() {
233-
// Test that Long.parseLong properly handles overflow by throwing NumberFormatException
234-
// This tests values that pass length validation but cause overflow
235-
assertThrows(NumberFormatException.class,
236-
() -> jsonHexToLong("0x8000000000000000")); // Long.MAX_VALUE + 1
237-
}
238-
239-
@Test
240-
public void testJsonHexToInt_OverflowHandling() {
241-
// Test that Integer.parseInt properly handles overflow by throwing NumberFormatException
242-
// This tests values that pass length validation but cause overflow
243-
assertThrows(NumberFormatException.class,
244-
() -> jsonHexToInt("0x80000000")); // Integer.MAX_VALUE + 1
245-
}
246118
}

0 commit comments

Comments
 (0)