Skip to content

fix(permission): 支持跨平台删除指令与命令边界识别,避免参数误拦截 - #550

Closed
yee211 wants to merge 1 commit into
shareAI-lab:mainfrom
yee211:fix/cross-platform-permission-rules
Closed

fix(permission): 支持跨平台删除指令与命令边界识别,避免参数误拦截#550
yee211 wants to merge 1 commit into
shareAI-lab:mainfrom
yee211:fix/cross-platform-permission-rules

Conversation

@yee211

@yee211 yee211 commented Aug 26, 2026

Copy link
Copy Markdown

动机与背景 (Motivation)

  1. Windows 跨平台兼容性:在 Windows 环境下,大模型倾向于调用系统原生命令(如 delrmdirerase),原安全规则仅匹配了 Linux 的 rm ,导致在 Windows 上删除文件时绕过了安全闸门。
  2. 避免非破坏性命令误拦截:原先基于简单字符串包含(in command)的匹配方式,会导致作为参数出现的词(如 echo del test.txtgit commit -m "del test"grep rm file.py)被错误拦截。

主要修改 (Changes)

  1. 边界感知正则匹配 (Boundary-Aware Regex)
    统一引入命令边界正则:
    DESTRUCTIVE_PATTERN = r"(?:^|[;&|\n]|&&|\|\|)\s*(?:rm|del|rmdir|erase)(?:[\s/]|$)|> /etc/|chmod 777"
    • 确保只有位于行首或命令分隔符(&&||;&|\n)后的删除指令才会被识别;
    • 支持 Windows 紧凑参数语法(如 del/frmdir/s/q);
    • 彻底消除 echo / git / grep 等命令中携带关键字时的误拦截。
  2. 全课程同步:覆盖 s03 ~ s14s17 相关的 13 个章节代码。
  3. 参数化回归测试:新增 tests/test_permission_destructive_boundaries.py,包含 69 个参数化测试用例,覆盖独立命令、链式连接符、大小写及参数防误报场景。
  4. 网站数据同步:运行 extract-content 重新生成了 versions.jsondocs.json

测试验证 (Testing)

  • 运行 pytest tests/test_permission_destructive_boundaries.py,69 个测试用例全部通过(100% Passed)。
  • 本地 Windows 实机测试 python s03_permission/code.py,成功精准拦截 del test.txt 且正常放行 echo del test.txt

…dary-aware regex

- Update permission rules across s03-s14 and s17 with boundary-aware regex
- Support Windows deletion commands (del, rmdir, erase) and flag formats
- Prevent false positives on non-destructive commands like 'echo del test.txt' or 'git commit -m del'
- Add parameterized regression test suite tests/test_permission_destructive_boundaries.py
- Re-generate web versions.json via extract-content script
@Bill-Billion

Copy link
Copy Markdown
Collaborator

感谢提出PR,但是当前 PR 与 #548 的修改方向基本重合,#548 已在保留现有 rm 行为的基础上修复 del 绕过问题。

另外,本仓库主要讲解 Harness 的权限设计,后续不继续扩展完整的跨平台 Shell 权限规则。rmdir、erase、紧凑参数等更复杂的权限决策,可以由学习者按实际需求自行完善。因此这里先关闭该 PR,感谢贡献。

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.

2 participants