Skip to content

fix(backend): 修正 Shell 语法口径不一致与危险命令误伤 - #160

Merged
li-xiu-qi merged 1 commit into
devfrom
fix/shell-syntax-alignment-and-hardline-scope
Aug 3, 2026
Merged

fix(backend): 修正 Shell 语法口径不一致与危险命令误伤#160
li-xiu-qi merged 1 commit into
devfrom
fix/shell-syntax-alignment-and-hardline-scope

Conversation

@li-xiu-qi

Copy link
Copy Markdown
Collaborator

Closes #159

改动概述

本 PR 只含一个 commit(d40a80c,2026-07-24),修两个独立但同源的问题:提示词与执行层对「用哪个 shell、写哪种语法」的口径不统一。

一、统一 Shell 语法口径

  • 新增 build_shell_prompt_section(),按 recommended_family 生成语法口径段落,与 detect_interpreter("auto") 同序(Windows:Git Bash → WSL → busybox → PowerShell)
  • 消除提示词里「优先 bash」与「Windows 优先 PowerShell」的矛盾
  • 删除错误的 2>nul(cmd 语法,PowerShell 下会生成名为 nul 的文件)
  • AVAILABLE_SHELLS 改走 ShellExecutor 探测,避免把 System32 的 WSL 启动器误报为可用 bash
  • general_host_prompt.md 接入 ${SHELL_GUIDANCE_SECTION}

build_powershell_prompt_section() 的分工:本段负责「默认用哪个解释器、写哪种语法」的主口径,PowerShell 段只补充 5.1 / 7 版本兼容写法。

二、危险命令拦截拆档

hardline 原本把「高危但常用」与「纯破坏性」混在一档无条件 BLOCK,导致安装脚本等正规操作无法通过任何授权模式放行。

  • 纯破坏命令继续全模式硬拦:rm -rf /~mkfsdd of=/dev/、fork bomb
  • 高危但常用的操作(rm -rf 子目录、curl|bashsudo/sunmap/nc、带 $TOKENcurl)下沉到 shell_policy,按授权模式改为需确认,full_auto 放行
  • shell_tool.py 同步删除重复且误伤的拦截条

验证

在 Windows(win32 / Python 3.12.11)本地跑通,基线为含 back-merge 的最新 dev3e95b82):

结果
全量测试 1575 passed, 0 failed
shell / powershell / 授权 定向测试 155 passed
ruff format --check app/ 431 files already formatted

改动 9 个文件、+286 −50,其中 169 行是测试test_shell_environment.py +98、test_capability_authorization.py +50、test_powershell_prompt_section.py +21)。

审阅提示

本 commit 与刚合入 dev 的 c1b49bf(PowerShell auto 改用系统最新版本)都涉及 auto 行为,且都改动 shell_environment.pytest_powershell_prompt_section.py

cherry-pick 到 back-merge 后的 dev 时 git 自动合并、无冲突,但代码不冲突不等于语义兼容,因此额外单独跑了上述 155 例定向测试确认两者共存无问题:c1b49bf 管「选哪个 PowerShell 版本」,本 PR 管「默认选哪个解释器族、提示词教哪种语法」,作用层次不同。

统一 shell 检测口径,让提示词与执行层 auto 实际使用的解释器一致:
- 新增 build_shell_prompt_section,按 recommended_family(与 detect_interpreter
  "auto" 同序)生成语法口径;消除提示词里"优先 bash"与"Windows 优先 PowerShell"
  的矛盾,删除错误的 2>nul(cmd 语法,PowerShell 下会生成名为 nul 的文件)
- AVAILABLE_SHELLS 改走 ShellExecutor 探测,避免把 System32 的 WSL 启动器
  误报为可用 bash
- general_host_prompt.md 接入 ${SHELL_GUIDANCE_SECTION}

修复危险命令拦截误伤正规操作:
- hardline 拆档:纯破坏命令(rm -rf / 与 ~、mkfs、dd of=/dev/、fork bomb)
  仍全模式硬拦;高危但常用的操作(rm -rf 子目录、curl|bash、sudo/su、
  nmap/nc、带 $TOKEN 的 curl)从 hardline 移除,落到 shell_policy 按授权模式
  改为需确认(full_auto 放行),修复安装脚本等被无条件 BLOCK
- shell_tool.py 同步删除重复且误伤的拦截条,仅保留纯破坏命令
- 同步调整/新增 authorization、shell_environment、powershell 相关测试
@li-xiu-qi
li-xiu-qi merged commit a18ca0b into dev Aug 3, 2026
4 checks passed
@li-xiu-qi
li-xiu-qi deleted the fix/shell-syntax-alignment-and-hardline-scope branch August 3, 2026 07:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant