Repository navigation
Fix PowerShell parsing for standalone -- tokens - #335465
Ingvar Stepanyan (RReverser) wants to merge 6 commits into
Conversation
📬 CODENOTIFYThe following users are being notified based on files changed in this PR: Anthony Kim (@anthonykim1)Matched files:
|
There was a problem hiding this comment.
🔵 Needs a closer look
Terminal auto-approval parsing is security-sensitive and warrants final human review of PowerShell edge cases.
Pull request overview
Fixes PowerShell parsing of standalone -- tokens used by native CLI tools.
Changes:
- Masks standalone
--tokens before tree-sitter parsing while preserving offsets. - Adds regression coverage for Git pathspecs and
npm exec.
File summaries
| File | Description |
|---|---|
treeSitterCommandParser.ts |
Extends PowerShell native-argument masking. |
treeSitterCommandParser.test.ts |
Adds standalone -- parsing tests. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 0
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
Hi Ingvar Stepanyan (@RReverser) I was also looking into the standalone
In my local testing, Tree-sitter parses the original Rather than masking every standalone |
Ugh it just keeps getting weirder. So tree sitter is fine with trailing But yeah, good catch if so, I'll update the PR. |
|
$val -- # OK
-- $val # OK
git diff -- # Error
git diff -- file.txt # Error |
|
Ugh that's trickier to distinguish. I guess an easy option might be to replace |
d15b09a to
2e599be
Compare
Did this. I think it's conceptually more correct too - standalone |
|
Megan Rogge (@meganrogge) Would really appreciate reviews on this one - GPT models love issuing a lot of |
|
Megan Rogge (@meganrogge) This PR addresses the |
|
Martin Aeschlimann (@aeschli) Anthony Kim (@anthonykim1) If Megan is not available, can someone else please prioritise reviewing this PR? The fix is already ready and fairly small - leaving it unresolved pushes users to the less secure |
|
Please, it's been stuck for a long time, wasting hours every week when agent gets stuck in yet another |
Anthony Kim (anthonykim1)
left a comment
There was a problem hiding this comment.
Thanks for pushing on this. git diff -- … prompting every time is a real pain, and the diagnosis is right. Two things block merging as-is (details inline):
- With
--, a redirect to a variable (> $target,>> $PROFILE) isn't detected as a file write. On main, the parse error still forced confirmation. With this change that guard clears, so the command becomes auto-approvable. - The new
git diff --test fails because the trailing--is dropped from the extracted command.
I left suggested fixes for both, plus a non-blocking ask to fail closed on stray --. Agent Host has its own equals-only copy of this workaround, so this fix won't reach it. No need to touch it here; consolidation is tracked in #329028.
2e599be to
5ad59e9
Compare
|
Anthony Kim (@anthonykim1) Addressed all comments, please take another look. |
|
Anthony Kim (@anthonykim1) Any outstanding concerns? I hate pinging on Github as much as the next OSS maintainer, but this one has genuinely been annoying issue for a while now... |
Anthony Kim (anthonykim1)
left a comment
There was a problem hiding this comment.
The earlier concerns are addressed, but one regression remains in the standalone -- handling. Details inline.
|
lets try to get this in for next week's release |
|
Looks like it didn't make it into 144. |
|
Oh no, why was it removed from new 1.145 release too? Anthony Kim (@anthonykim1) |
|
Aaron Munger (@amunger) This was supposed to go out couple of releases ago - any particular reason it was removed? |
|
Ping again... we are so close 😅 |
Anthony Kim (anthonykim1)
left a comment
There was a problem hiding this comment.
Thanks, this is close. The latest commit handles the comment case, and the cases from the earlier reviews now behave as expected. Two small things inline.
Authored with Copilot on behalf of anthonykim1
| const isWithin = (span: { start: number; end: number }) => span.start <= start && span.end >= start + 2; | ||
| // A `--` inside a command is a native CLI separator, and one inside a comment is inert text; | ||
| // anything else was dropped by the parser and must fail closed. | ||
| if (!commandSpans.some(isWithin) && !commentSpans.some(isWithin)) { |
There was a problem hiding this comment.
Same false positive as the comment case, but for strings: a -- inside a string that isn't part of a command now makes the line unanalyzable. git status; 'a -- b', (Get-Content f) -replace ' -- ', ' ' and if ($a -eq ' -- ') { git status } were analyzable on main and now need confirmation, even though git status and Get-Content are allowed by default.
Skipping matches inside single-quoted strings fixes it: capture (verbatim_string_characters) and (verbatim_here_string_characters) in the query on L87 and treat them like comment spans. I'd leave double-quoted strings alone, since a -- inside their $( … ) is code. I tried this against the tests in this file and they all still pass. Could you add git status; 'a -- b' as an analyzable case?
Authored with Copilot on behalf of anthonykim1
| .replace(pwshFlagEqualsRegex, (_, pre, flag) => `${pre}${flag} `) | ||
| .replace(pwshStandaloneDoubleDashRegex, (_, pre) => `${pre} `); |
There was a problem hiding this comment.
The two masks chain: the --flag= mask runs first and can create a new standalone --, which the second mask then blanks. So git log --format=-- is reported as an analyzable git log --format instead of failing closed as on main, and git status; --=x becomes analyzable with x captured as a command.
Running the standalone -- mask first fixes both, and the tests in this file all still pass. Could you add git log --format=-- as an unanalyzable case?
Authored with Copilot on behalf of anthonykim1
- Fixes microsoft#325635. - Fixes microsoft#324059. - Fixes microsoft#321748. Inspired by a similar fix in microsoft#310839.
7cc1b3a to
99aa5d0
Compare
|
Anthony Kim (@anthonykim1) The complexity started getting out of hand, and instead of fixing new cases one by one I've gone back to the simpler approach. Most of the new regressions ( Stripping a stray So I went back to the simpler version while keeping the new tests:
|
Anthony Kim (anthonykim1)
left a comment
There was a problem hiding this comment.
One more case where the -- mask hides a command. Details inline.
Authored with Copilot on behalf of anthonykim1
| * TODO: Remove once upstream tree-sitter PowerShell grammer is updated. | ||
| */ | ||
| const pwshFlagEqualsRegex = /(^|\s)(-{1,2}[\w-]+)=/g; | ||
| const pwshStandaloneDoubleDashRegex = /(^|\s)--(?=\s|$)/g; |
There was a problem hiding this comment.
This regresses a case that main handles correctly: a malformed here-string header requires confirmation on main, but is auto-approved with this change. The test at L289 covers -- in the body of a valid here-string. The header line is different: PowerShell rejects anything but whitespace after @' or @", and masking -- there turns an invalid header into one tree-sitter accepts. For this input:
Write-Output @' --
Remove-Item -Recurse -Force ./important
'@extractAutoApprovalSubCommands returns:
// main: requires confirmation
{ subCommands: [], hasUnanalyzableSyntax: true }
// this PR: the whole input as one sub-command, auto-approved by the default `Write-Output` rule
{ subCommands: [commandLine], hasUnanalyzableSyntax: false }PowerShell only rejects line 1. pwsh doesn't turn on bracketed paste, so sendText sends each line with Enter and line 2 runs on its own. I confirmed that in pwsh 7.5.4 on macOS. Please leave -- on a header line unmasked, for example by adding (?<!@["'][^\S\r\n]*) to the front of pwshStandaloneDoubleDashRegex, and add a test for each quote style asserting hasUnanalyzableSyntax: true. The tests in this file, including L289, still pass with that change.
Authored with Copilot on behalf of anthonykim1
There was a problem hiding this comment.
Just to be sure, is this the last regression? I know Copilot reviewer tends to be prone to finding and reporting one-two issues at a time, and it feels pretty tedious when you have to spend few days waiting for the next round.
I just wanted to fix a very common issue with the auto-approver, but now it's been 2.5 months of the review loop, and it doesn't feel very sustainable... If Copilot has a better fix, please feel free for it to push straight to this PR - I just want the issue with the git diff -- to be fixed.
This PR fixes an issue where VSCode would keep asking for approvals for any commands using
--for argument separation, even if such commands have already been included in theautoApprovepatterns.This is caused by treesitter being unable to parse this separator and VSCode forcing explicit approval for any unparsed commands.
chat.tools.terminal.autoApprovedoes not work fornpm exec -- ...commands #325635.Inspired by a similar fix in #310839.