blast-radius-guard
CSecurityUse before running ANY destructive file operation (rm -rf, rd /s /q, rmdir /s, Remove-Item -Recurse -Force, del, sudo rm, deleting /usr /etc or any system prefix, deleting a mount point, terraform destroy, prisma migrate reset, drizzle-kit push --force, alembic downgrade base, docker volume rm, docker compose down -v, docker system prune --volumes, kubectl delete namespace, psql DROP TABLE/TRUNCATE, mv across filesystems) on Windows, Linux, macOS, or WSL, or paths that contain spaces, CJK cha...
- 7 stars
- 0 votes
- 0 copies
- 3 views
- Added November 14, 2026
Works with
Security analysis
60/100- Performs destructive filesystem operations
- Reads or references SSH private keys
Pro scans all 11 files and shows the line behind each finding
npx -y skills add xinghe-labs/blast-radius-guard --agent claude-codeAre you the author of blast-radius-guard?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/xinghe-labs-blast-radius-guard)---
name: blast-radius-guard
description: Use before running ANY destructive file operation (rm -rf, rd /s /q, rmdir /s, Remove-Item -Recurse -Force, del, sudo rm, deleting /usr /etc or any system prefix, deleting a mount point, terraform destroy, prisma migrate reset, drizzle-kit push --force, alembic downgrade base, docker volume rm, docker compose down -v, docker system prune --volumes, kubectl delete namespace, psql DROP TABLE/TRUNCATE, mv across filesystems) on Windows, Linux, macOS, or WSL, or paths that contain spaces, CJK characters, quotes, drive letters, or /mnt mounts. Also use when the agent itself is about to delete a directory it did not create, when cleaning up before rewriting deliverables, or when a user asks to "清理/删除/清空" anything. Judges the true blast radius, catches NTFS/APFS junction and symlink traversal, case-insensitive path collisions, quote-chain degradation, sudo-escalated deletes, system-prefix wipes, mount-point deletions, and CJK silent failures - the platform-specific data-loss paths that POSIX safety habits do not cover. Converts permanent deletes into quarantine renames. Do not use for files inside a normal git worktree cleanup that the agent just created itself in a single-layer POSIX shell on ext4.
---
# Blast Radius Guard
你可以删错一个文件,但你不该删错一整个卷。这个 skill 存在的原因不是"删除危险",
而是**删除命令的真实语义和 agent 写出来的命令文本经常不是一回事**。
每一条规则下面都埋着一次真实的数据销毁事故。适用于 Windows、Linux、macOS 与 WSL。
## 它怎么被调用(三层)
| 层 | 机制 | 可靠性 |
|---|---|---|
| ① 手动 CLI | 直接跑 `scripts/guard.py command ...` | 100%,但要自己记得 |
| ② 本 skill | agent 读到本文件后,删除前自觉跑判定器 | 高,但依赖模型自觉 |
| ③ 硬拦截 | 宿主 hook / 扩展在命令**执行前**强制判定 | **100%,不依赖自觉(推荐)** |
安装硬拦截层:
- **pi**:把本仓库目录加入 pi 的 extensions(`package.json` 已含 `pi.extensions` 指向 `src/extension.ts`),扩展自动拦截 bash/powershell 删除命令,block 级直接拒绝并把改写建议回给模型
- **Claude Code**:把 `hooks/pretooluse.py` 挂进 `PreToolUse`(见该文件头部注释)
- **Codex**:无 hook 机制,只有 ② 层
## 铁律(先于一切分析)
1. **不把计算出的路径管道进 `cmd /c`。** 用 PowerShell 原生命令做文件操作
(`Remove-Item`、`Rename-Item`)。`cmd /c "rd /s /q ..."` 加上含空格或
Unicode 的路径,曾把引号退化成对整个盘符根的删除(cursor 论坛事故:
整个 C 盘被删到要重装系统)。
2. **永远不用 `rd /s /q` / `rm -rf` 作为第一步。** 第一步是 rename 进隔离区;
第二步才是观察期;第三步才轮到真删除。隔离目录默认
`%USERPROFILE%\.delete-quarantine\<date>\`(POSIX:`~/.delete-quarantine/<date>/`),
同卷 rename 瞬时完成且可撤销。
3. **路径大小写必须从磁盘上核对,不能凭记忆。** NTFS(Windows)与 APFS/HFS+
(macOS,默认)大小写不敏感:`rm -rf "D:\CampusconnECT"` 会解析到真实存在的
`D:\CampusConnect`,一次毁掉九个生产仓库(claude-code#60442)。
4. **CJK 路径的空输出不是"文件不存在"。** PowerShell/WSL2 对 CJK 路径可能
静默返回空结果(claude-code#68343)。看到空输出先换
`Get-Item -LiteralPath` 复核,再决定下一步。
5. **递归删除前必须检查子树里的 junction / 符号链接。** Claude Code 官方在
v2.1.205 修复的就是这个:删 worktree 时穿透嵌套 junction,删到了树外。
6. **不用 sudo/doas 执行删除。** root 权限会绕过几乎所有文件系统保护
(macrumors#1176135:`sudo rm -R / .Spotlight-v100` 多打一个空格)。
需要提权清理时,先以普通用户视角跑判定器确认目标。
7. **先 `mount` / `/proc/mounts` 再删目录。** 看起来是"空目录"的可能是挂载点:
删的是挂载进来的整个卷(rsync --delete 清空备份盘是运维最经典的整卷事故)。
8. **非判定宿主的平台语义不算数。** 在 WSL 里看不到 NTFS reparse 属性、
在 Linux 上验证不了 APFS 大小写语义。判定结果为"无法验证"时,去目标平台复跑。
9. **家目录和凭据目录不在"可清理"范围内,永远(判定器 H16/H17)。** `/home`、`/root`、
`/home/<user>`、`/Users/<user>`、`C:\Users\<user>` 是"这个人在机器上的
全部身份";`.ssh`、`.aws`、`.gnupg`、`.kube`、`.docker`、`.config/gh`、
`.netrc`、`id_rsa` 删除不可逆,私钥还必须按已泄露处理。事故:
claude-code#83058(~200GB 家目录)、STUPID-2026-0128(SSH/AWS/GH 凭据全毁)、
deepseek-harness#13675(%USERPROFILE% 下 86k 文件)。清理"缓存/临时文件"时,
路径必须显式落在具体项目目录内,且不得向上用 `..` 或家目录变量兜底。
10. **“恢复”动作比事故本身更容易毁掉剩下的东西(判定器 H18)。**
`git reset --hard`、`git restore <pathspec>`、`git checkout -- .`、
`git stash clear/drop`、`git gc --prune=now`、`git reflog expire --expire=now`
都不删路径,只把工作树与 index 写回 —— 未提交改动与未跟踪文件瞬间不可恢复。
事故现场还在时先停手:先 `git stash push` 或把目录整体复制一份,再谈清理。
claude-code#84829 里 agent 正是一次目录作用域的 `git restore`
把另一个 agent 的 rm 事故现场一起还原掉了,而它刚陈述过 bulk-op 教训。
## 操作流程
### 第 1 步 —— 执行任何删除/移动前,跑判定器
```bash
python scripts/guard.py command "<你要执行的原命令>"
```
退出码:`0` 允许,`1` 需人工确认,`2` 拒绝。输出里每个 hazard 都带事故编号。
**退出码 2 时,改写命令后重跑,而不是向用户 begged permission 后原样执行**——
工具拦的不是"用户没同意",是"命令文本表达不了你的意图"。
### 第 2 步 —— 把永久删除降级为隔离 rename
判定为 warn/block 时,第一个动作永远是改写成隔离 rename:
```powershell
# Windows(PowerShell):
$q = "$env:USERPROFILE\.delete-quarantine\$(Get-Date -Format yyyyMMdd-HHmmss)"
New-Item -ItemType Directory -Force -Path $q | Out-Null
Rename-Item -LiteralPath "D:\bad-old-output" -NewName $q\bad-old-output
```
```bash
# POSIX(Linux/macOS/WSL):
q=~/.delete-quarantine/$(date +%Y%m%d-%H%M%S)
mkdir -p "$q"
mv -- "/path/bad-old-output" "$q/bad-old-output"
```
`-LiteralPath`(Windows)不解释通配符,这是 CJK/特殊字符路径唯一可靠的选择。
对跨卷移动(rename 会 COPY+DELETE):先复制到同卷隔离区再删原目录。
### 第 3 步 —— 报告时给出爆炸半径,而不是"删除完成"
用户需要看到:`N 文件 / M 目录 / X GB / K 个链接`,链接数不为零时要显式点名。
`guard.py command ... --json` 直接给出这些数字。超过 20,000 项会截断并声明。
## 什么时候可以直接删
- 目标是**本会话内 agent 自己刚创建**的临时目录,路径是代码里写死的单层字符串,
判定器返回 allow
- 用户显式给出精确目标、命令为单层 shell、全 ASCII 路径、非卷根、
非系统前缀、非挂载点、爆炸半径里 reparse=0,且判定器返回 allow
- git clean 类操作:优先 `-n`(dry-run)看清单,再真执行
这三种之外不要再给自己找"看起来安全"的理由。工具说 block,就 block。
## 范围外形态(判定器不覆盖,需手动纪律)
`find -delete`、`git clean -f`、`git worktree remove` 已纳入判定器
(0.3 起);以下是仍需手动纪律的形态:
- **`find` 的 `-exec rm ... \;`**:判定器只认 `-delete` 谓词,
`-exec rm` 是任意命令执行,建议先 `-print` 预览。
- **`git clean` 的交互式路径参数**:`git clean -f <path>` 的路径会被
判定,但交互式选择(无路径参数全目录清理)依赖 `-n` 预览纪律。
- **`git worktree remove` 的嵌套 junction**:判定器给 H9 警告,
但 worktree 内的 junction 穿透(H4 事故原型)仍需先
`git worktree list` 核对目标。
- **删除逻辑写入脚本再执行**(2026-09 事故:Python 清理脚本经
os.walk 穿透 junction 删掉 48,218 个活文件):判定器只能看见
`python cleanup.py <dir>` 这行文本,脚本内部的删除行为不可见。
删除操作必须以 shell 直接可见的命令执行(跑判定器),绝不封装进
自己编写的脚本;批量删除先输出文件清单(dry-run)人工过目。
## 判定器的原理边界(MCP / 脚本间接删除)
判定器原理是**解析 shell 命令文本**。有两类数据销毁它原理上看不见:
1. **MCP 工具调用**(filesystem delete、数据库工具的 drop 等):
不经过 bash/powershell 工具,判定器无从介入。
2. **脚本间接删除**(agent 写一个 Python 清理脚本再执行):
shell 命令只是 `python cleanup.py <dir>`,删除行为在脚本内部。
对应的宿主层防御(判定器之外的第二道墙):
- **Claude Code**:permission 配置里对 `mcp__*` 的删除类工具加
`deny`/`ask` 规则;`bypassPermissions` 只在容器/VM 里用(官方文档
明确警告)。PreToolUse hook 的 matcher 可以同时挂 `Bash|PowerShell|Task`。
- **pi**:扩展已拦截 bash/powershell;MCP 工具调用可另装权限类扩展。
- **通用纪律**:`bypassPermissions`/`never ask` 是绝大多数事故的共同前提
(L 站/V2EX 事故复盘共识)——交互式确认挡不住 agent 自主性失控,
但至少把"每次执行"的知情权留给你。
## 与宿主安全机制的分工
Claude Code 的 permission 系统、Codex 的 sandbox 管的是"这次工具调用能不能跑";
本 skill 管的是"这条命令文本在目标平台上的真实语义"。两者不互替:
sandbox 拦不住引号链退化(它在单层里合法),permission 拦不住大小写碰撞
(命令文本本身"正确")。如果你的宿主有 PreToolUse hook,把
`guard.py command` 挂进去;没有的话,本 skill 的纪律就是最后一道防线。
Files in this skill
- CHANGELOG.md
- INCIDENTS.md
- SECURITY.md
- SKILL.md
- docs/demo.svg
- hooks/pretooluse.py
- package.json
- pyrightconfig.json
- scripts/guard.py
- src/extension.ts
- tests/test_guard.py
Attribution
Related skills
View all in ai-agents →Comments
Loading comments…
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.