r/ClaudeCode • • 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?

2 Upvotes

19 comments sorted by

•

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.

1

u/Ok_Gur_9033 2h ago

Your help lookup point already happened once. The first block, on 26 Oct, was the agent reading the help for shorebird patch ios to find a flag that allows diffs. It got denied, and the very next command edited the release script to add that option anyway, without the help text ever being read.

Cutting heredoc bodies off before parsing makes sense to me. Both of my edit blocks were text sitting inside one. Did you go with bashlex in the end, or shell quote?

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

1

u/Ok_Gur_9033 2h ago

The deny message already names the path. It tells the agent to use the release script with the app and the mode. What it doesn't say is that reads and help calls are fine, and you can see that in what came next. After 5 of the 10 blocks the next command was a reworded copy of the same one. Three were greps with the phrase taken out, and one of those spelled it [s]horebird pat[c]h so the regex wouldn't see it.

That one is the agent learning to work around the hook, which is the last thing I want. Did adding the allowed cases to your message stop the retries completely?

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

1

u/Ok_Gur_9033 2h ago

The fix that held for me was one level down. One of the apps has a make.sh, and its prod branch now just hands off to the release script, so asking for a prod build there goes the right way whatever the hook makes of the string. Did you end up moving the check into the Makefile itself, or keep it in the hook?

2

u/atlascirrus 22h ago

one thing that might save you writing the parser yourself: the hook's if field 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. a ps | grep "shorebird patch" wouldn't trigger it, since that subcommand is grep.

an if only 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 --help through. 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?

1

u/Ok_Gur_9033 2h ago

Both were python3 heredocs doing a string replace in a file. One was editing a build script in one of the apps, the other was my CLAUDE.md, and in both the phrase only sat inside the text being swapped out. So a per subcommand if would have let both through, since the subcommand there is just python3.

Thanks for the bashlex pointer. With a handler for each pattern, did you ever hit a case where the if matched and the script still had to say no?

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?

1

u/Ok_Gur_9033 2h ago

Yes, in the week before the rule. On 12, 15 and 16 Oct the agent called shorebird release or patch directly in one of the apps, outside the script, and the 12 Oct one was a prod Android release. The rule went into my CLAUDE.md on the 17th and the hook came after that.

Since then there's been nothing real for it to catch, and that's the part I can't separate. The CLAUDE.md line might be doing all the work while the hook just sits there. Do you hold CLAUDE.md lines to the same rule, or only blockers?

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 | grep and 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.

1

u/Ok_Gur_9033 2h ago

The apostrophe case is a good one to know about. On my 10 it would have cleared the three greps, since the phrase sat inside double quotes each time, and done nothing for the two heredocs or the help calls. The help calls look like the cheapest fix of the lot, a plain allow before any matching starts.

Did the person with the hand written walker end up switching to a parser?

2

u/RealFullMetal 23h ago

before writing your own splitter, the hook's if field may already do it: it takes permission-rule patterns like Bash(shorebird patch *), and per the hooks docs claude code checks each subcommand of something like npm 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/RealFullMetal 21h ago

one more row from that docs table worth knowing for your case: a pattern that names more than the command, like Bash(shorebird patch *), runs the hook anyway whenever the command contains $(), backticks or a $VAR. so with a two-word pattern, those commands still reach your script and it has to make the call itself.

1

u/Ok_Gur_9033 2h ago

I hadn't used the if field at all. The hook is registered on every Bash call and does all of its own matching, so moving the two word patterns into if would at least stop a ps or a grep from ever reaching it.

Your second point matters for me because 3 of the 10 blocked commands set SHOREBIRD_TOKEN from a $( ) read of the key file before anything else, so those would still land in the script. Reading it again I found the opposite hole too: any command that mentions the release script anywhere exits before a single check runs. Does the subcommand split treat a heredoc as one command?

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