My Claude Code Guardrails Passed Every Test. On Windows, They Blocked Nothing.
A hook that cannot start doesn't fail safe, it fails open. Here's how I found out, the two-line fix, and the one test that would have caught it. I had built what I thought was a solid safety net for my coding agent.
A hook that cannot start doesn't fail safe, it fails open. Here's how I found out, the two-line fix, and the one test that would have caught it.
I had built what I thought was a solid safety net for my coding agent.
A set of Claude Code hooks that refuse the actions an agent must never take on a multi-tenant SaaS: writing a .env file, editing a database migration that has already run, creating a table without tenant isolation, pushing straight to main, force-pushing. Each rule had unit tests, and every one of them was green.
Then I copied the whole setup into a fresh repository on a Windows machine, started a real Claude Code session, and asked the agent to do the forbidden things on purpose.
It created .env without a word of protest.
None of the hooks had fired. Every guard I'd written was silent, and nothing on screen told me anything was wrong.
Why the tests didn't catch it
My unit tests called the rule functions directly: given this file path and this content, should the write be refused? Those answers were all correct. The logic was fine.
What the tests never exercised was the part between Claude Code and my logic: the command line in settings.json that actually launches the hook. And that's where it broke.
The hook was registered the way almost every example shows it:
{
"type": "command",
"command": "node \"$CLAUDE_PROJECT_DIR/.claude/hooks/guard-files.mjs\""
}
That's a shell command. On macOS and Linux, a POSIX shell expands $CLAUDE_PROJECT_DIR from the environment and everything works.
On my Windows machine, the command ran through PowerShell. In PowerShell, $CLAUDE_PROJECT_DIR isn't an environment variable, it's a PowerShell variable (environment variables are $env:NAME). It doesn't exist, so it expanded to an empty string. Node was asked to run /.claude/hooks/guard-files.mjs, couldn't find it, and exited with code 1.
Why did exit code 1 mean "allow"
This is the part worth remembering even if you never touch Windows.
Claude Code reads a command hook's exit code like this:
- Exit 0: success. The hook may still deny the action through JSON on stdout.
- Exit 2: blocking error. The action is stopped and stderr is shown to the agent.
- Anything else: non-blocking error. It's noted, and the action goes ahead.
A hook that crashes, because the script is missing, a config file has a typo, or the input looks unexpected, usually exits with 1. Claude Code treats that as a hiccup, not a veto. So a guard that cannot start lets everything through. It fails open.
Nothing about this is a bug in Claude Code. It's a reasonable default: you don't want a flaky formatter hook to stop all your work. But it means a security hook and a formatting hook fail in exactly the same way unless you design for the difference.
There was a second, quieter casualty. My lint hook runs after each edit with asyncRewake, which wakes the agent to fix errors when the hook exits with 2. Through PowerShell, that 2 arrived as a 1. Lint errors were found, reported nowhere useful, and the agent carried on.
The fix, part 1: exec form
Claude Code also accepts hooks in exec form: the program in command, its arguments in args, and no shell in between.
{
"type": "command",
"command": "node",
"args": ["${CLAUDE_PROJECT_DIR}/.claude/hooks/guard-files.mjs"],
"timeout": 15
}
Claude Code fills in ${CLAUDE_PROJECT_DIR} itself and starts node directly. There's no shell to misread the variable or rewrite the exit code, so it behaves the same on Windows, macOS and Linux. After the change, a rerun of the same session got the refusal I'd expected all along, and the lint hook started waking the agent to fix its errors.
The fix, part 2: make the guard fail closed
Exec form fixes this particular launch problem. It doesn't fix the general one: a guard can still crash for other reasons, and a crash still means "allow".
So every guard now runs inside a small wrapper that turns any error into a block:
export async function runGuard(guard) {
try {
await guard(await readHookInput());
} catch (err) {
process.stderr.write(
`Guard ${process.argv[1]} failed, so the action is blocked: ${err?.message ?? err}. ` +
'Fix the hook or its config, or ask the human.');
process.exit(2);
}
}
A real refusal goes out as a normal JSON deny with a reason the agent can act on. A broken guard exits 2, stops the action, and says why. If my config has a typo, the agent gets stuck and tells me, which is annoying for thirty seconds. That beats months of believing I was protected when I wasn't.
I added two tests that launch the hooks as real processes, the way Claude Code does: one checks that a guard denies through stdout, and one feeds a guard input that makes it crash and asserts exit code 2. I then broke the wrapper on purpose to confirm the second test goes red. A test you've never seen fail hasn't proven anything yet.
The test that would have caught it
Unit tests prove your rules are right. They can't prove your rules are running.
The only check that does is the dumbest one: start a real session and ask the agent to do something forbidden. "Create a .env file with a dummy key." If it isn't refused, your harness isn't live, whatever your test suite says.
That takes thirty seconds. Do it on every machine and operating system your team uses, and again after you change settings.json. I now treat it as a step you can't skip when setting up the project, not a nice-to-have.
The same pattern, one more time
While testing on the same machine, I found a third instance of the same failure.
My read-only reviewer agents were configured with tools: Read, Grep, Glob, Bash, so they could run git diff. On Windows, the session's shell tool was PowerShell, not Bash. The security reviewer politely reported that it had no shell, couldn't see the diff, and reviewed what it could. Adding PowerShell to its tools fixed it, and on the next run it caught a cross-tenant foreign key I'd planted, marked it [must fix] and said "not ready".
Three different components, one shape: something meant to protect me degraded quietly instead of failing loudly.
What I'd tell anyone building agent guardrails
- Decide how each hook should fail. Formatters and linters can fail open. Anything that guards secrets, data or production must fail closed: catch your own errors and exit 2.
- Prefer exec form for hook commands. Don't let a shell you didn't choose sit between Claude Code and your script.
- Test the launch, not just the logic. Spawn the hook as a process in your tests, including the case where it crashes.
- Prove it live. Ask the agent to do one forbidden thing in a real session, on every OS your team uses.
- Distrust silence. A guardrail that has never refused anything might be working, or it might never have run. Find out which.
Tested with Claude Code 2.1.294 and Node.js 24 on Windows 11. Hook behaviour can change between versions, so check the current hooks documentation, and above all, run the live test.
This is part of a series on building a multi-tenant SaaS with coding agents. Start with the overview: Building a Multi-Tenant Micro SaaS with AI Coding Agents. For the database side, see Row-Level Security Is On. Your Tenants Can Still See Each Other's Data.
Where this came from
I found this while testing the companion pack for my book, The Multi-Tenant Micro SaaS Playbook: a practical guide to building a multi-tenant SaaS with a coding agent doing most of the typing, without letting one customer see another's data.
Every copy comes with the Claude Code harness pack free: the hooks from this article (exec form, failing closed), skills, two reviewer agents, and a PostgreSQL row-level-security starter kit. It was all tested in live sessions and against real PostgreSQL 15 and 16 databases before release, which is how this bug got found before a reader hit it.
Get the book and the free harness pack →
Have you had a guardrail fail silently? I'd like to hear about it in the comments.
Anshul Mittal builds multi-tenant SaaS products with coding agents and writes about the practices that make them dependable.
Independent article, not affiliated with or endorsed by Anthropic. Claude and Claude Code are trademarks of Anthropic, PBC.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.