Skip to content

Commit ca16c0c

Browse files
author
saurav-shakya
committed
fix(core): scan PowerShell commands the grammar fails to parse
tree-sitter-powershell returns an ERROR tree with no command nodes for commands containing a bare `--` (and for tab or CR separators), so the legacy scanner reported zero commands and the shell tool skipped its permission check entirely. When the parse has errors and yields no commands, fall back to the portable scanner, which either finds the commands or fails closed. Fixes anomalyco#54393
1 parent e31136b commit ca16c0c

3 files changed

Lines changed: 18 additions & 28 deletions

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ const scanLegacy = Effect.fnUntraced(function* (command: string, shell: string,
178178
const tree = (powershell ? parsers.ps : parsers.bash).parse(command)
179179
if (!tree) return yield* Effect.fail(new Error("Failed to parse shell command"))
180180

181-
return yield* Effect.acquireUseRelease(
181+
const error = tree.rootNode.hasError
182+
const result = yield* Effect.acquireUseRelease(
182183
Effect.succeed(tree),
183184
(tree) =>
184185
Effect.sync(() =>
@@ -204,6 +205,9 @@ const scanLegacy = Effect.fnUntraced(function* (command: string, shell: string,
204205
),
205206
(tree) => Effect.sync(() => tree.delete()),
206207
)
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
207211
})
208212

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

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

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ describe("portable shell parser compatibility", () => {
4242
["pwsh", "Set-Location $HOME; Set-Location $PWD; Set-Location $target", { HOME: "/session-home" }],
4343
["pwsh", "Set-Item Env:T /outside; Set-Location $env:T", { T: "/workspace" }],
4444
["pwsh", "sl /outside; Microsoft.PowerShell.Management\\Set-Location /outside", {}],
45+
["pwsh", "Get-ChildItem\rRemove-Item victim", {}],
46+
["pwsh", "git\tstatus", {}],
47+
["pwsh", "Set-Location a,b", {}],
4548
] as const)(
4649
"matches supported legacy resources, saved prefixes, and directories natively: %s %s %j",
4750
async (shell, command, env) => {
@@ -195,40 +198,13 @@ describe("current native and legacy parity gaps", () => {
195198
legacy: { commands: [], directories: ["before", "after"] },
196199
native: { commands: [], directories: ["before\\\nafter"] },
197200
},
198-
{
199-
name: "native recognizes PowerShell carriage-return separators omitted by the legacy AST",
200-
shell: "pwsh",
201-
command: "Get-ChildItem\rRemove-Item victim",
202-
legacy: { commands: [], directories: [] },
203-
native: {
204-
commands: [
205-
{ resource: "Get-ChildItem", save: "Get-ChildItem *" },
206-
{ resource: "Remove-Item victim", save: "Remove-Item *" },
207-
],
208-
directories: [],
209-
},
210-
},
211-
{
212-
name: "native recognizes tab-separated PowerShell commands omitted by the legacy AST",
213-
shell: "pwsh",
214-
command: "git\tstatus",
215-
legacy: { commands: [], directories: [] },
216-
native: { commands: [{ resource: "git\tstatus", save: "git\tstatus *" }], directories: [] },
217-
},
218201
{
219202
name: "native preserves complete PowerShell flag=value resources",
220203
shell: "pwsh",
221204
command: "git --flag=value",
222205
legacy: { commands: [{ resource: "git --flag", save: "git --flag *" }], directories: [] },
223206
native: { commands: [{ resource: "git --flag=value", save: "git --flag=value *" }], directories: [] },
224207
},
225-
{
226-
name: "native does not split comma-separated PowerShell directory operands",
227-
shell: "pwsh",
228-
command: "Set-Location a,b",
229-
legacy: { commands: [], directories: ["a", ",b"] },
230-
native: { commands: [], directories: ["a,b"] },
231-
},
232208
]) {
233209
test(fixture.name, async () => {
234210
const native = await Effect.runPromise(ShellParse.scanPortable(fixture.command, fixture.shell, "/workspace"))

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ describe("ShellParse", () => {
148148
])
149149
})
150150

151+
test.each([
152+
['git grep -n "x" -- README.md', "git grep *"],
153+
["git rev-parse HEAD --", "git rev-parse *"],
154+
['node -e "console.log(1)" --', "node *"],
155+
["Get-Content README.md -TotalCount 1 --", "Get-Content *"],
156+
])("finds PowerShell commands containing a bare -- (%s)", async (command, save) => {
157+
const result = await Effect.runPromise(ShellParse.scan(command, "pwsh", "/workspace", { portable: false }))
158+
expect(result.commands).toEqual([{ resource: command, save }])
159+
})
160+
151161
test("does not permission directory changes separately", async () => {
152162
const result = await Effect.runPromise(ShellParse.scan("cd 'src dir' && git status", "/bin/bash", "/workspace"))
153163
expect(result).toEqual({

0 commit comments

Comments
 (0)