A free Claude Code hook makes the agent flag risky commands before running them
A small open script blocks Claude Code commands that touch production, data or money until the agent writes a short risk note you can read and judge.
The short version
- The hook checks every Bash command Claude Code is about to run and sorts risky ones into three tiers.
- A flagged command is denied until the agent writes a risk note in chat and re-runs it with a RISK_ACK tag.
- Setup is one downloaded file plus two entries in the ~/.claude/settings.json file.
- It fails open and only sees Bash, so it is a prompt for human attention, not real protection.
- The post also warns that API keys leak into the model's context through ordinary debugging, which no risk rule catches.
Why it matters
Most agent accidents come from a human approving a step on autopilot, not from dramatic attacks. This hook adds a short, readable pause in front of the commands that are hardest to undo. Even if you never install it, the three-tier idea is a useful template for your own rules about what an agent must ask before doing.
The moment the hook targets
Picture an agent that has been doing good work for an hour. You have started clicking approve without reading. Then a command like kubectl delete deployment api -n production appears in the queue, and it may or may not be what you meant.
As dev.to reported, a developer built a small script to make that moment harder to skip. It is a hook, meaning a bit of code Claude Code runs automatically at set points in its workflow. This one inspects each shell command before it executes and refuses the risky ones until the agent explains itself.
Three tiers of risk
The script is written in Node, a common JavaScript runtime. It listens at two points: when you submit a prompt, where it re-injects a short rule telling the agent how to flag risk, and just before any Bash command runs, where it checks the command against its patterns.
Matches fall into three tiers:
- High risk: changes to production or staging, data writes, database schema changes and migrations, killing processes or machines, sudo, disk operations, deploys and publishing. The agent must write four lines: the risk, the blast radius, how to undo it, and a gate that defaults to waiting for your OK.
- Warning: anything that spends money or credits, such as paid APIs, cloud services, payment processors and backfill or scrape jobs. The agent states the cost, any cap, and the gate, then reports what was spent afterward.
- Caution: actions that are local but painful to reverse, such as recursive deletes, hard resets, force-pushes to a feature branch, or exposing a .env file, plus outward-facing steps like merging or posting. One line covering what, why and how to undo is enough.
Cheap-to-lose things pass without a flag. Deleting a node_modules folder or a temp build directory is allowed, and a local file that merely has "prod" in its name does not get bumped up a tier.
What a flag looks like, and the override
When the agent tries the Kubernetes delete, the hook denies it and tells the agent to write a red-highlighted block first. Lines starting with a minus sign show up red in most viewers, so the note stands out. For the example, the agent would say the live deployment disappears, every client of the production API is affected, the fix is re-applying a config file with minutes of downtime, and it is waiting for your OK.
You read four lines and decide. Only after that does the agent re-run the command with a prefix such as RISK_ACK=high, or a trailing comment with the same tag. The hook then lets it through. The tag must match or exceed the detected tier, so a "caution" tag on a high-risk command is rejected again.
The hook also looks inside nested commands, following wrapped shell calls up to two levels down, and it ignores text that is only being written into a file.
Installing and tuning it
You need Node and Claude Code. Save the script, risk-flag.js, into a hooks folder under ~/.claude, then add two entries to your settings.json file. If you already have hook lists there, add to them rather than overwriting. Start a fresh session and ask the agent to hard-reset a git repository you do not care about. You should get a caution note instead of an instant run.
All the rules sit as plain pattern tables at the top of that one file. You can add command-line tools your team uses, relax rows that are too noisy, and, most usefully, list your own production hostnames, database ports and live-key prefixes so generic matches become high-risk ones on your real systems. You can test edits by feeding the script a sample command; empty output means allowed.
Where it stops
The author is blunt that this is a nudge for a tired human, not a lock. If the script crashes or cannot read its input, the command runs anyway. The one exception is remote ssh commands that it cannot show to be read-only, which it treats as high risk.
It only watches Bash, so file edits and other tools are unchecked. Because it matches patterns, a determined or prompt-injected model could word a command the rules miss. And the agent writes its own acknowledgement tag, so nothing technical stops it adding one without a flag. The note in chat is your real check, which means you have to keep reading it.
The leak no rule catches
The post adds that the bigger exposure may be secrets, not commands. Printing a .env file, echoing a key to confirm it is set, running curl in verbose mode, reading a log with a connection string in it, or pasting a key into chat all put the value in front of the model. None of these is a dangerous command, so the hook stays quiet.
The suggested habits are simple: check that a key exists rather than printing it, keep a .env.example with names but no values, add a permissions deny rule for reading real .env files, avoid verbose flags, and use test or short-lived keys. If a live key lands in chat, rotate it. The author also promotes their own macOS tool, EnvSecure, for cases where the agent must actually use a key. That is the maker's own pitch, and we have not tested it or the hook.
What to watch
- How well the pattern rules hold up on real projects, since we have not tested what the hook does and does not catch.
- Whether Claude Code adds built-in risk prompts that make add-on hooks less necessary.
- Whether the same checkpoint idea extends past Bash to file edits and other agent tools.
Try it
Write a short list of commands your agent must always ask about first, such as deletes, deploys and anything using a payment key. Then keep production credentials out of its reach.
Sources and links
- Reportingdev.to: the article we sourced
Liked it? Say why
Add a line for other readers.
No account needed. We never show your email. Comments with links, ads or abuse are held and not shown. Your comment shows after a quick check.
Listen to the AI Wire headlines
Listen to the AI WireListen to our daily AI shows every morning: on your commute, in the shower, wherever you are
Listen to the daily showsFirst reported by dev.to · Read the original. Written by the SI Agent News Desk from the reporting above.Written with AI from the source above. Every number is checked against the source. Spot a mistake? Corrections.
