展示 HN:一个 Claude 代码插件,用于捕获破坏性的 Git 和文件系统命令。
Show HN: A Claude Code plugin that catch destructive Git and filesystem commands

原始链接: https://github.com/kenryu42/claude-code-safety-net

## Claude 代码安全网:插件摘要 在经历因未检查的 AI 代理操作导致数据丢失(如 `rm -rf`)后,一个新的 Claude 代码插件“安全网”提供了一层关键的保护,以防止破坏性的 git 和文件系统命令。与 `settings.json` 中的简单基于前缀的拒绝规则不同,此插件利用**语义命令分析**的钩子。 它解析参数,理解标志,并递归分析 shell 包装器以区分安全和危险的操作——防止意外擦除数据或丢失未提交的更改等问题。该插件专门针对危险的命令模式(例如 `git checkout --`、`rm -rf /`),同时允许安全的替代方案。 当检测到破坏性命令时,该插件会阻止执行并提供明确的原因。会维护详细的日志以进行审计,敏感数据会自动删除。用户可以使用“严格”和“偏执”模式调整安全级别以提高保护。 **安装:** `/plugin marketplace add kenryu42/cc-marketplace`,然后 `/plugin install safety-net@cc-marketplace`,最后重启 Claude 代码。

一个新的 Hacker News 讨论集中在一个 Claude Code 插件上,该插件旨在防止破坏性的 Git 和文件系统命令。然而,评论者普遍批评其有效性,认为这是解决严重问题的错误方法。 核心问题在于,Claude(以及类似的人工智能模型)可以通过采用创造性的解决方法来绕过命令过滤——创建和执行 shell 脚本,利用像 `awk` 这样的工具,或者寻找其他晦涩的方法。由于实现相同结果的方式有很多,因此危险命令的黑名单被认为是不充分的。 用户建议更强大的解决方案,例如在容器化环境中运行 AI 代理,而不是过滤命令。这限制了它的访问权限,并允许进行频繁备份,从而防止数据丢失。其他人建议利用 Claude 的“计划模式”来避免出现问题调试循环。 讨论还涉及对过度依赖人工智能进行编码任务的担忧,以及对基本编程技能下降的看法。
相关文章

原文

CI Version Claude Code License: MIT

A Claude Code plugin that acts as a safety net, catching destructive git and filesystem commands before they execute.

We learned the hard way that instructions aren't enough to keep AI agents in check. After Claude Code silently wiped out hours of progress with a single rm -rf ~/ or git checkout --, it became evident that "soft" rules in an CLAUDE.md or AGENTS.md file cannot replace hard technical constraints. The current approach is to use a dedicated hook to programmatically prevent agents from running destructive commands.

Why Hooks Instead of settings.json?

Claude Code's .claude/settings.json supports deny rules for Bash commands, but these use simple prefix matching—not pattern matching or semantic analysis. This makes them insufficient for nuanced safety rules:

Limitation Example
Can't distinguish safe vs. dangerous variants Bash(git checkout) blocks both git checkout -b new-branch (safe) and git checkout -- file (dangerous)
Can't parse flags semantically Bash(rm -rf) blocks rm -rf /tmp/cache (safe) but allows rm -r -f / (dangerous, different flag order)
Can't detect shell wrappers sh -c "rm -rf /" bypasses a Bash(rm) deny rule entirely
Can't analyze interpreter one-liners python -c 'os.system("rm -rf /")' executes without matching any rm rule

This hook provides semantic command analysis: it parses arguments, understands flag combinations, recursively analyzes shell wrappers, and distinguishes safe operations (temp directories, within cwd) from dangerous ones.

/plugin marketplace add kenryu42/cc-marketplace
/plugin install safety-net@cc-marketplace

Note

After installing the plugin, you need to restart your Claude Code for it to take effect.

  1. Run /plugin → Select Marketplaces → Choose cc-marketplace → Enable auto-update
Command Pattern Why It's Dangerous
git checkout -- files Discards uncommitted changes permanently
git checkout <ref> -- <path> Overwrites working tree with ref version
git restore files Discards uncommitted changes
git restore --worktree Explicitly discards working tree changes
git reset --hard Destroys all uncommitted changes
git reset --merge Can lose uncommitted changes
git clean -f Removes untracked files permanently
git push --force / -f Destroys remote history
git branch -D Force-deletes branch without merge check
git stash drop Permanently deletes stashed changes
git stash clear Deletes ALL stashed changes
git worktree remove --force Force-deletes worktree without checking for changes
rm -rf (paths outside cwd) Recursive file deletion outside the current directory
rm -rf / or ~ or $HOME Root/home deletion is extremely dangerous
find ... -delete Permanently removes files matching criteria
xargs rm -rf Dynamic input makes targets unpredictable
xargs <shell> -c Can execute arbitrary commands
parallel rm -rf Dynamic input makes targets unpredictable
parallel <shell> -c Can execute arbitrary commands
Command Pattern Why It's Safe
git checkout -b branch Creates new branch
git checkout --orphan Creates orphan branch
git restore --staged Only unstages, doesn't discard
git restore --help/--version Help/version output
git branch -d Safe delete with merge check
git clean -n / --dry-run Preview only
git push --force-with-lease Safe force push
rm -rf /tmp/... Temp directories are ephemeral
rm -rf /var/tmp/... System temp directory
rm -rf $TMPDIR/... User's temp directory
rm -rf ./... (within cwd) Limited to current working directory

What Happens When Blocked

When a destructive command is detected, the plugin blocks the tool execution and provides a reason.

Example output:

BLOCKED by Safety Net

Reason: git checkout -- discards uncommitted changes permanently. Use 'git stash' first.

Command: git checkout -- src/main.py

If this operation is truly needed, ask the user for explicit permission and have them run the command manually.

You can manually test the hook by attempting to run blocked commands in Claude Code:

# This should be blocked
git checkout -- README.md

# This should be allowed
git checkout -b test-branch
just setup
# or
uv sync && uv run pre-commit install
.claude-plugin/
  plugin.json
  marketplace.json
hooks/
  hooks.json
scripts/
  safety_net.py          # Entry point
  safety_net_impl/
    __init__.py
    hook.py              # Main hook logic
    rules_git.py         # Git command rules
    rules_rm.py          # rm command rules
    shell.py             # Shell parsing utilities
tests/
  safety_net_test_base.py
  test_safety_net_audit.py
  test_safety_net_edge.py
  test_safety_net_find.py
  test_safety_net_git.py
  test_safety_net_parsing_helpers.py
  test_safety_net_rm.py

By default, unparseable commands are allowed through. Enable strict mode to fail-closed when the hook input or shell command cannot be safely analyzed (e.g., invalid JSON, unterminated quotes, malformed bash -c wrappers):

export SAFETY_NET_STRICT=1

Paranoid mode enables stricter safety checks that may be disruptive to normal workflows. You can enable it globally or via focused toggles:

# Enable all paranoid checks
export SAFETY_NET_PARANOID=1

# Or enable specific paranoid checks
export SAFETY_NET_PARANOID_RM=1
export SAFETY_NET_PARANOID_INTERPRETERS=1

Paranoid behavior:

  • rm: blocks non-temp rm -rf even within the current working directory.
  • interpreters: blocks interpreter one-liners like python -c, node -e, ruby -e, and perl -e (these can hide destructive commands).

The guard recursively analyzes commands wrapped in shells:

bash -c 'git reset --hard'    # Blocked
sh -lc 'rm -rf /'             # Blocked

Interpreter One-Liner Detection

Detects destructive commands hidden in Python/Node/Ruby/Perl one-liners:

python -c 'import os; os.system("rm -rf /")'  # Blocked

Block messages automatically redact sensitive data (tokens, passwords, API keys) to prevent leaking secrets in logs.

All blocked commands are logged to ~/.cc-safety-net/logs/<session_id>.jsonl for audit purposes:

{"ts": "2025-01-15T10:30:00Z", "command": "git reset --hard", "segment": "git reset --hard", "reason": "...", "cwd": "/path/to/project"}

Sensitive data in log entries is automatically redacted.

MIT

联系我们 contact @ memedata.com