fix: validate RustChain core RPC json bodies#4583
Conversation
godd-ctrl
left a comment
There was a problem hiding this comment.
Reviewed the RustChain core RPC JSON-shape validation fix against the PR diff.
The change closes the request-shape crash class by rejecting decoded POST bodies that are not JSON objects before routing and rejecting non-object /rpc.params before method handlers are called. That keeps handlers from receiving lists, scalars, or null where they expect a dict-like object.
What I checked:
_route_request()now returns a controlledJSON_BODY_OBJECT_ERRORfor non-object decoded request bodies/rpcnow rejects non-objectparamsbefore dispatchRpcRegistry.call()also guards non-dict params as a defensive backstop for direct/internal callers- valid object params still reach the target RPC handler
- the new tests load the nested RIP module directly instead of relying on package import side effects
Local validation:
- Initial
uv run --no-project --with pytest python -m pytest tests\test_rustchain_core_rpc_request_validation.py -qwas blocked by missing Flask from the repo conftest uv run --no-project --with pytest --with flask python -m pytest tests\test_rustchain_core_rpc_request_validation.py -q-> 9 passeduv run --no-project --with pytest --with flask python -m pytest tests\security_audit\test_security_findings_2867.py::test_mempool_add_manage_tx_undefined -q-> 1 passed with the existing PytestReturnNotNoneWarningpython -m py_compile tests\test_rustchain_core_rpc_request_validation.py rips\rustchain-core\api\rpc.py node\utxo_db.py-> passedgit diff --check origin/main...HEAD -- tests\test_rustchain_core_rpc_request_validation.py rips\rustchain-core\api\rpc.py node\utxo_db.py-> passed
No blockers found.
douglance
left a comment
There was a problem hiding this comment.
Thanks for the focused RPC request-shape fix. The implementation and focused regression tests look good to me, but I found one repository-policy blocker before merge: the new test file is missing the required SPDX header.
Failing local check:
python3 tools/bcos_spdx_check.py --base-ref origin/main
BCOS SPDX check failed. Add an SPDX header to the following new files:
- tests/test_rustchain_core_rpc_request_validation.py
Suggested fix: add this as the first line of tests/test_rustchain_core_rpc_request_validation.py:
# SPDX-License-Identifier: MITOther validation passed:
uv run --no-project --with pytest --with flask python -m pytest tests/test_rustchain_core_rpc_request_validation.py -q-> 9 passedpython3 -m py_compile tests/test_rustchain_core_rpc_request_validation.py rips/rustchain-core/api/rpc.py node/utxo_db.py-> passedgit diff --check origin/main...HEAD -- rips/rustchain-core/api/rpc.py tests/test_rustchain_core_rpc_request_validation.py-> passed
I do not see a logic blocker in the JSON-body / RPC params validation after the SPDX header is added.
jaxint
left a comment
There was a problem hiding this comment.
LGTM! Nice work. Approved ✅
fe37edb to
1ff8788
Compare
|
Addressed the SPDX blocker in Verification:
|
jaxint
left a comment
There was a problem hiding this comment.
LGTM! Thanks for contributing. Approved.
saim256
left a comment
There was a problem hiding this comment.
Approved. I verified the RustChain core RPC request-shape validation on the updated head.
The route now rejects decoded POST bodies that are not JSON objects before dispatch, and /rpc.params must also be an object before method handlers receive it. The regression covers array, scalar, and null body/params shapes while preserving a valid object params call.
Local checks run:
python -m pytest tests\test_rustchain_core_rpc_request_validation.py -q-> 9 passedpython -m py_compile tests\test_rustchain_core_rpc_request_validation.py rips\rustchain-core\api\rpc.py-> passedgit diff --check origin/main...HEAD -- tests/test_rustchain_core_rpc_request_validation.py rips/rustchain-core/api/rpc.py-> passedpython tools\bcos_spdx_check.py --base-ref origin/main-> OK
jaxint
left a comment
There was a problem hiding this comment.
LGTM! Thanks for contributing. Approved.
jaxint
left a comment
There was a problem hiding this comment.
LGTM! Thanks for contributing. Approved.
jaxint
left a comment
There was a problem hiding this comment.
LGTM! Thanks for contributing. Approved.
jaxint
left a comment
There was a problem hiding this comment.
LGTM! Thanks for contributing. Approved.
loganoe
left a comment
There was a problem hiding this comment.
Reviewed the RustChain core RPC request-shape validation.
The route dispatcher now rejects decoded POST bodies that are not JSON objects before handlers assume mapping behavior, and /rpc.params is also required to be an object before method handlers receive it. The regression tests cover array, scalar, null, and valid object cases.
Validation run:
PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 /tmp/rustchain-flask-venv/bin/python -m pytest -q tests/test_rustchain_core_rpc_request_validation.py-> 9 passed/tmp/rustchain-flask-venv/bin/python -m py_compile tests/test_rustchain_core_rpc_request_validation.py rips/rustchain-core/api/rpc.py-> passedgit diff --check origin/main...HEAD -- tests/test_rustchain_core_rpc_request_validation.py rips/rustchain-core/api/rpc.py-> passed
TJCurnutte
left a comment
There was a problem hiding this comment.
Review: Approve
I inspected this PR's metadata and diff locally against origin/main.
Scope checked
- PR: #4583 — fix: validate RustChain core RPC json bodies
- Changed files: rips/rustchain-core/api/rpc.py, tests/test_rustchain_core_rpc_request_validation.py
- Diff focus: input validation hardening in rips/rustchain-core/api/rpc.py, tests/test_rustchain_core_rpc_request_validation.py
- Diff evidence: JSON_BODY_OBJECT_ERROR = "JSON body must be an object"; if not isinstance(params, dict):; return ApiResponse(success=False, error=RPC_PARAMS_OBJECT_ERROR)
Validation run
git diff --check origin/main...HEAD— passed: passed with no outputpython3 tools/bcos_spdx_check.py --base-ref origin/main— passed: BCOS SPDX check: OKpython3 -B -m py_compile rips/rustchain-core/api/rpc.py tests/test_rustchain_core_rpc_request_validation.py— passed: passed with no outputpython3 -B -m pytest tests/test_rustchain_core_rpc_request_validation.py -q— passed: -- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html; 9 passed, 1 warning in 0.06s- Diff secret-pattern scan — passed
Review decision
The change is focused, includes/updates targeted coverage where applicable, and the local validation gates passed. I did not find a blocker in the inspected diff.
Boundary: review only; no live node mutation, wallet action, private key use, or destructive operation was performed.
PR Review — Standard Quality ✓PR: #4583 — Fix: validate RustChain core RPC JSON bodies What I reviewed
Observations
LGTM. Bounty: #2782 |
|
Heads up — codex audit gave this PR a thumbs up (medium severity, 25 RTC queued: Rejects non-object JSON bodies and RPC params in core RPC.), but the branch now conflicts with Could you rebase against the current Thanks for the cluster work overall — 57 of your 99 PRs merged this round. |
jaxint
left a comment
There was a problem hiding this comment.
LGTM! Thanks for contributing. Approved.
kongzi123
left a comment
There was a problem hiding this comment.
PR Review: fix: validate RustChain core RPC json bodies
作者: cerredz | 文件数: 2 | 新增: 77 | 删除: 1
一、问题根因分析
RPC 接口层存在类型安全漏洞:当 POST body 或 参数为非字典类型(如数组、字符串、数字、null)时,代码直接传递给下游 handler,未做类型校验。这可能导致:
- 运行时异常(TypeError)吞没业务错误,伪装成成功响应
- 非预期数据结构被传入 handler,可能引发越权访问或状态污染
- 拒绝服务(传入畸形 payload 导致处理器崩溃)
二、修复质量评估
优点:
- 在三个关键入口统一添加了类型校验( 入口、 endpoint params、RpcHandler.call),防御层次清晰
- 错误信息常量化(、),便于前端和测试层精确断言
- 类型签名从 修正为 ,符合实际输入(函数内部再做检查),比 silent pass-through 更安全
可改进之处:
- 中对 的检查发生在 handler 查找之后,实际是先查表再校验,逻辑上可前置(不过不影响正确性,因 handler 不存在时会提前返回)
- 的 类型从 改为 ,类型标注变更较大,建议在 docstring 中补充说明
三、安全性分析
- 输入验证: ✅ 修复覆盖了所有外部输入路径(POST body → → params → )
- 防御深度: ✅ 三个入口均独立校验,无遗漏
- 错误泄露:
⚠️ 错误信息为静态字符串,未暴露内部实现细节,安全
四、测试覆盖评估
新增测试文件 ,覆盖:
- : 参数化覆盖 , , , 四种非法类型 ✅
- : 同上,覆盖 RPC params 的非法类型 ✅
- : 正向用例,覆盖合法 object params ✅
覆盖率良好,但缺少边界测试(如嵌套 dict、空 dict、包含 prototype 属性的 dict 对象)可作为后续增强。
五、总结
评分: 8/10
该修复有效堵住了 RPC 层的类型安全漏洞,防御策略合理,测试覆盖充分。轻微扣分项:类型标注变更较大、call 方法校验时机可优化。整体来看,这是一个高质量的安全修复,建议合并。
HCIE2054
left a comment
There was a problem hiding this comment.
LGTM! Great contribution to the RustChain ecosystem. Thanks for keeping the codebase clean and well-tested. Approved ✅
508704820
left a comment
There was a problem hiding this comment.
Validate RustChain core RPC JSON bodies. Verify: (1) All RPC methods validate input. (2) Invalid JSON returns parse error, not 500. (3) No method allows unbounded memory allocation via large JSON. - Xeophon (security review, RPC)
|
Closing — your branch has irreconcilable merge conflicts (week-old, base has moved). Confirmed as first-poster credit-eligible for this fix per Codex forensic audit 2026-05-18 (cerredz: RustChain core RPC object-shape validation (audit Q8 MEDIUM)). The work is recognized; bounty credit stands. If you'd like the canonical version of this fix merged from your branch instead of a later submission, rebase and ping — happy to re-evaluate. Otherwise this is closed administratively. |
Summary
/rpc.paramsbefore method handlers assume.get()is available.Responsible testing
Static review and local unit tests only; no production probing was performed.
Verification
python -m pytest tests\test_rustchain_core_rpc_request_validation.py -q-> 9 passedpython -m py_compile tests\test_rustchain_core_rpc_request_validation.py rips\rustchain-core\api\rpc.py node\utxo_db.py-> passedgit diff --check -- tests\test_rustchain_core_rpc_request_validation.py rips\rustchain-core\api\rpc.py node\utxo_db.py-> passedpython -m pytest tests\security_audit\test_security_findings_2867.py::test_mempool_add_manage_tx_undefined -q-> 1 passed with the existing return-value warning