Skip to content

fix(hook): gate reads the rewrite's own wrapper scan; assignment redirects and continuation joins match bash - #4554

Open
akushonkamen wants to merge 2 commits into
rtk-ai:developfrom
akushonkamen:fix/4545-perm-gate-wrapper-scan
Open

akushonkamen wants to merge 2 commits into
rtk-ai:developfrom
akushonkamen:fix/4545-perm-gate-wrapper-scan

Conversation

@akushonkamen

Copy link
Copy Markdown

Summary

Root cause

  • Gap 1: the gate's check_shell_wrapper_permissions only set contains_shell_wrapper at depth == 0, so a wrapper behind a transparent prefix was gated by the outer rule alone: env bash -c 'git status' rode an env:* allow (exit 0) on a line RTK had rewritten.
  • Gap 2: the lexer's split_segments counted an assignment word as command text, so the segment was cut at the redirect in FOO=a>/dev/null git push and the gate saw FOO=a while the rewriter swallowed the whole prefix and emitted rtk git push.
  • Gap 3: registry::collapse_line_continuations joined \⏎ with a space where bash deletes it, rewriting git p\⏎ush into a different command and hiding it from deny rules.

Fix

  • At depth >= 1 the gate calls registry::wrapper_script_changed_by_rewrite, which replays the rewrite's own rewrite_shell_wrapper at the positions shell_wrapper_positions yields — it reads what the rewrite did rather than re-deriving reach, so gate and rewrite cannot drift by construction. If the rewrite changed the wrapper's script inside, the verdict is Ask. fix(hook): defer on a denied command inside a prefixed shell wrapper #4530's Defer-not-Deny on a denied inner script is kept, and wrappers the rewrite cannot rewrite inside (bash -lc, unrewritable scripts, pipeline stages the rewrite preserves) keep the outer verdict.
  • Assignment words no longer count as command text, so an assignment with a glued redirect takes the existing leading-redirect step-over and the gate sees the command that follows.
  • collapse_line_continuations now removes \⏎ (bash semantics) via BASH_JOIN_RE.

Notes

  • The issue's row 3 (env bash -c 'git reset --hard' under ask git reset:*) shows 0 in its ffa5651 baseline column; on ffa5651 it is actually 1 — git reset --hard is not rewritten at all, so no gate question can surface. The gate's verdict moves Allow → Ask, the rtk rewrite exit code stays 1. Recorded as found.
  • The issue's --print-outcome flag does not exist in the CLI; verification below uses the real rtk rewrite contract (exit 0 allow-covering rewrite · 1 no rewrite · 2 deny · 3 ask) as documented in src/main.rs.
  • cargo test with several filter words needs cargo test --quiet -- lexer registry (the words are independent substring filters after --).
allow Bash(*) + deny Bash(git push:*) unless noted (rtk rewrite exit: 0 · 1 · 2 · 3) ffa5651 (#4530) this PR
env bash -c 'git status' 0 3, rewrite env bash -c "rtk git status"
env bash -c 'git -C . push' 0 3, rewrite env bash -c "rtk git -C . push"
FOO=a>/dev/null git push 0 2
env bash -c 'git push' (allow Bash(env:*) + deny) 1 1 — #4530's Defer kept
env bash -c "git c\⏎heckout -b x" (allow Bash(env:*) + deny) 1 3, rewrite env bash -c "rtk git checkout -b x"
env bash -c 'git reset --hard' (allow Bash(env:*) + ask git reset:*) 1 (issue table says 0) 1 — not rewritten, exit unchanged

Test plan

  • Re-run on this head (6ec2cdf): timeout 300 cargo test --quiet -- lexer registry → 616 passed / 0 failed (acceptance red run before the fix: 613 / 3, the three new tests); timeout 300 cargo test --quiet -- permissions → 135 passed / 0 failed (red: 131 / 3); timeout 600 cargo test --quiet → 4397 passed / 0 failed over 36 targets
  • cargo fmt --all --check clean; cargo clippy --all-targets 0 warnings
  • CLI matrix above re-run on this head with a fixture settings.json, HOME and CLAUDE_CONFIG_DIR both pinned to the fixture directory so no real agent config leaks in; baseline column from the acceptance runs on ffa5651

🤖 Generated with Claude Code

…rects and continuation joins match bash

Closes the three gaps rtk-ai#4530 left open (rtk-ai#4545):

- The permission gate now asks when the rewrite reaches a wrapper
  behind a transparent prefix and changes its script inside. Previously
  only a wrapper at the segment start refused the outer auto-allow, so
  'env bash -c "git status"' rode an 'env:*' allow (exit 0) on a line
  RTK had rewritten. The gate reads the rewrite's own wrapper handling
  (rewrite_shell_wrapper at the positions shell_wrapper_positions
  yields) instead of re-deriving reach, so the two cannot drift.
  rtk-ai#4530's Defer-not-Deny semantics on a denied inner script are kept.

- split_for_permissions no longer truncates a segment at a redirect
  glued to an assignment prefix: bash attaches 'FOO=a>/dev/null' to the
  assignment's null command and the words after it are the command, so
  the gate now sees and denies the 'git push' that follows instead of
  gating on 'FOO=a' alone.

- collapse_line_continuations removes '\<NL>' the way bash does,
  joining the bytes either side with nothing. The old space-join
  rewrote 'sh -c "rm\<NL>-rf x"' as 'sh -c "rm -rf x"' — a different
  command — and hid 'git p\<NL>ush' (bash: 'git push') from deny rules.

Wrappers the rewrite cannot rewrite inside ('bash -lc', unrewritable
scripts, pipeline stages the rewrite preserves) keep the outer verdict.

Submitted with AI assistance (Claude, Anthropic) as part of an
open-source contribution exercise.
src/hooks/README.md still said a wrapper behind a prefix 'is only
checked for deny rules and otherwise keeps the outer command's verdict,
as on master' — exactly the behavior rtk-ai#4545 changed. Now: a reached
wrapper whose script the rewrite changes inside is asked (the gate reads
registry::wrapper_script_changed_by_rewrite), a wrapper the rewrite
parses but leaves unchanged keeps the outer verdict, and a denied inner
script still defers (exit 1).
@CLAassistant

CLAassistant commented Oct 13, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

Fixes three gaps in rtk's permission-gate hook left open by PR #4530: the gate now reads the rewrite's own wrapper-scan positions instead of re-deriving reach, so wrappers behind transparent prefixes (e.g. env bash -c) are gated correctly. It also stops treating assignment words as command text so redirects glued to an assignment prefix no longer hide the following command, and fixes line-continuation collapsing to match bash's actual join-with-nothing semantics instead of joining with a spac…

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #4545


Analyzed automatically by wshm · This is an automated analysis, not a human review.

@pszymkowiak

Copy link
Copy Markdown
Collaborator

Thanks for the contribution! Before we can review and merge this, the CLA needs to be signed. The CLAassistant bot left the link above, it only takes a minute. Once it's signed we'll pick the review up.

@KuSh KuSh added the needs-cla label Oct 31, 2026
@akushonkamen

Copy link
Copy Markdown
Author

Just signed the CLA via the cla-assistant.io link — the badge may take a moment to refresh. Happy to proceed with the review whenever you get a chance, and let me know if anything needs changing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

hooks: permission gate should read the rewrite's own wrapper scan (follow-up to #4530)

4 participants