Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/core/src/shell/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type OpaqueReason =
| "dynamic-command-name"
| "dynamic-execution"
| "heredoc"
| "input-too-long"
| "invalid-redirect"
| "invalid-structure"
| "unsupported-escape"
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 &&
Expand Down
25 changes: 24 additions & 1 deletion packages/core/test/shell-scan/powershell-safety.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}# '`)
Expand Down Expand Up @@ -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", () => {
Expand Down
Loading