Skip to content

Agent hooks read shell redirections: 2>&1 no longer ends the command and hides the flags after it (#154) - #155

Merged
paureis merged 4 commits into
mainfrom
fix/154-redirections
Oct 26, 2026
Merged

paureis merged 4 commits into
mainfrom
fix/154-redirections

Conversation

@paureis

@paureis paureis commented Oct 26, 2026

Copy link
Copy Markdown
Owner

Closes #154. Milestone 1.2.0: the owner moved it in on 2026-10-25, so it ships Thursday 2026-10-29.

The bypass

In agent hooks and cloudpin explain, findInvocations split 2>&1 at the &, as if & ended the command. az 2>&1 group list --subscription <another subscription> was read as az 2>, and the verdict was "the account matches the pin". This was checked against a real az login. Redirection words also stayed in the arguments (> out.txt, 2>/dev/null). The shell wrappers were never affected.

The fix

Tests

The tests are in the new file test/shellwords-redirections.test.ts, 79 of them. They were committed failing first: 69 failed.

  • Hook-level tests, through runHook(AGENTS.claude) with the real azure and vercel providers and a fake Exec:
    • 7 Bash and 3 PowerShell cases of --subscription other with the redirection in different places are denied;
    • 3 pinned controls are allowed;
    • vercel 2>&1 deploy --scope side is denied, and with --scope acme it's allowed.
  • Results: typecheck passes. Full suite: 53 files, 1467 passed, 8 skipped.
  • Mutations: the builder ran 7, and all 7 were caught. I ran one more: redirection detection switched off makes 69 tests fail.
  • End to end: I re-ran the real-az cloudpin explain check on the build. Both 2>&1 placements are now stopped, and az group list 2>&1 with the matching pin is still allowed.

Left open

  • Words after a process substitution start a new segment: vercel deploy <(x) --scope other loses --scope other. This existed before this change; it gets its own issue.
  • PowerShell passes word>file (no space) as one argument, but this parser follows bash and splits it. A CLI would then get an invalid value and fail.
  • A stream number before a here-doc (0<<EOF) stays as the argument 0. That errs on the side of checking.

🤖 Generated with Claude Code

paureis and others added 4 commits October 25, 2026 22:13
A pure move, no behaviour change: USER_ONLY_VARS and envFor, with their
doc comments, go verbatim from src/shellwords.ts (over its size limit) into
the new src/user-env.ts, which imports only the Invocation type from
shellwords.ts. The importers (hooks/agents.ts, explain.ts and three tests)
import envFor from user-env.ts directly; nothing else used it.

(cherry-picked from 5322307; its hunks for two tests that do not exist on this branch were dropped)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
#154)

Failing on its own, before the fix. findInvocations reads `2>&1` as the
end of the command at its `&`, so `az 2>&1 group list --subscription other`
is checked as `az 2>`, and it keeps `>`, `out.txt` and `2>/dev/null` as
arguments.

The new tests cover every bash and PowerShell redirection form, with the
target attached and separate, before the command word, between and after
the flags; that && || ; | |& and & still split commands; heredocs, process
substitution and a command substitution in the target; the cases where the
word is kept (x2>out, "2">out, \2>out, 2&>out, 12>out); and wrong-pin
controls through the Claude Code hook for az (Bash and PowerShell tools)
and vercel, with matching pins still allowed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…154)

The tokenizer split `2>&1` at its `&`, as if `&` ended the command, so
everything after it was lost: `az 2>&1 group list --subscription other`
was checked as `az 2>` against the default subscription. It also kept
redirection words as arguments, where they could pass for a command word,
a deploy path or a flag value.

The new src/redirections.ts recognises the operators (bash: > >> < >| <>
>& <& &> &>>; PowerShell's have the same shapes) and the stream number
written right before one (a single digit, or PowerShell's *). The
tokenizer drops the stream number, the operator and its target word,
attached or after spaces, wherever they appear in the command, before the
command word too. A command substitution in the target still runs and is
still checked. && || ; | |& and & split commands as before, and <( and >(
stay process substitutions.

Where the line could mean either, the word is kept: x2>out, "2">out and
\2>out pass their word as bash does, 2&>out passes 2, and 12>out keeps 12
(bash reads 12 as a stream, PowerShell passes `12>out` as one argument).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@paureis paureis added this to the 1.2.0 milestone Oct 26, 2026
@paureis
paureis merged commit 1b67e38 into main Oct 26, 2026
5 checks passed
@paureis
paureis deleted the fix/154-redirections branch October 26, 2026 02:33
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.

Agent hooks: 2>&1 ends the command and hides the flags after it; redirections stay in the arguments

1 participant