dsh-delete-guard 0.0.0-stage → 2.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +257 -0
- package/LICENSE +21 -0
- package/README.en.md +146 -0
- package/README.ja.md +143 -0
- package/README.md +140 -2
- package/SECURITY.md +51 -0
- package/cordis.patch.yml +14 -0
- package/lib/guard.js +1048 -0
- package/lib/index.js +389 -0
- package/lib/move-guard.js +127 -0
- package/lib/recycle.js +174 -0
- package/package.json +61 -4
- package/scripts/recycle-bin.ps1 +271 -0
package/README.md
CHANGED
|
@@ -1,3 +1,141 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-delete-guard
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[English](README.en.md) | **简体中文** | [日本語](README.ja.md)
|
|
4
|
+
|
|
5
|
+
**让 agent 删不掉「恢复不了」的东西。**
|
|
6
|
+
|
|
7
|
+
删除只有两条路:进 Windows 回收站(资源管理器里能还原),或者被拦下来换成一次
|
|
8
|
+
`file_trash` 调用。没有第三条。
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
agent: Remove-Item -Recurse -Force .\src
|
|
12
|
+
被拒: The delete guard blocked this delete. Re-run this as: file_trash with paths = ["…\src"]
|
|
13
|
+
随后: file_trash → 移入回收站,「还原」可用
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## 解决什么问题
|
|
17
|
+
|
|
18
|
+
DSH 里模型执行 `pwsh` 的删除命令,文件是被直接抹掉的:`ctx.fs` 没有 delete,
|
|
19
|
+
`ctx.shell` 只有 `execute()`,没有任何中间层。一句 `Remove-Item -Recurse -Force`
|
|
20
|
+
就能带走一整个目录,没有回收站,没有撤销。
|
|
21
|
+
|
|
22
|
+
本插件挂在 `tools/pre-execute`(宿主真正路由每个工具调用的接缝),把这些调用**拒绝**,
|
|
23
|
+
并告诉模型改用 `file_trash`;后者用 `SHFileOperationW(FOF_ALLOWUNDO)` 移动文件——和资源
|
|
24
|
+
管理器删文件是同一套 Win32 API,所以 `$Recycle.Bin` 的 `$I`/`$R` 元数据正确,「还原」可用。
|
|
25
|
+
|
|
26
|
+
挡住的不只是 `Remove-Item`:管道、`xargs`、变量拼接的目标、写在 `if { }` 里的删除、内联
|
|
27
|
+
`-Command` / `-EncodedCommand`、持久终端里敲的命令、Git Bash 的 `/c/...` 路径、以及清空
|
|
28
|
+
回收站的各种写法,各有对应规则。
|
|
29
|
+
|
|
30
|
+
## 安装
|
|
31
|
+
|
|
32
|
+
```shell
|
|
33
|
+
# 从 npm 安装
|
|
34
|
+
dsh plugin --profile web add dsh-delete-guard
|
|
35
|
+
# 或直接从仓库安装(npm 不可用时)
|
|
36
|
+
dsh plugin --profile web add github:deepestevan/dsh-delete-guard
|
|
37
|
+
# 或者从本地 tarball(改过源码时用这条)
|
|
38
|
+
pnpm pack --pack-destination ./temp/pack # 先升 package.json 的 version
|
|
39
|
+
dsh plugin --profile web add file:./temp/pack/dsh-delete-guard-2.2.2.tgz
|
|
40
|
+
# 重启 dsh web:lib/*.js 只在进程启动时加载
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
`file:` 必须指向 tarball,不能指向源码目录——目录形式 pnpm 会硬链接,源码一挪插件就悬空。
|
|
44
|
+
|
|
45
|
+
更新走 `remove` + `add` 两步:单跑 `add` 可能返回 `Already up to date` 而什么都没换,而 `lib/*.js` 只有进程重启才会重新加载。
|
|
46
|
+
|
|
47
|
+
## 挡住什么
|
|
48
|
+
|
|
49
|
+
**会进回收站(被重定向)**
|
|
50
|
+
|
|
51
|
+
```
|
|
52
|
+
Remove-Item C:\tmp\a.txt del / erase / rd / rmdir / rm / unlink
|
|
53
|
+
Remove-Item -Recurse -Force C:\tmp\dir
|
|
54
|
+
if (Test-Path $p) { Remove-Item $p -Force }
|
|
55
|
+
terminal_send: "Remove-Item C:\tmp\a.txt"
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
**直接拒绝**
|
|
59
|
+
|
|
60
|
+
| 类别 | 例子 |
|
|
61
|
+
| --- | --- |
|
|
62
|
+
| 目标不可枚举(没法证明搬对东西) | `Remove-Item $(…)`、`… \| Remove-Item`、`xargs rm`、`Remove-Item @targets`、`-Recurse C:\tmp\*` |
|
|
63
|
+
| 危险路径 | `C:\`、`\`、`C:\Windows`、`Program Files`、`..\..\`、Git Bash 的 `~` / `/tmp` |
|
|
64
|
+
| 无删除动词的擦除 | `[System.IO.File]::Delete(…)`、`Invoke-Expression "…"`、`cmd /c del`、`robocopy /MIR`、`find -delete` |
|
|
65
|
+
| 永久擦除工具 | `shred`、`sdelete`、`srm`、`cipher /w`、`format X:`(只按程序位置判定) |
|
|
66
|
+
| 清空回收站 | `Clear-RecycleBin`(含 `&`、子壳、`-EncodedCommand` 形态)、`SHEmptyRecycleBin`、对回收站条目驱动 shell 动词、直接删 `$Recycle.Bin` 内容 |
|
|
67
|
+
| 没有回收站的位置 | UNC 共享、exFAT/FAT32 卷(在那里"回收"等于永久擦除) |
|
|
68
|
+
| 脚本内删除 | 内联 shell / Python / Node 体里的删除动词与擦除调用;把含删除的文本写进 `.ps1`/`.sh`/`.py` |
|
|
69
|
+
|
|
70
|
+
**放行**:只读命令、只提到删除词的操作(`Select-String -Pattern "Remove-Item"`、
|
|
71
|
+
`Get-Content shred.exe`)、以及从回收站**还原**文件。
|
|
72
|
+
|
|
73
|
+
## 两个工具
|
|
74
|
+
|
|
75
|
+
| 工具 | 作用 |
|
|
76
|
+
| --- | --- |
|
|
77
|
+
| `file_trash { paths, reason? }` | 唯一允许的删除方式。移入回收站,逐条报告成功与失败,绝不降级成永久删除 |
|
|
78
|
+
| `trash_status { drive? }` | 报告该卷回收站的项目数与占用,用于确认删除确实落进了回收站 |
|
|
79
|
+
|
|
80
|
+
## 配置
|
|
81
|
+
|
|
82
|
+
在 profile 的 `cordis.patch.yml` 里覆盖(`config` 是整块替换,要保留的键必须重述):
|
|
83
|
+
|
|
84
|
+
| 键 | 默认 | 含义 |
|
|
85
|
+
| --- | --- | --- |
|
|
86
|
+
| `enabled` | `true` | 总开关 |
|
|
87
|
+
| `blockDestructiveCommands` | `true` | 拦截 shell 删除并重定向到 `file_trash` |
|
|
88
|
+
| `forbidEmptyRecycleBin` | `true` | 禁止清空或改动回收站 |
|
|
89
|
+
| `forbidWipe` | `true` | 禁止 `shred` / `sdelete` / `srm` / `cipher /w` / `format` |
|
|
90
|
+
| `blockScriptDeletes` | `true` | 禁止脚本文件与内联字符串里的删除 |
|
|
91
|
+
|
|
92
|
+
## 恢复已删的文件
|
|
93
|
+
|
|
94
|
+
恢复不是插件的功能,也不需要——回收站是操作系统的。脚本里这样还原(与界面语言无关):
|
|
95
|
+
|
|
96
|
+
1. 读 `X:\$Recycle.Bin\<SID>\$I*` 记录:偏移 24 是原路径字符数,偏移 28 起是 UTF-16LE 原路径;
|
|
97
|
+
2. 按原路径匹配到目标条目,取配对的 `$R` 文件;
|
|
98
|
+
3. `Move-Item` 搬回原位。
|
|
99
|
+
|
|
100
|
+
`$R`/`$I` 的文件名是随机的(`$R3LCXYR.txt`),原文件名不在文件名里,只能靠解析 `$I`
|
|
101
|
+
反查;动词还原后可能残留孤儿 `$I` 记录,跳过 payload 缺失的即可。资源管理器里右键
|
|
102
|
+
「还原」与之一致,只是脚本化时别依赖本地化的动词名。
|
|
103
|
+
|
|
104
|
+
## 不挡什么
|
|
105
|
+
|
|
106
|
+
| 不覆盖 | 原因 |
|
|
107
|
+
| --- | --- |
|
|
108
|
+
| 脚本文件里已有的删除(守卫从未见过内容) | 只能拒绝写入与看得见的内联形态,不能执行期改写 |
|
|
109
|
+
| `Add-Type` 里写任意 C#(如 `DeleteFileW`) | 文本判定够不到;只有针对回收站的 API 被点名拦掉 |
|
|
110
|
+
| 命令名先拼进变量再 `& $c` 调用 | 文本层面无法求值 |
|
|
111
|
+
| 表格外工具名的执行通道(自定义 shell 工具、第三方插件) | 覆盖名单是显式的,见下 |
|
|
112
|
+
| 终端里分两次发送才拼出的删除 | 每次 send 只看到这一次的文本 |
|
|
113
|
+
| 非 Windows 平台 | 桥接是 `SHFileOperationW` |
|
|
114
|
+
| 空余空间不足时的删除 | NTFS 同卷回收是 rename,不占新空间,没有可预检的东西 |
|
|
115
|
+
|
|
116
|
+
覆盖的 shell 工具名:`pwsh`、`powershell`、`cmd`、`bash`、`sh`、`zsh`、`shell`、
|
|
117
|
+
`terminal_send`、`terminal_write`。判定看的是语句的命令词、调用的形态和程序位置,不是「这一行里出现过这个词」。
|
|
118
|
+
|
|
119
|
+
## 测试
|
|
120
|
+
|
|
121
|
+
```shell
|
|
122
|
+
node test/run-all.mjs
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
90 项、五层:命令分类(49)、`file_trash` 操作数守卫(15)、宿主 schema 与钩子接线(11)、
|
|
126
|
+
宿主 schema 受理(5)、对真实回收站的端到端(10)。`test/` 不进 tarball,所以打包前必须
|
|
127
|
+
全绿;其中两层会加载宿主自己的校验器,找不到 DSH 安装时报「跳过」而非失败。
|
|
128
|
+
|
|
129
|
+
## 文档
|
|
130
|
+
|
|
131
|
+
| 文件 | 内容 |
|
|
132
|
+
| --- | --- |
|
|
133
|
+
| [CHANGELOG.md](CHANGELOG.md) | 版本摘要 |
|
|
134
|
+
| [SECURITY.md](SECURITY.md) | 威胁模型:策略层,不是沙箱 |
|
|
135
|
+
| [LICENSE](LICENSE) | MIT |
|
|
136
|
+
|
|
137
|
+
设计取舍、逐轮实测与发布跑批属于工程笔记,保存在维护者侧,不随仓库与包发布。
|
|
138
|
+
|
|
139
|
+
## 许可
|
|
140
|
+
|
|
141
|
+
MIT,见 [LICENSE](LICENSE)。
|
package/SECURITY.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# Security notes
|
|
2
|
+
|
|
3
|
+
## What this plugin is
|
|
4
|
+
|
|
5
|
+
A **policy layer** on top of the model's tool calls. It intercepts the command
|
|
6
|
+
text a model is about to run and either refuses it or redirects it to a tool that
|
|
7
|
+
moves the target into the OS Recycle Bin.
|
|
8
|
+
|
|
9
|
+
It is **not** a sandbox. It does not confine what a process may do to the file
|
|
10
|
+
system, and it is not meant to stop a deliberate attacker who already controls the
|
|
11
|
+
command line.
|
|
12
|
+
|
|
13
|
+
## What it does protect
|
|
14
|
+
|
|
15
|
+
- An agent deleting files the user still needs — the ordinary failure mode. Every
|
|
16
|
+
recognised delete is refused and redirected to `file_trash`
|
|
17
|
+
(`SHFileOperationW(FOF_ALLOWUNDO)`), so the item is recoverable from Explorer.
|
|
18
|
+
- Locations where "delete" would actually mean "erase": UNC shares and
|
|
19
|
+
exFAT/FAT32 volumes are refused before the shell is called, because
|
|
20
|
+
`SHFileOperationW` erases there and still reports success.
|
|
21
|
+
- Irreversible side doors: permanent-erase programs, erases performed through
|
|
22
|
+
.NET APIs, `Invoke-Expression`, deletes delegated to `cmd.exe`, and every
|
|
23
|
+
spelling of emptying the Recycle Bin.
|
|
24
|
+
|
|
25
|
+
## Out of scope (by design, and verified as limits)
|
|
26
|
+
|
|
27
|
+
The full list, with evidence, is in [README.md](README.md) 「不挡什么」; the per-rule measurements live in the maintainer's engineering notes (not shipped). The ones that matter for
|
|
28
|
+
a security read:
|
|
29
|
+
|
|
30
|
+
- `Add-Type` compiles arbitrary C#; nothing textual can stop it from calling
|
|
31
|
+
`DeleteFileW`. The guard names the bin-specific API (`SHEmptyRecycleBin`) and
|
|
32
|
+
leaves the general case to the operating system's permissions.
|
|
33
|
+
- Commands assembled at run time (`& $cmd`, `$c='Clear-Recycle'+'Bin'; & $c`) are
|
|
34
|
+
invisible to a text-level parser.
|
|
35
|
+
- A delete split across two `terminal_send` calls cannot be assembled from either
|
|
36
|
+
half.
|
|
37
|
+
- Tools outside the covered list (`pwsh`, `powershell`, `cmd`, `bash`, `sh`,
|
|
38
|
+
`zsh`, `shell`, `terminal_send`, `terminal_write`) are not inspected at all.
|
|
39
|
+
- Deletes inside a script file whose body the guard never sees: it refuses the
|
|
40
|
+
write and the inline forms it can see, not the executed body.
|
|
41
|
+
|
|
42
|
+
## Reporting
|
|
43
|
+
|
|
44
|
+
Once this repository is public, use a GitHub Security Advisory (or a private
|
|
45
|
+
message to the maintainer) rather than a public issue for anything that lets a
|
|
46
|
+
recognised delete bypass the Recycle Bin.
|
|
47
|
+
|
|
48
|
+
Please include: the exact command text, which tool carried it, the DSH version,
|
|
49
|
+
and whether the target was on NTFS, a UNC path, or another file system. Do not
|
|
50
|
+
paste real paths from a private machine — reduce them to the smallest form that
|
|
51
|
+
still reproduces the behaviour.
|
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# dsh-delete-guard bundle patch.
|
|
2
|
+
#
|
|
3
|
+
# Mounts the recycle-bin guard into the DSH profile. The plugin has a host half
|
|
4
|
+
# only: it hooks `tools/pre-execute` and registers two tools. No core file is
|
|
5
|
+
# touched and no existing plugin source is modified.
|
|
6
|
+
- insert:
|
|
7
|
+
- id: delete-guard
|
|
8
|
+
name: 'dsh-delete-guard'
|
|
9
|
+
config:
|
|
10
|
+
enabled: true
|
|
11
|
+
blockDestructiveCommands: true
|
|
12
|
+
forbidEmptyRecycleBin: true
|
|
13
|
+
forbidWipe: true
|
|
14
|
+
blockScriptDeletes: true
|