diff --git a/packages/core/src/shell/scan.ts b/packages/core/src/shell/scan.ts index d930faa5c8887..838eced4d5b99 100644 --- a/packages/core/src/shell/scan.ts +++ b/packages/core/src/shell/scan.ts @@ -6,6 +6,7 @@ export type OpaqueReason = | "dynamic-command-name" | "dynamic-execution" | "heredoc" + | "input-too-long" | "invalid-redirect" | "invalid-structure" | "unsupported-escape" @@ -1836,9 +1837,9 @@ function bashHeredoc( } export function scanPowerShell(input: string): Result { - if (input.length > MAX_INPUT_LENGTH) return { kind: "opaque", reason: "invalid-structure" } + if (input.length > MAX_INPUT_LENGTH) return { kind: "opaque", reason: "input-too-long" } // PowerShell's Unicode quotes, dashes, and whitespace differ from JavaScript's token rules. - if (/[\0\u0085\u2013-\u2015\u2018-\u201e\ufeff]/.test(input)) return { kind: "opaque", reason: "invalid-structure" } + if (/[\0\u0085\u2018-\u201e\ufeff]/.test(input)) return { kind: "opaque", reason: "invalid-structure" } const result = scanPowerShellList(input, 0, 0, { remaining: MAX_INPUT_LENGTH * MAX_SUBSTITUTION_DEPTH }) if (result.kind === "opaque") return result return { kind: "scanned", commands: result.commands } @@ -1959,6 +1960,7 @@ function scanPowerShellList( if (stopParsing) { const stop = powerShellStopParsing(input, index, skipped) const text = input.slice(index, stop).trim() + if (/[\u2013-\u2015]/.test(text)) return { kind: "opaque", reason: "invalid-structure" } if (text) { wordStart = index + input.slice(index, stop).search(/\S/) word = text @@ -2073,6 +2075,7 @@ function scanPowerShellList( index = input[newline] === "\r" && input[newline + 1] === "\n" ? newline + 1 : newline continue } + if (/[\u2013-\u2015]/.test(char)) return { kind: "opaque", reason: "invalid-structure" } if (inExpression && started && (char === ">" || (char === "*" && input[index + 1] === ">"))) finishWord(index) const redirect = !started && diff --git a/packages/core/test/shell-scan/powershell-safety.test.ts b/packages/core/test/shell-scan/powershell-safety.test.ts index 1191ce786b00b..f95dbc67ee7cf 100644 --- a/packages/core/test/shell-scan/powershell-safety.test.ts +++ b/packages/core/test/shell-scan/powershell-safety.test.ts @@ -155,6 +155,26 @@ describe("PowerShell scanner safety", () => { expect(ShellScan.scanPowerShell(input).kind).toBe("opaque") }) + test.each([ + ["Write-Output 'budget \u2013 r\u00e9el'", "budget \u2013 r\u00e9el"], + ['Write-Output "budget \u2014 r\u00e9el"', "budget \u2014 r\u00e9el"], + ['Write-Output "$(Write-Output \'budget \u2015 r\u00e9el\')"', '$(Write-Output \'budget \u2015 r\u00e9el\')'], + ])("preserves Unicode dashes inside recognized string literals: %s", (source, value) => { + const result = ShellScan.scanPowerShell(source) + expect(result.kind).toBe("scanned") + if (result.kind === "opaque") return + expect(result.commands[0]?.words).toEqual(["Write-Output", value]) + }) + + test("ignores Unicode dashes in comments but refuses them in unquoted command text", () => { + expect(ShellScan.scanPowerShell("Write-Output ok # comment \u2013 \u2014 \u2015")).toMatchObject({ + kind: "scanned", + commands: [{ words: ["Write-Output", "ok"] }], + }) + for (const dash of ["\u2013", "\u2014", "\u2015"]) + expect(ShellScan.scanPowerShell(`Write-Output ${dash}StackName old`).kind).toBe("opaque") + }) + test.each(["\n", "\r", "\r\n"])("ends stop-parsing at %j even adjacent to completed tokens", (newline) => { for (const argument of ["'x'", '"x"', "{}"]) { const result = ShellScan.scanPowerShell(`Write-Output ${argument}--% '${newline}Remove-Item victim${newline}# '`) @@ -303,7 +323,10 @@ describe("PowerShell scanner safety", () => { test("bounds script block nesting and input size", () => { expect(ShellScan.scanPowerShell("% { ".repeat(33) + "Get-Item x" + " }".repeat(33)).kind).toBe("opaque") - expect(ShellScan.scanPowerShell(`Write-Output ${"x".repeat(64 * 1024)}`).kind).toBe("opaque") + expect(ShellScan.scanPowerShell(`Write-Output ${"x".repeat(64 * 1024)}`)).toEqual({ + kind: "opaque", + reason: "input-too-long", + }) }) test("scans compound subexpressions with expression-mode comments inside expandable strings", () => {