Repository navigation
fix(approval): run auto-approved shell commands from a validated argv - #287
openjiuwen-sync-bot[bot] wants to merge 10 commits into
Conversation
|
mulderlover seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
|
head_sha: 变更摘要本 PR 修复审批层将“只读命令文本过滤”误当作安全边界的问题:新增 主要改动
同时 |
|
head_sha: 代码审查审查结论已报告问题(3 项)
各文件审查状态
总体风险判断方向正确:审批层从“文本过滤器”切换为“解析并直执 argv + 净化环境 + Windows fail-closed”,显著收窄了自动放行面,也未把任何原本需要审批的命令变为自动批准。剩余风险集中在三处:CI 上的架构 wait-manifest gate 未同步(确定性失败)、文本检测与 argv 执行的分裂仍留引号拼接缺口、以及 direct-exec 路径把“命令缺失”误报成“shell 缺失”。建议合并前至少修复 #1 与 #2。
⛔ 需要修改 |
| if decision.safe and decision.command is not None: | ||
| sanitized = True | ||
| if decision.command.execution == "exec": | ||
| argv = decision.command.argv |
There was a problem hiding this comment.
head_sha: e911814fd45acc66d3347890cd2c794f6b12c492
🟡 Medium Priority
变更行:src/chrys/service/tools/builtins/shell.py:1201-1204 在分类为 exec 时把 argv 下传,_spawn_pty/_spawn_pipe 于是 create_subprocess_exec(*direct_argv) / spawn_managed_stdio_process(*direct_argv) 直接 exec 目标程序,不再经过 shell(703-704、807-808 行)。
失败模式:目标二进制不存在时(如 macOS 上 md5sum/sha256sum/free,它们在 _APPROVAL_SAFE_EXTERNAL 中被无条件放行),create_subprocess_exec 抛 FileNotFoundError(OSError 子类),_spawn_pty 在最后一个 variant 上直接 raise(723-724 行),ShellTools._run 的 except FileNotFoundError(1277-1279 行)把它当作“shell 不存在”,返回 Error: shell not found at <shell.path>。旧路径由 shell 负责查找命令,输出 bash: md5sum: command not found 并给出 [exit_code: 127];现在同一场景返回“shell 找不到”的误导性错误,且丢失 exit code 127。Windows + Git Bash 更明显:ls/cat/stat 只存在于 Git 的 usr/bin,父进程 PATH 通常没有它们,原本经 bash -c 可用的自动放行命令改为直接 exec 后可能直接失败并报“shell not found”。
建议:将 direct-argv 路径的 FileNotFoundError 与“shell 缺失”区分开:捕获后返回 command_not_found(含 exit_code 127 语义)而不是复用 shell_not_found 文案,避免误导 agent 与用户。
There was a problem hiding this comment.
head_sha: 746d4cbce5eb38a2437edd810a7f63b9978b376e
done
| cmd, shell_name=tool_name | ||
| ): | ||
| approval_safe = classify_shell_for_approval(cmd, shell_name=tool_name) | ||
| if approval_safe.safe and not shell_command_may_access_sensitive_data(cmd, shell_name=tool_name): |
There was a problem hiding this comment.
head_sha: e911814fd45acc66d3347890cd2c794f6b12c492
🟡 Medium Priority
变更行:src/chrys/service/agent_middleware/control/approval.py:382-383 现在先用 classify_shell_for_approval(cmd) 把命令文本解析成确切 argv(SafeShellCommand.argv),但紧邻的敏感性判定仍是 shell_command_may_access_sensitive_data(cmd, ...),即对原始文本跑 _SENSITIVE_TARGET_RE;而 shell.py:1198-1204 会用该 argv 直接 exec。
失败模式:approval_safe.py 的解析器会把引号拼接归一化成一个词——cat .en''v 的 argv 是 ("cat", ".env")(' 起止单引号后继续拼词,见 _simple_posix_argv 201-204 行),但正则看到的是 .en''v,_SENSITIVE_TARGET_RE 不匹配,于是 safe and not sensitive 成立 → 自动放行 → 直接 exec cat .env,.env 被静默读取。同类:.gi''t/con''fig、.ss''h/id_r''sa、/proc/self/envi''ron。本 PR 新增了 */? 的 glob 边界(safety_classifier.py:15/39)并宣称“cat .env* 不会再被静默自动放行”,但没有覆盖引号拼接:文本形状与将要执行的 argv 形状不一致。
说明:旧代码同样会被 .en''v 绕过(旧路径经 bash -c 执行,同样读出 .env),因此这属于既有缺口;但本 diff 建立了“校验后的 argv 即实际执行内容”的新契约后,一处基于文本、一处基于 argv 的分裂变成明确的不一致,且修复成本很低。
建议:在 approval_safe.safe and not shell_command_may_access_sensitive_data(cmd, ...) 之外,再对 approval_safe.command.argv(每个元素或 join 后的字符串)调用 target_may_access_sensitive_data,任一命中即不自动放行。
There was a problem hiding this comment.
head_sha: 746d4cbce5eb38a2437edd810a7f63b9978b376e
done
| ) -> tuple[CapturedOutput, int, CapturedOutput]: | ||
| """Run *command* with pipes (Windows fallback). Returns ``(stdout, returncode, stderr)``.""" | ||
| run = await _spawn_pipe(command, shell, cwd) | ||
| run = await _spawn_pipe(command, shell, cwd, direct_argv=argv, sanitized=sanitized) |
There was a problem hiding this comment.
head_sha: e911814fd45acc66d3347890cd2c794f6b12c492
🟠 High Priority
变更行:src/chrys/service/tools/builtins/shell.py 中 ShellTools._execute_pty / _execute_pipe / _run 的 await 表达式被改写(新增 direct_argv=/sanitized= 关键字),_spawn_pty/_spawn_pipe 内的 *argv 改名为 *launch_argv(shell.py:703-719、807-815、1349、1366、1235-1242)。
受影响契约:tests/architecture/trajectory_wait_manifest.json 是变更登记 gate,tests/support/trajectory_wait_inventory.py 里 scan_wait_nodes() 用 ast.unparse(node) 生成 expression,manifest_errors()(该文件 656-672 行)逐 identity 比较 ("primitive", "expression", "wrapper_target"),不一致即报 "<identity>: expression drifted";测试 tests/architecture/test_trajectory_wait_inventory.py::test_wait_manifest_matches_source 会因此失败。
失败模式:manifest 中仍是旧表达式(例如 "ShellTools._execute_pty": ["wrapper", "await _spawn_pty(command, shell, cwd)", ...]、"_spawn_pty": [..., "await asyncio.create_subprocess_exec(*argv, ...)"]),而源码已是 await _spawn_pty(command, shell, cwd, direct_argv=argv, sanitized=sanitized) 与 *launch_argv;该文件不在本 diff 的 15 个改动文件里,因此 CI 的架构 gate 必然报 drifted(需 uv run python -m tests.support.trajectory_wait_inventory 重新生成并提交)。
建议:运行 uv run python -m tests.support.trajectory_wait_inventory 重新生成 tests/architecture/trajectory_wait_manifest.json,并把生成结果一并提交,使 _execute_pty/_execute_pipe/_run/_spawn_pty/_spawn_pipe 的 expression 与源码一致。
There was a problem hiding this comment.
head_sha: 746d4cbce5eb38a2437edd810a7f63b9978b376e
done
|
head_sha: 代码审查审查结论上一轮问题复查
本轮发现P2(2 项)
P3(1 项) 逐文件复查确认
总体风险判断方向上正确且显著收敛了自动放行面(
💬 仅评论 上次发现的问题已修复
上次发现的问题仍存在
新问题
|
| "LD_AUDIT", | ||
| "LD_PRELOAD", | ||
| } | ||
| ) |
There was a problem hiding this comment.
head_sha: 746d4cbce5eb38a2437edd810a7f63b9978b376e
🟡 Medium Priority
变更行:src/chrys/service/tools/builtins/shell.py:323-361 新增 _APPROVAL_SAFE_ENV_DROP,其文档(362-367)明确宣称“净化环境是为了让环境中的配置不能把自动放行的只读命令变成执行器”,并确实移除了 LESS/LESSOPEN/LESSCLOSE/GIT_EXTERNAL_DIFF/GIT_ASKPASS 这类“能把只读命令变成 exec 的工具配置”。
但同一类注入面仍有三处遗漏,而自动放行名单里恰好包含会走分页器的 git 子命令(readonly.commands._is_safe_git 允许 git log/git diff/git show 等,见 tests/service/tools/test_approval_safe_shell.py:22-23 中 git log --oneline -5、git diff 被判为 approval-safe):PAGER、GIT_PAGER、GIT_CONFIG_GLOBAL/GIT_CONFIG_SYSTEM(只删了 GIT_CONFIG_COUNT)没有处理;另外“Dynamic-loader injection”一节删掉了 DYLD_LIBRARY_PATH 却保留了同类的 LD_LIBRARY_PATH。
触发条件与失败模式:_spawn_pty(669-720)把 stdout 接在 PTY 上,git log 会因 isatty(stdout) 为真而启动分页器;只要 GIT_PAGER/PAGER/core.pager(含 !cmd 形式,可由 GIT_CONFIG_GLOBAL 指向的文件或仓库 .git/config 提供)被设置为任意程序,一条被自动批准、用户界面上完全没有弹窗的 git log --oneline 就会在用户不知情下执行该程序——正是本 PR 要消除的那类绕过。LD_LIBRARY_PATH 未清理也使 exec 到的二进制可能加载非预期共享库。
建议:补齐同类的注入面:把 PAGER、GIT_PAGER、GIT_CONFIG_GLOBAL、GIT_CONFIG_SYSTEM、GIT_CONFIG_NOSYSTEM、LD_LIBRARY_PATH 加入 _APPROVAL_SAFE_ENV_DROP;由于删除 PAGER 只会让 git 回退到默认 less,应在 _approval_safe_env() 里显式设置 PAGER=cat / GIT_PAGER=cat(或对自动放行的 git 命令追加 --no-pager),否则就把该常量的文档承诺收窄为“尽力而为”。
|
head_sha: 代码审查审查结论上一轮问题回查
本轮发现
逐文件复核
总体风险判断方向正确且收窄到位:审批自动放行改为「单条简单 POSIX 命令 + 精确 argv + 无 shell 执行」,
💬 仅评论 上次发现的问题已修复
新问题
|
| ("echo hello", ("echo", "hello"), "shell"), | ||
| ('echo "a b"', ("echo", "a b"), "shell"), | ||
| ("echo '(not execution)'", ("echo", "(not execution)"), "shell"), | ||
| ("printf %s", ("printf", "%s"), "shell"), |
There was a problem hiding this comment.
head_sha: a2249ec3e591f38a0ba8389c7f68e7fc609fed12
🟡 Medium Priority
变更行:tests/service/tools/test_approval_safe_shell.py:41-44 把 sed -n 1p README.md、find . -name README.md、python --version、ruff --version 固化为 approval-safe(exec 路径),并断言返回的确切 argv。
行为/契约:classify_shell_for_approval 是新增的审批自动放行唯一安全边界。本文件是这条边界的行为契约测试,但正向断言只覆盖了上述命令的良性形态。
失败模式:这几个命令本身就是"能执行其他程序"的命令——find -exec/-ok/-delete/-fprint、GNU sed 的 e 命令与 s///e 标志与 -i 就地写、python -c/-m。一旦实现对 find/sed/python 的"子选项白名单"被放宽(例如退化为"命令名命中即放行",正是 issue #159 中 rg --pre 的原始形态),find . -exec sh -c 'id' \;、sed -i 's/a/b/' f、python -c 'import os;os.system("id")' 就会和 rg --pre 一样被静默自动放行,而本文件所有断言依旧全绿——即该 PR 声称修复的那一类绕过没有任何回归护栏。
建议:在 test_classifier_requires_approval 的参数表中补上 find -exec/-delete、sed -i/e 命令、python -c、git -c core.pager=... 等可执行选项的负向用例并断言 safe is False,避免白名单被放宽时静默回归到 #159 的绕过形态。
|
|
||
| def _shell_tool(): | ||
| runtime = SessionEnvironment.capture() | ||
| return ShellTools(runtime).tools()[0] |
There was a problem hiding this comment.
head_sha: a2249ec3e591f38a0ba8389c7f68e7fc609fed12
🟡 Medium Priority
变更行:tests/service/approval/test_shell_auto_approval.py:32-34 _shell_tool() 直接使用 ShellTools(SessionEnvironment.capture()) 探测到的真实 shell 构造工具,配合第 47 行只对 sys.platform == "win32" 做 skipif。
行为/契约:test_safe_shell_commands_skip_the_dialog(58-67 行)断言 ls -la、cat README.md、grep TODO ...、git log --oneline、echo hello 不产生 ApprovalRequest。该断言成立的前提是"探测到的 shell 名被 classify_shell_for_approval 认定为可自动放行的 POSIX shell"。
失败模式:同一个 PR 内 tests/service/tools/test_shell_approval_exec.py:23-27 特意写了 shell._shell = ShellInfo(name="bash", ...) 并注释"Force the POSIX classifier even on Windows",说明分类器只对有限 shell 名放行(tests/service/tools/test_approval_safe_shell.py:96-117 只把 bash/git_bash 视为受支持,pwsh/powershell/cmd 一律 fail-closed)。本文件却没有做同样的强制:在 macOS(默认 zsh)或 Linux 上把 pwsh/zsh/fish 设为当前 shell 时,SessionEnvironment.capture() 会得到该 shell 名,分类器返回 shell_not_supported:* → 进入审批分支 → requests 非空、called 未被 await,测试失败;更糟的是第 64 行没有超时(见另一条 finding),此时测试是挂死而不是失败。也就是说同一 PR 的两个测试文件对环境依赖的处理不一致,造成平台相关的假失败。
建议:让测试不依赖开发机真实 shell:在 _shell_tool() 中显式把 ShellTools 的 shell 强制为 ShellInfo(name="bash", path="bash", args=["-c"])(做法与 test_shell_approval_exec.py:23-27 相同),或把该用例的 skipif 扩展为"探测到的 shell 不是 bash/git_bash 时跳过"。
| return ShellTools(runtime).tools()[0] | |
| def _shell_tool(): | |
| runtime = SessionEnvironment.capture() | |
| shell = ShellTools(runtime) | |
| # Force the POSIX classifier so the test does not depend on the developer's shell. | |
| shell._shell = ShellInfo(name="bash", path="bash", args=["-c"]) | |
| return shell.tools()[0] |
|
head_sha: 复核了
另有一个兼容性问题:默认全局 Git 配置已经恢复,但显式指定 |
Resolve the approval test conflict against main's replaced-root cases, then limit shell identity checks to real ShellTools executors with a command, check classifier argv for sensitive targets, require approval for worktree Git comparisons and restricted ps forms, always gate pytest, and keep explicit Git global config.
|
head_sha:
|
|
head_sha: 复核了最新提交 a1836a2。上一轮 list_workspace_dirs 误弹审批、引擎元数据测试失败和自定义 GIT_CONFIG_GLOBAL 的问题都已修复。本轮本地重跑了 620 项相关测试,全部通过,三平台 CI 也已通过。仍有以下问题,建议优先处理前四项。PR #287 · CI 审批判断和 Git 执行
|
Check the simple-command parser's argv even for commands outside the auto-approval allowlist, fall back to a POSIX parse for unknown dialects, and run every dialect's sensitive-env checks when the shell is unknown. Restrict git describe to object-only forms and remove inherited GIT_CONFIG_PARAMETERS before pinning Git config.
|
head_sha: 已处理。
|
|
head_sha: 看看这些问题是否成立: P1 · git describe 的 --abbrev 会让校验器跳过后续选项。 Git 把 --abbrev 定义为可选值参数,裸写时不消费下一个 token,显式值使用 --abbrev=。当前校验器把它当成必带值参数,会跳过下一个 token。Git 仍会把该 token 作为选项处理,因此工作区检查选项可能漏过审批。我用普通只读查询确认了两边的解析差异。建议把裸 --abbrev 加入普通 flag 集合,保留 --abbrev= 的值校验,并确保后续选项逐一检查。当前实现 · Git 参数定义 go version 仍可能下载并运行工具链。 Go 1.21+ 在自动选择模式下会根据 go.mod 或 go.work 选择工具链。所需版本尚未安装时,版本查询也可以触发下载和缓存写入。建议将它交给审批,或明确把自动放行的查询限定为本机自带工具链。白名单 · Go 官方说明 macOS 的 ps 白名单依赖兼容模式。 -e 和 -ef 目前仍会被放行。Apple 的 ps 在默认模式下把 -e 解释为选择所有进程,在 legacy 模式下会输出进程环境。建议固定自动放行时的兼容模式,或在 macOS 移除含义随模式变化的形式。白名单 · Apple 实现 ruff format --check 和 --diff 仍可写缓存。 我用一个已格式化的临时文件走 ShellTools.execute, 直接执行遇到权限错误时仍丢失退出码 126。 我用 PATH 中存在但没有执行权限的临时文件复测,Bash 返回 126, |
Treat bare git describe --abbrev as a flag, require approval for go version/help, drop the mode-dependent ps -e/--ef forms, require --no-cache for ruff format checks, and keep shell exit code 126 for a direct executable without permission.
|
head_sha: @Ox7c13
|
|
head_sha: P1 · GNU xargs 的可选值参数会让校验器跳过实际执行的程序。 --eof、 P1 · macOS find 的路径参数会干扰动作校验。 BSD find 的 -f 可以把以 - 开头的 token 作为路径。当前校验器平铺扫描 argv,可能把路径误判成 -exec 动作,再跳过后续需要检查的真实动作。建议按前置选项、起始路径和表达式分别解析,或先让含 -f 的命令走审批。校验代码 · Apple find 手册 P1 · ruff format 会把 -- 后的文件名当成只读选项。 我验证了正常的 --check --no-cache 已经不写缓存。但 --check 和 --diff 仍用全量参数查找,只有 --no-cache 的检查会在 -- 停止。如果同名 token 实际是路径,Ruff 仍按写入模式运行。建议统一使用会在 -- 停止的选项检查,并补上路径与选项同名的回归测试。校验代码 · Ruff 模式选择 P1 · git ls-files --modified 仍可能触发工作区过滤器。 ls-files 目前按子命令名放行全部参数。 P2 · 归一化参数只检查了敏感路径,仍可能漏掉敏感命令。 新的 parsed_argv 用于逐参数检查路径,但命令级判断仍依赖原始文本和覆盖范围更窄的复用解析器。在 shell: skip 下,如果命令名需要引号归一化才能识别,且另一个参数使复用解析器拒绝解析,就可能漏掉环境变量转储。建议让同一份归一化结果同时参与路径和命令级敏感判断。审批判断 · 命令级检查 P2 · cargo 版本和帮助查询也可能下载工具链。 Go 的查询已移出白名单,但 cargo 仍在其中。如果 PATH 上的 cargo 是 rustup 代理、允许自动安装,且项目指定的工具链尚未安装,查询也可能触发下载和安装。建议按 Go 的同类场景处理。白名单 · rustup 工具链选择 · 自动安装行为 P2 · pip list 的联网限制只覆盖独立短选项。 当前代码检查独立的 -o、 P2 · 无法进入工作目录时,会被误报为命令不可执行。 我用一个存在但没有进入权限的临时目录验证,直接执行 ls 被报告为 command_not_executable 并记录 126,走 shell 的 pwd 则被报告为 shell_not_executable。建议根据异常来源区分工作目录访问失败和程序启动失败,只在后者确实属于程序不可执行时记录 126。异常处理 |
Route xargs and ambiguous find path options through approval, make ruff mode checks stop at --, whitelist git ls-files index queries, use parsed argv for command-level sensitivity, stop auto-approving cargo queries, block pip short outdated clusters and force no-index, and report working-directory permission failures separately from exit 126.
|
head_sha:
|
Paired: GitHub #287 ↔ GitCode !199
关联 Issue
Closes #286
背景
审批层此前使用
is_safe_readonly_command()判断 Shell 命令能否跳过审批。rg位于_SAFE_READONLY_COMMANDS中,命中后不检查参数,因此:rg --pre ./scripts/probe.sh -e x .在
tools.approval.shell: require的 profile 下不会弹出审批,直接执行--pre指定的程序。根因
原因不是单纯漏掉
--pre,而是把文本命令过滤器当成了审批安全边界:is_safe_readonly_command()只做命令名和少量危险 token 判断;bash -c/pwsh -Command执行,$IFS、引号拼接、glob 等会在检查后改变实际 argv;--pre加进黑名单仍可被rg --pre$IFS...、--p''re、r''g等形式绕过;less/more、tree -o、date -s、hostname等可执行或可修改系统状态的命令。修复内容
approval_safe.py,作为审批自动放行的唯一安全边界:$、反引号、反斜杠、!的双引号文本;classify_shell_for_approval(),不再使用is_safe_readonly_command()。create_subprocess_exec(*argv)直接执行,不经过 Shell,确保“校验的 argv = 实际执行的 argv”;保留 PTY/pipe、超时、后台任务、进程组、fsatrace 等原有机制。RIPGREP_CONFIG_PATH、LESSOPEN、BASH_ENV、BASH_FUNC_*、PYTHONPATH、NODE_OPTIONS、LD_PRELOAD等启动/配置注入变量。_SAFE_READONLY_COMMANDS:移除rg、less、more、tree、date、hostname,并更新函数 docstring,明确它只是尽力而为的过滤器,不是审批安全边界。cat .env*、cat *.env、cat ~/.ssh/id_rsa*、cat /proc/self/environ等不会再被静默自动放行。行为变化
rg --pre .../ 所有rg命令less/more/tree -o/date -s/hostnamels/cat/grep/git logread_onlyshell filter presetrg目前所有命令都需要审批;后续可以为rg补一个严格的 option validator,再恢复常用搜索的自动放行。验证
ruff check:通过ruff format --check:通过ty check --python .venv --error-on-warning:Linux / macOS / Windows 三个平台均通过tests/service/tools/test_shell_filter.py:125 passed覆盖的绕过形式:
rg --pre ./x.sh -e y .rg --pre=./x.sh -e y .rg --pre$IFS./x.sh -e y .rg --p''re ./x.sh -e y .r''g --pre ./x.sh -e y .\rg --pre ./x.sh -e y .cat .env*cat /proc/self/environ以上均会进入审批;POSIX 下普通
ls、cat、git log、带引号的路径仍可自动放行。兼容性与风险
read_onlyshell filter 的过滤行为保持不变;它仍然不是安全沙箱。grep -r扫描到.env)仍依赖敏感目标检测和审批策略;本 PR 不改变这一既有边界。Linked Closing Issues: