r/ClaudeCode • u/Ok_Gur_9033 • 1d ago
Discussion My Claude Code hook blocked 10 commands in a month. None of them was the build it was written to stop.
I have a PreToolUse hook on Bash for seven Flutter apps whose store builds have to go through one release script. It denies flutter build ipa and appbundle, and any shorebird release or shorebird patch that isn't called through that script. It works by matching the command string.
I went through my session logs since 25 Oct to see what it actually caught. It fired 10 times. The release script itself ran 50 times in the same stretch.
3 of the blocks were --help calls. The agent wanted to read the flags for shorebird patch ios before running anything, and got denied.
3 were reads of a build already running: a ps | grep and two greps of a patch log, all searching for the text "shorebird patch".
2 were Python heredocs editing files that mention the command. One was a build script, the other was my CLAUDE.md, where I was updating the line that documents this exact rule.
1 was a read-only shorebird patches list loop across the apps.
1 was a real flutter build ipa, in an app that isn't one of the seven. The first version of the hook didn't check which repo it was in, and I narrowed it within four minutes.
So none of the 10 was the agent trying to get a direct store build past the rule, and each one cost a turn. The fix I'm looking at is to split the command first and match the executable and its subcommand, so text inside a heredoc or a grep pattern stops counting, and to let --help through.
This is one person, one hook and about a month. I can't tell from it whether the CLAUDE.md line did the real work or the agent simply never needed a direct build. If you've written a Bash hook that parses the command instead of pattern matching it, what did you use to split it?
•
u/AutoModerator 1d ago
Hey! Thanks for posting to r/ClaudeCode
While participating in this thread, please follow our community rules. Keep discussions constructive. Attack the idea, not the person.
For help, project discussions, tips, and general chat, join the ClaudeCode Discord.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/Ok-Distance-6388 23h ago
For the split itself: Python's shlex.split handles quotes but treats | and && as plain text, so you still match across a whole pipeline. bashlex gives you a parse tree with each simple command as its own node, and in Node, shell-quote's parse returns operators as separate tokens. Either way, you match argv[0] and argv[1] per command, and a grep pattern or a quoted string becomes just an argument.
Heredoc bodies are the messy part. I'd cut them off before parsing rather than trust any parser with them.
I'd fix the --help case first. A denied help lookup teaches the agent to guess the flags instead, which is worse than the build you're guarding against.
Your log is the whole argument for parsing, by the way: 9 false positives against 1 real catch, and the real one was a scoping bug, not a pattern bug.
2
u/AryanThakor 23h ago
I'd also put the reason and the allowed path in the deny message itself, like "store builds only through ./release.sh, --help and reads are fine". when the block just says no, the agent tends to retry small variations of the same command, and each of those is another wasted turn
2
u/Far_Reindeer8428 23h ago
our hooks had the same problem. they matched on the command string and blocked a make target that never deployed anything
2
u/atlascirrus 22h ago
one thing that might save you writing the parser yourself: the hook's
iffield uses permission rule syntax, and it matches per subcommand, not against the raw string. so"if": "Bash(flutter build *)"only spawns your hook when one of the subcommands actually starts with flutter build. aps | grep "shorebird patch"wouldn't trigger it, since that subcommand is grep.an
ifonly takes one rule, so you'd want a handler each for flutter build and shorebird. then the script itself only has to check repo and let--helpthrough. if you still want to parse inside the script, bashlex in python gives you an actual bash ast instead of regex.were the heredoc blocks python3 <<EOF style, or cat > file?
2
u/ChemicalExcellent463 22h ago
my rule is no blocker without a failure i've actually seen, a log line or a red test. did an agent ever run a direct store build before you wrote the hook?
2
u/Excellent-Issue-5956 19h ago
Mine doesn't parse, it does the cheap version: sed strips everything inside quotes before the regex runs. That fixed the same kind of block you got on the
ps | grepand the log greps, since the search text is in quotes and just drops out.It has holes though, I tested these. An apostrophe inside double quotes, like don't in a commit message, gets read as the start of a single quoted string, so everything up to the next ' gets stripped and a real pkill after it goes with it. And
echo "$(pkill myapp)"reads as plain echo because the whole double quoted part is dropped, even though bash runs what's inside. It doesn't help your heredoc cases either, the body isn't in quotes.So if you're going to split it properly I'd go with one of the parsers people mentioned instead of writing your own. I tested someone's hand written quote walker over a few rounds in another thread here, and each time they fixed one I found another case it got wrong, like a comment inside backticks or the word case inside
$(...). Haven't fixed mine yet.2
u/RealFullMetal 23h ago
before writing your own splitter, the hook's
iffield may already do it: it takes permission-rule patterns likeBash(shorebird patch *), and per the hooks docs claude code checks each subcommand of something likenpm test && git push, plus commands inside$()and backticks, so a grep whose quoted pattern contains "shorebird patch" shouldn't trip it. two caveats from the same page: when claude code can't split the input into subcommands it runs your hook anyway, so the script still needs its own check, and since the filter is best-effort they say to use permission rules rather than a hook for a hard deny.2
u/Gigaslavx 22h ago
God I hate hooks and Claude's goal mode
Don't know if I'm doing something wrong but I tried so much many ways and never had remotely similar issues on Codex
I don't know if it's the harness or the models in general but it seems long autonomous work is Claude's weakness