Repository navigation
Description
Security: command lexer doesn't split on newlines → rtk hook claude auto-allows dangerous trailing statements
Summary
The Claude Code PreToolUse hook (rtk hook claude) decides permissionDecision: "allow" by splitting the command into segments and requiring every segment to match a user allow rule. But the lexer does not treat a newline as a command separator — only |, ||, ;, &&. A newline-separated, multi-statement command therefore collapses into a single segment, which is matched by prefix on the command at its start. If that leading command is allow-listed (git log, cat, ls, tail, grep, …), the entire block is approved — including arbitrary trailing statements such as rm -rf /, git push --force-with-lease, or git commit --amend — with no prompt, bypassing the user's allow/deny rules.
Reproduction
A safe leading command launders a destructive one:
printf '%s' '{"tool_name":"Bash","tool_input":{"command":"cat /etc/hostname\nrm -rf --no-preserve-root /"}}' \
| rtk hook claude{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecisionReason":"RTK auto-rewrite","updatedInput":{"command":"rtk read /etc/hostname\nrm -rf --no-preserve-root /"},"permissionDecision":"allow"}}permissionDecision: "allow" is returned for the whole block, so rm -rf --no-preserve-root / runs without a prompt — even though rm is not in any allow rule. The same is true for git push --force-with-lease, git commit --amend, dd …, chmod -R 777 /, eval …, etc.
The same dangerous command on its own is correctly not auto-allowed (no permissionDecision, so Claude Code prompts):
printf '%s' '{"tool_name":"Bash","tool_input":{"command":"git push --force-with-lease origin main"}}' \
| rtk hook claude(Use printf '%s' so \n reaches rtk as a literal \n in the JSON string, which is decoded to a real newline.)
Why pipes behave differently (confirms the root cause)
| is a real split point, so it exposes the post-pipe command as its own segment that must independently match an allow rule:
| Command | Verdict | Why |
|---|---|---|
git log -1\nrm -rf / |
allow | one segment, prefix-matches git log |
git log -1\nrm -rf / | tail |
allow | segments git log…rm -rf / + tail; both match (tail is allow-listed) |
git log -1\nrm -rf / | bash |
none | bash segment matches no allow rule |
git log -1\nbash evil.sh |
allow | no pipe → one segment, prefix-matches git log |
So the verdict is a correct per-segment allowlist for the operators the lexer knows about; the defect is purely that newline is not one of them.
Root cause (code)
src/discover/lexer.rs—tokenize()emits a separator token only for|,||,;,&&. There is no case for\n/\r; newlines are consumed as ordinary whitespace inside a segment.split_on_operators()(and thussplit_compound_command()insrc/hooks/permissions.rs) therefore never cuts on a newline.src/hooks/permissions.rs—command_matches_pattern()matches a non-wildcard / trailing-*rule by prefix:cmd == pattern || cmd.starts_with("{pattern} "). A segment that beginsgit log -1\nrm -rf /satisfiesstarts_with("git log ")and so matchesBash(git log:*).
Result: a multi-line block is one prefix-matched segment, validated solely by its first command; everything after the first newline is approved unchecked.
This is the same newline-blindness reported in #2191 (there framed as a token-savings miss). The per-segment allowlist hardening (#260 / #1213) is defeated by it, because the dangerous statement is never isolated as its own segment.
Impact
Any allow/deny guardrail can be bypassed by prefixing a dangerous command with a newline after a benign, allow-listed one — and git log, git diff, git status, ls, cat, grep, find are exactly the commands agents run at the start of almost every turn.
Affected versions
Confirmed in released 0.42.1 and on current main (dev-0.43.0-rc.260): src/discover/lexer.rs and src/hooks/permissions.rs are byte-identical between tag v0.42.1 and HEAD.
Suggested fix
Treat \n (and \r) as a command separator in tokenize() so each line becomes its own segment that must independently match an allow rule. With that change, cat …\nrm -rf / splits into two segments, rm -rf / fails the allowlist, and the block falls through to Claude Code's prompt.
Activity
pszymkowiak commentedon Jun 4, 2026
🔍 Automated Triage
bugcriticalSummary
Security bypass: newlines not treated as command separators in lexer.rs, allowing dangerous trailing commands after a benign allow-listed command to be auto-approved without prompting — fix by adding '\n'/'\r' as separator tokens in tokenize().
📁 Relevant files
src/discover/lexer.rssrc/hooks/permissions.rsTriaged automatically by wshm · This is an automated analysis, not a human review.
rpelevin commentedon Jun 9, 2026
This is exactly the kind of case where the approval unit has to be the executed command segment, not the first prefix that matched an allow rule.
I would make the hook compute a decision per parsed execution segment, then aggregate to one final decision for the full Bash block.
The record for a compound command could carry:
Regression tests I would add:
git log ...\nrm ...is split into two segments and does not inheritgit logapproval.&&,||, pipe, and newline all exercise the same segment-verdict path.permissionDecision: "allow"is emitted only if every executable segment independently matches an allow rule.That keeps a safe prefix from laundering authority for the rest of the command block.
KuSh commentedon Jul 25, 2026
Hi, should have been fixed by 952245d. If not the case or some edge cases missing, please reopen with additional details
bastewart commentedon Sep 8, 2026
Ah, nice. Thank you!