Validate Rent-a-Relic text fields#5682
Conversation
|
Welcome to RustChain! Thanks for your first pull request. Before we review, please make sure:
Bounty tiers: Micro (1-10 RTC) | Standard (20-50) | Major (75-100) | Critical (100-150) A maintainer will review your PR soon. Thanks for contributing! |
PR Review — PR #5682What I reviewed:
Observations:
LGTM pending CI. Well-structured validation with good test coverage. |
jaxint
left a comment
There was a problem hiding this comment.
LGTM! Great work on this PR. 🚀
Code Review: PR #5682Title: Validate Rent-a-Relic text fields SummaryIntroduces CriticalNone Warning
Suggestion
VerdictApprove - The PR successfully closes the type-confusion gap for text fields in GPU render endpoints. The missing length bounds and job_type enumeration are hardening items that should be addressed in a follow-up. The nested function placement should be fixed before merge. Review by Herr Amano | 2026-05-19 |
kongzi123
left a comment
There was a problem hiding this comment.
代码审查:PR #5682 — Validate Rent-a-Relic text fields
作者:dazer1234 | 文件:2 个(+131/-22)
一、根因分析
原始代码对文本字段(miner_id、job_id、job_type、from_wallet、to_wallet、actor_wallet、escrow_secret)的校验极度宽松,仅使用 .get() 取值后以 if not x 或 all([...]) 做存在性检查,未校验类型。这一缺陷会导致两类风险:
- 类型混淆注入:非字符串类型(如
{"from_wallet": {"id": "payer"}})会直接进入后续业务逻辑或数据库操作,在 SQLite 语句中使用占位符时可能导致意外行为或类型相关的运行时错误。 - 空格绕过:输入值为纯空白字符串(如
" ")时,if not value判定为False但strip()后为空字符串,仍能绕过校验进入业务流程。
二、修复质量:✅ 良好
- 引入
_parse_text_field()统一校验逻辑,支持required参数控制是否可空,复用性高。 - 每个端点对所有文本字段逐一校验,错误返回即时短路,不再使用批量
all([...])断言,错误信息从模糊的"Missing required escrow fields"精确到具体字段名,便于调试。 - 对
required=False的字段(job_id、escrow_secret)保留了向后兼容的默认值生成逻辑(secrets.token_hex(8/16)),无破坏性变更。
三、安全性:✅ 有明显提升
| 维度 | 原代码 | 修复后 |
|---|---|---|
| 类型校验 | ❌ 无 | ✅ 显式 isinstance(value, str) 检查 |
| 空格处理 | ❌ 无 | ✅ .strip() 后判断非空 |
| SQL 注入 | ✅ 使用参数化查询(未引入新风险) | ✅ 未引入变更 |
| 错误信息泄漏 | ✅ 字段级精确错误,攻击面减小 |
原始代码若输入 {"from_wallet": {"id": "payer"}, ...},flask 在 JSON 反序列化后得到 dict 对象,后续尝试在 SQLite 参数化语句中插入时会触发类型错误(sqlite3.InterfaceError),属 DoS 类风险;新代码在进入数据库层之前即以 400 拒绝。
四、测试覆盖:⚠️ 覆盖不足
新增测试文件 test_gpu_render_endpoint_validation.py 共 79 行,覆盖了 2 个场景:
test_escrow_rejects_non_string_wallet_before_sqlite—from_wallet为dict类型test_release_rejects_non_string_job_id_before_sqlite—job_id为dict类型
建议补充的测试用例(未覆盖的关键路径):
- 空白字符串输入(
{"from_wallet": " "})—— 验证strip()逻辑 None值输入(明确传入{"from_wallet": None})—— 验证None处理miner_id端点的类型校验测试(当前仅escrow/release有覆盖)gpu_refund端点的类型校验测试job_type字段为空字符串""的拒绝测试required=False字段缺失时的默认值生成验证
五、技术问题
在 _parse_text_field 中,当 required=False 且字段值为非字符串类型(如 {"job_id": 12345})时,修复后代码会在 isinstance(value, str) 检查处返回错误并中断流程。但对于可选字段,这种严格类型校验是否有意为之?若上游 JSON schema 允许传入数字类型但期望自动转为字符串显示,可能需要先做类型强制转换再校验,而非直接拒绝。请确认:可选字段的类型校验策略是严格拒绝非字符串,还是允许自动转换?
TJCurnutte
left a comment
There was a problem hiding this comment.
Requesting changes after a focused pass on PR #5682 at head 02c922f8c8f1e47c72a49f00830530bb5dfc2142.
The blocker is scope/reviewability: this PR is titled “Validate Rent-a-Relic text fields”, but the live diff does not touch Rent-a-Relic. It only changes:
node/gpu_render_endpoints.py
node/tests/test_gpu_render_endpoint_validation.py
That means the added validation/tests exercise GPU render escrow fields, not Rent-a-Relic text fields such as the Rent-a-Relic server/test paths.
Validation I ran:
gh pr view 5682 --repo Scottcjn/Rustchain --json number,title,headRefOid,mergeable,files
git diff --check origin/main...HEAD -- \
node/gpu_render_endpoints.py \
node/tests/test_gpu_render_endpoint_validation.py
python3 -B -m py_compile \
node/gpu_render_endpoints.py \
node/tests/test_gpu_render_endpoint_validation.py \
tests/test_gpu_render_endpoints_security.py
PYTHONPATH=. python3 -B -m pytest -q \
node/tests/test_gpu_render_endpoint_validation.py \
tests/test_gpu_render_endpoints_security.py --tb=shortResults:
gh pr viewreported titleValidate Rent-a-Relic text fields,mergeable: CONFLICTING, and only the two GPU endpoint files above.git diff --checkpassed.py_compilepassed.- Focused GPU endpoint tests passed:
6 passed, 1 warning in 0.10s.
Mergeability check:
git merge --no-commit --no-ff origin/mainThis failed with a content conflict in node/gpu_render_endpoints.py.
Please either retitle/rescope this PR as GPU endpoint text-field validation or move the validation/tests to the Rent-a-Relic code path, then rebase/resolve the node/gpu_render_endpoints.py conflict. The current branch is not a valid Rent-a-Relic text-field fix as titled.
Summary
Validation
Bounty: Scottcjn/rustchain-bounties#305
RTC wallet/miner id:
eB51DWp1uECrLZRLsE2cnyZUzfRWvzUzaJzkatTpQV9Implemented with OpenAI Codex assistance.