AIToday
AI Coding AssistantsAI Safety & AlignmentZenn AI/MLPublished: Nov 19, 2026, 16:00 JST

Claude Code hooks in practice: bash-guard, gitleaks pre-push

Send on LINE
Claude Code hooks in practice: bash-guard, gitleaks pre-push

3 Key Points

  1. What happened

    The author runs four guards from settings.json — a PreToolUse hook that flags rm -rf, git push --force, git reset --hard, sudo and commands that print secrets such as .env, plus a SessionStart/PreToolUse pair and a Stop hook.

  2. Why it matters

    Each guard was added only after one accidental failure, and risky commands are routed to "ask" rather than "deny" — denial made the model retry the same action in another form, while a human pressing once keeps genuinely needed work moving.

Not sure about something? Ask the AI

Questions and answers are published on this page.

Summaries like this, every morning.

Context & Analysis

The setup is less a product announcement than a field report on what actually broke. The preflight pair, for instance, exists because a conversation opened in another folder once skipped the project's MEMORY.md and posted a long tracking URL instead of the short link the rules required — and writing "read first" in CLAUDE.md was obeyed only some of the time. The git pre-push hook came from a similar accident: pushing two commits also sent 11 commits another conversation had made in the same folder, so the hook now checks every commit being pushed, whoever made it.

Performance shapes the design throughout. All four guards use plain string matching rather than calling an AI, and the preflight guard stores a read position in a small state file so a transcript of tens of MB is not re-read on every operation. The author is explicit about the limits: deliberately evasive tricks are not covered, non-Bash tools are only seen if their names are added to the matcher one by one, and the pre-push hook can be skipped with --no-verify. These are guards against slips, not a security boundary — permissions deny and sandboxing are left for that.

The recommended order of adoption is itself a conclusion from experience: start with the check on commands that print key files, since that failure is the most damaging and the most common, then add gitleaks on outgoing commits, then the destructive-command guards, and only add the read-first pair once a rules-skipping failure actually happens. Building everything upfront is explicitly not advised.

FAQ
Why route dangerous commands to "ask" instead of blocking them?
With deny, the model tries to accomplish the same thing in a different form. With ask, a human reads the reason and presses once, so genuinely needed operations still proceed.
Why put the secret check in a git pre-push hook rather than Claude Code's PreToolUse?
Push also runs from scheduled shell and Python scripts, and Claude Code's PreToolUse only covers Claude's Bash calls — git itself catches the rest, and --redact keeps the found key value off screen.
What happens if a guard script itself crashes?
Each guard wraps its logic in a try/except that returns silently, so a buggy watchman lets the operation through rather than stopping every conversation and scheduled run.
Send on LINE

AI news that matters for your work, delivered every morning.

Pick your industry and the AI tools you use, and get news related to your work every day.

Free · 30 seconds with Google · unsubscribe anytimeWhat is AIToday? →

Ask AI

Ask AI anything about this article. The AI reads this article, earlier AIToday articles, and Wikipedia, and cites its sources. Q&As are published on this page for other readers too.

Questions and answers are published on this page.

Related Articles

Next articleHempfield schools hold off on CurvePoint AI gun detectors after false alarms