fix: strip env variable prefixes from permission pattern matching#31103
Closed
weiconghe wants to merge 1 commit into
Closed
fix: strip env variable prefixes from permission pattern matching#31103weiconghe wants to merge 1 commit into
weiconghe wants to merge 1 commit into
Conversation
The `source()` function returned the full command text including leading `variable_assignment` nodes (e.g., `GOFLAGS=-mod=vendor go test ./...`). Permission patterns like `go *` could not match, causing unnecessary permission prompts even when the rule should apply. Skip leading `variable_assignment` children in `source()` so the pattern becomes just the command portion (`go test ./...`), matching what `parts()` already does for the `always` auto-approval patterns. Closes anomalyco#14110 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Contributor
|
The following comment was made by an LLM, it may be inaccurate: Potential Duplicate PRs Found:
These PRs seem to be addressing the same or very similar issue. PR #31103 (the current PR) should be checked against these two to ensure it's not duplicating work already done or merged. |
Contributor
Author
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #14110
Type of change
What does this PR do?
The
source()function inpackages/opencode/src/tool/shell.tsreturns the full command text for permission pattern matching. When a command has inline environment variable prefixes (e.g.,GOFLAGS=-mod=vendor go test ./...), the tree-sitter AST includesvariable_assignmentnodes at the start of thecommandnode.source()returned the full text including these nodes, so the permission pattern becameGOFLAGS=-mod=vendor go test ./...instead ofgo test ./....This meant permission rules like
"go *": "allow"would not match, and users would get unnecessary permission prompts even though the command should be allowed.Note: The
parts()function (used foralwaysauto-approval patterns) already correctly skipsvariable_assignmentnodes. Only thepatternspath viasource()was affected.Fix: Skip leading
variable_assignmentchildren insource()by finding the first non-variable_assignmentchild and slicing the text from its offset. This correctly handles:NODE_ENV=production echo hello→echo helloNODE_ENV=production DEBUG=1 go test ./...→go test ./...go test ./...→go test ./...(unchanged)NODE_ENV=production go test ./... > /dev/null→go test ./... > /dev/nullHow did you verify your code works?
Added two new test cases in
packages/opencode/test/tool/shell.test.ts:NODE_ENV=production echo helloproduces patternecho helloNODE_ENV=production DEBUG=1 go test ./...produces patterngo test ./...Both tests verify that the
alwaysauto-approval pattern is also correct (e.g.,echo *,go test *). Tests run for bash and cmd shells (PowerShell doesn't support this syntax).Test results: 68 pass, 1 fail (pre-existing Windows path normalization test unrelated to this change).
Screenshots / recordings
N/A
Checklist