Skip to content

Commit fb3a88a

Browse files
author
saurav-shakya
committed
fix(core): check PowerShell commands hidden beside parsed ones
A bare `--` could still hide a command when another command in the same line parsed (e.g. `git status; Remove-Item victim --`): legacy found one command, so the fallback was skipped and only `git status` was checked. For PowerShell parses with errors, compare against the portable scanner and use its result unless both agree on the commands and directories, which keeps saved prefixes stable when nothing is hidden. A tab-separated `git\tstatus; Write-Output done` was also dropping `git\tstatus`; the permission impact fixtures now expect legacy to check it like the portable scanner does.
1 parent ca16c0c commit fb3a88a

3 files changed

Lines changed: 48 additions & 8 deletions

File tree

‎packages/core/src/shell/parse.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,16 @@ const scanLegacy = Effect.fnUntraced(function* (command: string, shell: string,
205205
),
206206
(tree) => Effect.sync(() => tree.delete()),
207207
)
208-
// Grammar errors (e.g. a bare `--` under PowerShell) can hide every command node and skip permission checks.
209-
if (error && result.commands.length === 0) return yield* scanPortable(command, shell, cwd)
210-
return result
208+
if (!error || (!powershell && result.commands.length > 0)) return result
209+
// Grammar errors (e.g. a bare `--` under PowerShell) can hide command nodes and skip their permission checks.
210+
// Keep legacy resources, and the saved prefixes derived from them, only when both scanners agree on what runs.
211+
const portable = yield* scanPortable(command, shell, cwd)
212+
const name = (resource: string) => resource.split(/\s/, 1)[0].toLowerCase()
213+
const agree =
214+
result.commands.length === portable.commands.length &&
215+
result.commands.every((item, index) => name(item.resource) === name(portable.commands[index].resource)) &&
216+
result.directories.join("\0") === portable.directories.join("\0")
217+
return agree ? result : portable
211218
})
212219

213220
export const scanPortable = Effect.fnUntraced(function* (command: string, shell: string, cwd: string) {

‎packages/core/test/permission.test.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -518,9 +518,9 @@ describe("shell scanner permission impact", () => {
518518
approved: ["git status *", "Write-Output *"],
519519
exact: ["git status", "Write-Output done"],
520520
denied: "git\tstatus",
521-
savedEffect: ["allow", "ask"],
522-
exactEffect: ["allow", "ask"],
523-
deniedEffect: ["allow", "deny"],
521+
savedEffect: ["ask", "ask"],
522+
exactEffect: ["ask", "ask"],
523+
deniedEffect: ["deny", "deny"],
524524
},
525525
{
526526
name: "PowerShell equals-joined argument",
@@ -662,9 +662,12 @@ describe("shell scanner permission impact", () => {
662662
name: "PowerShell tab prefix",
663663
shell: "pwsh",
664664
command: "git\tstatus; Write-Output done",
665-
grants: [["Write-Output *"], ["git\tstatus *", "Write-Output *"]],
665+
grants: [
666+
["git\tstatus *", "Write-Output *"],
667+
["git\tstatus *", "Write-Output *"],
668+
],
666669
repeat: [
667-
["allow", "ask"],
670+
["allow", "allow"],
668671
["allow", "allow"],
669672
],
670673
next: "git status",

‎packages/core/test/shell-parse.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,36 @@ describe("ShellParse", () => {
158158
expect(result.commands).toEqual([{ resource: command, save }])
159159
})
160160

161+
test.each([
162+
[
163+
"git status --short; echo MIXED-RAN --",
164+
[
165+
{ resource: "git status --short", save: "git status *" },
166+
{ resource: "echo MIXED-RAN --", save: "echo *" },
167+
],
168+
],
169+
[
170+
"git status; Remove-Item victim --",
171+
[
172+
{ resource: "git status", save: "git status *" },
173+
{ resource: "Remove-Item victim --", save: "Remove-Item *" },
174+
],
175+
],
176+
[
177+
"Remove-Item x --; Get-ChildItem",
178+
[
179+
{ resource: "Remove-Item x --", save: "Remove-Item *" },
180+
{ resource: "Get-ChildItem", save: "Get-ChildItem *" },
181+
],
182+
],
183+
])(
184+
"finds every PowerShell command when a bare -- hides one beside a parsed command (%s)",
185+
async (command, commands) => {
186+
const result = await Effect.runPromise(ShellParse.scan(command, "pwsh", "/workspace", { portable: false }))
187+
expect(result.commands).toEqual(commands)
188+
},
189+
)
190+
161191
test("does not permission directory changes separately", async () => {
162192
const result = await Effect.runPromise(ShellParse.scan("cd 'src dir' && git status", "/bin/bash", "/workspace"))
163193
expect(result).toEqual({

0 commit comments

Comments
 (0)