Skip to content

guardrails: & $var script record dir blocked as a file write; block reason names the wrong cause #4234

Closed

Description

@kyle-sexton

Problem

block-hook-bypass blocked a read-only PowerShell command with the reason "PowerShell file-write cmdlet/redirect bypasses Write/Edit hooks". The command contains no file-write cmdlet and no redirect.

Session: Windows 11, Claude Code 2.1.278, auto mode, guardrails 0.35.0 (marketplace melodic-software), PowerShell lane. The blocked command (some path spans elided by the session that recorded it):

$b='...evidence'; $s="$b/<session>"; $sh='C:/Program Files/Git/bin/bash.exe'; $r='.../plugin-quality/0.7.21/scripts'; foreach ($t in 'disk-hygiene','discovery-research','guardrails') { & $sh "$r/packet-seal.sh" record "$s/$t/20260919T170952Z"; "seal $t=$LASTEXITCODE"; (Get-Item "$s/$t/20260919T170952Z/evidence.md").Length }; & $sh "$r/packet-prune.sh" --root $b --apply; "prune=$LASTEXITCODE"

The same work passed when rewritten as flat & "C:/Program Files/Git/bin/bash.exe" script args calls.

Trigger: the unquoted subcommand word record sitting beside other positional operands in a call through a variable (& $sh "..." record "..."). The guard reads & $w <path> <value> as Set-Content <path> <value>.

Workaround until fixed: call the program by a literal quoted path, & 'C:/Program Files/Git/bin/bash.exe' script.sh record dir, or put a flag before the positionals.

The same shape covers ordinary PowerShell: & $py tool.py run x, & $sh script.sh record dir, & $node cli.js build. Every such call is refused as a file write, and the only lever is the user-global kill switch (see #4235).

Root cause

All in lib/powershell/ps-command.sh, reached from hooks/block-hook-bypass.sh line 1219. Traced by reading the installed source; not executed (see Limitations).

  1. & $sh ... is a call through a bare variable, so ps::call_target_is_bare_computed (lines 864-868) matches and the computed-target gate is entered. The foreach plays no part.
  2. None of these arms fire: subexpression target, --%, splat, >, -va*.
  3. ps::computed_call_has_positional_write_signal (lines 1037-1128) runs on opaque_gate, where ps::opaque_quoted_spans_to (lines 716-737) has replaced each double-quoted span containing $ with $q, counted as computed, not literal. The first call site's operand region is $q record $q:
    • "$r/packet-seal.sh" becomes $q (count 1, literal 0)
    • record is a bare word (count 2, literal 1)
    • "$s/$t/..." becomes $q (count 3)
  4. Line 1120: if ((count >= 2 && count_lit >= 1)); then return 0. The command is blocked.
  5. The second call site, & $sh "$r/packet-prune.sh" --root $b --apply, stops counting at --root (count 1) and does not trip.
  6. The flat literal-path form passes because a quoted non-writer target never enters the computed-target gate; only quoted writer names match at line 2104.
  7. The whole path sits in ps::write_bypass, lines 2117-2192.

The shipped tests pin this class as intended: block-hook-bypass.test.sh lines 1036-1037 (& $py $script out.jsonl gives 2) and the note at line 1112 ("& $py script.py arg, which already blocked").

Predicted minimal reproduction (not run): the PowerShell payload & $sh x.sh record gives exit 2.

Limitations

  • The Bash tool was blocked in the audit session by another plugin, so no reproduction, claude plugin validate, or packet seal verification ran. Packet integrity is ungradable.
  • The hooks doc was read at rung 2 only (WebFetch of https://code.claude.com/docs/en/hooks.md, 2026-09-19, summarizer-reported size roughly 95,000-100,000 characters, no byte count). Any harness-behavior claim from it is unverified. This finding depends on nothing beyond "PreToolUse exit 2 blocks the call".
  • Not checked: whether ps::might_write_via_python3's call_target_is_interpolating_string arm (& "$env:X" ... with a python-ish write token) has the same over-block.

Proposed fix

Cheapest first:

  1. Name the arm in the block reason. Replace "cmdlet/redirect" with, for example, "call through a variable (& $var) with 2+ positional operands, read as Set-Content ", and print the working rewrite (literal quoted path, or a flag before the positionals). One string in block_bypass's caller at block-hook-bypass.sh line 1220, plus a form token distinct from powershell-write.
  2. Resolve same-command literal assignments. When the call variable is assigned a single-quoted literal in the same command ($sh='C:/Program Files/Git/bin/bash.exe') and that literal's basename is not a writer or alias name, treat the target as literal and skip the computed-target gate. This covers the observed shape and keeps the evasive "writer name staged in a variable" shape blocked.
  3. Add a narrow allow token for the arm, for example block_hook_bypass_allow=ps-computed-positional, matching the block_dangerous_git_allow sink tokens.
  4. README. Document this shape and its rewrite in the block-hook-bypass section (see guardrails: all guards run after a block, and the README does not document the PowerShell over-blocks #4236).

Acceptance criteria

  • $sh='C:/Program Files/Git/bin/bash.exe'; & $sh x.sh record dir is allowed by block-hook-bypass.
  • $w='Set-Content'; & $w f.txt x is still blocked.
  • & $py $script out.jsonl with $py not assigned in the same command is still blocked, or is allowed only through the new narrow allow token.
  • When the positional arm blocks, the reason names the & $var positional rule and prints the literal-path rewrite, not "cmdlet/redirect".
  • The positional arm has its own form token distinct from powershell-write.
  • A regression test covers the & $sh "<quoted>" record "<quoted>" shape.

Found by /plugin-quality:audit (run 20260919T170952Z).

Activity

  1. added
    needs-triage
    on Sep 19, 2026
  2. added
    agent-ready
    status: ready
    and removed
    needs-triage
    on Sep 23, 2026
  3. kyle-sexton commented on Sep 23, 2026

    @kyle-sexton
    MemberAuthor

    This was generated by AI during triage.

    Decision defaulted: implement the issue's proposed same-command literal-assignment resolution exactly as specified, using its own listed acceptance-criteria tests (including the "still blocked" negative cases) as the bypass fence — this reverses a block the test suite currently pins as intended (block-hook-bypass.test.sh, "already blocked" note), so the existing regression tests are the safety net, not a free-form fix — veto before merge

  4. added theissue type on Sep 23, 2026
  5. kyle-sexton commented on Sep 23, 2026

    @kyle-sexton
    MemberAuthor

    This was generated by AI during triage.

    Agent Brief

    Type: Bug
    Summary: block-hook-bypass blocks a PowerShell call-through-a-variable command as a file write whenever it carries two or more positional operands, even when the variable was assigned a literal, non-writer path earlier in the same command, and the block's stated reason names the wrong cause.

    Current behavior:
    block-hook-bypass's computed-call-target check inspects a PowerShell command that invokes a program through a variable. If the call's operand region contains at least one interpolated/quoted operand and at least one bare-word operand, the check reads the shape as a content-write cmdlet call and blocks it as a file write — regardless of what the variable was actually assigned, and even when the assignment (a single-quoted literal path to a non-writer, non-alias executable) appears earlier in the very same command. The block's message names the cause as a file-write cmdlet or redirect bypassing the Write/Edit hooks, which is inaccurate: the command contains no file-write cmdlet and no redirect.

    Desired behavior:
    Defaulted decision: when the call variable is assigned a single-quoted literal in the same command, and that literal's basename is not a known writer or alias name, the check should resolve the call's target to that literal and skip the computed-target write check entirely — the call is not evasive, and no positional-operand ambiguity applies. When no same-command literal assignment can be resolved (the variable's value is unknown, computed, or assigned in a way this check does not resolve), the computed-target check should still fire as it does today, but the block reason should specifically name the call-through-a-variable-with-positional-operands rule rather than the generic file-write cmdlet/redirect message, and should suggest the working rewrite (a literal quoted path, or a flag before the positional operands).
    Maintainer-vetoable alternative: ship only the block-reason correction (name the actual rule and print the rewrite) without adding same-command literal-assignment resolution, leaving the block itself unchanged and the call still refused.

    Key interfaces:

    • The computed-call-target write check needs a same-command literal-assignment resolver: given a call-through-a-variable, determine whether that variable was assigned a single-quoted literal earlier in the same command, and if so, whether that literal's basename is a known writer or alias name.
    • The block message for the positional-operand arm needs its own distinct wording and its own form/telemetry token, separate from the generic file-write cmdlet/redirect token used elsewhere in the same guard.
    • A narrow allow token for this specific arm, parallel to the allow tokens already available for other guard arms in this hook, gives operators a documented, scoped way to permit the pattern without disabling the whole guard.

    Acceptance criteria:

    • A command that assigns a variable a single-quoted literal path to a non-writer executable, then calls that variable with two or more positional operands (at least one bare word), is allowed by block-hook-bypass.
    • A command that assigns a variable a single-quoted literal whose basename is itself a writer cmdlet or alias name, then calls it the same way, is still blocked.
    • A call through a variable that is not assigned a same-command single-quoted literal (its value is computed, passed in, or otherwise unresolved), with two or more positional operands, is still blocked, or allowed only through an explicit narrow allow token.
    • When the positional-operand arm blocks a command, the printed reason names the call-through-a-variable positional rule specifically and shows the literal-path (or flag-first) rewrite, rather than the file-write cmdlet/redirect message.
    • The positional-operand arm's block carries a form/telemetry token distinct from the generic file-write token used elsewhere in the guard.
    • A regression test covers a call through a variable, assigned a same-command literal, invoking a script with two quoted positional operands.

    Out of scope:

    • The read-only git command false positives inside brace/parenthesis grouping, and the irrelevant commit-form guidance printed on unrelated blocks — tracked separately (see the sibling issue on read-only git inside grouping constructs).
    • Any change to how the guard treats a computed target that is genuinely unresolved (no same-command literal assignment) beyond what the acceptance criteria specify.
    • README documentation of this rule and its rewrite — tracked separately as part of the guardrails documentation follow-up.
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

    agent-readystatus: ready

    Type

    Fields

    No fields configured for Bug.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions