dcg:AI 编码代理破坏性命令拦截器
用 Rust 编写的高性能 hook,在 AI 编码代理执行破坏性命令前拦截,保护代码和未提交的改动。
- 浏览
- 4
- 访问
- 0
- 下载
- 0
- 版本
- v0.16.0
- 平台
- Windows、macOS、Linux
详细介绍
项目简介
dcg(Destructive Command Guard)是一个用 Rust 编写的高性能 hook,在 AI 编码代理执行破坏性命令之前将其拦截,避免 git reset --hard、rm -rf ./src、DROP TABLE users 这类命令在几秒内毁掉未提交的工作。它支持 Claude Code、Codex CLI、Gemini CLI、GitHub Copilot CLI、VS Code Copilot Chat、Cursor、Hermes Agent、Posit Assistant、Oh My Pi、Grok(xAI)、OpenCode、Amp 等工具,借助 SIMD 加速过滤,不会带来可感知的延迟。
主要功能
- 零配置保护:默认拦截危险的 git 和文件系统命令。
- 50+ 安全包:覆盖数据库、Kubernetes、Docker、AWS/GCP/Azure、Terraform 等场景。
- heredoc 与内联脚本扫描:能识别
python -c "os.remove(...)"这类嵌入脚本。 - 上下文识别:不拦截
grep "rm -rf"这样的数据,但会拦截真正执行的rm -rf /。 - 可读输出:stderr 上给出拦截面板、规则上下文和安全建议,stdout 保持供 agent 读取的机器可读输出。
- Scan 模式:可用于 pre-commit 钩子和 CI,在代码审查阶段发现危险命令。
- Explain 模式:用
dcg explain "command"查看某条命令为何被拦截。 - 按 agent 的配置档案:可针对不同代理设置
disabled_packs、extra_packs、additional_allowlist等字段。
安装与使用
Linux、macOS 以及 Windows 的 WSL 环境可用一键脚本安装,脚本会自动识别平台、下载对应二进制,并配置检测到的 agent hook:
curl -fsSL "https://raw.githubusercontent.com/Dicklesworthstone/destructive_command_guard/main/install.sh?$(date +%s)" | bash -s -- --easy-mode
原生 Windows 使用 PowerShell 安装脚本 install.ps1(带 -EasyMode -Verify 参数):安装 dcg.exe,校验 SHA256,把它加入用户 PATH 并运行自检。
要启用更多规则包,编辑 ~/.config/dcg/config.toml:
[packs]
enabled = [
"database.postgresql",
"kubernetes.kubectl",
"cloud.aws",
"containers.docker",
]
部分代理有独立安装开关,例如 dcg install --opencode、dcg install --crush、dcg install --amp、dcg install --grok、dcg install --agy。
注意事项
- 升级后需要刷新 hook:从 v0.15.3 起 Claude Code 的 hook matcher 变为
Bash|PowerShell|Monitor,运行dcg install(或dcg doctor --fix)即可就地迁移旧条目。 [agents.*]中的trust_level只是写入 JSON 输出和日志的建议性标签,本身不改变规则评估;实际行为差异来自disabled_packs、extra_packs、additional_allowlist、disabled_allowlist等字段。extra_packs/disabled_packs使用dcg packs列出的包或分类 ID;"paranoid"是渐进响应模式,不是规则包。- 原生 Windows 默认启用
windows.filesystem与windows.system包,del /s、rd /s、Remove-Item -Recurse、format、vssadmin delete shadows等默认被拦截。 - Aider 仅支持有限的 git 钩子,Continue 只做检测。
本作品整理自 GitHub 开源项目 Dicklesworthstone/destructive_command_guard,原作者 @Dicklesworthstone。中文介绍由 AI 根据项目 README 整理,以原仓库为准。
更新记录
- v0.16.0当前版本
This release closes commands that ran unguarded (writes hidden by `IFS` splitting, by shell variables or by `git rev-parse` paths, redirections written against the command word, upper-case program names, ssh_config command options, OpenCode v2's `shell` tool, and Python, Deno, Bun and awk process calls dcg did not read) and adds Amp, Muse Code and Factory Droid integrations. It also fixes a long list of false positives reported against 0.15.3, most of them in quoted Python and Node heredocs, and makes a configuration file that fails to load block commands instead of being skipped.
- v0.15.3
补上 Claude Code Monitor 工具、wrapper/远程 shell 重定向、GNU tar 辅助程序、解压到 .git 等此前未拦截的命令,并修复 0.15.2 的多处误报;升级后需运行 dcg install 刷新 hook。
评论