Skip to content

Commit 012e9ca

Browse files
committed
fix(toolkit): handle single-quoted grep patterns in claim-verification hook (DOJ-4872)
Round-3 bot review at 4/5 confidence flagged 1 P3: the pattern extractor only handled double-quoted forms, so `grep -rn 'Symbol' src/` would fall through to the awk fallback and (until the awk fallback also stripped single quotes) emit `'Symbol'` with the quotes attached. Applied: - Added a second extractor branch that mirrors the double-quote form but for single-quoted patterns. Tries double-quote first (cheapest grep), then single-quote, then the multi-flag awk fallback. - Extended `tr -d` in the awk fallback to strip both `"` and `'`, so the unquoted-token output is clean regardless of which quote style the original command used. Verification: - shellcheck → 0 issues - "grep -rn 'Symbol' src/" → "Symbol" (was: 'Symbol') - "grep -rn \"Symbol\" src/" → "Symbol" (regression) - "grep -i -r 'Symbol' src/" → "Symbol" (new, multi-flag + single quote) - "grep -i -r \"Symbol\" src/" → "Symbol" (regression) - "ls src/foo.tsx" → src/foo.tsx (regression) - "echo hello" → silent (regression) Created by Claude Code on behalf of @lapc506.
1 parent 067c74b commit 012e9ca

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

hooks/pre-tool-use-claim-verification.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,26 @@ if printf '%s' "$COMMAND" | grep -qE '(^|[;&|][[:space:]]*)ls([[:space:]]|$)'; t
165165
SUGGESTED="git fetch origin <branch> --quiet && git ls-tree origin/<branch> -- <path>"
166166
fi
167167
elif printf '%s' "$COMMAND" | grep -qE 'grep[[:space:]]+-[rRnliN]+'; then
168+
# First try the double-quoted form: `grep -rn "Symbol" src/`.
168169
pattern_arg="$(printf '%s' "$COMMAND" \
169170
| grep -oE 'grep[[:space:]]+-[rRnliN]+[[:space:]]+"[^"]+"' \
170171
| head -1 \
171172
| sed -E 's/^grep[[:space:]]+-[rRnliN]+[[:space:]]+"(.+)"$/\1/')"
173+
# Then the single-quoted form: `grep -rn 'Symbol' src/`.
174+
if [ -z "$pattern_arg" ]; then
175+
pattern_arg="$(printf '%s' "$COMMAND" \
176+
| grep -oE "grep[[:space:]]+-[rRnliN]+[[:space:]]+'[^']+'" \
177+
| head -1 \
178+
| sed -E "s/^grep[[:space:]]+-[rRnliN]+[[:space:]]+'(.+)'$/\\1/")"
179+
fi
172180
if [ -z "$pattern_arg" ]; then
173181
# Nested awk loop: skip subsequent flag tokens so multi-flag forms
174182
# like `grep -i -r "Symbol" src/` don't mis-extract `-r` as the pattern.
183+
# Strip both " and ' so unquoted-token output is clean regardless of
184+
# whether the original command used double or single quotes.
175185
pattern_arg="$(printf '%s' "$COMMAND" \
176186
| awk '{for(i=1;i<=NF;i++) if ($i ~ /^-[rRnliN]+$/) {for(j=i+1;j<=NF;j++) if ($j !~ /^-/) {print $j; exit}}}' \
177-
| tr -d '"')"
187+
| tr -d "\"'")"
178188
fi
179189
if [ -n "$pattern_arg" ]; then
180190
SUGGESTED="git fetch origin <branch> --quiet && git grep \"$pattern_arg\" origin/<branch> -- src/"

0 commit comments

Comments
 (0)