Skip to content

fix(guardrails): refuse PowerShell here-string and bare-CR shapes hiding git #4683

Closed

Description

@kyle-sexton

Problem

plugins/guardrails/lib/powershell/ps-command.sh reduces a PowerShell command to Bash-tokenizer text by blanking here-string bodies (ps::blank_herestrings, opener test at :385-387). It confirms openers that PowerShell's own parser does not see, drops the following lines as "body", and then proves git-freedom (ps::might_invoke_git, reached at :1734) over text that no longer contains the live call. At default configuration block-dangerous-git allows these parse-clean commands, each of which PowerShell 7.6.6 ParseInput reports as a live git push --force:

Lines are separated by /; <CR> is a bare CR byte.

id payload dg on main
shape 9 Write-Host {x} # @' / git push --force / '@' 0
shape 2 Write-Output "\"a" @" / git push --force / "@" 0
bare CR Write-Output x<CR>git push --force 0
A1 Write-Output "a\'b'" c" @" / git push --force / "@" 0
A2 Write-Output "a'b" c'd @' / git push --force / '@' 0
N1 Write-Output "a'" '@' / git push --force / '@' 0
B1 Write-Output `@' / '; git push --force / '@' 0
S5' <# x / @' / #> / git push --force / '@' 0
N2 Write-Output "it's" @' / it's / '@ / git push --force 0 (ledger)
control git push --force 2

The ledger also measured block-no-verify (same shapes with git commit --no-verify -m x) at 0, and block-hook-bypass at 0 for the Set-Content f.txt x forms of shape 2, bare CR, A2, N1, B1 and S5'. Root cause is one class: the library confirms "phantom openers" and bare CR is not a line terminator to it (it splits on LF only, :192-199, :348), while PowerShell ends a statement at a bare CR.

Evidence

Verified this pass (2026-09-27, main a6ba321): the dg column above for shape 9, shape 2, bare CR, A1, A2, N1, B1 and S5' plus the control, by piping {"tool_name":"PowerShell","tool_input":{"command":...}} to block-dangerous-git.sh directly (not through run-guards.sh), CLAUDE_PLUGIN_ROOT unset, CLAUDE_PROJECT_DIR set to the checkout. The no-verify and hook-bypass columns were NOT re-measured this pass; they are the ledger's (main b877295, which carries #4303; no commit has touched these files since).

Carried from the handoff item and its interview/audit ledger (not re-verified):

  • Liveness per shape came from pwsh 7.6.6 ParseInput (parse only).
  • Withdrawn as distinct bypasses: shapes 1 and 4 block with a literal git (their $g forms are the documented variable-command-word residual, ps-command.sh:14-24); shapes 3 and 5 as the item first wrote them are parse errors, and a parse error runs nothing (measured with -Command, not with the tool's -EncodedCommand). Shape 6 as a here-string is inert. Shape 7 (block-convention-violation.sh:342 private opener copy) is a subject-extraction gap only, since dg and nv refuse it.
  • A CRLF-terminated @' / feat: ok subject / '@ | git commit -F - is allowed by dg and nv today (re-measured this pass for dg: 0) and must stay allowed.

Proposed approach

Recommended (audit validator B, class-level, monotone): raise the existing no-token refusal flag (PS_HERESTRING_OPENER_COMMENT_CHAR, generalized to a "reduction untrusted" flag plus a reason variable for the message and telemetry form) when:

  • a confirmed opener line's prefix contains any ', ", backslash, backtick or # (covers shapes 2 and 9, A1, A2, N1, B1);
  • a <# appears earlier in the command (S5');
  • the raw command, before any normalization, contains a CR not followed by LF (bare CR).
    And, whenever any body was dropped, run the git-freedom check over the raw normalized command with the body included, instead of over PS_BLANKED (closes the shape-9 mechanism for any trigger, including B1's backtick). This is a superset of the instance fix for shape 9 (suspend the proof on the flag beside the expandable-body suspension at :1733).

N2 has no confirmed opener, so no opener predicate catches it; it needs its own case in the design.

Alternatives considered:

  • Instance refusals only (a raw \" substring test for shape 2, the flag for shape 9): the audit found A1 and N1 slip past a \" test and B1 past a # test. Rejected.
  • A tokenizer-level model of PowerShell (comments, here-strings, bare CR) in pure bash: replaces every decision path at once; the fix(guardrails): refuse a PowerShell here-string opener sharing a line with a # #4303 quote-walk rewrite was withdrawn after it kept creating main-blocks/branch-allows cells. Deferred to the A2b follow-up the library already names (ps-command.sh:64-68).
  • Delegating to pwsh's parser: breaks the fork-free contract (ps-command.sh:150-165); perf(guardrails): make the PowerShell guard path fork-free and narrow its fail-close to executable git #4188 took this lane from 80 process creations to 3.
  • Normalizing bare CR to LF: faithful, but re-splitting can confirm new openers and drop text main keeps visible (Write-Host {x}; Write-Output @'<CR>git push --force<CR>'@ is dg 2 on main and would allow). Rejected.

Files: plugins/guardrails/lib/powershell/ps-command.sh, hooks/block-dangerous-git.sh (remediation line and form token at :1426-1431), hooks/block-dangerous-git.test.sh, block-no-verify.test.sh, block-hook-bypass.test.sh, CHANGELOG, plugin.json. Last slice: replace the private opener test at block-convention-violation.sh:342 with the library's opener predicate.

Decisions needed (Kyle)

  1. Class-level refusals now (recommended above) versus a tokenizer-level model. The item reserved this call for the owner.
  2. Bare CR: refuse every PowerShell command containing a bare CR, unconditionally and with no allow token? Recommended yes. Cost: inert bare CRs inside a string literal or here-string body (Write-Output 'a<CR>b', rc 0 today) become refusals in all five blocking guards, including a trailing CR. No suite or docs input contains a bare CR today.

Acceptance criteria

  • Every payload in the table above (except the control) exits 2 from block-dangerous-git, and its git commit --no-verify -m x form exits 2 from block-no-verify.
  • The Set-Content f.txt x forms of shape 2, bare CR, A2, N1, B1 and S5' exit 2 from block-hook-bypass.
  • The CRLF canonical commit here-string above still exits 0 from dg and nv.
  • The literal-git and parse-clean variants of shapes 1, 3, 4 and 5 are pinned as must-block cases in block-dangerous-git.test.sh.
  • The refusal message and telemetry form token name the actual shape (not herestring-comment-char) for each new reason; no new grantable ps-unparsable-* token or trigger name exists.
  • The differential from draft 20260921-164559-a shows zero base-2/branch-non-2 cells under every allow-token subset, and its table is in each PR body.
  • block-convention-violation's here-string subject scan uses the library's opener predicate; no private opener test remains.

Constraints and gotchas

  • Depends on draft 20260921-164559-a (the differential must exist first; the flag-keyed commit guards there make the flag reach both commit guards).
  • Refusals only: rc may move 0 or 1 to 2, never back. Trigger-chain order in ps::classify_git_command stays unchanged so existing attribution does not move.
  • Probes are JSON on stdin only. Never execute a probe command, and never run a command to see what a shell does with it.
  • guardrails: read-only git inside {} blocked by two guards, with irrelevant commit guidance, Bash-lane advice, and no narrow lever #4235 (open) asks to loosen the same {} sink path for read-only git; the shape-9 fix tightens it when a body was dropped. Keep both in view so one does not undo the other.
  • The bar is Bash parity, not airtight (ps-command.sh:14-24); shape 8 (Write-Output $(& $g push --force)) stays a documented residual.

Context

Source: local handoff item 20260921-164559-guardrails-powershell-classifier-nine-residual-bypasses-measured-on-main.md (retired into this issue and draft 20260921-164559-a). The item's harness guardrails-ps-residual-probe-4303.sh lived only in the local handoff reference folder; every payload it needs is inlined above. Prior: #4302 / PR #4303, #4237, #2965.

Activity

  1. added
    needs-owner-decision
    area: security
    on Sep 27, 2026
  2. kyle-sexton commented on Sep 29, 2026

    @kyle-sexton
    MemberAuthor

    Posted from the 2026-09-29 audit of the Cursor run (499 PRs, 2026-09-27 to 2026-09-29). The owner authorized fixing the findings; merging is not authorized. Fix PR: #5340 (draft).

    PR #5036 carried only [x] PS differential vs origin/main exit 0 with no table; the acceptance asked for zero base-2/branch-non-2 cells under every allow-token subset. The audit ran scripts/check-guardrails-ps-differential.sh --jobs 24 f9fcaf9d3 (the parent of 0e2b4bd) from a scratch worktree at 0e2b4bd (WSL2 Linux, bash 5.3.9; Linux only). 14001 cells, exit 0, no FAIL.

    cells count base exits 2 branch exits 2 base 2, branch not branch 2, base not
    block-dangerous-git 613 333 335 0 2
    block-no-verify 613 262 265 0 3
    block-convention-violation 613 32 39 0 7
    block-noncanonical-commit 613 35 42 0 7
    block-hook-bypass 613 221 226 0 5
    run-guards.sh (Bash row) 613 471 472 0 1
    block-dangerous-git, 31 token subsets 10323 6223 6223 0 0

    The zero base-2/branch-non-2 result holds, with the limit that the corpus does not contain the #4683 payloads (it has not changed since #4715), so the table cannot show those cases. A cross-group request asks the scripts group to add them. #5340 also rebuilds the five-trigger budget test and unifies the two untrusted flags.

    This comment does not state or imply that the owner has ratified the two decisions the issue reserved; that is the owner's to say.

  3. added
    needs-human
    on Sep 29, 2026
  4. kyle-sexton commented on Sep 29, 2026

    @kyle-sexton
    MemberAuthor

    Posted from the 2026-09-29 audit of the Cursor run (499 PRs, 2026-09-27 to 2026-09-29). The owner authorized fixing the findings; merging is not authorized. Fix PR: #5340 (draft).

    Reopened. This issue reserved two decisions for you under "Decisions needed (Kyle)":

    1. class-level refusals now, or a tokenizer-level model;
    2. refuse every PowerShell command that contains a bare CR, with no allow token.

    #5036 (0e2b4bd, merged by app/cursor) adopted both recommendations with no comment from you. The differential in the comment above found no cell main refuses that the branch allows (Linux only). #5340 lists the resulting no-token over-blocks in the README Scope notes and rebuilds the sink-budget tests. It does not change either decision. status: needs-decision is kept and needs-human added.

    Decision needed (nothing is implemented)

    Question. Ratify both decisions as shipped, or reverse one?

    Options.

    • A. Ratify both, and close.
    • B. Keep the class-level refusals, but narrow the bare-CR refusal (for example to a CR outside quoted text) so inert CRs in string literals pass again. The issue's own acceptance forbids a new ps-unparsable-* token, so this would be a classifier change, not a token.
    • C. Replace the class-level refusals with a tokenizer-level model (the A2b follow-up ps-command.sh names). The issue's alternatives section records that the fix(guardrails): refuse a PowerShell here-string opener sharing a line with a # #4303 attempt at this was withdrawn after it kept creating main-blocks/branch-allows cells.

    Recommendation: A. Basis: the issue's own recommendations; the audit's owner-decision table rates #4683 "ratify"; the zero-lost-cell differential above; the over-block cost is now written down for users (#5340). This comment does not state or imply that you have ratified either decision.

    Unblocks. Closing this issue. Adding the #4683 payloads to scripts/guardrails-ps-differential-corpus.jsonl is with the scripts group and does not wait on this.

  5. kyle-sexton commented on Sep 30, 2026

    @kyle-sexton
    MemberAuthor

    Owner decision (2026-09-29): accepted the recommendation: "A. Ratify both, and close."

    The owner accepted every standing recommendation on 2026-09-29.

    Evidence: both decisions shipped in #5036 (0e2b4bd), which is an ancestor of origin/main (verified with git merge-base --is-ancestor after fetch). The differential in the audit comment above found no cell main refuses that the branch allows. #5340 documents the no-token over-blocks and does not change either decision.

    Next: closing as completed. Adding the #4683 payloads to scripts/guardrails-ps-differential-corpus.jsonl stays with the scripts group and does not wait on this issue.

  6. removed
    needs-owner-decision
    needs-human
    on Sep 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: security

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions