Skip to content

fix(core): preserve env prefixes in saved shell patterns - #53802

Closed
zenv-dev wants to merge 7 commits into
anomalyco:devfrom
zenv-dev:env-saved-pattern
Closed

zenv-dev wants to merge 7 commits into
anomalyco:devfrom
zenv-dev:env-saved-pattern

Conversation

@zenv-dev

@zenv-dev zenv-dev commented Oct 7, 2026 •

Copy link
Copy Markdown

Issue for this PR

Fixes #52720. Related: #45914.

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Saved rules retain literal env prefixes and existing executable arity. Raw authorization resources and deny precedence stay unchanged. Unrepresentable prefixes containing *, ?, or backslash are omitted.

How did you verify your code works?

Tests cover quoted here-strings and native reply(always) persistence, including repeats, scoped negatives and raw deny.

Local isolated suite: 3300 passed, 356 skipped, 0 failed. bun run check: 36/36 tasks with process-local memory limits. GitHub CI is not verified.

Screenshots / recordings

Not applicable (no UI change).

Checklist

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

@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Keep the literal leading source of commands with inline assignments in
shell approval proposals from both scanners. Retain raw authorization
resources and omit proposals whose new prefix cannot be represented
literally by the matcher. Leave no-assignment proposals unchanged.

Constraint: Preserve raw authorization and configured deny precedence
Not-tested: Installed CLI update and Windows execution
@zenv-dev zenv-dev changed the title fix(opencode): preserve env prefixes in saved shell patterns fix(core): preserve env prefixes in saved shell patterns Oct 8, 2026
@zenv-dev
zenv-dev changed the base branch from dev to v2 October 8, 2026 10:16
Extend the native shell approval fixture so an additional assignment, a
different executable and a bare command must still ask before the
auxiliary broad grant is added. Also inline the single-use scan
argument, rename the changed-argument check and label each scanner
assertion so a failure names the scanner that ran.

Constraint: raw authorization unchanged; test-only, no production change
@zenv-dev
zenv-dev marked this pull request as ready for review October 8, 2026 19:10

@opencode-agent opencode-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reproduced the bug on the base branch, and this PR fixes it. On the base, both scanners propose an 'Always allow' pattern without the assignment (FOO=1 git status → git status *), and that pattern never matches the command, so the user is asked every time. With this PR the pattern is FOO=1 git status *, and it matches. I checked this with a direct ShellParse.scan script on both checkouts. The closest tests in packages/core (shell-parse, shell-scan, permission, tool-shell) pass, and typecheck is clean.

The approach looks right. Dropping a missing pattern from the save list is safe, because the permission code saves that list as a whole and never pairs it with the commands by position. Omitting patterns whose prefix contains *, ? or \ covers every character Wildcard.match treats as special.

Two things worth fixing:

  • With the Tree-sitter scanner, which is the default outside local/dev channels, the saved prefix can run past a ;.
  • The tests are much larger than the fix needs.

Details are in the inline comments.

Comment thread packages/core/src/shell/parse.ts Outdated
result.commands.push({ resource, save: `${prefix(tokens).join(" ")} *` })
return result
}
const save = proposal(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the Tree-sitter scanner, span can cover more than one statement. For example, FOO=bar > output; printf done is read as a single command, so the saved prefix becomes FOO=bar > output; and the user gets a saved rule FOO=bar > output; printf * (the updated assignment redirect prefix case in permission.test.ts now expects this). The portable scanner gives printf * for the same input. It's narrower rather than broader, so not a safety issue, but it's a rule nobody would expect to see in their saved list. Could you skip the proposal (or fall back to the portable behaviour) when the prefix contains a statement separator or newline, instead of changing the test to expect the glued rule?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed, this change introduced the glued saved prefix. In 97b94b4, Tree-sitter omits that proposal when the head crosses a statement boundary, while retaining the existing raw resource. The portable result is unchanged. Added semicolon/newline regressions and quoted-value controls so separators inside an assignment or redirect are not mistaken for statement boundaries.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I checked 97b94b4. With Tree-sitter, FOO=bar > output; printf done and the newline form now save no pattern, and a quoted ; (FOO="a;b" git status) still gets FOO="a;b" git status *. Both are good.

The same glued span still happens with the other operators, though. On the new head, Tree-sitter still saves a rule that spans two commands for:

  • FOO=bar > output & printf done → FOO=bar > output & printf *
  • FOO=bar > output && printf done → FOO=bar > output && printf *
  • FOO=bar > output || printf done → FOO=bar > output || printf *
  • FOO=bar > output | printf done → FOO=bar > output | printf *

The portable scanner gives printf * for all of these. Could crossesStatementBoundary also treat & and | outside assignment/redirect nodes as boundaries? Another option is to skip the proposal whenever the text between those nodes isn't just whitespace. One small case per operator next to the ;/newline ones would cover it.

}),
))

test(`${scanner}: environment prefixes persist scoped approval and preserve raw deny`, () =>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test (about 75 lines, run once per scanner) checks a lot in one go: always → repeat → changed argument → three negative commands → a manually added printf * rule → a deny override, plus marker file I/O between steps. The parser-level tests in shell-parse.test.ts already pin the proposals. Here, one end-to-end check would be enough: an approved FOO=bar printf hello repeats without a prompt, and FOO=bar echo hello still asks. Please cut this down, and likewise trim the long test.each tables in shell-parse.test.ts to the core cases (single and multiple assignments, a leading redirect, an unsafe character, a compound command).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trimmed the repeated success checks, redundant marker cleanup and overlapping empty-executable coverage in 97b94b4. Kept the scope-negative cases, no env-stripped fallback check, configured deny and unsafe-save round-trip: these exercise persisted permission matching, which parser-level assertions alone do not cover. The tests still use the existing native fixture and actual always approval.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I see the trim in 97b94b4. The test went from about 75 to about 70 lines and still runs once per scanner. Fair point that the scope-negative and deny cases test matching against saved rules, which the parser tests don't reach. I won't push further on this one; maintainers can decide whether it's worth cutting more. The open item on this PR is the other thread: Tree-sitter still saves glued rules across &, &&, || and |.

Avoid saving a prefix that joins an assignment and redirect statement
with a later executable. Preserve quoted values and raw authorization.
Reduce repeated test mechanics without removing permission boundaries.

Constraint: No raw-resource normalization or portable-scanner changes
Treat background, conditional and pipeline operators as boundaries in
recovered Tree-sitter heads. Keep quoted values and raw authorization intact.

Constraint: Preserve newline checks and portable behavior
Exclude here-string redirect content from recovered statement boundaries.
Keep resource collection separate from Tree-sitter proposal policy and
strengthen literal parser and native approval regression coverage.

Constraint: Preserve raw resources, executable arity and configured deny priority
Confidence: medium
Scope-risk: narrow
Not-tested: Full affected suite and canonical check await independent verification
Share actual approval lifecycle mechanics while preserving literal scoped
grants, temp-local marker checks, saved rules and raw-deny precedence.
Require usable PowerShell and unprefixed pipeline proposals.

Constraint: Preserve production behavior and independent literal expectations
Confidence: medium
Scope-risk: narrow
Not-tested: Renewed independent whole-diff review and full checks pending
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.

bash permission "Always allow" never matches commands with leading environment variable assignments

1 participant