fix(api): input validation and rate limiting#11
Open
barbatos2011 wants to merge 4 commits intodevelopfrom
Open
fix(api): input validation and rate limiting#11barbatos2011 wants to merge 4 commits intodevelopfrom
barbatos2011 wants to merge 4 commits intodevelopfrom
Conversation
added 3 commits
April 2, 2026 09:38
- TronJsonRpcImpl: add MAX_HEX_PARAM_LENGTH=128 check before all 5 hexToBigInteger calls to prevent large memory allocation from oversized hex strings - Wallet.getByJsonBlockId: same hex length check - RequestBodySizeLimitFilter: new filter checking Content-Length before buffering, returns HTTP 413 if exceeds limit (default 5MB, aligned with geth defaultBodyLimit) - HttpService.addFilter: register body size filter in parent class, all 7 subclasses inherit via super.addFilter()
- JsonRpcServlet: rewrite doPost() with batch size check (default 1000), response size limit via BufferedResponseWrapper (default 25MB), request timeout via ExecutorService (default 30s). All defaults aligned with geth. Error codes: -32600, -32003, -32002 - CachedBodyRequestWrapper: replay pre-read body for jsonrpc4j - BufferedResponseWrapper: eagerly check response size during writes - LogFilter: reject eth_getLogs with >1000 addresses (aligned with geth) - 4 new config items: node.jsonrpc.maxBatchSize, maxResponseSize, maxRequestTimeout, maxAddressSize
…imit - RateLimiterServlet: validate adapter class name against ALLOWED_ADAPTERS whitelist before Class.forName to prevent arbitrary class loading - Wallet.triggerConstantContract: add Semaphore (default 8 concurrent) with tryAcquire/release to limit constant call parallelism and mitigate free-computation DoS. Original logic moved to doTriggerConstantContract() - New config: vm.maxConcurrentConstantCalls
968db25 to
d2d0c03
Compare
- node.http.maxRequestBodySize: 5MB default - node.jsonrpc: maxBatchSize, maxResponseSize, maxRequestTimeout, maxAddressSize with geth-aligned defaults - rate.limiter.http: recommended QPS for TriggerConstantContractServlet and EstimateEnergyServlet - global.qps / global.ip.qps: recommended values in comments - vm.maxEnergyLimitForConstant / maxConcurrentConstantCalls: recommended values in comments
d2d0c03 to
5a723c3
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
Add input validation, request limiting, and security hardening for HTTP and JSON-RPC APIs. All JSON-RPC limits are aligned with go-ethereum (geth) defaults for ecosystem compatibility.
Why
Multiple API-layer vulnerabilities identified in security audit enable resource exhaustion attacks:
hexToBigIntegeraccepts arbitrary-length strings, allowing attackers to force largeBigIntegermemory allocations via JSON-RPC parametersUtil.checkBodySize()validates after the body is fully buffered into memory, and only 30% of servlets call it. Oversized requests consume memory before being rejectedeth_getLogswith broad filters) can generate unbounded response bodiesRateLimiterServlet.Class.forName()loads any class name from config before validation, enabling arbitrary class instantiationtriggerConstantContractcosts no TRX, defaults to 100M energy (~100s CPU), and has no concurrency limitChanges
Commit 1: Input validation and HTTP body size limit
MAX_HEX_PARAM_LENGTH=128check before all 5hexToBigIntegercall sitesContent-Lengthheader, returns HTTP 413 if exceeds limit (default 5MB, aligned with gethdefaultBodyLimit)HttpServicesubclasses inherit viasuper.addFilter(). Five subclasses that overrideaddFilter()now callsuper.addFilter(context)firstCommit 2: JSON-RPC request limits (aligned with geth)
doPost()with three-layer protection:BatchRequestLimit=1000, error-32600)BufferedResponseWrapper(default 25MB, geth:BatchResponseMaxSize=25MB, error-32003)ExecutorService+Future.get()(default 30s, geth:WriteTimeout=30s, error-32002)ResponseTooLargeExceptioneagerly during writes to bound memoryeth_getLogsfilter with >1000 addresses (aligned with gethLogQueryLimit=1000)node.jsonrpc.maxBatchSize,maxResponseSize,maxRequestTimeout,maxAddressSizeCommit 3: Security hardening
ALLOWED_ADAPTERSwhitelist (GlobalPreemptibleAdapter,QpsRateLimiterAdapter,IPQPSRateLimiterAdapter,DefaultBaseQqsAdapter) beforeClass.forName()to prevent arbitrary class loadingSemaphorewithtryAcquire()/release()(default 8 concurrent). Original logic moved todoTriggerConstantContract(). Rejects excess calls immediately withContractValidateExceptioninstead of blockingvm.maxConcurrentConstantCallsCommit 4: Config template with recommended security settings
config.confas commented-out entries with descriptionsTriggerConstantContractServlet(QPS=20) andEstimateEnergyServlet(QPS=10)global.qps(10000),global.ip.qps(1000),maxEnergyLimitForConstant(10000000)Scope
Test
RequestBodySizeLimitFilterTest: normal/oversized/exact-limit/missing-content-length requestsHexParamValidationTest: normal hex, 128-char boundary, oversized hexJsonRpcLimitsTest: CachedBodyRequestWrapper replay, BufferedResponseWrapper normal/exceeds/no-limitRateLimiterWhitelistTest: whitelist contains expected adapters, blocks unknown namesJsonrpcServiceTest(8 methods),HttpApiAccessFilterTest,ArgsTest,WalletMockTest