Skip to content

fix: strip inline env var prefixes from bash permission patterns#28475

Draft
prullanferragut wants to merge 1 commit into
anomalyco:devfrom
prullanferragut:fix/shell-env-var-prefix-permission-bypass
Draft

fix: strip inline env var prefixes from bash permission patterns#28475
prullanferragut wants to merge 1 commit into
anomalyco:devfrom
prullanferragut:fix/shell-env-var-prefix-permission-bypass

Conversation

@prullanferragut
Copy link
Copy Markdown

@prullanferragut prullanferragut commented May 20, 2026

Issue for this PR

Closes #16075

Type of change

  • Bug fix

What does this PR do?

In collect() inside shell.ts, the permission pattern for a command was built by calling source(node), which returns the raw node.text. For a command like GITHUB_TOKEN=x git push --force, tree-sitter parses the variable_assignment as a child of the command node, so node.text includes it. The resulting pattern GITHUB_TOKEN=x git push --force does not match a rule like "git push --force*": deny, allowing the bypass.

The parts() function already walks the command node's children and intentionally skips variable_assignment nodes — it's what builds the tokens array used for the always pattern, which was already correct. The fix replaces source(node) with tokens.join(" ") for the patterns entry, bringing it in line with how always was already computed. Redirection suffixes (e.g. > output.txt, 2>&1) are re-attached from the parent redirected_statement node so that existing redirect-pattern matching continues to work.

The now-dead source() function is removed.

How did you verify your code works?

Ran bun test test/tool/shell.test.ts --timeout 60000 — 26 pass, 0 fail.

Three new integration tests exercise the permission-scanning path directly via the capture() helper (halting execution at the permission prompt):

  • CI=true git commit -m "test" → pattern must be git commit -m "test", not contain CI=true
  • FOO=1 BAR=2 echo hello → pattern must be echo hello, not contain FOO=1
  • CI=true echo hello > output.txt → pattern must be echo hello > output.txt, not contain CI=true (exercises both branches simultaneously)

The existing redirect test (echo test > output.txtecho test > output.txt) continues to pass, confirming the redirection re-attachment is correct.

Test groundwork from #16086.

Screenshots / recordings

N/A — no UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Commands like 'GITHUB_TOKEN=x git push --force' were matching '*': allow
instead of the intended deny rule because variable_assignment nodes were
included in the pattern string used for permission matching.

Build the permission pattern from parts() tokens (which already exclude
variable_assignment children) rather than from the raw node text, then
re-attach any redirection suffix from the parent redirected_statement so
'echo test > output.txt' still produces the full pattern as before.

Also adds integration tests verifying the corrected behavior.

Based on test groundwork from PR anomalyco#16086.
Fixes issue anomalyco#16075
@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels May 20, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

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.

[Bug]: Inline env var prefix (e.g. CI=true git commit) bypasses bash permission rules

1 participant