CC Safety Net —— AI 编码代理的安全网
在 AI 编码代理执行破坏性 Git 和文件系统命令、访问敏感文件之前,及时拦截并阻止它们。
- Star 数
- ★ 1.7k
- 最近更新
- 今天
- License
- MIT
- 主语言
- TypeScript
- FA 评分
- 63/100 · 存在缺口
30 秒速览
- 运行形态
- 可在哪里用
- 平台专用Codex · Claude Code
- 费用
- 免费,无需付费服务
- 上手难度
- 低 · 几分钟可跑通
- 开始前需要
- 典型场景
- 开发者在自己的机器上使用 Claude Code,希望防止代理意外执行
rm -rf ~/或git checkout --导致未提交工作丢失。 - 不适合
- 不使用支持 PreToolUse 钩子的编码代理的团队
- 需要沙箱或文件系统权限隔离而非命令拦截的场景
- 需要完全离线安装、无法使用 npx 或 npm 的环境
- 源码审查
- 63/100 · 存在缺口
这个 Agent 能做什么,适合哪些场景?
CC Safety Net 是一个 PreToolUse 钩子,可防止 AI 编码代理运行危险的命令,例如 rm -rf、git reset --hard 或 git push --force。它通过语义解析命令,而非简单的字符串匹配,因此标志位重排、shell 包装器或解释器单行代码都无法绕过。支持 Claude Code、Codex、Gemini CLI、GitHub Copilot CLI、Kimi Code、OpenCode 和 Pi,覆盖 Windows、macOS 和 Linux。它采用默认失败-关闭策略,即使输入畸形或配置无效,也会阻止命令执行。它提供审计日志、自定义规则(通过规则文件)以及严格和偏执等可选模式。安装方式因代理而异,包括插件市场或 npx ,并可通过 npx cc-safety-net doctor 进行诊断。
作为运行前钩子,拦截 Bash 调用。底层使用 @clack/prompts 之类的组件(虽未明确,但通过交互式提示推断)进行交互式确认。核心逻辑通过语义分析命令:区分 git checkout -b feature(安全)和 git checkout -- file(破坏性)。递归分析 bash -c、sh -c 等 shell 包装器层,并检测 python -c、node -e 等解释器单行代码中的破坏性代码。默认情况下阻止命令,并记录到 ~/.cc-safety-net/logs/<session_id>.jsonl。支持通过环境变量(如 CC_SAFETY_NET_STRICT=1)启用严格模式、偏执模式和工作树模式。提供 CLI 命令 npx cc-safety-net doctor 和 npx cc-safety-net explain。
- 开发者在自己的机器上使用 Claude Code,希望防止代理意外执行
rm -rf ~/或git checkout --导致未提交工作丢失。 - 使用 Codex 的团队希望强制执行安全策略,即便代理尝试通过包装器或标志位重排来绕过简单命令阻止,也能拦截破坏性命令。
- 使用 Kimi Code 或 OpenCode 的开发者,希望通过单一工具实现跨所有 CLI 代理的一致防护。
- 在 Windows 上使用 AI 编码代理的开发者,需要路径比较和命令解析均能正确处理
\和cmd.exe的用户。 - 希望通过严格模式强制阻止不可解析命令,并增加偏执模式以确保安全,即使正在积极使用代理的开发者也适用。
如何安装或部署这个 Agent?
需要 Node.js 18 或更高版本。各代理安装方式不同:
- Codex:在 ~/.codex/config.toml 中启用 plugin_hooks,添加市场 codex plugin marketplace add kenryu42/cc-marketplace,然后在 TUI 中通过 /plugins 和 /hooks 安装并信任钩子。
- Claude Code:运行 /plugin marketplace add kenryu42/cc-marketplace,然后运行 /plugin install safety-net@cc-marketplace,接着运行 /reload-plugins。
- Gemini CLI:gemini extensions install https://github.com/kenryu42/gemini-safety-net。
- GitHub Copilot CLI:/plugin install kenryu42/copilot-safety-net。
- Kimi Code:npx -y cc-safety-net hook install --kimi-code,可选运行 npx skill add kenryu42/cc-safety-net。
- OpenCode:opencode plugin -g cc-safety-net。
- Pi:pi install npm:cc-safety-net。
如何使用这个 Agent?
安装后,钩子会自动拦截 Bash 调用。正常使用 AI 代理即可;当执行破坏性命令时会被阻止。可通过 npx cc-safety-net doctor 验证安装并运行自检,通过 npx cc-safety-net explain "git reset --hard" 查看命令如何被分析。可选地,设置环境变量(例如 CC_SAFETY_NET_STRICT=1)启用严格模式。旧版自定义规则(.safety-net.json 等)已不再加载,需运行 npx -y cc-safety-net rule migrate 迁移。
这个 Agent 有哪些优点和局限?
- 语义分析能识别命令意图,而不只是匹配字符串,从而区分安全与危险的
git checkout和rm用法。 - 支持多个代理 CLI(Claude Code、Codex、Gemini CLI 等),且跨 Windows、macOS、Linux 可用。
- 默认失败-关闭、严格模式、偏执模式、自定义规则文件以及审计日志,提供细致的安全策略控制。
- 通过
npx cc-safety-net explain和doctor提供诊断与追踪,便于理解安全行为。
- 仅作为 PreToolUse 钩子工作,因此仅适用于支持此类钩子的代理,不适用于所有 AI 工具。
- 安装需按代理分别进行,且首次安装流程复杂(例如 Codex 需要 TUI 操作,OpenCode 有缓存问题)。
- 运行需要 Node.js 18+,可能引入额外依赖。
- 旧版自定义规则不会自动迁移,需要手动命令,否则命令会保持被阻止(失败-关闭)。
- 依赖网络来获取 GitHub 规则文件(如果通过 URL 引用),且每个代理需要单独安装。
这个 Agent 与同类方案有什么区别?
与沙箱相比,沙箱允许工作区内的破坏性命令(如 git reset --hard 或 rm -rf .),而 CC Safety Net 会主动阻止这些操作,因此两者可叠加使用。
与相关度最高的同类 agent 并排比较关键指标。
| Agent | 源码审查 | 形态 / 费用 | Star | 最近更新 | 主语言 | 完整支持的平台 |
|---|---|---|---|---|---|---|
| CC Safety Net —— AI 编码代理的安全网 当前 | 63 · 存在缺口 | 命令行工具免费 | ★ 1.7k | 今天 | TypeScript | Codex · Claude Code |
| 破坏性命令守卫 (dcg) | 61 · 存在缺口 | 命令行工具免费 | ★ 6.2k | 今天 | Rust | Codex · Claude Code |
| OrchestKit AI 开发工具箱 | 86 · 表现良好 | Agent 插件 / 技能免费 + 模型费 | ★ 296 | 今天 | TypeScript | Claude Code |
| Plannotator | 78 · 表现良好 | 命令行工具免费版 + 付费版 | ★ 9.5k | 今天 | TypeScript | Codex · Claude Code |
FollowAgents 如何评估这个 Agent?
查看各维度的扣分理由
证据显示:工具以PreToolUse钩子运行,拦截破坏性命令,默认fail-closed,有审计日志和秘密自动编辑。扣分:用户确认机制未明确(仅信任钩子),回滚能力未提及,发布者身份未验证。
证据显示:有CI、测试、构建脚本,依赖单一(shell-quote),错误消息有测试覆盖。扣分:未验证实际运行,依赖可用性未完全确认。
证据显示:支持多平台、多代理,有模式配置,触发精确性通过语义分析。扣分:环境适配细节未完全验证。
证据显示:有README、安装说明、文档链接、MIT许可证、版本号、变更日志生成脚本。扣分:维护责任未明确,发布者未验证。
证据显示:提供doctor和explain命令,输出可用,边际价值高(防止数据丢失)。扣分:成本效益未量化。
证据显示:README声明与代码测试一致,有测试覆盖。扣分:跨来源验证不足,仅依赖单一仓库。
- 发布者身份未验证,需谨慎信任。
- 用户确认机制不明确,可能影响安全性。
- 回滚能力未提及,数据丢失风险需注意。
最近版本
来自该项目 GitHub Releases 的最新发布记录,说明为上游原文摘录,未经本站改写或测试。
-
发布说明摘录
This release closes an rsync --delete gap where an unscoped destination, including one that came from empty variables, was allowed at every protection level. Added - Added an rsync.delete rule that blocks rsync delete modes (--delete, --del, and abbreviations such as --delete-af) when the destination is outside the current directory, outside a trusted temp root, or supplied through a variable. Mirroring inside the workspace or between trusted temp paths stays allowed, except under paranoid rm, which blocks it too. (#271) …在 GitHub 查看 ↗ -
发布说明摘录
The command analyzer now resolves more shell-script and source paths before deciding, so fewer safe scripts are blocked as unverifiable and heredoc-written scripts are still inspected when they are run through those paths. Changed - Improved heredoc script coverage: a script written by a heredoc and then executed directly by its path ($DIR/run.sh) is now checked against its recorded body instead of running unexamined. A body with a non-shell shebang such as #!/usr/bin/env python3, a body that cannot be parsed, or a body that exceeds analysis limits is allowed rather than blocked. ( …在 GitHub 查看 ↗ -
发布说明摘录
This release closes several ways a dangerous command could reach the shell unchecked: commands sent by Amp bypassing the secret, policy and Git-metadata guards, shell options that look like a syntax check but still execute, ambiguous -c option clusters, and Git discard relaxations in repositories that can redirect Git elsewhere. Commands containing a carriage return or an unquoted Unicode blank are now refused as unparseable. Highlights …在 GitHub 查看 ↗
常见问题
使用需要付费吗?
如何让代理执行破坏性命令?
哪些命令会被阻止?
rm -rf、git reset --hard、git push --force、git stash clear、git clean -f、find -delete、dd、mkfs、shred 等,即使通过 shell 包装器或解释器单行代码包装也会被阻止。