delete-guard-agent 0.5.1 → 0.6.0

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/README.md CHANGED
@@ -134,8 +134,10 @@ python scripts/guard.py command "rm -rf /mnt/d/data" --json
134
134
  | H11 | 目标是系统关键前缀(/usr /etc /bin /home…) | Linux | block |
135
135
  | H12 | 目标是挂载点(删除作用于整个卷) | Linux/macOS | block |
136
136
  | H13 | 目标是 macOS 家目录特殊目录(Documents/Library…) | macOS | block |
137
- | H14 | 云/容器/ORM 工具破坏性动词(terraform destroy、prisma migrate reset、docker 卷删除、kubectl delete namespace、SQL DROP) | 全平台 | warn/block |
137
+ | H14 | 云/容器/ORM 工具破坏性动词(terraform destroy、prisma migrate reset、docker 卷/容器删除、kubectl delete namespace、SQL DROP) | 全平台 | warn/block |
138
138
  | H15 | 覆盖型销毁(通配 mv/cp 落入已存在目录、`>` 重定向清空非空文件) | 全平台 | warn |
139
+ | H16 | 目标是家目录根(`/home`、`/root`、`/home/<u>`、`/Users/<u>`、`C:\Users\<u>`) | 全平台 | block |
140
+ | H17 | 目标是凭据/密钥目录或文件(`.ssh` `.aws` `.gnupg` `.kube` `.docker` `.config/gh`、`id_rsa`、`.netrc`…) | 全平台 | block |
139
141
 
140
142
  ## 代码结构(0.2 起拆包;0.3 起 find/git 意图在 core.py 内原生解析)
141
143
 
@@ -168,7 +170,7 @@ python -m unittest discover tests -v
168
170
  以下项已由 CI 的 `ubuntu-latest` / `windows-latest` / `macos-latest` runner
169
171
  或真机 E2E 覆盖:
170
172
 
171
- - [x] 单元测试(131 个,含 H3/H4/H11-H15/find/git 断言、中文社区与第四轮事故回归、预筛一致性比对、过度拦截反例)在
173
+ - [x] 单元测试(140 个,含 H3/H4/H11-H15/find/git 断言、中文社区与第四轮事故回归、预筛一致性比对、过度拦截反例)在
172
174
  ubuntu + windows + macos 三个 runner 上跑
173
175
  - [x] macOS 真机回归(H3 APFS 大小写、H13 家目录特殊目录)——由
174
176
  `macos-latest` runner 真机执行,不再是仅静态判定
package/SKILL.md CHANGED
@@ -48,6 +48,13 @@ description: Use before running ANY destructive file operation (rm -rf, rd /s /q
48
48
  删的是挂载进来的整个卷(rsync --delete 清空备份盘是运维最经典的整卷事故)。
49
49
  8. **非判定宿主的平台语义不算数。** 在 WSL 里看不到 NTFS reparse 属性、
50
50
  在 Linux 上验证不了 APFS 大小写语义。判定结果为"无法验证"时,去目标平台复跑。
51
+ 9. **家目录和凭据目录不在"可清理"范围内,永远(判定器 H16/H17)。** `/home`、`/root`、
52
+ `/home/<user>`、`/Users/<user>`、`C:\Users\<user>` 是"这个人在机器上的
53
+ 全部身份";`.ssh`、`.aws`、`.gnupg`、`.kube`、`.docker`、`.config/gh`、
54
+ `.netrc`、`id_rsa` 删除不可逆,私钥还必须按已泄露处理。事故:
55
+ claude-code#83058(~200GB 家目录)、STUPID-2026-0128(SSH/AWS/GH 凭据全毁)、
56
+ deepseek-harness#13675(%USERPROFILE% 下 86k 文件)。清理"缓存/临时文件"时,
57
+ 路径必须显式落在具体项目目录内,且不得向上用 `..` 或家目录变量兜底。
51
58
 
52
59
  ## 操作流程
53
60
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "delete-guard-agent",
3
- "version": "0.5.1",
3
+ "version": "0.6.0",
4
4
  "description": "Blast-radius judge for destructive commands on Windows, Linux and macOS: blocks filesystem-root and volume-root deletes, quote-chain degradation, NTFS/APFS case collisions, junction and symlink traversal, sudo-escalated deletes, system-prefix wipes and mount-point deletions before the agent executes them.",
5
5
  "license": "MIT",
6
6
  "author": "xinghe-labs",
@@ -30,6 +30,7 @@ from .paths import (
30
30
  cloud_tool_match,
31
31
  glob_base_dir,
32
32
  has_cjk,
33
+ home_destruction,
33
34
  human_bytes,
34
35
  is_ancestor_of_cwd,
35
36
  is_drive_root,
@@ -590,6 +591,11 @@ def is_mount_point(path: str) -> bool:
590
591
  # ---------------------------------------------------------------------------
591
592
 
592
593
 
594
+ def _home_code(kind: str) -> str:
595
+ """home_destruction 的类别 → 规则码。"""
596
+ return "H17_CREDENTIAL_TARGET" if kind == "cred" else "H16_HOME_ROOT"
597
+
598
+
593
599
  def judge_command(
594
600
  command: str,
595
601
  cwd: str | None = None,
@@ -840,6 +846,15 @@ def judge_command(
840
846
  if special:
841
847
  add("H13_MACOS_HOME_SPECIAL", f"{target!r} 命中 macOS 家目录特殊目录 {special}")
842
848
 
849
+ # H16/H17 家目录根 & 凭据目录/文件:删除即不可逆、无回收余地。
850
+ # 家目录根只在递归删除时成灾(非递归只会 rmdir 失败),凭据目录/文件
851
+ # 即使单文件删除也已不可逆,故不受 recursive 约束。
852
+ hit = home_destruction(ap)
853
+ if hit:
854
+ kind, desc = hit
855
+ if kind == "cred" or intent.recursive:
856
+ add(_home_code(kind), f"{target!r} 命中{desc}")
857
+
843
858
  unknowns.extend(unknowns_for_host())
844
859
  if any(br.truncated for br in radii):
845
860
  unknowns.append("爆炸半径统计已达上限,实际文件数更多")
@@ -886,6 +901,10 @@ def judge_path(path: str, cwd: str | None = None) -> dict:
886
901
  special = macos_home_special(ap)
887
902
  if special:
888
903
  add("H13_MACOS_HOME_SPECIAL", f"{path!r} 命中 macOS 家目录特殊目录 {special}")
904
+ hit = home_destruction(ap)
905
+ if hit:
906
+ kind, desc = hit
907
+ add(_home_code(kind), f"{path!r} 命中{desc}")
889
908
 
890
909
  unknowns.extend(unknowns_for_host())
891
910
 
@@ -201,6 +201,60 @@ def has_cjk(s: str) -> bool:
201
201
  return False
202
202
 
203
203
 
204
+ # H16 家目录 / 凭据目录:递归删除这里 = 毁掉一个人在机器上的全部身份
205
+ # (SSH/AWS/GitHub 凭据、未备份的工作成果)。依据见 INCIDENTS.md 候选 21-23:
206
+ # 家目录整体 ~200GB(claude-code#83058)、700GB 主目录、
207
+ # %USERPROFILE% 下 86k 文件(deepseek-harness#13675)、~/.ssh 全毁。
208
+ POSIX_HOME_ROOTS = {"home", "root"}
209
+
210
+ # 凭据/密钥目录:按段序列匹配,不限前缀(~/.ssh、/home/u/.ssh、C:\Users\u\.ssh 同判)
211
+ CREDENTIAL_DIRS = {
212
+ (".ssh",), (".aws",), (".gnupg",), (".pki",), (".kube",), (".docker",),
213
+ (".config", "gh"), (".config", "gcloud"),
214
+ }
215
+ # 单个凭据文件(删掉一个私钥同样是不可逆的凭据丢失)
216
+ CREDENTIAL_FILES = {
217
+ ".netrc", ".git-credentials", ".npmrc", ".pypirc",
218
+ "id_rsa", "id_ed25519", "id_ecdsa",
219
+ }
220
+
221
+
222
+ def home_destruction(path: str) -> tuple[str, str] | None:
223
+ """H16/H17:目标是家目录根本身、或家目录下的凭据目录/文件。
224
+
225
+ 返回 ("home" | "cred", 描述文本),否则 None。
226
+ 入参应是归一后的形态(analysis_path 已展开 ~ / $HOME / %USERPROFILE%)。
227
+ """
228
+ p = path.strip().strip("'\"")
229
+ if not p:
230
+ return None
231
+ norm = p.replace("\\", "/")
232
+ # 盘符路径(C:/Users/...)与 POSIX 路径共用 split,但跳过驱动器段
233
+ segs = [s for s in norm.split("/") if s and not (len(s) == 2 and s[1] == ":")]
234
+ if not segs:
235
+ return None
236
+ low = [s.lower() for s in segs]
237
+
238
+ # 1) POSIX 家目录根:/home、/root、/home/<user>
239
+ if len(low) == 1 and low[0] in POSIX_HOME_ROOTS:
240
+ return "home", f"家目录根 `/{low[0]}`(所有用户数据与凭据)"
241
+ # 2) 家目录根:/home/<user>、/Users/<user>、C:\\Users\\<user>(恰好两段)
242
+ if len(low) == 2 and low[0] in ("home", "users"):
243
+ return "home", f"家目录根 `/{segs[0]}/{segs[1]}`(全部个人数据与凭据)"
244
+
245
+ # 3) 凭据目录:任一段序列命中 CREDENTIAL_DIRS
246
+ for cred in CREDENTIAL_DIRS:
247
+ n = len(cred)
248
+ for i in range(len(low) - n + 1):
249
+ if tuple(low[i:i + n]) == cred:
250
+ shown = segs[i] if n == 1 else "/".join(segs[i:i + n])
251
+ return "cred", f"凭据/密钥目录 `{shown}`"
252
+ # 4) 凭据文件
253
+ if low[-1] in CREDENTIAL_FILES:
254
+ return "cred", f"凭据文件 `{segs[-1]}`"
255
+ return None
256
+
257
+
204
258
  def same_path(a: str, b: str) -> bool:
205
259
  """大小写敏感的字面比较(H3 用)。
206
260
 
@@ -374,6 +428,15 @@ def cloud_tool_match(command: str) -> tuple[str, str] | None:
374
428
  if "--help" in norm or " -h " in norm:
375
429
  return None
376
430
  # SQL 破坏语句优先于工具表匹配(TRUNCATE/DROP 的破坏语义精确,block 级)
431
+ # docker 容器级删除:容器文件系统与日志随之销毁(STUPID-2026-0133 /
432
+ # claude-code#101241:用户只授权 restart,agent 跑了 docker rm,毁掉
433
+ # 唯一一份待诊断的崩溃日志)。不作用于卷,所以只 warn。
434
+ # 用 \b(?!i) 排除 docker rmi(删镜像,可重建,不是数据销毁)。
435
+ if re.search(r"\bdocker(\s+container)?\s+rm\b(?!i)", norm):
436
+ return "docker rm", "STUPID-2026-0133 (claude-code#101241)"
437
+ if re.search(r"\bdocker\s+container\s+prune\b", norm):
438
+ return "docker container prune", "STUPID-2026-0133 (claude-code#101241)"
439
+
377
440
  sql_stmt = re.search(r"\b(drop\s+(table|database|schema)\b|truncate\b)", norm)
378
441
  sql_client = re.search(
379
442
  r"\b(psql|mysql|mariadb|sqlite3|sqlcmd|neon|supabase)\b"
@@ -57,4 +57,22 @@ RULES_COMMON: dict[str, tuple[str, str, str]] = {
57
57
  "目标解析错误时没有任何回收余地",
58
58
  "macrumors#1176135 (sudo rm -R / .Spotlight-v100), steam-for-linux 2015",
59
59
  ),
60
+ # H16/H17:家目录根与凭据目录 —— 删除它们等于抹掉一个人在机器上的
61
+ # 全部身份(私钥、云凭据、未备份的成果)。事故链见 INCIDENTS.md 候选 21-23:
62
+ # ~200GB 家目录(claude-code#83058)、700GB 主目录、%USERPROFILE% 下
63
+ # 86k 文件(deepseek-harness#13675)、~/.ssh 全毁(私钥须按泄露处理)。
64
+ "H16_HOME_ROOT": (
65
+ "block",
66
+ "目标是家目录根本身(/home、/root、/home/<user>、/Users/<user>、"
67
+ "C:\\Users\\<user>),删除会毁掉整台机器上的个人数据与全部凭据",
68
+ "claude-code#83058 (~200GB home), STUPID-2026-0128, "
69
+ "deepseek-harness#13675 (86k files under %USERPROFILE%)",
70
+ ),
71
+ "H17_CREDENTIAL_TARGET": (
72
+ "block",
73
+ "目标是凭据/密钥目录或文件(.ssh/.aws/.gnupg/.kube/.docker、"
74
+ "id_rsa 等),删除不可逆且私钥必须按已泄露处理",
75
+ "STUPID-2026-0128 (SSH/AWS/GH credentials destroyed), "
76
+ "deepseek-harness#13675 (~/.ssh wiped)",
77
+ ),
60
78
  }