Skip to content

fix(hook): prevent flag injection in rtk-rewrite hooks (#1350)#2475

Open
ousamabenyounes wants to merge 1 commit into
rtk-ai:developfrom
ousamabenyounes:fix/issue-1350
Open

fix(hook): prevent flag injection in rtk-rewrite hooks (#1350)#2475
ousamabenyounes wants to merge 1 commit into
rtk-ai:developfrom
ousamabenyounes:fix/issue-1350

Conversation

@ousamabenyounes

@ousamabenyounes ousamabenyounes commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Fixes #1350.

The Claude/Cursor rewrite hooks called rtk rewrite "$CMD" without a -- terminator. When a user command begins with a hyphen (--help, -h), clap interprets it as a flag to the rewrite subcommand: it prints help/usage text and exits 0. The hook then feeds that help text back to the agent as the "rewritten" command, which is shell-executed — a flag-injection footgun.

Fix

  • Add the -- terminator: rtk rewrite -- "$CMD" in both hooks/claude/rtk-rewrite.sh and hooks/cursor/rtk-rewrite.sh, so hyphen-prefixed input is always captured as a positional arg.
  • Add a clap-level regression test asserting rtk rewrite -- <flagish> parses the input as a positional rather than triggering clap's built-in help/version paths.
  • Add hook-level shell tests (section 5b) covering --help, -h, --version, -V, and lone --.

Test verification (RED → GREEN)

RED - prod fix reverted (rtk rewrite "$CMD"), hooks/claude/test-rtk-rewrite.sh section 5b:

--- Flag injection safety (#1350) ---
  FAIL flag injection: --help must not trigger clap help
  -h, --help
  FAIL flag injection: -h must not trigger clap short help
  -h, --help           Print help (see more with '--help')

GREEN - with the -- terminator, after rebasing onto current develop:

--- Flag injection safety (#1350) ---
  PASS flag injection: --help must not trigger clap help -> (no rewrite)
  PASS flag injection: -h must not trigger clap short help -> (no rewrite)
  PASS flag injection: --version must not trigger clap version -> (no rewrite)
  PASS flag injection: -V must not trigger clap short version -> (no rewrite)
  PASS flag injection: lone -- must not crash the hook -> (no rewrite)

Rust targeted regression:

cargo test test_rewrite_clap_double_dash_blocks_flag_injection
test tests::test_rewrite_clap_double_dash_blocks_flag_injection ... ok
test result: ok. 1 passed; 0 failed

Full local gate after rebase:

cargo fmt --all --check && cargo clippy --all-targets && cargo test
test result: ok. 2393 passed; 0 failed; 8 ignored
test result: ok. 6 passed; 0 failed
test result: ok. 11 passed; 0 failed
test result: ok. 6 passed; 0 failed
test result: ok. 14 passed; 0 failed
test result: ok. 5 passed; 0 failed
test result: ok. 11 passed; 0 failed

(Re-proposes the work from the accidentally-closed #1370, rebased onto current develop.)

@ousamabenyounes

ousamabenyounes commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

Rebased this PR onto current develop and pushed 960dc37. The conflict is resolved, the PR is mergeable again, and the latest GitHub CI is green across fmt, clippy, tests on Ubuntu/Windows/macOS, security scans, benchmark, and CLA. Ping @aeppling

@pszymkowiak pszymkowiak left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reproduced the bug live, and re-tested with real usage after the fix — no regressions (git status, git log -10, cargo test --release, chained commands, ls -al all rewrite identically to before). Hook shell test suite passes 5/5 on the flag-injection cases. cargo fmt, cargo clippy --all-targets, cargo test --all all clean.

@pszymkowiak
pszymkowiak requested a review from aeppling July 9, 2026 09:32
Before this change, the Claude and Cursor rewrite hooks called
`rtk rewrite "$CMD"` without a `--` terminator. An LLM-generated command
starting with `-`/`--` (e.g. "--help") would be intercepted by clap as a
flag to the `rewrite` subcommand, causing the help text to be emitted as
the "rewritten" command and fed back to the agent, which would then
shell-execute the help-text content.

Adding `--` makes clap treat `$CMD` as a positional argument so no
flag-injection is possible. New tests exercise `--help`, `-h`, `--version`,
and `-V` at both the CLI parser level (Rust) and the hook level (bash).

Co-Authored-By: Claude <noreply@anthropic.com>
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.

Security: rtk-rewrite.sh missing -- terminator on rtk rewrite "$CMD" (line 55) enables flag-injection

2 participants