Skip to content

fix(guardrails): match git in command position only - #2622

Merged
kyle-sexton merged 5 commits into
mainfrom
cursor/fix-guardrails-git-command-position-f8b0
Aug 14, 2026
Merged

kyle-sexton merged 5 commits into
mainfrom
cursor/fix-guardrails-git-command-position-f8b0

Conversation

@kyle-sexton

Copy link
Copy Markdown
Member

Closes #2592

Summary

block-dangerous-git engaged on PowerShell that never invoked git, because substring matching treated .git directory names as git commands, then failed closed on ordinary ForEach-Object { } grouping.

Fix

Require git in command position for PowerShell (not substring matches inside .git / path fragments). Keep real git invocations blocked. Added regression tests.

Verification

  • bash plugins/guardrails/hooks/block-dangerous-git.test.sh → PASS=395 FAIL=0

Related

Refs #2594 — separate Windows /tmp → C:\tmp guard (Wave 3/guardrails lane).

Open in Web Open in Cursor 

PowerShell scripts that only mention .git as a directory name were
blocked because the dangerous-git guard matched the substring "git".
Require git in command position so classification arrays stay allowed.

Closes #2592.

Co-authored-by: Kyle Sexton <[email protected]>
@kyle-sexton
kyle-sexton marked this pull request as ready for review August 14, 2026 22:44
@cursor

cursor Bot commented Aug 14, 2026

Copy link
Copy Markdown

Bugbot is not enabled for your account, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

@claude

claude Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @kyle-sexton's task in 5m 28s —— View job


Security review complete

  • Gather context (PR diff, changed files)
  • Skip-gate check (open PR, security-relevant surface: yes — regex change in a fail-closed git-blocking guard)
  • Review changed files for security issues
  • Post findings (inline + summary)

1 IMPORTANT finding, posted inline on ps-command.sh#L272:

The new command-position git predecessor whitelist ([[:space:]\;\|\&\(\{\}\"\'/\\]) omits =. In PowerShell, an assignment's right-hand side starts a new pipeline/statement without requiring whitespace, so $x=git reset --hard (no space) invokes git exactly as $x = git reset --hard does. The old predecessor class ([^[:alnum:]_.]) included =, so this is a regression introduced by this PR, not a pre-existing gap.

Concrete bypass — both route to the fail-closed sink (via (/{) but the git token's immediate predecessor = isn't in the new whitelist, so ps::might_invoke_git returns false and ps::classify_git_command takes the "provably git-free" branch, allowing a destructive command through:

1..1 | ForEach-Object {$x=git reset --hard}
if($x=git reset --hard){}

This is the mirror image of the PR's own positive-control test at block-dangerous-git.test.sh#L807-808 ({ git reset --hard }, correctly blocked) — just with a no-space assignment inserted before git. It violates the file's own stated "OVER-BLOCK, NEVER UNDER-BLOCK" invariant. The identical regex is duplicated in ps::git_command_is_readonly at line 299.

Suggested fix: add = to the predecessor class in both functions and add a regression test for the no-space-assignment shape.

Note: pwsh was not executable in this sandboxed CI environment, so I could not empirically confirm the parse behavior — the finding rests on documented PowerShell semantics (assignment RHS begins a new pipeline without requiring whitespace). Recommend a maintainer verify with pwsh before merging.

No other security issues found in the version bump (plugin.json) or CHANGELOG.md; the added tests in block-dangerous-git.test.sh are test-only and don't themselves introduce risk.
· branch cursor/fix-guardrails-git-command-position-f8b0

@claude

claude Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @kyle-sexton's task in 5m 22s —— View job


Code review (/review:code-review)

  • Gather context (PR diff, CLAUDE.md, changed files)
  • Run /review:code-review criteria against changed files
  • Post findings (inline comments + summary)

Scope reviewed: plugins/guardrails/lib/powershell/ps-command.sh, plugins/guardrails/hooks/block-dangerous-git.test.sh, plugins/guardrails/.claude-plugin/plugin.json, plugins/guardrails/CHANGELOG.md (diff against origin/main).

The .claude-plugin/plugin.json version bump and CHANGELOG.md entry are routine and match the code change — no issues.

Finding: predecessor character class regression in the git command-position probe

Posted inline on ps-command.sh#L272 (ps::might_invoke_git) and #L299 (ps::git_command_is_readonly).

Summary: the new predecessor class [[:space:];|&(){}"'/\] is narrower than the old [^[:alnum:]_.], and drops =. PowerShell doesn't require whitespace around =, so a scriptblock like:

1..1 | ForEach-Object { $x=git reset --hard }

still trips the fail-closed sink (via the {} special-construct trigger — the exact shape this PR's own tests target), but ps::might_invoke_git no longer recognizes git immediately after = as command-position, so ps::classify_git_command returns "allow/skip" instead of "block." The old regex would have caught this (predecessor = was non-alnum/underscore/dot), so this is a behavior regression introduced by this diff, not a pre-existing gap — worth fixing (add =, and double-check other no-space-required operators) and covering with a regression test before merge.

I could not execute the shell test suite or a standalone regex harness myself in this sandbox (script execution beyond git/gh read commands isn't permitted here), so this is based on careful manual trace of the regex and the ps::classify_git_command control flow rather than a run I performed — flagging that explicitly per the review's verification rules. The author's own bash .../block-dangerous-git.test.sh → PASS=395 result is author-claimed and unverified by me. I'd recommend the author (or a follow-up CI run) add the PoC test above and confirm it currently fails.

No other findings — the rest of the diff (comments, other test additions, changelog/version bump) is accurate and well-scoped to the stated #2592 fix.

The #2592 regression fixture uses a Git\bin Windows path string; mark it
portability-ok like sibling PowerShell fixture tests.

Co-authored-by: Kyle Sexton <[email protected]>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7b3aa3403c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@kyle-sexton
kyle-sexton enabled auto-merge (squash) August 14, 2026 22:49
Include ':' in the command-position predecessor class so PowerShell
drive-relative git paths stay blocked (Codex review on #2592).

Co-authored-by: Kyle Sexton <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

Last security-reviewed head: 7b3aa3403c75946178b615c8aefe5cfb09c14eef. On the next push, the relevance gate compares only the commits since this SHA; delete this comment to force a full re-review.

Include '=' in the command-position predecessor class so PowerShell
`$x=git reset --hard` (no spaces) remains blocked (Claude review on #2592).

Co-authored-by: Kyle Sexton <[email protected]>
@github-actions

github-actions Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Claude has reviewed this PR 2 times. The lane skips further automatic reviews after 5; deleting this comment resets the count.

Strip PowerShell `$var=` / `$var+=` prefixes when reducing for the Bash
hand-off so bare `$x=git reset --hard` is blocked, not only braced forms
caught by the `=` predecessor class.

Co-authored-by: Kyle Sexton <[email protected]>
@kyle-sexton
kyle-sexton merged commit 302a6fa into main Aug 14, 2026
1 check passed
@kyle-sexton
kyle-sexton deleted the cursor/fix-guardrails-git-command-position-f8b0 branch August 14, 2026 23:02
@claude

claude Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @cursor[bot]'s task in 5m 6s —— View job


Code review (/review:code-review)

  • Gather context (PR diff, CLAUDE.md, changed files)
  • Run /review:code-review criteria against changed files
  • Post findings (inline comments + summary)

Note: this PR is already merged, and GitHub rejected my attempts to anchor inline review comments to it (could not be resolved — merged PRs don't support new diff-anchored review threads here), so both findings below are in the summary with permalinks instead.

Summary

I reviewed the final merged diff (8fee5c30461... → current ps-command.sh), specifically re-checking the two regressions flagged by the earlier review rounds on this PR (missing = and : in the command-position predecessor class). Both are fixed correctly in the code that landed:

  • ps::might_invoke_git and ps::git_command_is_readonly both now include = and : in the predecessor class, so $x=git reset --hard (no space) and & 'C:git.exe' --% reset --hard (drive-relative) still route to "might invoke git" and get blocked. I confirmed this isn't just a whitespace nit: since only the single character immediately before git is checked (not the whole operator token), = alone also covers the compound forms +=, -=, *=, /=, %= for free — no separate handling needed.
  • The new $var=/$var+= stripping in ps::classify_git_command only runs on the "no sink trigger fired" path (after the if [[ -n "$PS_SINK_TRIGGER" ]] early-return block), so it's exposing the RHS command word to the downstream Bash tokenizer for the unbracketed assignment case ($x=git reset --hard with no {}/()), while the bracketed case ({$x=git reset --hard}) is independently caught by the predecessor-class fix in ps::might_invoke_git. Both paths check out.

Two minor, non-blocking findings:

  1. Test coverage gap for the exact reported PoC. block-dangerous-git.test.sh#L72 tests the no-space assignment alone, and L76-77 test the braced scriptblock alone — but the combination that was actually reported (1..1 | ForEach-Object {$x=git reset --hard}) isn't tested directly. I traced it manually and it does still block under the current code (braces trip the fail-closed sink, and the predecessor-class fix catches git right after = inside it), but since this combined shape is literally the reviewers' PoC, it's worth its own regression test so a future change to either probe can't silently reopen it unnoticed:

    run_pwsh "PS: assignment inside scriptblock \$x=git reset --hard (blocked)" \
      '1..1 | ForEach-Object {$x=git reset --hard}' 2
  2. Possible residual no-whitespace-operator gap (low confidence, unverified). The predecessor class still doesn't include PowerShell 7's ternary operator ? (e.g. $cond?git reset --hard:0) or the comma operator. I wasn't able to run pwsh in this sandbox to confirm whether git reset --hard is even syntactically valid as an unparenthesized ternary/comma operand (PowerShell may require it to be a full statement, not a bare expression, in which case this isn't reachable at all). Flagging as a "worth a maintainer double-check" item, not a confirmed bug — I don't have enough certainty here to call it a real bypass.

I could not execute the shell test suite (bash plugins/guardrails/hooks/block-dangerous-git.test.sh) or pwsh in this sandboxed environment (script execution beyond git/gh read commands isn't permitted here), so the above is based on manual regex/control-flow tracing rather than an actual run — same limitation the earlier review rounds on this PR noted.

No other issues in the version bump (plugin.json) or CHANGELOG.md — both are routine and match the code change.

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.

guardrails: block-dangerous-git.sh blocks PowerShell commands that invoke no git command

2 participants