claude-code - 💡(How to fix) Fix [BUG] examples/hooks/bash_command_validator_example.py: regex bugs cause silent false negatives on common shapes [1 pull requests]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Root Cause

  1. find invocations with predicates before -name (e.g. find / -type f -name '*.log') are missed because the regex ^find\s+\S+\s+-name\b only accepts find PATH -name. This shape is arguably the most common real-world form of find -name.

Fix Action

Fixed

Code Example

import re
   re.search(r"^grep\b(?!.*\|)", "grep foo | wc -l")  # returns None — bug
   re.search(r"^find\s+\S+\s+-name\b", "find / -type f -name '*.log'")  # returns None — bug
RAW_BUFFERClick to expand / collapse

What's Wrong?

The bash validator hook example silently fails to flag two common command shapes that the rules were intended to catch:

  1. grep as the source of a pipeline (e.g. grep foo | wc -l) is exempted by the negative lookahead (?!.*\|), even though grep is the leading command and rg foo is the better substitute. The lookahead was apparently intended to exempt grep-as-downstream-filter (e.g. cat foo | grep bar), but the ^grep\b anchor already handles that case correctly — the lookahead does no useful work and produces a false negative.

  2. find invocations with predicates before -name (e.g. find / -type f -name '*.log') are missed because the regex ^find\s+\S+\s+-name\b only accepts find PATH -name. This shape is arguably the most common real-world form of find -name.

What Should Happen?

Both shapes should match their respective rules. Empirical case matrix (run against examples/hooks/bash_command_validator_example.py at HEAD):

CommandCurrentShould be
grep foo | wc -lnot flaggedflagged
grep foo bar.txt | head -5not flaggedflagged
find / -type f -name '*.log'not flaggedflagged
find . -type d -name node_modulesnot flaggedflagged
find . -maxdepth 2 -name '*.md'not flaggedflagged

Steps to Reproduce

  1. Open examples/hooks/bash_command_validator_example.py at HEAD of anthropics/claude-code.
  2. Copy _VALIDATION_RULES into a Python REPL.
  3. Run:
    import re
    re.search(r"^grep\b(?!.*\|)", "grep foo | wc -l")  # returns None — bug
    re.search(r"^find\s+\S+\s+-name\b", "find / -type f -name '*.log'")  # returns None — bug

Claude Code Version

N/A — bug is in examples/hooks/bash_command_validator_example.py at HEAD of anthropics/claude-code, observable by reading the file.

Platform / OS / Terminal

N/A — file-level bug in a checked-in example.

Is this a regression?

No, this never worked.

Additional Information

Fix is two regex tweaks (drop the broken lookahead, allow predicates between find and -name). Happy to send a PR if you'd like — let me know if there's a preferred scope (e.g. fix-only vs. fix-plus-test-file). Considered switching to shlex-based parsing to also catch prefix forms (sudo grep, time grep, etc.) but that would substantially expand the example's scope; can do as a follow-up if desired.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix [BUG] examples/hooks/bash_command_validator_example.py: regex bugs cause silent false negatives on common shapes [1 pull requests]