cc-safe-setup 29.6.40 → 29.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +66 -0
- package/.claude-plugin/plugin.json +11 -0
- package/README.md +123 -12
- package/SETTINGS_REFERENCE.md +2 -0
- package/SKILL.md +47 -0
- package/examples/README.md +11 -1
- package/examples/auto-approve-compound-git.sh +3 -0
- package/examples/auto-compact-context-monitor.sh +35 -0
- package/examples/auto-mode-safety-enforcer.sh +57 -0
- package/examples/background-task-guard.sh +57 -0
- package/examples/broad-find-guard.sh +62 -0
- package/examples/cache-creation-spike-detector.sh +32 -0
- package/examples/case-insensitive-path-guard.sh +96 -0
- package/examples/cjk-punctuation-guard.sh +44 -0
- package/examples/clipboard-secret-guard.sh +29 -0
- package/examples/compact-circuit-breaker.sh +72 -0
- package/examples/context-size-alert.sh +38 -0
- package/examples/context-usage-drift-alert.sh +33 -0
- package/examples/dangerous-pip-flag-guard.sh +51 -0
- package/examples/deny-bypass-detector.sh +143 -0
- package/examples/dotenv-read-guard.sh +48 -0
- package/examples/dotfile-protection-guard.sh +60 -0
- package/examples/effort-tracking-logger.sh +30 -0
- package/examples/exploration-budget-guard.sh +77 -0
- package/examples/financial-operation-guard.sh +47 -0
- package/examples/full-rewrite-detector.sh +63 -0
- package/examples/home-critical-bash-guard.sh +56 -0
- package/examples/idle-session-cost-alert.sh +36 -0
- package/examples/model-version-alert.sh +18 -0
- package/examples/model-version-change-alert.sh +31 -0
- package/examples/move-delete-sequence-guard.sh +92 -0
- package/examples/pii-upload-guard.sh +72 -0
- package/examples/pr-duplicate-guard.sh +14 -0
- package/examples/production-port-kill-guard.sh +60 -0
- package/examples/quota-reset-cycle-monitor.sh +30 -0
- package/examples/repo-visibility-guard.sh +33 -0
- package/examples/sandbox-relative-path-audit.sh +51 -0
- package/examples/session-agent-cost-limiter.sh +43 -0
- package/examples/session-cost-alert.sh +62 -0
- package/examples/session-memory-watchdog.sh +9 -0
- package/examples/settings-integrity-monitor.sh +55 -0
- package/examples/settings-json-model-guard.sh +89 -0
- package/examples/shell-config-truncation-guard.sh +97 -0
- package/examples/shell-wrapper-guard.sh +4 -4
- package/examples/subagent-spawn-rate-monitor.sh +34 -0
- package/examples/subcommand-chain-guard.sh +44 -0
- package/examples/system-dir-protection-guard.sh +100 -0
- package/examples/thinking-display-enforcer.sh +25 -0
- package/examples/thinking-stall-detector.sh +61 -0
- package/examples/tool-retry-budget-guard.sh +59 -0
- package/examples/worktree-branch-pollution-detector.sh +35 -0
- package/examples/worktree-create-log.sh +6 -0
- package/examples/worktree-hook-linker.sh +72 -0
- package/examples/worktree-remove-uncommitted-guard.sh +20 -0
- package/hooks/hooks.json +60 -0
- package/index.mjs +92 -6
- package/memory/market-anthropic-japan-strategy-2026-04-13.md +4 -0
- package/package.json +2 -2
- package/plugins/credential-guard/.claude-plugin/plugin.json +58 -0
- package/plugins/git-protection/.claude-plugin/plugin.json +58 -0
- package/plugins/safety-essentials/.claude-plugin/plugin.json +58 -0
- package/plugins/token-guard/.claude-plugin/plugin.json +51 -0
- package/skills/safety-setup/SKILL.md +47 -0
- package/tests/dotenv-read-guard.test.sh +65 -0
- package/tests/test-auto-mode-safety-enforcer.sh +55 -0
- package/tests/test-case-insensitive-path-guard.sh +78 -0
- package/tests/test-compact-circuit-breaker.sh +134 -0
- package/tests/test-context-usage-drift-alert.sh +52 -0
- package/tests/test-dangerous-pip-flag-guard.sh +56 -0
- package/tests/test-dotfile-protection-guard.sh +68 -0
- package/tests/test-effort-tracking-logger.sh +55 -0
- package/tests/test-exploration-budget-guard.sh +164 -0
- package/tests/test-financial-operation-guard.sh +59 -0
- package/tests/test-home-critical-bash-guard.sh +59 -0
- package/tests/test-model-version-change-alert.sh +55 -0
- package/tests/test-move-delete-sequence-guard.sh +63 -0
- package/tests/test-pr-duplicate-guard.sh +29 -0
- package/tests/test-quota-reset-cycle-monitor.sh +52 -0
- package/tests/test-shell-config-truncation-guard.sh +104 -0
- package/tests/test-subagent-spawn-rate-monitor.sh +43 -0
- package/tests/test-system-dir-protection-guard.sh +81 -0
- package/tests/test-thinking-stall-detector.sh +151 -0
- package/tests/test-tool-retry-budget-guard.sh +75 -0
- package/tests/test-worktree-branch-pollution-detector.sh +50 -0
- package/tests/test-worktree-lifecycle-hooks.sh +29 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "cc-safe-setup",
|
|
3
|
+
"owner": {
|
|
4
|
+
"name": "yurukusa",
|
|
5
|
+
"email": "yurukusa@users.noreply.github.com"
|
|
6
|
+
},
|
|
7
|
+
"metadata": {
|
|
8
|
+
"description": "Safety hooks for Claude Code — 688 battle-tested hooks from 800+ hours of autonomous operation. Prevents rm -rf, force-push, credential leaks, unauthorized fund transfers, deny-rules bypass, and more.",
|
|
9
|
+
"version": "1.0.0",
|
|
10
|
+
"pluginRoot": "./plugins"
|
|
11
|
+
},
|
|
12
|
+
"plugins": [
|
|
13
|
+
{
|
|
14
|
+
"name": "safety-essentials",
|
|
15
|
+
"source": "./plugins/safety-essentials",
|
|
16
|
+
"description": "5 essential safety hooks: block rm -rf, force-push, hard-reset, env overwrites, and package publish. The minimum viable safety net.",
|
|
17
|
+
"version": "1.0.0",
|
|
18
|
+
"author": { "name": "yurukusa" },
|
|
19
|
+
"homepage": "https://yurukusa.github.io/cc-safe-setup/",
|
|
20
|
+
"repository": "https://github.com/yurukusa/cc-safe-setup",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"keywords": ["safety", "hooks", "rm-rf", "force-push", "env", "essential"],
|
|
23
|
+
"category": "safety",
|
|
24
|
+
"tags": ["beginner-friendly", "must-have", "pretooluse"]
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
"name": "git-protection",
|
|
28
|
+
"source": "./plugins/git-protection",
|
|
29
|
+
"description": "Git safety hooks: block force-push, protect main/master, prevent hard-reset, guard rebase, and auto-backup before destructive operations.",
|
|
30
|
+
"version": "1.0.0",
|
|
31
|
+
"author": { "name": "yurukusa" },
|
|
32
|
+
"homepage": "https://yurukusa.github.io/cc-safe-setup/",
|
|
33
|
+
"repository": "https://github.com/yurukusa/cc-safe-setup",
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"keywords": ["git", "force-push", "main-branch", "reset", "rebase", "backup"],
|
|
36
|
+
"category": "safety",
|
|
37
|
+
"tags": ["git", "version-control", "pretooluse"]
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"name": "credential-guard",
|
|
41
|
+
"source": "./plugins/credential-guard",
|
|
42
|
+
"description": "Protect secrets and credentials: block writes to .env files, detect API keys in commands, prevent hardcoded tokens, guard service account files.",
|
|
43
|
+
"version": "1.0.0",
|
|
44
|
+
"author": { "name": "yurukusa" },
|
|
45
|
+
"homepage": "https://yurukusa.github.io/cc-safe-setup/",
|
|
46
|
+
"repository": "https://github.com/yurukusa/cc-safe-setup",
|
|
47
|
+
"license": "MIT",
|
|
48
|
+
"keywords": ["credentials", "secrets", "api-keys", "env", "tokens", "security"],
|
|
49
|
+
"category": "security",
|
|
50
|
+
"tags": ["credentials", "secrets", "pretooluse"]
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
"name": "token-guard",
|
|
54
|
+
"source": "./plugins/token-guard",
|
|
55
|
+
"description": "Token consumption guards: large-read warning (100KB+), read budget limit, subagent cap, and token budget tracking. Reduce waste by 20-40%.",
|
|
56
|
+
"version": "1.0.0",
|
|
57
|
+
"author": { "name": "yurukusa" },
|
|
58
|
+
"homepage": "https://yurukusa.github.io/cc-safe-setup/token-book.html",
|
|
59
|
+
"repository": "https://github.com/yurukusa/cc-safe-setup",
|
|
60
|
+
"license": "MIT",
|
|
61
|
+
"keywords": ["tokens", "cost", "budget", "optimization", "large-read", "subagent"],
|
|
62
|
+
"category": "optimization",
|
|
63
|
+
"tags": ["token-saving", "cost-reduction", "pretooluse", "posttooluse"]
|
|
64
|
+
}
|
|
65
|
+
]
|
|
66
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "cc-safe-setup",
|
|
3
|
+
"description": "665 safety hooks for Claude Code — prevent file deletion, credential leaks, git disasters, and token waste during autonomous AI coding sessions",
|
|
4
|
+
"version": "30.0.0",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "yurukusa"
|
|
7
|
+
},
|
|
8
|
+
"homepage": "https://yurukusa.github.io/cc-safe-setup/",
|
|
9
|
+
"repository": "https://github.com/yurukusa/cc-safe-setup",
|
|
10
|
+
"license": "MIT"
|
|
11
|
+
}
|
package/README.md
CHANGED
|
@@ -4,23 +4,29 @@
|
|
|
4
4
|
[](https://www.npmjs.com/package/cc-safe-setup)
|
|
5
5
|
[](https://github.com/yurukusa/cc-safe-setup/actions/workflows/test.yml)
|
|
6
6
|
|
|
7
|
-
**
|
|
7
|
+
> 🚀 **Launching on [Product Hunt](https://www.producthunt.com/products/cc-safe-setup) — April 21!** Follow us and upvote to support open source safety for AI coding agents.
|
|
8
|
+
|
|
9
|
+
**One command to make Claude Code safe for autonomous operation.** 701 example hooks · 9,200+ tests · 30K+ total installs · [日本語](docs/README.ja.md)
|
|
8
10
|
|
|
9
11
|
```bash
|
|
10
12
|
npx cc-safe-setup
|
|
11
13
|
```
|
|
12
14
|
|
|
13
|
-
Installs 8 safety hooks in ~10 seconds. Blocks `rm -rf /`, prevents pushes to main, catches secret leaks, validates syntax after every edit. Zero dependencies.
|
|
15
|
+
Installs 8 safety hooks in ~10 seconds. Blocks `rm -rf /`, prevents pushes to main, catches secret leaks, validates syntax after every edit. Zero npm dependencies. Hooks use [`jq`](https://jqlang.github.io/jq/) at runtime (`brew install jq` / `apt install jq`).
|
|
14
16
|
|
|
15
17
|
> **What's a hook?** A checkpoint that runs before Claude executes a command. Like airport security — it inspects what's about to happen and blocks anything dangerous before it reaches the gate.
|
|
16
18
|
|
|
17
|
-
[**Getting Started**](https://yurukusa.github.io/cc-safe-setup/getting-started.html) · [**Hook Selector**](https://yurukusa.github.io/cc-safe-setup/hook-selector.html) · [**All Tools**](https://yurukusa.github.io/cc-safe-setup/hub.html) · [**Recipes**](https://yurukusa.github.io/cc-safe-setup/recipes.html) · [Validate your settings.json](https://yurukusa.github.io/cc-safe-setup/validator.html) · [**Check your score**](https://yurukusa.github.io/cc-health-check/) (`npx cc-health-check`)
|
|
19
|
+
[**Getting Started**](https://yurukusa.github.io/cc-safe-setup/getting-started.html) · [**Incident Tracker**](https://yurukusa.github.io/cc-safe-setup/incidents.html) · [**Hook Selector**](https://yurukusa.github.io/cc-safe-setup/hook-selector.html) · [**Token Checkup**](https://yurukusa.github.io/cc-safe-setup/token-checkup.html) · [**Cache Health**](https://yurukusa.github.io/cc-safe-setup/cache-health.html) · [**Version Check**](https://yurukusa.github.io/cc-safe-setup/version-check.html) · [**CLAUDE.md Analyzer**](https://yurukusa.github.io/cc-safe-setup/claudemd-analyzer.html) · [**All Tools**](https://yurukusa.github.io/cc-safe-setup/hub.html) · [**Recipes**](https://yurukusa.github.io/cc-safe-setup/recipes.html) · [Validate your settings.json](https://yurukusa.github.io/cc-safe-setup/validator.html) · [**Check your score**](https://yurukusa.github.io/cc-health-check/) (`npx cc-health-check`) · [**Safety Audit**](https://yurukusa.github.io/cc-safe-setup/safety-audit.html)
|
|
18
20
|
|
|
19
21
|
```
|
|
20
22
|
cc-safe-setup
|
|
21
23
|
Make Claude Code safe for autonomous operation
|
|
22
24
|
|
|
23
25
|
Prevents real incidents (from GitHub Issues):
|
|
26
|
+
✗ rm -rf permanently destroyed ~50 GB / 1,500 files (#49129) ← April 2026
|
|
27
|
+
✗ Auto mode approved ~/.ssh deletion — all SSH keys gone (#49554)
|
|
28
|
+
✗ ~/.git-credentials PATs deleted without confirmation (#49539)
|
|
29
|
+
✗ rm -rf deleted 3,467 files (~7 GB) without confirmation (#46058)
|
|
24
30
|
✗ rm -rf deleted entire user directory via NTFS junction (#36339)
|
|
25
31
|
✗ Remove-Item -Recurse -Force destroyed unpushed source (#37331)
|
|
26
32
|
✗ Entire Mac filesystem deleted during cleanup (#36233)
|
|
@@ -30,6 +36,8 @@ Installs 8 safety hooks in ~10 seconds. Blocks `rm -rf /`, prevents pushes to ma
|
|
|
30
36
|
✗ Syntax errors cascading through 30+ files
|
|
31
37
|
✗ Sessions losing all context with no warning
|
|
32
38
|
✗ CLAUDE.md rules silently ignored after context compaction
|
|
39
|
+
✗ Claude ran destructive DDL on production database (#46684)
|
|
40
|
+
✗ AI executed delete/kill operations on production environment (#46650)
|
|
33
41
|
✗ Subagents ignoring all CLAUDE.md rules since v2.1.84 (#40459)
|
|
34
42
|
|
|
35
43
|
Hooks to install:
|
|
@@ -49,21 +57,35 @@ Installs 8 safety hooks in ~10 seconds. Blocks `rm -rf /`, prevents pushes to ma
|
|
|
49
57
|
|
|
50
58
|
## Why This Exists
|
|
51
59
|
|
|
52
|
-
A Claude
|
|
60
|
+
A user [lost 3,467 files (~7 GB)](https://github.com/anthropics/claude-code/issues/46058) when Claude ran `rm -rf` on their data directory without confirmation. Another [lost their entire C:\Users directory](https://github.com/anthropics/claude-code/issues/36339) when `rm -rf` followed NTFS junctions. Another [lost all source code](https://github.com/anthropics/claude-code/issues/37331) when Claude ran `Remove-Item -Recurse -Force *` on a repo. One user's Claude [ran destructive DDL on a production database](https://github.com/anthropics/claude-code/issues/46684) when asked only to investigate. Another had Claude [execute delete and kill operations on production systems](https://github.com/anthropics/claude-code/issues/46650). Others had untested code pushed to main at 3am. API keys got committed via `git add .`. Syntax errors cascaded through 30+ files before anyone noticed. And [CLAUDE.md rules get silently dropped](https://github.com/anthropics/claude-code/issues/6354) after context compaction — your instructions vanish mid-session.
|
|
61
|
+
|
|
62
|
+
One user [analyzed 6,852 sessions](https://github.com/anthropics/claude-code/issues/42796) and found the Read:Edit ratio dropped from 6.6 to 2.0 — Claude editing files it never read jumped from 6% to 34%. That issue has over 2,100 reactions. The `read-before-edit` example hook catches this pattern before damage happens.
|
|
53
63
|
|
|
54
|
-
|
|
64
|
+
In April 2026, [$1,446 was transferred without authorization](https://github.com/anthropics/claude-code/issues/46828) when Claude moved funds between exchange accounts. A user [lost $367 and got their account suspended](https://github.com/anthropics/claude-code/issues/47046) from a Claude-generated script. [Physical coordinates were uploaded to a public website](https://github.com/anthropics/claude-code/issues/46910) despite 17 sessions of "no PII" in CLAUDE.md. And [deny rules can be bypassed with 50+ subcommands](https://adversa.ai/blog/claude-code-security-bypass-deny-rules-disabled/).
|
|
55
65
|
|
|
56
|
-
Claude Code ships with no safety hooks by default. This tool fixes that.
|
|
66
|
+
Claude Code ships with no safety hooks by default. This tool fixes that. ([Standalone guard script](https://gist.github.com/yurukusa/87f51b97bb655357dd148b66109d0c14) for quick setup | [Database protection hooks](https://gist.github.com/yurukusa/ad27e541769992e9e0cd15c1b487a1d2) | [Credential protection hooks](https://gist.github.com/yurukusa/7292ead735df0aa673f0485eba5587f3) | [Fabrication detection hook](https://gist.github.com/yurukusa/03f4bbbab61f7ddf31049cc28a01d0d9) | [Security vulnerability hooks](https://gist.github.com/yurukusa/81f79ae6d760b27c17f2cd642ea846d7))
|
|
57
67
|
|
|
58
68
|
**Works with Auto Mode.** Claude Code's [Auto Mode sandboxing](https://www.anthropic.com/engineering/claude-code-sandboxing) provides container-level isolation. cc-safe-setup adds process-level hooks as defense-in-depth — catching destructive commands even outside sandboxed environments.
|
|
59
69
|
|
|
60
70
|
**Works with subagents.** Since v2.1.84, subagents and teammates [don't receive CLAUDE.md](https://github.com/anthropics/claude-code/issues/40459) — your project rules are silently skipped. Hooks operate at the process level, but [subagent tool calls may bypass PreToolUse hooks](https://github.com/anthropics/claude-code/issues/21460) in some configurations. As defense-in-depth, cc-safe-setup installs hooks at the user level (`~/.claude/settings.json`). The `subagent-claudemd-inject` example hook re-injects critical rules into subagent prompts.
|
|
61
71
|
|
|
72
|
+
### 🚨 Opus 4.7 Crisis (April 2026)
|
|
73
|
+
|
|
74
|
+
Opus 4.7 broke auto mode's safety classifier — it was [hardcoded to Opus 4.6](https://github.com/anthropics/claude-code/issues/49618). **If you use auto mode with Opus 4.7, dangerous commands run without the built-in safety check.** In 3 days: [50 GB permanently deleted](https://github.com/anthropics/claude-code/issues/49129), [~/.ssh wiped](https://github.com/anthropics/claude-code/issues/49554), [git credentials destroyed](https://github.com/anthropics/claude-code/issues/49539), [shell configs truncated to 0 bytes](https://github.com/anthropics/claude-code/issues/49615). Users report [4x token consumption](https://github.com/anthropics/claude-code/issues/49541) from silent model switches.
|
|
75
|
+
|
|
76
|
+
**One command to fix it:**
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
npx cc-safe-setup --opus47
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Installs 4 hooks targeting known Opus 4.7 regressions. [Full details →](https://yurukusa.github.io/cc-safe-setup/opus-47-survival-guide.html) · [Emergency Defense Kit (Gist)](https://gist.github.com/yurukusa/6747ea655cc5c374a1ec9ed4fba027e4) · [Safety Scanner](https://yurukusa.github.io/cc-safe-setup/opus47-scanner.html)
|
|
83
|
+
|
|
62
84
|
## What Gets Installed
|
|
63
85
|
|
|
64
86
|
| Hook | Prevents | Related Issues |
|
|
65
87
|
|------|----------|----------------|
|
|
66
|
-
| **Destructive Guard** | `rm -rf /`, `git reset --hard`, `git clean -fd`, `git checkout --force`, `sudo` + destructive, PowerShell `Remove-Item -Recurse -Force`, `rd /s /q`, NFS mount detection | [#36339](https://github.com/anthropics/claude-code/issues/36339) [#36640](https://github.com/anthropics/claude-code/issues/36640) [#37331](https://github.com/anthropics/claude-code/issues/37331) |
|
|
88
|
+
| **Destructive Guard** | `rm -rf /`, `git reset --hard`, `git clean -fd`, `git checkout --force`, `sudo` + destructive, PowerShell `Remove-Item -Recurse -Force`, `rd /s /q`, NFS mount detection | [#46058](https://github.com/anthropics/claude-code/issues/46058) [#36339](https://github.com/anthropics/claude-code/issues/36339) [#36640](https://github.com/anthropics/claude-code/issues/36640) [#37331](https://github.com/anthropics/claude-code/issues/37331) |
|
|
67
89
|
| **Branch Guard** | Pushes to main/master + force-push (`--force`) on all branches | |
|
|
68
90
|
| **Secret Guard** | `git add .env`, credential files, `git add .` with .env present | [#6527](https://github.com/anthropics/claude-code/issues/6527) |
|
|
69
91
|
| **Syntax Check** | Python, Shell, JSON, YAML, JS errors after edits | |
|
|
@@ -72,10 +94,25 @@ Claude Code ships with no safety hooks by default. This tool fixes that.
|
|
|
72
94
|
| **cd+git Auto-Approver** | Permission prompt spam for `cd /path && git log` | [#32985](https://github.com/anthropics/claude-code/issues/32985) [#16561](https://github.com/anthropics/claude-code/issues/16561) |
|
|
73
95
|
| **API Error Alert** | Silent session death from rate limits or API errors — desktop notification + log | |
|
|
74
96
|
|
|
75
|
-
> 📘 Tokens disappearing too fast? [The practical guide](https://zenn.dev/yurukusa/books/6076c23b1cb18b) covers 10 token consumption patterns (cache corruption, excessive reads, compact cycles) and how to detect them — from 700+ hours of autonomous operation. Chapter 3 free.
|
|
76
|
-
|
|
77
97
|
Each hook exists because a real incident happened without it.
|
|
78
98
|
|
|
99
|
+
### Free diagnostic tools
|
|
100
|
+
|
|
101
|
+
| Tool | What it does |
|
|
102
|
+
|------|-------------|
|
|
103
|
+
| **[Token Checkup](https://yurukusa.github.io/cc-safe-setup/token-checkup.html)** | 5 questions → find where your tokens are going (30 seconds) |
|
|
104
|
+
| **[Security Checkup](https://yurukusa.github.io/cc-safe-setup/security-checkup.html)** | 6 questions based on real incidents ($1,800+ in losses) |
|
|
105
|
+
| **[Version Check](https://yurukusa.github.io/cc-safe-setup/version-check.html)** | Is your CC version affected by cache inflation? |
|
|
106
|
+
|
|
107
|
+
### Go deeper
|
|
108
|
+
|
|
109
|
+
| Resource | What you get | Price |
|
|
110
|
+
|----------|-------------|-------|
|
|
111
|
+
| **[Token Book](https://yurukusa.github.io/cc-safe-setup/token-book.html)** | Cut token consumption in half. CLAUDE.md templates, hook configs, context management, 32 failure patterns with fixes. 44,000+ words from 800+ hours of real operation data. | ¥2,500 (~$17). Ch.1 free |
|
|
112
|
+
| **[Safety Guide](https://zenn.dev/yurukusa/books/6076c23b1cb18b)** | End-to-end Claude Code safety setup. From first install to overnight autonomous runs. | ¥800 (~$5). Ch.3 free |
|
|
113
|
+
|
|
114
|
+
**Why pay?** A Max plan costs $200/month. One token waste incident burns 50–80% of your weekly quota in hours ([#46727](https://github.com/anthropics/claude-code/issues/46727)). One `rm -rf` incident costs days of recovery. The Token Book costs less than 2 hours of Max subscription time — and the CLAUDE.md templates alone can reduce consumption by 40%.
|
|
115
|
+
|
|
79
116
|
### v2.1.85: `if` Field Support
|
|
80
117
|
|
|
81
118
|
Hooks now support an `if` field for conditional execution. The hook process only spawns when the command matches the pattern — `ls` won't trigger a git-only hook.
|
|
@@ -153,7 +190,7 @@ Guards against issues that corrupt sessions or waste tokens silently.
|
|
|
153
190
|
| `--scan [--apply]` | Tech stack detection |
|
|
154
191
|
| `--export / --import` | Team config sharing |
|
|
155
192
|
| `--verify` | Test each hook |
|
|
156
|
-
| `--install-example <name>` | Install from
|
|
193
|
+
| `--install-example <name>` | Install from 701 examples |
|
|
157
194
|
| `--examples [filter]` | Browse examples by keyword |
|
|
158
195
|
| `--full` | All-in-one setup |
|
|
159
196
|
| `--status` | Check installed hooks |
|
|
@@ -211,6 +248,23 @@ Guards against issues that corrupt sessions or waste tokens silently.
|
|
|
211
248
|
| Maximum protection mode | `npx cc-safe-setup --safe-mode` |
|
|
212
249
|
| Migrate from Cursor/Windsurf | [Migration Guide](https://yurukusa.github.io/cc-safe-setup/migration-guide.html) |
|
|
213
250
|
|
|
251
|
+
## Plugin Marketplace
|
|
252
|
+
|
|
253
|
+
Install safety hooks as Claude Code plugins — no npm required:
|
|
254
|
+
|
|
255
|
+
```bash
|
|
256
|
+
/plugin marketplace add yurukusa/cc-safe-setup
|
|
257
|
+
/plugin install safety-essentials@cc-safe-setup
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
| Plugin | What it blocks |
|
|
261
|
+
|---|---|
|
|
262
|
+
| `safety-essentials` | rm -rf, force-push, hard-reset, .env overwrite, npm publish |
|
|
263
|
+
| `git-protection` | Force-push, main/master push, git clean, branch -D |
|
|
264
|
+
| `credential-guard` | .env write/edit, API keys in commands, service account files |
|
|
265
|
+
|
|
266
|
+
Also listed on [claudemarketplaces.com](https://claudemarketplaces.com).
|
|
267
|
+
|
|
214
268
|
## Common Pain Points (from GitHub Issues)
|
|
215
269
|
|
|
216
270
|
| Problem | Issue | Fix |
|
|
@@ -295,6 +349,8 @@ cc-safe-setup gives you 8 essential hooks. Want to know what else your setup nee
|
|
|
295
349
|
|
|
296
350
|
Run `npx cc-health-check` (free, 20 checks) to see your current score. If it's below 80, the **[Claude Code Ops Kit](https://yurukusa.github.io/cc-ops-kit-landing/?utm_source=github&utm_medium=readme&utm_campaign=safe-setup)** fills the gaps — 6 hooks + 5 templates + 9 scripts + install.sh. Pay What You Want ($0+).
|
|
297
351
|
|
|
352
|
+
**Starter Kit:** Want hooks + settings + templates in one download? The **[Claude Code Safety Kit](https://yurukusa.itch.io/claude-code-safety-kit)** bundles 5 safety hooks, a pre-configured settings.json, CLAUDE.md templates, and 800-hour operation tips. Name your price ($0+).
|
|
353
|
+
|
|
298
354
|
Or browse the free hooks: [claude-code-hooks](https://github.com/yurukusa/claude-code-hooks)
|
|
299
355
|
|
|
300
356
|
## Examples
|
|
@@ -362,7 +418,7 @@ Or browse all available examples in [`examples/`](examples/):
|
|
|
362
418
|
- **edit-guard.sh** — Block Edit/Write to protected files (defense-in-depth for [#37210](https://github.com/anthropics/claude-code/issues/37210))
|
|
363
419
|
- **auto-approve-build.sh** — Auto-approve npm/yarn/cargo/go/python build, test, and lint commands
|
|
364
420
|
- **auto-approve-docker.sh** — Auto-approve docker build, compose, ps, logs, and other safe commands
|
|
365
|
-
- **block-database-wipe.sh** — Block destructive database commands: Laravel `migrate:fresh`, Django `flush`, Rails `db:drop`, raw `DROP DATABASE` ([#37405](https://github.com/anthropics/claude-code/issues/37405) [#37439](https://github.com/anthropics/claude-code/issues/37439))
|
|
421
|
+
- **block-database-wipe.sh** — Block destructive database commands: Laravel `migrate:fresh`, Django `flush`, Rails `db:drop`, raw `DROP DATABASE` ([#46684](https://github.com/anthropics/claude-code/issues/46684) [#46650](https://github.com/anthropics/claude-code/issues/46650) [#37405](https://github.com/anthropics/claude-code/issues/37405) [#37439](https://github.com/anthropics/claude-code/issues/37439))
|
|
366
422
|
- **auto-approve-python.sh** — Auto-approve pytest, mypy, ruff, black, isort, flake8, pylint commands
|
|
367
423
|
- **auto-snapshot.sh** — Auto-save file snapshots before edits for rollback protection ([#37386](https://github.com/anthropics/claude-code/issues/37386) [#37457](https://github.com/anthropics/claude-code/issues/37457))
|
|
368
424
|
- **allowlist.sh** — Block everything not explicitly approved — inverse permission model ([#37471](https://github.com/anthropics/claude-code/issues/37471))
|
|
@@ -439,13 +495,18 @@ See [Issue #1](https://github.com/yurukusa/cc-safe-setup/issues/1) for details.
|
|
|
439
495
|
|
|
440
496
|
## Learn More
|
|
441
497
|
|
|
442
|
-
- **[
|
|
498
|
+
- **[Opus 4.7 Survival Guide](https://yurukusa.github.io/cc-safe-setup/opus-47-survival-guide.html)** — 61 known issues (76+ GitHub Issues + CVEs) with fixes: data loss, recursive spawn DoS, billing mismatch, subagent OOM, cache_read anomaly, allowedTools bypass, 1.7x token inflation, classifier failure, thinking summary bugs, 30-min stalls, enterprise hooks bypass, and more. [`npx cc-safe-setup --opus47`](#-opus-47-crisis-april-2026)
|
|
499
|
+
- **[Token Book (¥2,500)](https://zenn.dev/yurukusa/books/token-savings-guide)** — Cut token consumption in half. CLAUDE.md optimization, hook-based guards, context management, workflow design. 44,000 words with copy-paste templates. Intro + Ch.1 free. [Details](https://yurukusa.github.io/cc-safe-setup/token-book.html)
|
|
500
|
+
- **[Safety Guide (¥800)](https://zenn.dev/yurukusa/books/6076c23b1cb18b)** — Token consumption diagnosis, file loss prevention, autonomous operation safety. From 800+ hours of real incidents. [Chapter 3 free](https://zenn.dev/yurukusa/books/6076c23b1cb18b/viewer/3-code-quality)
|
|
501
|
+
- **[800 Hours Operation Record (¥800)](https://zenn.dev/yurukusa/books/3c3c3baee85f0a19)** — Non-engineer running Claude Code autonomously for 800 hours. Failures, recovery, revenue reality. [Chapter 2 free](https://zenn.dev/yurukusa/books/3c3c3baee85f0a19/viewer/2-first-failures)
|
|
502
|
+
- **Wiki Guides**: [Token FAQ](https://github.com/yurukusa/cc-safe-setup/wiki/Claude-Code-Token-FAQ) · [CLAUDE.md Best Practices](https://github.com/yurukusa/cc-safe-setup/wiki/CLAUDE-md-Best-Practices) · [Token Optimization](https://github.com/yurukusa/cc-safe-setup/wiki/Token-Optimization-Guide)
|
|
443
503
|
- [Cookbook](COOKBOOK.md) — 26 practical recipes (block, approve, protect, monitor, diagnose)
|
|
444
504
|
- [Official Hooks Reference](https://code.claude.com/docs/en/hooks) — Claude Code hooks documentation
|
|
445
505
|
- [Hooks Cookbook](https://github.com/yurukusa/claude-code-hooks/blob/main/COOKBOOK.md) — 25 recipes from real GitHub Issues ([interactive version](https://yurukusa.github.io/claude-code-hooks/))
|
|
446
506
|
- [Skills Guide deep-dive (Qiita, 19K+ views)](https://qiita.com/yurukusa/items/f69920b4a02cf7e2988c) — Anthropic's official Skills PDF analyzed with 40% token reduction
|
|
447
507
|
- [Japanese guide (Qiita)](https://qiita.com/yurukusa/items/a9714b33f5d974e8f1e8) — この記事の日本語解説
|
|
448
508
|
- [v2.1.85 `if` field guide (Qiita)](https://qiita.com/yurukusa/items/7079866e9dc239fcdd57) — Reduce hook overhead with conditional execution
|
|
509
|
+
- [Deny rules bypass vulnerability (Qiita)](https://qiita.com/yurukusa/items/f9c48bb44569bbf4492e) — 50+ subcommands disable all deny rules; hook-based defense
|
|
449
510
|
- [Hook Test Runner](https://github.com/yurukusa/cc-hook-test) — `npx cc-hook-test <hook.sh>` to auto-test any hook
|
|
450
511
|
- [Hook Registry](https://github.com/yurukusa/cc-hook-registry) — `npx cc-hook-registry search database` ([browse online](https://yurukusa.github.io/cc-hook-registry/))
|
|
451
512
|
- [Hooks Cheat Sheet](https://yurukusa.github.io/cc-safe-setup/cheatsheet.html) — printable A4 quick reference
|
|
@@ -455,6 +516,46 @@ See [Issue #1](https://github.com/yurukusa/cc-safe-setup/issues/1) for details.
|
|
|
455
516
|
- [How to prevent force-push to main](https://yurukusa.github.io/cc-safe-setup/prevent-force-push.html) — branch protection via hooks
|
|
456
517
|
- [How to prevent secret leaks](https://yurukusa.github.io/cc-safe-setup/prevent-secret-leaks.html) — stop git add . from committing .env
|
|
457
518
|
|
|
519
|
+
### Free Gists
|
|
520
|
+
|
|
521
|
+
- [settings.json Complete Template](https://gist.github.com/yurukusa/8ec367cf65042bf9fbd83c35931e7ed1) — copy-paste ready safety configuration
|
|
522
|
+
- [First 3 Safety Steps](https://gist.github.com/yurukusa/72513272be9a4ee29b058e2b08453e1a) — 5-minute safety setup from scratch
|
|
523
|
+
- [CLAUDE.md Before/After](https://gist.github.com/yurukusa/f9d7df5930bfb6d36a25673e69720f7e) — 40% token reduction through better writing patterns
|
|
524
|
+
- [Token Savings Cheat Card](https://gist.github.com/yurukusa/cfe44bfbb3756eccaf51660466913a2d) — 5 techniques to cut consumption in half
|
|
525
|
+
- [Token Consumption Checklist](https://gist.github.com/yurukusa/db8700a9f9fa331d36664df2868274cb) — 10-item diagnostic
|
|
526
|
+
- [Outage Survival Kit](https://gist.github.com/yurukusa/a0e31171eecb527d0df1d5498bf5f5d0) — what to do when Claude Code is down
|
|
527
|
+
- [CLAUDE.md Token Optimizer](https://gist.github.com/yurukusa/2b98fd2e90c0c13f6918c9f915e08e27) — 35-line template, 40% token reduction (800h tested)
|
|
528
|
+
- [Worktree Safety Hooks](https://gist.github.com/yurukusa/98bd43c5d0d8a6ebbf2cf21bfc1e2907) — 3 hooks to protect against worktree deletion and cross-tree destruction
|
|
529
|
+
- [Opus 4.7 Emergency Checklist](https://gist.github.com/yurukusa/c95efaee4b670e067369ece08092960c) — token burn diagnosis + immediate fixes
|
|
530
|
+
- [Cache TTL Mitigation Guide](https://gist.github.com/yurukusa/178d3949cd2bd6fbfc275b408f9711d4) — #46829 cache TTL change (1h→5m) impact and 4 mitigations
|
|
531
|
+
- [Security Checkup Hooks](https://gist.github.com/yurukusa/81f79ae6d760b27c17f2cd642ea846d7) — 4 hooks for financial, PII, deny bypass, and background task protection
|
|
532
|
+
- [Cache Breakage Fix](https://gist.github.com/yurukusa/fe6ba0a6aee14207f27ecc84419878b4) — 2 root causes of prompt cache invalidation (#47107 git status, #47098 session restart)
|
|
533
|
+
- [CLAUDE.md Token Optimization Cheat Sheet](https://gist.github.com/yurukusa/556f67c493a2729ce9b1703f5003a227) — 5 CLAUDE.md patterns that reduce token consumption with before/after examples
|
|
534
|
+
- [Token Troubleshooting Guide](https://gist.github.com/yurukusa/47b8c3eadb77cf74946f450f992ddac2) — fix quota drain, cache bugs, 1M context trap. Symptom-based diagnosis with latest issue references
|
|
535
|
+
- [Token Optimization Guide (English)](https://gist.github.com/yurukusa/70ff830c0ad3dff83e53be26cd80bd0a) — 3 biggest token levers with hook code, practical walkthrough
|
|
536
|
+
- [Token Book Sampler: 5 Techniques](https://gist.github.com/yurukusa/4a867ba301b480f996c5b76e4b6a6fbc) — free preview of the Token Book — 5 immediate techniques to reduce consumption
|
|
537
|
+
- [Token Optimization Checklist](https://gist.github.com/yurukusa/4b75025beee916f9904f56b79eeb1217) — 10-step checklist to cut token consumption in half, with hook configs
|
|
538
|
+
- [3 Things That Actually Work](https://gist.github.com/yurukusa/621f6d1cc35816df3da2e07876b44e16) — CLAUDE.md sizing, cache TTL, subagent control — based on 800h data
|
|
539
|
+
- [Cache TTL Diagnostic](https://gist.github.com/yurukusa/3a5bdcfdd295bef17b3ee00978b299f2) — 3 patterns that break prompt cache + fixes
|
|
540
|
+
- [Token Book Ch.1 Free Preview](https://gist.github.com/yurukusa/de862573f18d1a0a68d411b696dbcb73) — Where are your Claude Code tokens going? The 4 layers of token consumption explained
|
|
541
|
+
- [Deny Rules Break After 50 Subcommands](https://gist.github.com/yurukusa/0463d240d7b725218289a556414c72a5) — the hook that fixes Claude Code's deny rule bypass vulnerability
|
|
542
|
+
- [Opus 4.7 Emergency Kit](https://gist.github.com/yurukusa/1970b20fed95a682b72eb6e857e61d30) — 5 commands to protect your data from Opus 4.7 regressions (auto mode broken, 23+ data loss incidents)
|
|
543
|
+
- [cache_read Billing Bug Guide](https://gist.github.com/yurukusa/d5dc731dbc69e3ca92d69832bed641cb) — Opus 4.7 cache_read billed at full rate. Anthropic confirmed. Max plan users losing quota 3-6x faster
|
|
544
|
+
- [Opus 4.7 Survival Guide Summary](https://gist.github.com/yurukusa/5d66f0bcfe3fbfc73e6db106e10c533d) — 50 known issues with quick reference table, free diagnostic tools, and one-command fix
|
|
545
|
+
- [Opus 4.7 Known Issues Quick Reference](https://gist.github.com/yurukusa/2c1effab34a7554130d2704fdac59dff) — 26 issues / 43+ GitHub bugs in one table. Severity ratings and direct issue links
|
|
546
|
+
- [4 New Critical Issues (April 18)](https://gist.github.com/yurukusa/37c19b5b7f50fd8bbbeda5e1336c352e) — DoS via recursive spawn, subagent OOM, billing mismatch, UI/CLI model mismatch
|
|
547
|
+
- [トークン消費を半分にする方法](https://gist.github.com/yurukusa/bf4040a905148d9ca02898a53185fae1) — 800時間の実測データ+設定テンプレート(日本語)
|
|
548
|
+
- [How to Cut Token Usage in Half](https://gist.github.com/yurukusa/704d5cf9874f553dad5c46fccf53b09f) — 800h real data + config templates (English)
|
|
549
|
+
- [Compaction Triple Threat](https://gist.github.com/yurukusa/aa15f2065199c6fac4dcd3796fbaf90f) — 3 compaction bugs active simultaneously (#50402 + #50467 + #50492)
|
|
550
|
+
- [Sandbox Relative Path Bug (CRITICAL)](https://gist.github.com/yurukusa/a98efb6c561f92c82bcd49125af3b32a) — denyWrite/denyRead silently ignores relative paths (#50454)
|
|
551
|
+
- [27 Token Symptoms Quick Reference](https://gist.github.com/yurukusa/03a379854fa0f8eca091a75f7aab593b) — all 27 known token failure modes with top 5 killers table and April 2026 new symptoms
|
|
552
|
+
- [Token Saving Checklist (15 Items)](https://gist.github.com/yurukusa/6bd0d0a38a4887fc36475dd1f765ecd1) — ordered by impact: critical (30-50%), important (10-20%), good practice (5-10%)
|
|
553
|
+
- [Opus 4.7 Survival Cheatsheet](https://gist.github.com/yurukusa/f2d6e261338eeda70f0ed9507f995c13) — 46 known problems, quick fixes under 60 seconds, full reference table
|
|
554
|
+
|
|
555
|
+
### Professional Services
|
|
556
|
+
|
|
557
|
+
Need help configuring Claude Code safely? [**Safety Setup Service**](https://yurukusa.github.io/cc-safe-setup/services.html) — audit, token optimization, and custom hooks by the cc-safe-setup team.
|
|
558
|
+
|
|
458
559
|
## FAQ
|
|
459
560
|
|
|
460
561
|
**Q: I installed hooks but Claude says "Unknown skill: claude-code-hooks:setup"**
|
|
@@ -481,6 +582,14 @@ Since v2.1.78, protected directories always prompt regardless of permission mode
|
|
|
481
582
|
|
|
482
583
|
`allow` takes precedence over `ask`. If you allow all Bash, ask rules are ignored ([#6527](https://github.com/anthropics/claude-code/issues/6527)). Use PreToolUse hooks to block dangerous commands instead of relying on the ask/allow priority system.
|
|
483
584
|
|
|
585
|
+
**Q: Hooks silently fail on macOS (Homebrew `jq` not found)**
|
|
586
|
+
|
|
587
|
+
Claude Code runs hooks with a restricted PATH that excludes `/opt/homebrew/bin` ([#46954](https://github.com/anthropics/claude-code/issues/46954)). If `jq` is installed via Homebrew, hooks silently exit 0. Fix: add `export PATH="/opt/homebrew/bin:$PATH"` at the top of your hook script, or use absolute paths like `/opt/homebrew/bin/jq`. Inline hooks in `settings.json` may also be affected — add a PATH export prefix: `export PATH="/opt/homebrew/bin:$PATH"; INPUT=$(cat); ...`
|
|
588
|
+
|
|
589
|
+
**Q: How is this different from [claude-token-efficient](https://github.com/drona23/claude-token-efficient)?**
|
|
590
|
+
|
|
591
|
+
Different goals. claude-token-efficient optimizes CLAUDE.md to make Claude's responses shorter and cheaper. cc-safe-setup prevents dangerous operations (file deletion, credential leaks, force-push). They work well together: use claude-token-efficient for cost reduction, cc-safe-setup for safety. For comprehensive token optimization beyond CLAUDE.md (hooks, context management, workflow design), see the [Token Book](https://yurukusa.github.io/cc-safe-setup/token-book.html).
|
|
592
|
+
|
|
484
593
|
**Still stuck?** See the full [Permission Troubleshooting Flowchart](https://gist.github.com/yurukusa/b64217ffcb908fa309dbfcfa368cd84d) for step-by-step diagnosis.
|
|
485
594
|
|
|
486
595
|
## Contributing
|
|
@@ -496,6 +605,8 @@ Since v2.1.78, protected directories always prompt regardless of permission mode
|
|
|
496
605
|
|
|
497
606
|
**Share your experience:** Used cc-safe-setup and have feedback? Open a discussion or comment on any issue. We read everything.
|
|
498
607
|
|
|
608
|
+
If cc-safe-setup saved you from a disaster (or just saved you time), a ⭐ helps others find it too.
|
|
609
|
+
|
|
499
610
|
## Also by yurukusa
|
|
500
611
|
|
|
501
612
|
- [quiet life](https://yurukusa.github.io/quiet-life/) — Touch the dark. Something alive appears
|
package/SETTINGS_REFERENCE.md
CHANGED
|
@@ -280,3 +280,5 @@ Run `npx cc-safe-setup --doctor` for automated diagnosis.
|
|
|
280
280
|
- [COOKBOOK.md](https://github.com/yurukusa/claude-code-hooks/blob/main/COOKBOOK.md) — 20 hook recipes
|
|
281
281
|
- [Migration Guide](MIGRATION.md) — from permissions to hooks
|
|
282
282
|
- [Ecosystem Comparison](https://yurukusa.github.io/cc-safe-setup/ecosystem.html) — all hook projects
|
|
283
|
+
- [Token Checkup](https://yurukusa.github.io/cc-safe-setup/token-checkup.html) — free 30-second token diagnostic
|
|
284
|
+
- [Token Book](https://zenn.dev/yurukusa/books/token-savings-guide) — cut your token consumption in half (¥2,500, chapter 1 free)
|
package/SKILL.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: cc-safe-setup
|
|
3
|
+
description: Safety hooks for Claude Code — 700 pre-built hooks that prevent file deletion, credential leaks, git disasters, and token waste during autonomous AI coding sessions. 9,200+ tests. Install with npx cc-safe-setup.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# cc-safe-setup
|
|
7
|
+
|
|
8
|
+
Safety-first configuration for Claude Code. Prevents the accidents that happen when AI writes code autonomously.
|
|
9
|
+
|
|
10
|
+
## What it does
|
|
11
|
+
|
|
12
|
+
Installs pre-built safety hooks into your Claude Code environment. These hooks run automatically before/after tool calls to block dangerous operations.
|
|
13
|
+
|
|
14
|
+
**Categories:**
|
|
15
|
+
- **File protection**: Block `rm -rf`, prevent overwriting files outside project
|
|
16
|
+
- **Git safety**: Prevent force-push to main, block `reset --hard`
|
|
17
|
+
- **Credential guards**: Stop `.env` files from being committed or read by AI
|
|
18
|
+
- **Token optimization**: Warn on large file reads, limit subagent spawning
|
|
19
|
+
- **Quality gates**: Detect lazy rewrites, verify claims before committing
|
|
20
|
+
|
|
21
|
+
## Quick start
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npx cc-safe-setup
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
This runs an interactive wizard that configures hooks based on your risk profile.
|
|
28
|
+
|
|
29
|
+
## Install individual hooks
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
npx cc-safe-setup --install-example large-read-guard
|
|
33
|
+
npx cc-safe-setup --install-example prevent-rm-rf
|
|
34
|
+
npx cc-safe-setup --install-example git-force-push-block
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Why hooks instead of CLAUDE.md rules
|
|
38
|
+
|
|
39
|
+
Rules in CLAUDE.md are suggestions — Claude can forget them. Hooks are enforced at the system level. A hook that blocks `rm -rf` cannot be overridden by the AI.
|
|
40
|
+
|
|
41
|
+
From 800+ hours of autonomous operation: the hooks that matter most are the ones you don't notice until something goes wrong.
|
|
42
|
+
|
|
43
|
+
## Resources
|
|
44
|
+
|
|
45
|
+
- Repository: https://github.com/yurukusa/cc-safe-setup
|
|
46
|
+
- Hook Selector (find hooks for your setup): https://yurukusa.github.io/cc-safe-setup/hook-selector.html
|
|
47
|
+
- Token Checkup (diagnose waste): https://yurukusa.github.io/cc-safe-setup/token-checkup.html
|
package/examples/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Example Hooks
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
675 installable hooks. Each solves a real problem from GitHub Issues or autonomous operation. 9,200+ tests.
|
|
4
4
|
|
|
5
5
|
```bash
|
|
6
6
|
npx cc-safe-setup --install-example <name> # install one
|
|
@@ -39,6 +39,16 @@ npx cc-safe-setup --shield # install recommended set
|
|
|
39
39
|
- [OWASP MCP Top 10 Defense](https://yurukusa.github.io/cc-safe-setup/owasp-mcp-hooks.html)
|
|
40
40
|
- [COOKBOOK](../COOKBOOK.md)
|
|
41
41
|
|
|
42
|
+
## Token Optimization
|
|
43
|
+
|
|
44
|
+
Using too many tokens? These hooks help monitor and reduce consumption:
|
|
45
|
+
|
|
46
|
+
- **`token-budget-guard`** — Alert when session exceeds token budget
|
|
47
|
+
- **`large-read-guard`** — Block reading files over 1000 lines
|
|
48
|
+
- **`context-monitor`** — Track context window usage
|
|
49
|
+
|
|
50
|
+
For a complete guide: [Token Book](https://zenn.dev/yurukusa/books/token-savings-guide) — cut token consumption in half with templates and measured data (¥2,500, chapter 1 free). Or try the [free diagnostic](https://yurukusa.github.io/cc-safe-setup/token-checkup.html).
|
|
51
|
+
|
|
42
52
|
## Write Your Own
|
|
43
53
|
|
|
44
54
|
See [CONTRIBUTING.md](../CONTRIBUTING.md).
|
|
@@ -15,6 +15,9 @@
|
|
|
15
15
|
# See: https://github.com/anthropics/claude-code/issues/30519
|
|
16
16
|
# See: https://github.com/anthropics/claude-code/issues/16561
|
|
17
17
|
#
|
|
18
|
+
# Auto-approve hooks reduce token waste from permission prompts.
|
|
19
|
+
# For more token optimization patterns: https://zenn.dev/yurukusa/books/token-savings-guide
|
|
20
|
+
#
|
|
18
21
|
# TRIGGER: PermissionRequest MATCHER: ""
|
|
19
22
|
|
|
20
23
|
INPUT=$(cat)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# auto-compact-context-monitor.sh — Detect unexpected auto-compaction via context size drops
|
|
3
|
+
#
|
|
4
|
+
# PreCompact hooks do NOT fire on auto-compaction (only on manual /compact).
|
|
5
|
+
# This PostToolUse hook monitors for sudden context size drops that indicate
|
|
6
|
+
# auto-compaction occurred without PreCompact firing.
|
|
7
|
+
#
|
|
8
|
+
# Born from: https://github.com/anthropics/claude-code/issues/50467
|
|
9
|
+
# Related: https://github.com/anthropics/claude-code/issues/50492 (24% early fire)
|
|
10
|
+
#
|
|
11
|
+
# TRIGGER: PostToolUse MATCHER: ""
|
|
12
|
+
# Runs after every tool use to track context size changes.
|
|
13
|
+
|
|
14
|
+
INPUT=$(cat)
|
|
15
|
+
|
|
16
|
+
# Track context tokens (approximate via tool input size)
|
|
17
|
+
MONITOR_FILE="/tmp/cc-context-monitor-$$"
|
|
18
|
+
CURRENT_SIZE=$(echo "$INPUT" | wc -c)
|
|
19
|
+
|
|
20
|
+
if [ -f "$MONITOR_FILE" ]; then
|
|
21
|
+
PREV_SIZE=$(cat "$MONITOR_FILE")
|
|
22
|
+
# If current input is significantly smaller than previous (>50% drop),
|
|
23
|
+
# auto-compaction likely occurred
|
|
24
|
+
if [ "$PREV_SIZE" -gt 1000 ] && [ "$CURRENT_SIZE" -gt 0 ]; then
|
|
25
|
+
RATIO=$((CURRENT_SIZE * 100 / PREV_SIZE))
|
|
26
|
+
if [ "$RATIO" -lt 30 ]; then
|
|
27
|
+
echo "⚠ AUTO-COMPACTION DETECTED: Context dropped ${RATIO}% (${PREV_SIZE}→${CURRENT_SIZE} bytes)" >&2
|
|
28
|
+
echo " PreCompact hooks did NOT fire for this compaction (#50467)" >&2
|
|
29
|
+
echo " Important context may have been lost. Verify key facts." >&2
|
|
30
|
+
fi
|
|
31
|
+
fi
|
|
32
|
+
fi
|
|
33
|
+
|
|
34
|
+
echo "$CURRENT_SIZE" > "$MONITOR_FILE"
|
|
35
|
+
exit 0
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# auto-mode-safety-enforcer.sh — Block dangerous operations in auto/acceptEdits mode
|
|
3
|
+
#
|
|
4
|
+
# Solves: Auto mode safety classifier hardcoded to opus-4-6, fails with Opus 4.7
|
|
5
|
+
# - #49618: Safety classifier doesn't work with non-opus-4-6 models
|
|
6
|
+
# - #49554: auto mode approved ~/.ssh deletion
|
|
7
|
+
# - #18740: Auto-allow mode data loss without warning
|
|
8
|
+
#
|
|
9
|
+
# How it works: PreToolUse hook on Bash that blocks destructive commands
|
|
10
|
+
# regardless of which model or permission mode is active. Acts as a
|
|
11
|
+
# user-space safety net when the built-in classifier fails.
|
|
12
|
+
#
|
|
13
|
+
# What it blocks:
|
|
14
|
+
# - rm -rf on non-safe paths (/, ~, .., /home, /etc, /usr, /var, .git)
|
|
15
|
+
# - Credential file deletion (.ssh, .git-credentials, .env, .npmrc)
|
|
16
|
+
# - dd/mkfs/fdisk (disk operations)
|
|
17
|
+
# - kill -9 on system processes
|
|
18
|
+
# - chmod 777 on sensitive paths
|
|
19
|
+
#
|
|
20
|
+
# TRIGGER: PreToolUse MATCHER: "Bash"
|
|
21
|
+
|
|
22
|
+
set -euo pipefail
|
|
23
|
+
|
|
24
|
+
INPUT=$(cat)
|
|
25
|
+
COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command // empty' 2>/dev/null)
|
|
26
|
+
[ -z "$COMMAND" ] && exit 0
|
|
27
|
+
|
|
28
|
+
# --- Critical rm operations ---
|
|
29
|
+
if echo "$COMMAND" | grep -qE '(^|\s|;|&&|\|)(sudo\s+)?rm\s'; then
|
|
30
|
+
# Always block rm on root-level and home-level critical paths
|
|
31
|
+
if echo "$COMMAND" | grep -qE 'rm\s.*(/\s|/;|/$|~\/?\s|~\/?$|~\/\.|/home\b|/etc\b|/usr\b|/var\b|/opt\b|/root\b)'; then
|
|
32
|
+
echo "BLOCKED: rm targeting critical system/home path" >&2
|
|
33
|
+
echo "This operation would cause irreversible data loss." >&2
|
|
34
|
+
echo "Command: $COMMAND" >&2
|
|
35
|
+
exit 2
|
|
36
|
+
fi
|
|
37
|
+
# Block rm on dotfiles in home directory
|
|
38
|
+
if echo "$COMMAND" | grep -qE "rm\s.*(${HOME}|\~)/\."; then
|
|
39
|
+
echo "BLOCKED: rm targeting home dotfile" >&2
|
|
40
|
+
echo "Command: $COMMAND" >&2
|
|
41
|
+
exit 2
|
|
42
|
+
fi
|
|
43
|
+
fi
|
|
44
|
+
|
|
45
|
+
# --- Disk-level operations ---
|
|
46
|
+
if echo "$COMMAND" | grep -qE '(^|\s)(sudo\s+)?(dd\s+.*of=/dev|mkfs\.|fdisk\s|parted\s)'; then
|
|
47
|
+
echo "BLOCKED: Disk-level operation (dd/mkfs/fdisk/parted)" >&2
|
|
48
|
+
exit 2
|
|
49
|
+
fi
|
|
50
|
+
|
|
51
|
+
# --- Kill system processes ---
|
|
52
|
+
if echo "$COMMAND" | grep -qE 'kill\s+(-9\s+)?1$|killall\s+(init|systemd)'; then
|
|
53
|
+
echo "BLOCKED: Killing system process" >&2
|
|
54
|
+
exit 2
|
|
55
|
+
fi
|
|
56
|
+
|
|
57
|
+
exit 0
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# background-task-guard.sh — Audit background Bash execution
|
|
3
|
+
#
|
|
4
|
+
# Solves: run_in_background:true on Bash tool skips the approval
|
|
5
|
+
# prompt, allowing dangerous commands to execute without user
|
|
6
|
+
# confirmation. (#46950)
|
|
7
|
+
#
|
|
8
|
+
# How it works: Checks if a Bash command is running in background
|
|
9
|
+
# mode. If the command matches dangerous patterns (destructive ops,
|
|
10
|
+
# network access, file deletion), blocks it. Background execution
|
|
11
|
+
# should only be used for safe, read-only operations.
|
|
12
|
+
#
|
|
13
|
+
# TRIGGER: PreToolUse
|
|
14
|
+
# MATCHER: "Bash"
|
|
15
|
+
|
|
16
|
+
set -euo pipefail
|
|
17
|
+
|
|
18
|
+
INPUT=$(cat)
|
|
19
|
+
CMD=$(echo "$INPUT" | jq -r '.tool_input.command // empty' 2>/dev/null)
|
|
20
|
+
[ -z "$CMD" ] && exit 0
|
|
21
|
+
|
|
22
|
+
# Check if this is a background execution
|
|
23
|
+
# Note: run_in_background is in tool_input for Bash
|
|
24
|
+
IS_BG=$(echo "$INPUT" | jq -r '.tool_input.run_in_background // false' 2>/dev/null)
|
|
25
|
+
[ "$IS_BG" != "true" ] && exit 0
|
|
26
|
+
|
|
27
|
+
# Background execution detected — apply strict safety rules
|
|
28
|
+
# Only allow read-only commands in background
|
|
29
|
+
|
|
30
|
+
# Block destructive operations
|
|
31
|
+
if echo "$CMD" | grep -qiE '\brm\s+-rf\b|\bgit\s+(push|reset|clean|checkout\s+--)\b|\bchmod\b|\bchown\b'; then
|
|
32
|
+
echo "BLOCKED: Destructive command not allowed in background mode." >&2
|
|
33
|
+
echo " Background tasks skip approval prompts — run this in foreground." >&2
|
|
34
|
+
exit 2
|
|
35
|
+
fi
|
|
36
|
+
|
|
37
|
+
# Block network writes
|
|
38
|
+
if echo "$CMD" | grep -qiE 'curl\s+.*-X\s*(POST|PUT|PATCH|DELETE)|curl\s+.*--data|curl\s+.*-d\s|wget\s+.*--post'; then
|
|
39
|
+
echo "BLOCKED: Network write operation not allowed in background mode." >&2
|
|
40
|
+
echo " Background tasks skip approval prompts — run this in foreground." >&2
|
|
41
|
+
exit 2
|
|
42
|
+
fi
|
|
43
|
+
|
|
44
|
+
# Block file writes to sensitive locations
|
|
45
|
+
if echo "$CMD" | grep -qiE '>\s*(/etc/|/usr/|/var/|~/.ssh/|~/.gnupg/|~/.claude/settings)'; then
|
|
46
|
+
echo "BLOCKED: Write to sensitive path not allowed in background mode." >&2
|
|
47
|
+
echo " Background tasks skip approval prompts — run this in foreground." >&2
|
|
48
|
+
exit 2
|
|
49
|
+
fi
|
|
50
|
+
|
|
51
|
+
# Block process killing
|
|
52
|
+
if echo "$CMD" | grep -qiE '\bkill\b|\bkillall\b|\bpkill\b'; then
|
|
53
|
+
echo "BLOCKED: Process termination not allowed in background mode." >&2
|
|
54
|
+
exit 2
|
|
55
|
+
fi
|
|
56
|
+
|
|
57
|
+
exit 0
|