fix(security): 验证命令参数级白名单 + 资源限制 + 审计日志#27
Conversation
Review — PR #27: 验证命令安全加固🟡 Major1. PR #28 已将 修复: 移除 2. 原函数返回 3. 移除所有包含 4. 验证命令 timeout 从 120s 降到 60s — 行为变更 长时间运行的测试套件(如集成测试)可能需要超过 60s。建议保持 120s 或提取为配置项。 🟢 Minor
总结: 需修复 Major #1(init.py 方向)和 #2-3(API 兼容性)后,rebase 到当前 main 合并。冲突文件为 |
1. __init__.py 对齐 PR #28 精简方向 — 移除私有符号 re-export 2. _build_sandbox_env 保留 AGENT_GO_* 前缀环境变量 3. 验证命令 timeout 恢复为 120s 4. 修复测试文件 import 适配精简后的 __init__.py Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
✅ Review 问题已修复
测试结果:226 passed |
- _CMD_ARG_RULES: 结构化白名单替代扁平前缀列表,每个命令定义允许的 flags/positionals 正则 - _is_safe_verification_command: 四阶段验证(shlex解析→shell注入扫描→命令查找→逐token校验) 返回 (bool, reason) 提供拒绝诊断信息 - executor.py: 两处 subprocess.run 前插入安全门禁,被拒绝命令记录并跳过 - _apply_resource_limits: preexec_fn 设置 ulimit(CPU 60s/文件50MB/fd 256/进程64) - _build_sandbox_env: 移除 API_KEY/SECRET/TOKEN/PASSWORD 等敏感环境变量 - _log_rejected_command: 结构化日志 + ~/.agent_go/verification_audit.jsonl 持久化审计 - _SHELL_CHAIN 正则修复: \b(&&|\|\|)\b → &&|\|\| 修复空格包围的命令链无法匹配 - 63 个新测试覆盖:Shell注入/合法命令/参数校验/路径验证/边界情况/审计日志/资源限制 - 226 tests 全部通过,无回归 Refs: #15 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1. __init__.py 对齐 PR #28 精简方向 — 移除私有符号 re-export 2. _build_sandbox_env 保留 AGENT_GO_* 前缀环境变量 3. 验证命令 timeout 恢复为 120s 4. 修复测试文件 import 适配精简后的 __init__.py Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
- _verify_changes: 添加 sub_id 参数以支持 _log_rejected_command - test_executor: 验证命令改为 pytest --co(适配新白名单) - test_is_safe_verification_command: 适配 (bool, reason) 返回值格式 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
618238b to
834f573
Compare
…sue #7) Rebased onto main (after PRs #20, #21, #27), conflicts resolved. Key changes: - 14 modules: type annotations added to all public functions - cli.py: argparse code preserved from PR #20 - executor.py: refactored functions preserved from PR #21, security functions from PR #27 - Fixed: logger definitions in eval.py, tui.py, git_utils.py Tests: 277 passed.
修复 Issue #15 — LLM 验证命令注入风险加固
核心问题
_is_safe_verification_command()被定义和导入但从未在命令执行前被调用,且仅检查命令前缀,参数完全不受控(如git log -c "rm -rf /"可通过前缀检查)。四柱加固
_CMD_ARG_RULES结构化规则字典,每个命令定义允许的 flags/positionals 正则;四阶段验证(shlex 解析 → shell 注入扫描 → 命令查找 → 逐 token 校验)subprocess.run前强制调用校验,被拒绝命令记录rejected: true并跳过_apply_resource_limits(CPU 60s/文件 50MB/fd 256/进程 64)+_build_sandbox_env(移除 API_KEY/SECRET/TOKEN/PASSWORD 等敏感环境变量)_log_rejected_command→ 结构化 log_event +~/.agent_go/verification_audit.jsonl持久化附带修复
_SHELL_CHAIN正则:\b(&&|\|\|)\b→&&|\|\|(原正则在空格包围的||/&&处无法匹配)变更文件
agent_go/utils.py_CMD_ARG_RULES、重写_is_safe_verification_command、新增_log_rejected_commandagent_go/executor.py_apply_resource_limits、_build_sandbox_envagent_go/__init__.pytests/test_safe_verification_command.py测试结果
验证
Closes #15
🤖 Generated with Claude Code