claude-code-hardened
π‘οΈ Stop your AI from pushing secrets to GitHub. Security hooks, battle-tested rules, and a live validator for Claude Code. 5 hooks Β· 6 rules Β· 1 install script Β· 0 dependencies.
π‘οΈ claude-code-hardened
Stop your AI from pushing secrets to GitHub.
Claude Code is powerful. It's also an unsupervised intern with root access. This repo adds the guardrails that should have been there from day one.
5 security hooks Β· 6 battle-tested rules Β· 1 install script Β· 0 dependencies
Quick Start Β· What It Catches Β· Hooks Β· Rules Β· Validate
The Problem
Claude Code can:
- Push API keys to public repos (it will, if you don't stop it)
- Skip pre-commit hooks with
--no-verifyto "save time" - Force-push to main and destroy your commit history
- Overwrite your landing page while "improving" a CSS class
- Commit database URLs, private IPs, and AWS credentials
These aren't hypothetical. They happened. This repo exists because of them.
π Quick Start
git clone https://github.com/renefichtmueller/claude-code-hardened.git
cd claude-code-hardened
bash install.sh
That's it. Restart Claude Code. You're hardened.
Manual Install (if you prefer)
# Copy hooks
cp hooks/*.sh ~/.claude/hooks/
chmod +x ~/.claude/hooks/*.sh
# Copy rules
cp -r rules/common ~/.claude/rules/common
# Add hooks to settings.json (see examples/settings-hooks.json)
π What It Catches
Run the validator on any machine to see your security posture:
bash scripts/validate.sh
Output:
βββββββββββββββββββββββββββββββββββββββββββββββββββββ
β claude-code-hardened validation β
βββββββββββββββββββββββββββββββββββββββββββββββββββββ
Hooks:
β
block-no-verify hook installed
β
pre-push-secrets-scan hook installed
β
protect-critical-files hook installed
β
enforce-branch-policy hook installed
Settings:
β
settings.json exists
β
hooks configured in settings.json
β
block-no-verify registered in settings
β
secrets scan registered in settings
Rules:
β
6 rules installed in common/
β
security rules installed
Dangerous patterns:
β
No permission bypass flags
β
No .env in home directory
βββββββββββββββββββββββββββββββββββββββββββββββββ
Results: 12 passed, 0 failed, 0 warnings (12 checks)
Status: HARDENED β
π‘οΈ Hooks
block-no-verify.sh β PreToolUse
Blocks --no-verify and --no-gpg-sign flags. AI agents love skipping hooks to avoid lint errors. This forces them to fix the actual issue.
pre-push-secrets-scan.sh β PreToolUse
Triple-layer scan before any git push:
| Scan | What | Examples |
|---|---|---|
| Secrets | API keys, tokens, passwords | api_key = "sk-...", auth_token: "ghp_..." |
| Private Network | RFC 1918 addresses | 192.168.1.1, 10.0.0.1, 172.16.x.x |
| Database URLs | Connection strings | postgres://user:pass@host, DATABASE_URL=... |
Exits with code 2 (blocks the push) if any scan fails.
enforce-branch-policy.sh β PreToolUse
- Blocks force-push to main/master
- Blocks
git reset --hardon main - Warns on direct push to main (suggests PR workflow)
protect-critical-files.sh β PostToolUse
Warns when Claude edits files that shouldn't change casually:
index.html,package.json,docker-compose.ymlDockerfile,.env,wrangler.toml- Database migrations, Prisma schema, CI/CD workflows
Customize the PROTECTED_PATTERNS array for your project.
post-edit-lint-reminder.sh β PostToolUse
After file edits, reminds about language-specific formatting:
- TypeScript/JS β prettier/biome + eslint
- Python β ruff format + ruff check
- Go β gofmt + go vet
- Rust β cargo fmt + cargo clippy
π Rules
Battle-tested from 200+ Claude Code sessions across production projects:
| Rule | What It Enforces |
|---|---|
| coding-style.md | Immutability, file size limits, error handling, input validation |
| security.md | Pre-commit checklist, secret management, .gitignore requirements |
| testing.md | TDD workflow, 80% coverage target, test quality rules |
| git-workflow.md | Conventional commits, branch strategy, PR templates |
| development-workflow.md | Research β Plan β Test β Code β Review β Push pipeline |
| performance.md | Model routing, context window discipline, parallel execution |
Rules are loaded automatically when placed in ~/.claude/rules/. Claude Code reads them at the start of every session.
π Structure
claude-code-hardened/
βββ hooks/ # Security hooks (bash scripts)
β βββ block-no-verify.sh # Block --no-verify flag
β βββ pre-push-secrets-scan.sh # Triple secrets scan before push
β βββ enforce-branch-policy.sh # Protect main branch
β βββ protect-critical-files.sh # Warn on critical file edits
β βββ post-edit-lint-reminder.sh # Remind about formatting
βββ rules/
β βββ common/ # Language-agnostic rules
β βββ coding-style.md
β βββ security.md
β βββ testing.md
β βββ git-workflow.md
β βββ development-workflow.md
β βββ performance.md
βββ scripts/
β βββ validate.sh # Audit your setup
βββ examples/
β βββ settings-hooks.json # Hook config for settings.json
βββ install.sh # One-command installer
βββ README.md
How Hooks Work
Claude Code hooks run shell commands before or after tool executions:
ββββββββββββββββ βββββββββββββββββ ββββββββββββββββ
β Claude says βββββΆβ PreToolUse βββββΆβ Tool runs β
β "git push" β β hooks fire β β (if allowed) β
ββββββββββββββββ βββββββββββββββββ ββββββββββββββββ
β
exit 0 = allow
exit 2 = BLOCK
Hooks are configured in ~/.claude/settings.json under the hooks key. See examples/settings-hooks.json for the full configuration.
Customization
Add Your Own Protected Files
Edit hooks/protect-critical-files.sh:
PROTECTED_PATTERNS=(
"index.html"
"package.json"
"your-landing-page.html" # add your files
"terraform/" # add entire directories
)
Add Custom Secrets Patterns
Edit hooks/pre-push-secrets-scan.sh β add patterns to the grep:
SECRETS=$(grep -rnE \
"(api[_-]?key|your_custom_pattern|STRIPE_SECRET)" \
...
Block vs. Warn
Every hook can either warn (exit 0) or block (exit 2):
# Warn only (default for protect-critical-files)
echo "WARNING: ..." >&2
exit 0
# Hard block (default for secrets-scan)
echo "BLOCKED: ..." >&2
exit 2
FAQ
Q: Does this slow down Claude Code?
A: No. Hooks run in milliseconds. The secrets scan greps the codebase but skips node_modules and .git.
Q: Can I use this with other AI coding tools?
A: The rules work with any tool that reads markdown. The hooks are specific to Claude Code's hook system.
Q: What about false positives in the secrets scan?
A: The scan ignores .example, .sample, .template files and references to process.env / os.environ. If you get false positives, add exclusion patterns to the grep.
Q: I already use pre-commit hooks. Do I need this?
A: Yes. Claude Code can bypass your pre-commit hooks with --no-verify. This repo blocks that. Defense in depth.
Contributing
PRs welcome. Especially:
- New hook ideas (must be zero-dependency bash)
- Language-specific rules (TypeScript, Python, Go, Rust)
- False positive fixes in secrets scan
- Translations of rules
License
MIT β use it, fork it, harden everything.
Built from real incidents, not hypotheticals.
Every rule exists because something went wrong without it.
Yorumlar (0)
Yorum birakmak icin giris yap.
Yorum birakSonuc bulunamadi