Skip to content

Commit 1be7ee5

Browse files
committed
fix(rpc): prevent memory exhaustion attack in eth_getStorageAt by limiting storageIdx length
1 parent 039821c commit 1be7ee5

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,12 @@ private String call(byte[] ownerAddressByte, byte[] contractAddressByte, long va
535535
@Override
536536
public String getStorageAt(String address, String storageIdx, String blockNumOrTag)
537537
throws JsonRpcInvalidParamsException {
538+
if (StringUtils.isBlank(storageIdx)
539+
|| "0x".equalsIgnoreCase(storageIdx)
540+
|| (storageIdx.startsWith("0x") ? storageIdx.length() > 66 : storageIdx.length() > 64)) {
541+
throw new JsonRpcInvalidParamsException("invalid storage index");
542+
}
543+
538544
if (EARLIEST_STR.equalsIgnoreCase(blockNumOrTag)
539545
|| PENDING_STR.equalsIgnoreCase(blockNumOrTag)
540546
|| FINALIZED_STR.equalsIgnoreCase(blockNumOrTag)) {

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,26 +525,49 @@ public void testGetStorageAt() {
525525
try {
526526
tronJsonRpc.getStorageAt("", "", "earliest");
527527
Assert.fail("Expected to be thrown");
528+
} catch (Exception e) {
529+
Assert.assertEquals("invalid storage index", e.getMessage());
530+
}
531+
532+
try {
533+
tronJsonRpc.getStorageAt("", "0", "earliest");
534+
Assert.fail("Expected to be thrown");
528535
} catch (Exception e) {
529536
Assert.assertEquals("TAG [earliest | pending | finalized] not supported",
530537
e.getMessage());
531538
}
532539

533540
try {
534-
tronJsonRpc.getStorageAt("", "", "pending");
541+
tronJsonRpc.getStorageAt("", "0", "pending");
535542
Assert.fail("Expected to be thrown");
536543
} catch (Exception e) {
537544
Assert.assertEquals("TAG [earliest | pending | finalized] not supported",
538545
e.getMessage());
539546
}
540547

541548
try {
542-
tronJsonRpc.getStorageAt("", "", "finalized");
549+
tronJsonRpc.getStorageAt("", "0", "finalized");
543550
Assert.fail("Expected to be thrown");
544551
} catch (Exception e) {
545552
Assert.assertEquals("TAG [earliest | pending | finalized] not supported",
546553
e.getMessage());
547554
}
555+
556+
try {
557+
tronJsonRpc.getStorageAt("",
558+
"0x00000000000000000000000000000000000000000000000000000000000000000", "latest");
559+
Assert.fail("Expected to be thrown");
560+
} catch (Exception e) {
561+
Assert.assertEquals("invalid storage index", e.getMessage());
562+
}
563+
564+
try {
565+
tronJsonRpc.getStorageAt("",
566+
"00000000000000000000000000000000000000000000000000000000000000000", "latest");
567+
Assert.fail("Expected to be thrown");
568+
} catch (Exception e) {
569+
Assert.assertEquals("invalid storage index", e.getMessage());
570+
}
548571
}
549572

550573
@Test

0 commit comments

Comments
 (0)