Skip to content

Security: command lexer doesn't split on newlines, so the Claude hook auto-allows dangerous trailing statements #2262

Closed

Description

@bastewart

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 thus split_compound_command() in src/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 begins git log -1\nrm -rf / satisfies starts_with("git log ") and so matches Bash(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

  1. pszymkowiak commented on Jun 4, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🔴 Priority critical
    🎯 Confidence 97%

    Summary

    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().

    This looks like a simple fix that could be auto-resolved. Repo maintainers can use /wshm fix to attempt it.

    📁 Relevant files
    • src/discover/lexer.rs
    • src/hooks/permissions.rs

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

  2. rpelevin commented on Jun 9, 2026

    @rpelevin

    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:

    • original Bash input digest;
    • rewritten Bash input digest, if any;
    • parsed segment list;
    • separator type for each segment, including newline and CRLF;
    • matched permission rule per segment;
    • segment verdict: allow, ask, deny, parse_error;
    • final verdict for the full command block.

    Regression tests I would add:

    1. git log ...\nrm ... is split into two segments and does not inherit git log approval.
    2. CRLF is treated the same as LF.
    3. semicolon, &&, ||, pipe, and newline all exercise the same segment-verdict path.
    4. a parse error fails closed to prompt/deny rather than auto-allow.
    5. redirects or shell forms attached to an allowed reader command do not disappear from the segment being evaluated.
    6. the final 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.

  3. KuSh commented on Jul 25, 2026

    @KuSh
    Collaborator

    Hi, should have been fixed by 952245d. If not the case or some edge cases missing, please reopen with additional details

  4. bastewart commented on Sep 8, 2026

    @bastewart
    Author

    Ah, nice. Thank you!

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions