Skip to content

Commit 19f306e

Browse files
fix(semble): encode Windows archive extraction command (#1168)
1 parent 8f838a6 commit 19f306e

4 files changed

Lines changed: 69 additions & 44 deletions

File tree

‎src/services/code-index/semble/__tests__/semble-downloader.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ describe("semble-downloader", () => {
550550
// Should call PowerShell for zip extraction
551551
expect(spawn).toHaveBeenCalledWith(
552552
"powershell",
553-
expect.arrayContaining(["-NoProfile", "-Command", expect.stringContaining("Expand-Archive")]),
553+
["-NoProfile", "-NonInteractive", "-EncodedCommand", expect.any(String)],
554554
expect.any(Object),
555555
)
556556
// Should NOT call chmod on windows

‎src/services/destructive-command-guard/__tests__/manager.spec.ts‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,7 @@ describe("Destructive Command Guard manager", () => {
174174
const expectedExecutable = process.platform === "win32" ? "powershell" : "unzip"
175175
const expectedArgs =
176176
process.platform === "win32"
177-
? [
178-
"-NoProfile",
179-
"-NonInteractive",
180-
"-Command",
181-
"$archivePath = $args[0]; $destination = $args[1]; Expand-Archive -LiteralPath $archivePath -DestinationPath $destination -Force",
182-
"C:\\dcg.zip",
183-
"C:\\staging",
184-
]
177+
? ["-NoProfile", "-NonInteractive", "-EncodedCommand", expect.any(String)]
185178
: ["-o", "C:\\dcg.zip", "-d", "C:\\staging"]
186179

187180
expect(mockSpawn).toHaveBeenCalledWith(expectedExecutable, expectedArgs, {
@@ -361,10 +354,11 @@ describe("Destructive Command Guard manager", () => {
361354
kill: vi.fn(),
362355
})
363356
setImmediate(async () => {
364-
const destinationIndex = args.indexOf(
365-
"$archivePath = $args[0]; $destination = $args[1]; Expand-Archive -LiteralPath $archivePath -DestinationPath $destination -Force",
366-
)
367-
const stagingDir = args[destinationIndex + 2]
357+
const encodedCommandIndex = args.indexOf("-EncodedCommand")
358+
const script = Buffer.from(args[encodedCommandIndex + 1], "base64").toString("utf16le")
359+
const destinationMatch = script.match(/\$destination = '((?:[^']|'')*)'/)
360+
if (!destinationMatch) throw new Error("Expected a PowerShell destination")
361+
const stagingDir = destinationMatch[1].replace(/''/g, "'")
368362
await writeFile(path.join(stagingDir, info.binary), "ZIP executable")
369363
child.emit("close", 0)
370364
})

‎src/services/managed-binary/__tests__/archive.spec.ts‎

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ vi.mock("child_process", () => ({ spawn: vi.fn() }))
1717

1818
const mockSpawn = vi.mocked(spawn)
1919

20+
function decodePowerShellCommand(args: readonly string[]): string {
21+
const encodedCommandIndex = args.indexOf("-EncodedCommand")
22+
if (encodedCommandIndex === -1) throw new Error("Expected an encoded PowerShell command")
23+
return Buffer.from(args[encodedCommandIndex + 1], "base64").toString("utf16le")
24+
}
25+
2026
function createChild() {
2127
return Object.assign(new EventEmitter(), {
2228
stdout: new PassThrough(),
@@ -97,7 +103,7 @@ describe("managed binary archive utilities", () => {
97103
if (process.platform === "win32") {
98104
expect(mockSpawn).toHaveBeenCalledWith(
99105
"powershell",
100-
["-NoProfile", "-NonInteractive", "-Command", expect.any(String), "/tmp/archive.zip", "/tmp/output"],
106+
["-NoProfile", "-NonInteractive", "-EncodedCommand", expect.any(String)],
101107
expect.objectContaining({ shell: false }),
102108
)
103109
} else {
@@ -109,6 +115,28 @@ describe("managed binary archive utilities", () => {
109115
}
110116
})
111117

118+
it("passes Windows ZIP paths through an encoded PowerShell command", async () => {
119+
const child = createChild()
120+
mockSpawn.mockReturnValue(child as unknown as ReturnType<typeof spawn>)
121+
const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform")
122+
Object.defineProperty(process, "platform", { value: "win32", configurable: true })
123+
try {
124+
const extraction = extractZipArchive("C:\\Roo's files\\archive.zip", "C:\\Roo's files\\output")
125+
child.emit("close", 0)
126+
await extraction
127+
128+
const args = mockSpawn.mock.calls[0][1]
129+
expect(args).toEqual(["-NoProfile", "-NonInteractive", "-EncodedCommand", expect.any(String)])
130+
const script = decodePowerShellCommand(args)
131+
expect(script).toContain("$archivePath = 'C:\\Roo''s files\\archive.zip'")
132+
expect(script).toContain("$destination = 'C:\\Roo''s files\\output'")
133+
expect(script).toContain("Expand-Archive -LiteralPath $archivePath")
134+
expect(script).not.toContain("$args")
135+
} finally {
136+
if (originalPlatform) Object.defineProperty(process, "platform", originalPlatform)
137+
}
138+
})
139+
112140
it("validates a single-file tar.xz layout before extraction", async () => {
113141
const listing = createChild()
114142
const extraction = createChild()
@@ -165,15 +193,11 @@ describe("managed binary archive utilities", () => {
165193
await extraction
166194

167195
const args = mockSpawn.mock.calls[0][1]
168-
const script = args[3]
196+
const script = decodePowerShellCommand(args)
169197
expect(script).toContain("$entries.Count -ne 1")
170-
expect(script).not.toContain("C:\\archive.zip")
171-
expect(args.slice(4)).toEqual([
172-
"C:\\archive.zip",
173-
path.join("C:\\output", "binary.exe"),
174-
"binary.exe",
175-
"Tool",
176-
])
198+
expect(script).toContain("$archivePath = 'C:\\archive.zip'")
199+
expect(script).toContain(`$outputPath = '${path.join("C:\\output", "binary.exe")}'`)
200+
expect(args).toEqual(["-NoProfile", "-NonInteractive", "-EncodedCommand", expect.any(String)])
177201
} finally {
178202
if (originalPlatform) Object.defineProperty(process, "platform", originalPlatform)
179203
}

‎src/services/managed-binary/archive.ts‎

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@ export interface ProcessResult {
66
stderr: string
77
}
88

9+
function quotePowerShellString(value: string): string {
10+
return `'${value.replace(/'/g, "''")}'`
11+
}
12+
13+
function encodePowerShellCommand(script: string): string {
14+
return Buffer.from(script, "utf16le").toString("base64")
15+
}
16+
17+
async function runPowerShell(script: string): Promise<ProcessResult> {
18+
return runProcess("powershell", [
19+
"-NoProfile",
20+
"-NonInteractive",
21+
"-EncodedCommand",
22+
encodePowerShellCommand(script),
23+
])
24+
}
25+
926
export function runProcess(executable: string, args: string[], timeoutMs = 30_000): Promise<ProcessResult> {
1027
return new Promise((resolve, reject) => {
1128
const child = spawn(executable, args, { shell: false, stdio: ["ignore", "pipe", "pipe"] })
@@ -50,14 +67,13 @@ export async function extractTarXzArchive(archivePath: string, destination: stri
5067

5168
export async function extractZipArchive(archivePath: string, destination: string): Promise<void> {
5269
if (process.platform === "win32") {
53-
await runProcess("powershell", [
54-
"-NoProfile",
55-
"-NonInteractive",
56-
"-Command",
57-
"$archivePath = $args[0]; $destination = $args[1]; Expand-Archive -LiteralPath $archivePath -DestinationPath $destination -Force",
58-
archivePath,
59-
destination,
60-
])
70+
const script = [
71+
"$ErrorActionPreference = 'Stop'",
72+
`$archivePath = ${quotePowerShellString(archivePath)}`,
73+
`$destination = ${quotePowerShellString(destination)}`,
74+
"Expand-Archive -LiteralPath $archivePath -DestinationPath $destination -Force",
75+
].join("; ")
76+
await runPowerShell(script)
6177
return
6278
}
6379

@@ -76,10 +92,10 @@ export async function extractSingleFileZipArchive(
7692

7793
const script = [
7894
"$ErrorActionPreference = 'Stop'",
79-
"$archivePath = $args[0]",
80-
"$outputPath = $args[1]",
81-
"$expectedFile = $args[2]",
82-
"$archiveName = $args[3]",
95+
`$archivePath = ${quotePowerShellString(archivePath)}`,
96+
`$outputPath = ${quotePowerShellString(path.join(destination, expectedFile))}`,
97+
`$expectedFile = ${quotePowerShellString(expectedFile)}`,
98+
`$archiveName = ${quotePowerShellString(archiveName)}`,
8399
"Add-Type -AssemblyName System.IO.Compression.FileSystem",
84100
"$archive = [System.IO.Compression.ZipFile]::OpenRead($archivePath)",
85101
"try {",
@@ -89,16 +105,7 @@ export async function extractSingleFileZipArchive(
89105
"} finally { $archive.Dispose() }",
90106
].join("; ")
91107

92-
await runProcess("powershell", [
93-
"-NoProfile",
94-
"-NonInteractive",
95-
"-Command",
96-
script,
97-
archivePath,
98-
path.join(destination, expectedFile),
99-
expectedFile,
100-
archiveName,
101-
])
108+
await runPowerShell(script)
102109
}
103110

104111
export async function extractSingleFileTarXzArchive(

0 commit comments

Comments
 (0)