Skip to content

Follow or refuse the variables a line sets (#163, part 2) - #183

Merged
paureis merged 5 commits into
mainfrom
fix/163-environment
Nov 5, 2026
Merged

paureis merged 5 commits into
mainfrom
fix/163-environment

Conversation

@paureis

@paureis paureis commented Nov 5, 2026

Copy link
Copy Markdown
Owner

Part of #163 (PR 2 of 5, the environment family). Design: docs/superpowers/specs/2026-11-04-fail-closed-design.md.

The bug. Variables that pick the account, set in forms the parser didn't read, reached the CLI unseen: set -a; GH_HOST=x, export X+=y, ${env:X}='v', Set-Item env:X v, [Environment]::SetEnvironmentVariable(...) and others. Measured with a printer named like the CLI: the wrong call really ran with no objection.

The fix: follow or refuse (new src/line-env.ts).

  • Followed in bash: set -a, set -o allexport, X+=y everywhere, local -x, export X; X=v, declare and friends on an exported name, export $V where the line knows V, export -n, bash -a -c.
  • Followed in PowerShell 5.1: ${env:X}=, $env:X ='v', +=, Set-Item, Set-Content, New-Item, Remove-Item, Clear-Item and their aliases, SetEnvironmentVariable with no target or Process. A User or Machine target changes nothing for later calls (from .NET's source; not run, because it writes to the registry).
  • Refused for any later guarded, non-exempt call: . file, source file, export $(...), read $V, Rename-Item and Copy-Item on env:, env:*. A call before them isn't affected.
  • Refused by name, only for the CLI that name picks the account for: read X, $env:X set inside a block, and an unknown value through any new form.
  • CLOUDPIN_* stays ignored in every new form. explain and cloudpin_check agree with the hook. With no pin file, or one that doesn't pin this CLI, nothing stops.

Known cost. source venv/bin/activate && aws ... now stops as "can't tell" in a project that pins aws, because a sourced file can set anything. It's rare in the commands measured, but other people may run this shape more often.

Evidence

  • npm run typecheck exit 0; npx vitest run: 3904 passed, 19 skipped. 435 new tests, seen failing first.
  • 156 mutations by the builder, every survivor read and tested until none was left among the re-runs; the full list was not re-run after three simplifications. One more here: dropping guard's check fails 44 tests.
  • Real shells with a printer (213 lines, Git Bash 5.2 and PowerShell 5.1): this branch 165 same, 47 refused, 1 wrong; main 52 same, 5 refused, 156 wrong, so the check can fail. The one wrong line is local -x FOO=l; gh outside a function, which bash rejects with an error.
  • The built hook, end to end: a right pin denies after . file, source file, export $(...) and set -a; GH_HOST=x, and passes for a call before the source, an exempt call, a line with no guarded call, an unrelated variable, and a folder with no pin file.
  • On about 31,340 of the owner's real commands (counts only): 1 new stop, a self-test in this repo.

Not tested

pwsh, zsh, macOS and Linux shells.

Left open

printf -v, ${X:=y}, declare -u/-l/-i and namerefs; .\script.ps1 and & script.ps1, which can set process variables; false && export X=y, read as if it ran. A plain PowerShell variable set in a block ($s='a'; if ($c) { $s='b' }; az --subscription $s) reads the first value; that goes in PR 3.

🤖 Generated with Claude Code

paureis and others added 5 commits November 4, 2026 21:33
A call after set -a, export X+=y, local -x, ${env:X}=, Set-Item env:X,
[Environment]::SetEnvironmentVariable or Remove-Item Env:X ran with variables
cloudpin didn't read, so it could land on the wrong account with no objection.

Each literal form is now followed, through the same path as export, so a value
reaches the identity lookup, and cloudpin's own variables stay ignored. What can
set a name cloudpin can't read (. file, source file, export $(...), read $V)
stops later guarded, non-exempt calls as "can't tell", through the existing
path and only with a pin in play. A name it can read with a value it can't
(read X, $env:X inside a block) stops only the CLI that name picks the account
for. A subshell, a pipe's side and a job keep what they set, as in bash.

New logic is in src/line-env.ts; shellwords.ts got shorter.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ns (#163)

read -- needs no branch of its own, PS names need one test, and a cast on a plain
PowerShell variable is outside this family, so it goes back.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Also where the User and Machine reading of SetEnvironmentVariable comes from.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@paureis
paureis merged commit 2e4ff65 into main Nov 5, 2026
4 checks passed
@paureis
paureis deleted the fix/163-environment branch November 5, 2026 02:47
Comment thread src/ps-words.ts

const ASSIGNMENT = /^\$(?:(?:script|global|local|private):)?([A-Za-z_]\w*)([-+*/%]?=)?/i;
// `$env:X` and `${env:X}`, with a cast or not (#163): PowerShell takes them all.
const ENV_ASSIGNMENT = /^(?:\[[\w.[\]]+\])*\$(?:env:([A-Za-z_]\w*)|\{env:([^}]+)\})([-+*/%]?=)?/i;
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.

2 participants