dsh-pwsh-guard
给 DSH 加的一道执行前守卫:拦截危险 PowerShell 命令,返回 deny 并把具体原因回给模型,让它自己重写。不弹窗、不打断人、不强制确认。
为什么需要它
DSH 的 pwsh 工具描述里本来就写着两条要求(dsh-tool-pwsh/src/index.ts 的 pwshDescription,第 132–133 行):
Before any delete or move, verify that the resolved absolute target path is the intended one; never run it against a computed path you have not checked.
Do not assign to automatic variables such as
$HOME; variable names are case-insensitive, so$homeis the same read-only variable.
但那是提示词,模型会忽略。下面是一次真实事故:
$root='D:\DS\agent-desktop\build\overlay-root'
$home='D:\DS\agent-desktop\build\overlay-home'
$ws='D:\DS\agent-desktop\build\overlay-ws'
Remove-Item $root,$home,$ws -Recurse -Force -ErrorAction SilentlyContinue
$home 是只读自动变量。赋值抛的是非终止错误——报一行红字,脚本继续跑,$home 仍等于 C:\Users\<用户名>。于是 Remove-Item 实际删掉了用户主目录下的 .ssh 和会话记录,-ErrorAction SilentlyContinue 还把中途的报错全吞了。
本插件把那两条从"提示"变成"硬校验"。
安装
dsh plugin --profile <你的 profile> add dsh-pwsh-guard
这会把它追加进该 profile 的 dsh.profile.bundles,重启 DSH 生效。
卸载:
dsh plugin --profile <你的 profile> remove dsh-pwsh-guard
桌面版(profile 由 Electron 独占)
桌面版的 profile 不能走 CLI,手动往 %USERPROFILE%\.dsh\profiles\desktop\cordis.patch.yml 里加一层:
- insert:
- id: pwsh-guard
name: '绝对路径/to/dsh-pwsh-guard/index.js'
insert 是增补,不覆盖任何内置行。删掉这一段,重启即恢复。
规则
| # | 触发条件 | 拦截原因 |
|---|---|---|
| 1 | 对 $HOME / $Host / $PID / $Error 赋值 |
只读变量,赋值静默失败,后续命令会拿旧值当路径 |
| 2 | 删除类命令(Remove-Item/rd/del/rmdir…)的目标含 C:\Users\、%USERPROFILE%、$env:USERPROFILE、$HOME |
用户主目录范围,几乎不可能是任务目标 |
| 3 | -Recurse -Force 与 -ErrorAction SilentlyContinue 同时出现 |
撞锁停下但吞掉报错,看不出删到哪一步 |
只处理 pwsh / bash 两个工具,其余调用原样放行。
$Args、$Input、$PWD 故意不收——它们其实可写,收进来会误伤。
工作原理
挂在 tools/pre-execute 这个 cordis waterfall 上(dsh-tools/src/events.ts 第 18 行):
'tools/pre-execute'(exec: ToolExecution, next: () => Promise<PreToolDecision>): Promise<PreToolDecision>
返回 { kind: 'deny', reason } 后,pipeline.ts 第 214–226 行把它变成 content: [{ type: 'text', text: 'Error: ' + reason }], isError: true 回给模型。模型看到具体原因就会重写命令再试。
插件骨架照官方 dsh-tool-call-timeout-policy 写,纯 JS、无编译步骤、无额外运行时依赖。
测试
node test-scan.mjs
10 条用例覆盖三条规则的正反两面,当前 10/10 通过。
调规则
改 index.js 顶部三个常量:
READONLY_VARS—— 可拦截的只读自动变量集合DELETE_VERBS—— 删除类 cmdlet 与别名USER_HOME_TARGETS—— 越界目标的特征
宁可严一点:误报的代价是模型重写一次,漏报的代价是删掉用户目录。
许可证
MIT
还没有评论,来写第一条。