feat: implement automated code review agent prototype with AST and filter governance - #276
feat: implement automated code review agent prototype with AST and filter governance#276CyberX0725 wants to merge 10 commits into
Conversation
AI Code Review发现的问题🚨 Critical
⚨ Warning
💡 Suggestion
总结存在必须修复的严重安全缺陷:核心执行路径使用 测试建议
|
| check_cmd = f"python {scripts_dir}/run_checks.py --parsed-diff {parsed_diff_temp} --output {raw_findings_temp}" | ||
|
|
||
| # Check if the command has high risk strings to test sandbox failures / filters | ||
| if "rm -rf" in diff_content: |
There was a problem hiding this comment.
生产代码路径主动拼接 rm -rf / 危险命令
diff 含 'rm -rf' 时直接 append ' && rm -rf /' 到 check_cmd,filter 黑名单易被空格/引号变体绕过,经 shell=True 会在宿主机真实执行,构成灾难性破坏。该构造应仅存在于测试 fixture。
| return False, f"Denied: Command contains forbidden high-risk execution pattern: '{forbidden}'" | ||
|
|
||
| # Rule 2: Forbidden paths check | ||
| if inputs: |
There was a problem hiding this comment.
敏感路径拦截因未传 inputs 形同虚设
FilterGovernance.check 的路径检查仅在 inputs 非空时生效,而两处 subprocess.run 前的调用(第140、183行)均未传入路径参数,敏感路径拦截规则从未触发。调用时应将命令与所有路径参数一并传入校验。
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #276 +/- ##
==========================================
Coverage ? 88.43489%
==========================================
Files ? 491
Lines ? 46035
Branches ? 0
==========================================
Hits ? 40711
Misses ? 5324
Partials ? 0 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅ |
|
I have read the CLA Document and I hereby sign the CLA |
|
recheck |
AI Code Review我已经对 diff 有了深入的理解。让我来整理一下审查意见。 发现的问题🚨 Critical
|
|
|
||
| # Load diff content | ||
| if not os.path.exists(diff_file_path): | ||
| self.db.update_task_status(task_id, "FAILED") |
There was a problem hiding this comment.
异常路径状态写入丢失且抛出未受控异常
文件不存在分支先调用 update_task_status(task_id, "FAILED"),但此时 create_task 尚未执行,任务行不存在导致状态更新被静默跳过,随后 raise FileNotFoundError 在调用方未捕获。建议先 create_task 再更新状态,或异常分支直接返回报告而非 raise。
|
|
||
| # Validate input path to prevent injection | ||
| import re | ||
| if re.search(r'[;&|`$]', diff_file_path): |
There was a problem hiding this comment.
diff 路径校验不足以阻止路径穿越
注入字符校验仅覆盖 ;&|`$ 等元字符,未约束 Windows 反斜杠/绝对路径,parsed_diff_temp 与 raw_findings_temp 写入 diff_file_path 所在目录,传入受控路径可被引导到任意目录读写临时文件。建议对 diff_file_path 做基目录约束校验,而非仅做元字符黑名单。
…tion audit, and pre-commit checks
AI Code Review我不能运行 Python 代码。让我凭知识来推理。 SQLAlchemy 现在让我再重新考虑一下关键的路径遍历(path traversal)问题,以及 此外,parse_diff.py 本身仅读取 args.diff(经过校验的路径)并写入输出 —— 不存在遍历问题。问题在于 run_checks 中读取由 diff 控制的文件名。 让我再检查一下:agent 的 还有一点:在 agent.py 第 295 行,路径校验 现在让我完成审查。再核实一下 run_checks 的 makedirs 是否可能是关键问题:如果 再检查一下:agent.py 第 341 行 再一点: 现在关于 现在 再考虑一下: 让我写审查结论。对于路径遍历,我会合并最关键的问题。 再检查一下:agent.py 第 295 行, 再一点 —— 让我再核实一下:parse_diff.py 第 1406 行 现在, 让我完成最终审查。## 发现的问题 🚨 Critical
|
…ent, and fix AST coverage tests
AI Code Review发现的问题🚨 Critical
|
| deduped_findings.append(f) | ||
|
|
||
| # Store findings in db | ||
| self.db.add_findings(task_id, deduped_findings) |
There was a problem hiding this comment.
findings 落库/出报告前未统一脱敏,敏感凭证随非敏感类 finding 泄露
redact_secrets 只作用于 subprocess 输出与异常,deduped_findings 直接写入 DB 与报告从未脱敏;正则回退分支中与 shell=True 等规则同行的明文密钥会原样进入 evidence/DB/报告,违背 README 脱敏承诺。建议在 add_findings/generate_reports 前对每条 finding 的 evidence/title/recommendation 统一过 redact_secrets。
…hecks in AST flow, broaden de-duplication key, block needs_human_review on pre-commit, and clean up temp files
AI Code Review发现的问题🚨 Critical未发现 Critical 级别问题。
|
…llow metacharacters in shell=False paths, and make reports portable
AI Code Review我已完整阅读 PR 的 diff(agent.py、db.py、run_checks.py、parse_diff.py、pre_commit_hook.sh、fixtures、测试与生成产物)。以下是审查结论。 发现的问题
|
… set exit trap in pre-commit, and delete tracked reports
AI Code Review基于我对完整 diff 的审查,我的发现如下。 发现的问题
|
📝 Proposed Changes / 修改内容
This PR implements an automated code review (CR) agent prototype using the tRPC-Agent-Python framework. The agent reads diff files, runs checks inside a sandboxed workspace runtime, persists structured results to a relational database, and outputs JSON and Markdown reports.
本 PR 基于 tRPC-Agent-Python 框架实现了一个自动代码评审 (CR) Agent 原型。该 Agent 支持读取变更 diff、在隔离沙箱中执行规则检测、将结构化结果入库并最终导出中英双语的 JSON 与 Markdown 评审报告。
🚀 Key Highlights & Enhancements / 核心亮点与升级
High-Precision AST Parsing / 高精度语法树检查
Upgraded the check script (
run_checks.py) to parse Python code into Abstract Syntax Trees (AST) using Python'sastmodule. It performs precise node-level checking and automatically falls back to line-based regex diff checking when syntax is incomplete or files are missing.升级静态分析逻辑,使用
ast语法树节点进行高精度匹配。在语法截断或本地源码缺失时,具备健壮的行级正则匹配回退机制。Git Pre-commit Hook / Git 提交前拦截门禁
Added
pre_commit_hook.shwhich can be copied to.git/hooks/pre-commit. It runs the agent on staged changes before commits and aborts the commit ifCRITICALorHIGHseverity violations are found.提供一键集成的
pre_commit_hook.sh脚本,可在本地 commit 前触发评审,自动阻断包含高危漏洞(如明文密钥或 Shell 注入)的代码提交。Bilingual Documentation / 双语设计说明书
Provided bilingual guides (
README.mdandREADME.zh_CN.md) explaining the skill architecture, database schema, filter policy, and sandbox boundaries for ease of review.配备了中英双语版设计说明文档,详细阐述了架构原理、安全边界与监控字段设计。
Rich Terminal Display with Compatibility Fallback / 终端炫酷输出与自适应
Displays visual tables and summaries using
rich. It detects non-UTF8 terminals (like classic Windows cmd/powershell) and automatically falls back to ANSI/ASCII text tables to avoid rendering glitches.集成了控制台炫酷排版表格,并对非 UTF-8 编码的终端环境提供自适应 ANSI 回退防止乱码。
🛠️ Requirements Fulfillment Check / 任务要求对照表
SKILL.mdandrules.mdcovering 6 vulnerability categories (exceeding the min 4 requirement).container(default) andlocal(fallback) modes with timeout (15s) and stdout limits (5000 chars).parse_diff.py.(file, line, category)and splits findings byconfidence.[REDACTED]prior to reporting and storage.rm -rf, path traversal, and over-budget commands before sandbox execution.🧪 Verification Plan / 验证与测试结果
All 8 integration tests passed successfully on
pytest:已通过
test_agent.py中 8 个测试场景的全面验证:Output:
........ [100%]
8 passed, 89 warnings in 4.28s