OutBlade/claude-code-hooks
GitHub: OutBlade/claude-code-hooks
为 Claude Code 提供安全钩子,防止危险命令与密钥泄露。
Stars: 0 | Forks: 0
Claude Code 是有史以来最强大的编码助手。它也能够删除你的项目、覆盖团队的工作或泄露你的凭据——并非出于恶意,而是因为没有任何东西阻止它。
这些钩子在 Claude Code 运行工具之前或之后调用一个 shell 脚本。它们不到一秒钟完成,且无需配置。
## 已安装
| Hook | When it runs | What it does |
|---|---|---|
| **bash-guard** | before every shell command | hard-blocks `rm -rf /~/.`, disk format, fork bombs, `DROP DATABASE`, pipe-to-shell |
| **git-guard** | before every git command | hard-blocks force-push to main, `reset --hard HEAD~N`, deleting protected branches |
| **secret-guard** | before every file write | warns before writing `.env`, private keys, or content matching API key patterns |
| **auto-format** | after every file edit | runs prettier / black / gofmt / rustfmt automatically |
| **notify** | when Claude stops | desktop notification so you can switch windows and come back |
| **session-log** | after every tool call | daily audit log at `~/.claude/logs/YYYY-MM-DD.log` |
**Hard-blocked** 意味着 Claude 无法继续。它会看到原因并必须寻找更安全的方案。
**Warned** 意味着 Claude 会注入上下文信息并自行做出决定。
## bash-guard 详细说明
完全阻止——无法覆盖:
```
rm -rf / rm -rf ~ rm -rf .
mkfs.* dd if=... of=/dev/sd*
:(){:|:&};: chmod -R 777 /
DROP DATABASE DROP SCHEMA ... CASCADE
shutdown poweroff halt
```
仅警告——Claude 继续执行但会看到消息:
```
curl
| bash sudo ... > /etc/
```
## git-guard 详细说明
阻止:
```
git push --force origin main git push -f origin master
git reset --hard HEAD~3 git push origin :main
```
仅警告:
```
git push --force
git commit --amend
git reset --hard HEAD
git clean -fd
```
## secret-guard 详细说明
在写入以下名称的文件时发出警告:
```
.env .env.* credentials.json secrets.yaml *.pem *.key *.p12
id_rsa id_ed25519 kubeconfig terraform.tfvars .netrc .npmrc
```
如果文件内容匹配则警告:
```
sk-... OpenAI key sk-ant-... Anthropic key
AKIA... AWS Access Key ghp_... GitHub token
xoxb-... Slack token sk_live_... Stripe live key
-----BEGIN * PRIVATE KEY-----
password = "..." api_key = "..."
```
不会阻止——Claude 可能合法地创建带有占位符的 `.env.example`。但它总是会看到警告并添加 `.gitignore` 条目。
## 自动格式化
在每次 `Edit` 或 `Write` 后运行。根据文件扩展名检测格式化程序:
```
.js .ts .tsx .jsx .css .html .json .yaml .md → prettier
.py → ruff, then black
.go → gofmt
.rs → rustfmt
.sh → shfmt
.lua → stylua
```
如果未安装格式化器,则静默跳过。
## 会话日志
每次工具调用追加到 `~/.claude/logs/YYYY-MM-DD.log`:
```
[09:14:02] [3f8a1c2b] Bash [success] git checkout -b feature/auth
[09:14:09] [3f8a1c2b] Write [success] src/auth/middleware.ts
[09:14:13] [3f8a1c2b] Bash [success] npm test -- --testPathPattern=auth
[09:14:35] [3f8a1c2b] Bash [error ] tsc --noEmit
```
每天一个日志。会话重启后仍然存在。适用于查看你不在时 Claude 的操作记录。
## 选择性安装
要单独安装一个钩子而不安装脚本:
```
curl -sSL https://raw.githubusercontent.com/OutBlade/claude-code-hooks/main/hooks/bash-guard.sh \
-o ~/.claude/hooks/bash-guard.sh && chmod +x ~/.claude/hooks/bash-guard.sh
```
然后添加到 `~/.claude/settings.json`:
```
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{ "type": "command", "command": "bash ~/.claude/hooks/bash-guard.sh" }
]
}
]
}
}
```
## 自定义编写
每个钩子从标准输入读取 JSON 并在退出时返回:
- `0` — 允许(可选地输出一个 JSON `systemMessage` 以注入 Claude 上下文)
- `2` — 阻止(将原因写入 stderr;Claude 读取它并必须调整)
```
#!/usr/bin/env bash
INPUT=$(cat)
COMMAND=$(echo "$INPUT" | python3 -c "
import sys, json
print(json.load(sys.stdin).get('tool_input', {}).get('command', ''))
")
if echo "$COMMAND" | grep -q "my-dangerous-thing"; then
echo "Blocked: reason here" >&2
exit 2
fi
```
完整 API 参考:[Claude Code hooks 文档](https://docs.anthropic.com/en/docs/claude-code/hooks)
欢迎提交拉取请求。每个钩子应是一个独立的 shell 脚本,在正常路径上保持静默,并且仅需要 Python 3 和标准 OS 工具。
## 卸载
```
curl -sSL https://raw.githubusercontent.com/OutBlade/claude-code-hooks/main/uninstall.sh | bash
```
移除钩子脚本,清理 `settings.json`,保留日志。
## 要求
Python 3。预装在 macOS 和 Linux 上。[Windows 下载](https://python.org)。
## 相关
[claude-mem](https://github.com/OutBlade/claude-mem) — 为 Claude Code 提供跨会话的持久记忆标签:API 密钥防护, Bash 防护, Claude Code, DevSecOps, Git 保护, Git 钩子, pre-commit, rm -rf 防护, Shell 脚本, XML 请求, 上游代理, 代码安全, 危险命令拦截, 命令行安全, 安全开发, 安全钩子, 密钥泄露防护, 开源安全工具, 强制推送防护, 提交拦截, 机器学习安全, 漏洞枚举, 版本控制安全, 逆向工程平台