Skip to content

fix(#564): pre-tool.sh regexes skip gh issue/pr body content - #566

Merged
admarble merged 2 commits into
mainfrom
feature/564-bug-pre-tool-sh-regexes-match-inside-quoted-gh-body
May 3, 2026
Merged

admarble merged 2 commits into
mainfrom
feature/564-bug-pre-tool-sh-regexes-match-inside-quoted-gh-body

Conversation

@admarble

@admarble admarble commented May 3, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes the pre-tool.sh regexes that match inside quoted gh issue/pr body content, blocking tool calls whose payloads merely reference the matched literals (e.g. a doc explaining the hook itself).

  • Fix: wraps the force-push regex (line 120 of canonical) and all four git commit regex sites (security checks, no-changes guard, worktree warning, conventional-commits validator) behind the same ^gh (issue|pr) exclusion that already guarded the secrets/credentials checks at line 87.
  • Mirrored across all three hook copies — templates/hooks/pre-tool.sh, hooks/pre-tool.sh, .claude/hooks/pre-tool.sh — none of which are symlinks.
  • Tests: new __tests__/pre-tool-hook.integration.test.ts (10 cases) spawns the canonical hook with real stdin/exit-code assertions; AC-4 uses an isolated tmp git repo so "no staged changes" is deterministic.

AC Coverage

AC Status Evidence
AC-1 Implemented force-push regex now gated by `^gh (issue
AC-2 Implemented no-changes guard now gated similarly (3 files, line 261/249) — tests AC-2, AC-2b
AC-3 Implemented regression test AC-3 blocks git push --force, AC-3b blocks git push -f origin main
AC-4 Implemented regression test AC-4 blocks git commit -m 'msg' with no staged changes; AC-4b confirms anchor is at start of TOOL_INPUT
AC-5 Implemented __tests__/pre-tool-hook.integration.test.ts — 10 passing cases (4 primary AC pairs + 4 sub-cases + 1 anchor regression + 1 sanity)
AC-6 Implemented CHANGELOG entry under [Unreleased] / ### Fixed references #564 and #532

Scope Note (broader than literal AC text)

The issue's AC-2 names line 248 (no-changes guard) of .claude/hooks/pre-tool.sh specifically. The same grep -qE 'git commit' regex appears at four places in the canonical templates/hooks/pre-tool.sh (security-checks/L228, no-changes/L260, worktree-warning/L287, commit-msg-validation/L298). All four share the same root cause and the same body-content false-positive surface, so the exclusion is applied at all four sites.

Per the spec comment's Open Questions, this is the recommended interpretation of the issue's intent. The Non-Goals carve-out ("Don't widen exclusion beyond gh issue|pr") refers to not adding exclusions for other parent commands, not to under-applying the same exclusion to all instances of the same regex.

Live reproduction during /spec

Both bugs reproduced live during planning (read-only grep inspection of the hook source tripped both regexes), and the heredoc that produced the spec body was itself blocked — it was rewritten via the Write tool instead. Validates the issue without needing synthetic fixtures.

Test plan

  • npm run build — passes
  • npm run lint — passes (0 warnings)
  • npx vitest run __tests__/pre-tool-hook.integration.test.ts — 10/10 passing
  • bash -n syntax check on all 3 hook copies — passes
  • Diff of hooks/pre-tool.sh vs templates/hooks/pre-tool.sh — identical (mirroring preserved)

Known gaps / out of scope

  • .claude/hooks/pre-tool.sh has drifted from templates/hooks/pre-tool.sh (older byte-count: 17896 vs 18509). This PR fixes the regexes in all three copies but does not re-sync the broader drift — that's a separate hygiene issue.
  • The new test file is named *.integration.test.ts so it runs in the integration project (subprocess-heavy tests).

Closes #564

Wraps the force-push regex (line 120) and all four `git commit` regex
sites (security checks, no-changes guard, worktree warning, conventional
commits validator) behind the same `^gh (issue|pr) ` exclusion that
already guarded the secrets/credentials checks at line 87.

Previously, `gh issue create` / `gh pr create` / `gh issue|pr comment`
calls whose body text merely *referenced* these tokens (e.g. a doc
explaining the hook itself, or a release note) were blocked at the
tool-call level. The friction was disproportionate to the protection:
the regexes already work against actual destructive commands at the
command-string boundary; matching free body content added noise without
adding safety.

Mirrored the fix across all three hook copies — `templates/hooks/pre-tool.sh`,
`hooks/pre-tool.sh`, and `.claude/hooks/pre-tool.sh` — none of which are
symlinks, all of which Claude Code may invoke depending on context.

Adds `__tests__/pre-tool-hook.integration.test.ts` covering 10 cases:
the four primary regression pairs from the issue (allow gh-body /
block real-command, for both regexes), plus a START-anchor regression
guard (`echo gh issue && git push --force` is still blocked) and a
sanity check that the no-changes guard does not fire when changes
are staged. AC-4 runs in an isolated tmp git repo so the assertion
is independent of the developer's working tree.

Follow-up to #532 (which deferred this as AC-5).

Closes #564
Addresses two gaps surfaced during QA review (#566):

1. Tests previously exercised only `templates/hooks/pre-tool.sh` (the canonical
   source). The fix is mirrored to `hooks/pre-tool.sh` and
   `.claude/hooks/pre-tool.sh`, but neither was directly verified — future
   regex changes could re-drift in either copy without the suite catching it.

   Wraps the existing 10 cases in `describe.each(HOOK_COPIES)`, threading the
   hook path through `runHook()`. Test count grows from 10 to 30 (10 × 3
   copies), all passing. Each copy is now a first-class regression target.

2. Inline comment asymmetry: only the force-push block carried the
   `# Skip for gh issue/pr commands — body text may legitimately reference
   these tokens (#564)` comment; the four `git commit` blocks (security,
   no-changes guard, worktree-warn, conventional-commits) didn't.

   Adds the same comment line above each of the four `git commit` blocks
   in all three hook copies (12 inserts total). The comment is indented
   correctly at the security-check site (which is nested inside the
   `if [[ "${CLAUDE_HOOKS_SECURITY:-true}" != "false" ]]` block).

A third gap surfaced during QA — same body-content false-positive surface in
the destructive-command, deployment, hard-reset, and gh-workflow regexes (lines
95/101/130/170 of the canonical file) — is filed as a follow-up issue rather
than expanding this PR's scope further.
@admarble
admarble merged commit 1bd6270 into main May 3, 2026
8 checks passed
@admarble
admarble deleted the feature/564-bug-pre-tool-sh-regexes-match-inside-quoted-gh-body branch May 3, 2026 15:17
admarble added a commit that referenced this pull request May 6, 2026
…ructive/deployment/reset/workflow commands (#578)

* fix(#570): pre-tool.sh regexes skip gh issue/pr body content for destructive/deployment/reset/workflow commands

Wraps four additional regex sites in pre-tool.sh with the same
`^gh (issue|pr) ` exclusion idiom introduced in #564 / #566:

- Destructive system commands site (line ~95)
- Deployment commands site (line ~101)
- git reset outer guard (inner BLOCK_REASONS accumulator unchanged)
- gh workflow run site

Applied across all three mirrored hook copies:
- templates/hooks/pre-tool.sh
- hooks/pre-tool.sh
- .claude/hooks/pre-tool.sh

Real destructive commands are still blocked at the bash command-string
level; only `gh issue|pr create|comment` payloads referencing these
tokens in body text are allowed through.

Adds 24 parametrized test cases (8 new tests x 3 hook copies) to
__tests__/pre-tool-hook.integration.test.ts covering allow-on-gh-body
and block-on-real-command for each of the four sites.

Closes #570

* test(#570): add verbatim issue-body fixtures to integration test

Per feedback_motivating_example_regression — examples quoted verbatim
in an issue body are mandatory test fixtures, not paraphrases. The
existing AC-1..AC-4 tests covered the spirit of each "Affected regex
sites" table row but used reworded fixtures (e.g. "never run rm -rf /
on production" vs the issue's verbatim "WARNING: never run `rm -rf /`
..."). This commit adds the EXACT strings from the #570 issue body:

- Row 1 destructive: cleanup-doc explainer payload
- Row 2 deployment: release-notes payload
- Row 3 git reset: tutorial payload (with dirty-repo fixture so the
  outer guard is actually exercised, not silently skipped via empty
  BLOCK_REASONS)
- Row 4 gh workflow: workflow-doc payload
- "Repro pattern" section: gh issue create with `rm -rf /` body

Each fixture parametrized over the 3 hook copies via the existing
describe.each(HOOK_COPIES) block = 15 net new test runs.

Test count: 72 -> 87, all passing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(hooks): pre-tool.sh regexes match inside quoted gh issue/pr body content (line 108 force-push, line 248 git-commit)

1 participant