forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
fix(jsonrpc): add blockNumOrHash length check #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b50d959
fix readme typo and format
Little-Peony 1fda1ed
fix rpc blockNum or hash or tag too long risk
Little-Peony b526ede
update getByJsonBlockId
Little-Peony b26ac14
Merge branch 'develop' of https://github.com/Sunny6889/java-tron into…
Little-Peony 7f78fb0
fix merge
Little-Peony 7d02487
revert some changes
Little-Peony ccc0a26
remove check
Little-Peony 6841903
lower the default QPS limit
Little-Peony 8591199
add ByteArrayTest
Little-Peony 370c972
fix test
Little-Peony eb0cf8d
fix qps config
Little-Peony b1970a6
update jsonrpcTest
Little-Peony ac528da
clean code
Little-Peony 214bf5f
fix lint
Little-Peony cd5159c
fix lint
Little-Peony 090d072
fix review
Little-Peony File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.getTransactionIndex; | ||
| import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.getTxID; | ||
| import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.triggerCallContract; | ||
| import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.validateBlockNumOrHashOrTag; | ||
|
|
||
| import com.alibaba.fastjson.JSON; | ||
| import com.google.common.cache.Cache; | ||
|
|
@@ -296,6 +297,7 @@ public String web3Sha3(String data) throws JsonRpcInvalidParamsException { | |
| @Override | ||
| public String ethGetBlockTransactionCountByHash(String blockHash) | ||
| throws JsonRpcInvalidParamsException { | ||
| validateBlockNumOrHashOrTag(blockHash); | ||
| Block b = getBlockByJsonHash(blockHash); | ||
| if (b == null) { | ||
| return null; | ||
|
|
@@ -308,6 +310,7 @@ public String ethGetBlockTransactionCountByHash(String blockHash) | |
| @Override | ||
| public String ethGetBlockTransactionCountByNumber(String blockNumOrTag) | ||
| throws JsonRpcInvalidParamsException { | ||
| validateBlockNumOrHashOrTag(blockNumOrTag); | ||
| List<Transaction> list = wallet.getTransactionsByJsonBlockId(blockNumOrTag); | ||
| if (list == null) { | ||
| return null; | ||
|
|
@@ -327,6 +330,7 @@ public BlockResult ethGetBlockByHash(String blockHash, Boolean fullTransactionOb | |
| @Override | ||
| public BlockResult ethGetBlockByNumber(String blockNumOrTag, Boolean fullTransactionObjects) | ||
| throws JsonRpcInvalidParamsException { | ||
| validateBlockNumOrHashOrTag(blockNumOrTag); | ||
| final Block b = wallet.getByJsonBlockId(blockNumOrTag); | ||
| return (b == null ? null : getBlockResult(b, fullTransactionObjects)); | ||
| } | ||
|
|
@@ -393,6 +397,9 @@ public String getLatestBlockNum() { | |
| @Override | ||
| public String getTrxBalance(String address, String blockNumOrTag) | ||
| throws JsonRpcInvalidParamsException { | ||
| // Add length check and validate hex format to prevent DDoS attacks | ||
Sunny6889 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| validateBlockNumOrHashOrTag(blockNumOrTag); | ||
|
|
||
| if (EARLIEST_STR.equalsIgnoreCase(blockNumOrTag) | ||
| || PENDING_STR.equalsIgnoreCase(blockNumOrTag) | ||
| || FINALIZED_STR.equalsIgnoreCase(blockNumOrTag)) { | ||
|
|
@@ -409,12 +416,6 @@ public String getTrxBalance(String address, String blockNumOrTag) | |
| } | ||
| return ByteArray.toJsonHex(balance); | ||
| } else { | ||
| try { | ||
| ByteArray.hexToBigInteger(blockNumOrTag); | ||
| } catch (Exception e) { | ||
| throw new JsonRpcInvalidParamsException(BLOCK_NUM_ERROR); | ||
| } | ||
|
|
||
| throw new JsonRpcInvalidParamsException(QUANTITY_NOT_SUPPORT_ERROR); | ||
| } | ||
| } | ||
|
|
@@ -535,6 +536,7 @@ private String call(byte[] ownerAddressByte, byte[] contractAddressByte, long va | |
| @Override | ||
| public String getStorageAt(String address, String storageIdx, String blockNumOrTag) | ||
| throws JsonRpcInvalidParamsException { | ||
| validateBlockNumOrHashOrTag(blockNumOrTag); | ||
| if (EARLIEST_STR.equalsIgnoreCase(blockNumOrTag) | ||
| || PENDING_STR.equalsIgnoreCase(blockNumOrTag) | ||
| || FINALIZED_STR.equalsIgnoreCase(blockNumOrTag)) { | ||
|
|
@@ -558,19 +560,14 @@ public String getStorageAt(String address, String storageIdx, String blockNumOrT | |
| DataWord value = storage.getValue(new DataWord(ByteArray.fromHexString(storageIdx))); | ||
| return ByteArray.toJsonHex(value == null ? new byte[32] : value.getData()); | ||
| } else { | ||
| try { | ||
| ByteArray.hexToBigInteger(blockNumOrTag); | ||
| } catch (Exception e) { | ||
| throw new JsonRpcInvalidParamsException(BLOCK_NUM_ERROR); | ||
| } | ||
|
|
||
| throw new JsonRpcInvalidParamsException(QUANTITY_NOT_SUPPORT_ERROR); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String getABIOfSmartContract(String contractAddress, String blockNumOrTag) | ||
| throws JsonRpcInvalidParamsException { | ||
| validateBlockNumOrHashOrTag(blockNumOrTag); | ||
| if (EARLIEST_STR.equalsIgnoreCase(blockNumOrTag) | ||
| || PENDING_STR.equalsIgnoreCase(blockNumOrTag) | ||
| || FINALIZED_STR.equalsIgnoreCase(blockNumOrTag)) { | ||
|
|
@@ -589,12 +586,6 @@ public String getABIOfSmartContract(String contractAddress, String blockNumOrTag | |
| } | ||
|
|
||
| } else { | ||
| try { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why remove this check?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I think this check is no need and cost computational resources. |
||
| ByteArray.hexToBigInteger(blockNumOrTag); | ||
| } catch (Exception e) { | ||
| throw new JsonRpcInvalidParamsException(BLOCK_NUM_ERROR); | ||
| } | ||
|
|
||
| throw new JsonRpcInvalidParamsException(QUANTITY_NOT_SUPPORT_ERROR); | ||
| } | ||
| } | ||
|
|
@@ -791,6 +782,7 @@ private TransactionResult getTransactionByBlockAndIndex(Block block, String inde | |
| @Override | ||
| public TransactionResult getTransactionByBlockHashAndIndex(String blockHash, String index) | ||
| throws JsonRpcInvalidParamsException { | ||
| validateBlockNumOrHashOrTag(blockHash); | ||
| final Block block = getBlockByJsonHash(blockHash); | ||
|
|
||
| if (block == null) { | ||
|
|
@@ -803,6 +795,9 @@ public TransactionResult getTransactionByBlockHashAndIndex(String blockHash, Str | |
| @Override | ||
| public TransactionResult getTransactionByBlockNumberAndIndex(String blockNumOrTag, String index) | ||
| throws JsonRpcInvalidParamsException { | ||
| // Add length check and validate hex format to prevent DDoS attacks | ||
| validateBlockNumOrHashOrTag(blockNumOrTag); | ||
|
|
||
| Block block = wallet.getByJsonBlockId(blockNumOrTag); | ||
| if (block == null) { | ||
| return null; | ||
|
|
@@ -888,6 +883,8 @@ private TransactionContext findTransactionContext(TransactionInfoList infoList, | |
| @Override | ||
| public List<TransactionReceipt> getBlockReceipts(String blockNumOrHashOrTag) | ||
| throws JsonRpcInvalidParamsException, JsonRpcInternalException { | ||
| // Add length check and validate hex format to prevent DDoS attacks | ||
| validateBlockNumOrHashOrTag(blockNumOrHashOrTag); | ||
|
|
||
| Block block = null; | ||
|
|
||
|
|
@@ -973,6 +970,8 @@ public String getCall(CallArguments transactionCall, Object blockParamObj) | |
|
|
||
| long blockNumber; | ||
| try { | ||
| // Add length check to prevent DDoS attacks | ||
| validateBlockNumOrHashOrTag(blockNumOrTag); | ||
| blockNumber = ByteArray.hexToBigInteger(blockNumOrTag).longValue(); | ||
| } catch (Exception e) { | ||
| throw new JsonRpcInvalidParamsException(BLOCK_NUM_ERROR); | ||
|
|
@@ -1014,12 +1013,6 @@ public String getCall(CallArguments transactionCall, Object blockParamObj) | |
| return call(addressData, contractAddressData, transactionCall.parseValue(), | ||
| ByteArray.fromHexString(transactionCall.getData())); | ||
| } else { | ||
| try { | ||
| ByteArray.hexToBigInteger(blockNumOrTag); | ||
| } catch (Exception e) { | ||
| throw new JsonRpcInvalidParamsException(BLOCK_NUM_ERROR); | ||
| } | ||
|
|
||
| throw new JsonRpcInvalidParamsException(QUANTITY_NOT_SUPPORT_ERROR); | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about also check tags and BlockNumer in this function?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi good idea, but because want to limit the PR changes to only one scope a time