This is a story about a hook that saved us from a production outage last Tuesday. It's short. The main point is at the end.
A staff engineer was investigating a data-quality issue in our production database — nothing severe, just some rows that looked off. She was using Claude Code to help her poke around. Read-only queries, exploratory work, the kind of thing you do with a PostgreSQL client normally but that Claude Code can make faster.
At some point, in the middle of a session, she asked Claude to "clean up the test users." She meant the ones in the staging environment. Claude Code was pointed at production. Neither of them noticed.
She meant the ones in the staging environment. Claude Code was pointed at production. Neither of them noticed.
What almost happened
Claude proposed a plan: identify test users by a pattern in the email (matching @example.com and a few other test domains), then delete them in a single SQL statement. It described the plan. It got a confirmation ("go ahead"). It ran the query.
The query it wanted to run was DELETE FROM users WHERE email LIKE '%@example.com'. On production. Against a table that has 400,000 real users, some of whom actually use @example.com-style test-looking addresses because we don't enforce email domain restrictions.
It would have deleted about 6,000 real user accounts. Nothing catastrophic — we have backups, we have a delete flag not a hard delete on our real user table, and recovery would have been maybe two hours. But it would have been bad. Customer-affecting. Post-mortem-generating.
What actually happened
Our block-dangerous-commands lifecycle hook caught it. This hook runs before any tool use that involves shell commands, SQL, or file operations. It has a list of patterns it flags as high-risk — things like DROP TABLE, DELETE FROM without a narrow WHERE, rm -rf with broad targets, chmod 777, and so on.
When it matches, the hook doesn't just log — it interrupts the session and requires a human to explicitly type "yes I understand the risk" before proceeding. Not a click. A typed confirmation.
The engineer typed the confirmation phrase, then paused. Read the query more carefully. Noticed the environment. Cancelled. Ran SELECT COUNT(*) FROM users WHERE email LIKE '%@example.com' instead. Saw 6,247. Realized what she'd almost done. Went to make tea.
// .claude/hooks/block-dangerous-commands.js
const DANGEROUS_PATTERNS = [
{ pattern: /DROP\s+TABLE/i, label: 'DROP TABLE' },
{ pattern: /TRUNCATE\s+TABLE/i, label: 'TRUNCATE' },
{ pattern: /DELETE\s+FROM\s+\w+(?!\s+WHERE\s+\w+\s*=)/i, label: 'unscoped DELETE' },
{ pattern: /UPDATE\s+\w+\s+SET.*(?!WHERE\s+\w+\s*=)/i, label: 'unscoped UPDATE' },
{ pattern: /rm\s+-rf\s+(?!\.\/)/i, label: 'rm -rf on absolute path' },
{ pattern: /chmod\s+777/i, label: 'chmod 777' },
// ...
];
export function preToolUse({ toolName, input }) {
const text = extractCommandText(toolName, input);
const matches = DANGEROUS_PATTERNS
.filter(({ pattern }) => pattern.test(text));
if (matches.length === 0) return { allow: true };
return {
allow: false,
prompt_user_confirmation: true,
confirmation_phrase: 'yes I understand the risk',
message: [
'🛑 Dangerous command detected:',
...matches.map(m => ` • ${m.label}`),
'',
'Type the confirmation phrase exactly to proceed.',
'Type anything else to cancel.',
].join('\n'),
};
}What we changed after
A few things. First, we added an environment-detection preamble to any session that touches a database. If it's prod, the terminal changes color and the session banner reminds you. Small friction; catches a real problem.
Second, we tightened the confirmation phrase for prod. Non-prod: "yes proceed." Prod: "yes I understand the risk on production." Longer phrase, harder to type on autopilot.
Third, we added a rule to our pr-review subagent: any change to hooks needs an extra review by the platform team. This category of code deserves the paranoia.
The engineer is one of our best. She's written more of our infrastructure than anyone else. She has a decade of experience. She still almost deleted 6,000 users because she said "clean up the test users" and Claude Code took her at her word.
This isn't about Claude Code doing something wrong. It did exactly what she asked. It's about the fact that AI agents are unusually good at doing exactly what you ask, which is fine when you're asking the right thing and terrible when you're not.
The actual point
People ask us why we bother with hooks. "Isn't Claude Code already good enough?" or "Aren't you just adding friction?" The answer is that Claude Code is very good, and that's exactly why the hooks matter. A less capable assistant would have hesitated, said something like "I want to double-check what environment you're in," and given her time to notice. A more capable one moves fast and follows through. The right guardrails don't need to be smart — they just need to be present.
This is why we ship the block-dangerous-commands hook by default in cctk. It's why we recommend the environment-banner pattern. It's why our hooks aren't optional in our team's CLAUDE.md — they're part of the baseline.
You don't adopt safety hooks after an incident. You adopt them before one, and then you get to have a story like this instead of a post-mortem.
The engineer wrote up the near-miss in our internal wiki. Title of the doc: "The tea break that saved 6,000 users." I asked her if we could tell this story publicly. She said yes, on one condition: "make it clear the hook did the work, not me. I would have run the query if the hook hadn't stopped me." Fair.
Get cctk running in one command
85 slash commands, 12 subagents, 12 MCP integrations, 12 hooks. All the patterns from this post are shipped in cctk.