-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathmanager.spec.ts
More file actions
387 lines (341 loc) · 14.7 KB
/
Copy pathmanager.spec.ts
File metadata and controls
387 lines (341 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import { createHash } from "crypto"
import { EventEmitter } from "events"
import { access, chmod, mkdtemp, mkdir, readFile, rm, stat, writeFile } from "fs/promises"
import { tmpdir } from "os"
import path from "path"
import { PassThrough } from "stream"
import { spawn } from "child_process"
import { get } from "https"
import type { IncomingMessage, RequestOptions } from "http"
import { DCG_ARCHIVES, DCG_VERSION } from "../constants"
import {
downloadFile,
extractSingleBinary,
getDcgArchiveInfo,
getDcgBinaryPath,
isDcgSupportedPlatform,
isTrustedDownloadUrl,
resolveTrustedRedirect,
ensureDcgInstalled,
verifyChecksum,
} from "../manager"
vi.mock("child_process", () => ({ spawn: vi.fn() }))
vi.mock("https", () => ({ get: vi.fn() }))
const mockSpawn = vi.mocked(spawn)
const mockGet = vi.mocked(get)
describe("Destructive Command Guard manager", () => {
let tempDir: string
beforeEach(async () => {
tempDir = await mkdtemp(path.join(tmpdir(), "dcg-manager-"))
mockSpawn.mockReset()
mockGet.mockReset()
})
afterEach(async () => {
await rm(tempDir, { recursive: true, force: true })
})
it("maps all supported platform and architecture combinations", () => {
expect(Object.keys(DCG_ARCHIVES).sort()).toEqual(["darwin-arm64", "linux-arm64", "linux-x64", "win32-x64"])
expect(getDcgArchiveInfo("darwin", "arm64")?.archive).toBe("dcg-aarch64-apple-darwin.tar.xz")
expect(getDcgArchiveInfo("win32", "x64")?.binary).toBe("dcg.exe")
})
it("rejects unsupported platforms", () => {
expect(isDcgSupportedPlatform("freebsd", "x64")).toBe(false)
expect(getDcgBinaryPath("/storage", "freebsd", "x64")).toBeUndefined()
})
it("returns the managed binary path", () => {
expect(getDcgBinaryPath("/storage", "linux", "x64")).toBe(
path.join("/storage", "destructive-command-guard", "dcg"),
)
})
it("accepts only HTTPS URLs on trusted host boundaries", () => {
expect(isTrustedDownloadUrl("https://github.com/release")).toBe(true)
expect(isTrustedDownloadUrl("https://cdn.objects.githubusercontent.com/release")).toBe(true)
expect(isTrustedDownloadUrl("http://github.com/release")).toBe(false)
expect(isTrustedDownloadUrl("https://evilgithub.com/release")).toBe(false)
expect(isTrustedDownloadUrl("https://github.com.evil.com/release")).toBe(false)
expect(isTrustedDownloadUrl("not a URL")).toBe(false)
})
it("rejects untrusted download URLs before opening a destination", async () => {
await expect(downloadFile("https://example.com/dcg", path.join(tempDir, "archive"))).rejects.toThrow(
"DCG download URL is not a trusted HTTPS host",
)
})
it("rejects non-successful HTTP responses", async () => {
const response = Object.assign(new PassThrough(), { statusCode: 503, headers: {}, destroy: vi.fn() })
const request = Object.assign(new EventEmitter(), { setTimeout: vi.fn(), destroy: vi.fn() })
mockGet.mockImplementation((_url, optionsOrCallback, optionalCallback) => {
const callback = typeof optionsOrCallback === "function" ? optionsOrCallback : optionalCallback
setImmediate(() => callback?.(response as unknown as IncomingMessage))
return request as unknown as ReturnType<typeof get>
})
await expect(downloadFile("https://github.com/release", path.join(tempDir, "archive"))).rejects.toThrow(
"DCG download failed with HTTP 503",
)
})
it("rejects request errors", async () => {
const request = Object.assign(new EventEmitter(), { setTimeout: vi.fn(), destroy: vi.fn() })
mockGet.mockReturnValue(request as unknown as ReturnType<typeof get>)
const download = downloadFile("https://github.com/release", path.join(tempDir, "archive"))
request.emit("error", new Error("socket failed"))
await expect(download).rejects.toThrow("socket failed")
})
it("times out stalled requests", async () => {
const request = Object.assign(new EventEmitter(), {
setTimeout: vi.fn((_timeout: number, callback: () => void) => setImmediate(callback)),
destroy: vi.fn((error: Error) => request.emit("error", error)),
})
mockGet.mockReturnValue(request as unknown as ReturnType<typeof get>)
await expect(downloadFile("https://github.com/release", path.join(tempDir, "archive"))).rejects.toThrow(
"DCG download timed out",
)
expect(request.setTimeout).toHaveBeenCalledWith(120_000, expect.any(Function))
})
it("rejects archives larger than 50 MiB", async () => {
const response = Object.assign(new PassThrough(), {
statusCode: 200,
headers: { "content-length": String(50 * 1024 * 1024 + 1) },
destroy: vi.fn(),
})
const request = Object.assign(new EventEmitter(), { setTimeout: vi.fn(), destroy: vi.fn() })
mockGet.mockImplementation((_url, optionsOrCallback, optionalCallback) => {
const callback = typeof optionsOrCallback === "function" ? optionsOrCallback : optionalCallback
setImmediate(() => callback?.(response as unknown as IncomingMessage))
return request as unknown as ReturnType<typeof get>
})
await expect(downloadFile("https://github.com/release", path.join(tempDir, "archive"))).rejects.toThrow(
"DCG archive exceeds the download size limit",
)
})
it("allows trusted relative redirects and rejects unsafe or exhausted redirects", () => {
expect(resolveTrustedRedirect("https://github.com/release", "/asset", 5)).toBe("https://github.com/asset")
expect(
resolveTrustedRedirect(
"https://github.com/release",
"https://release-assets.githubusercontent.com/asset",
5,
),
).toBe("https://release-assets.githubusercontent.com/asset")
expect(() => resolveTrustedRedirect("https://github.com/release", "https://example.com/asset", 5)).toThrow(
"DCG download redirected to an untrusted host",
)
expect(() => resolveTrustedRedirect("https://github.com/release", "/asset", 0)).toThrow(
"Too many DCG download redirects",
)
expect(() => resolveTrustedRedirect("https://github.com/release", undefined, 5)).toThrow(
"DCG download redirect is missing a Location header",
)
})
it("verifies matching checksums and rejects mismatches", async () => {
const filePath = path.join(tempDir, "archive")
const contents = Buffer.from("verified archive")
await writeFile(filePath, contents)
const checksum = createHash("sha256").update(contents).digest("hex")
await expect(verifyChecksum(filePath, checksum)).resolves.toBeUndefined()
await expect(verifyChecksum(filePath, "0".repeat(64))).rejects.toThrow(`got ${checksum}`)
})
it("uses the platform ZIP extractor", async () => {
const child = Object.assign(new EventEmitter(), {
stdout: new PassThrough(),
stderr: new PassThrough(),
kill: vi.fn(),
})
// The production code uses only the event and stream subset supplied by this test double.
mockSpawn.mockReturnValue(child as unknown as ReturnType<typeof spawn>)
const extraction = extractSingleBinary("C:\\dcg.zip", "C:\\staging", DCG_ARCHIVES["win32-x64"])
child.emit("close", 0)
await extraction
const expectedExecutable = process.platform === "win32" ? "powershell" : "unzip"
const expectedArgs =
process.platform === "win32"
? [
"-NoProfile",
"-NonInteractive",
"-Command",
"$archivePath = $args[0]; $destination = $args[1]; Expand-Archive -LiteralPath $archivePath -DestinationPath $destination -Force",
"C:\\dcg.zip",
"C:\\staging",
]
: ["-o", "C:\\dcg.zip", "-d", "C:\\staging"]
expect(mockSpawn).toHaveBeenCalledWith(expectedExecutable, expectedArgs, {
shell: false,
stdio: ["ignore", "pipe", "pipe"],
})
})
it("extracts tar archives without imposing a single-file layout", async () => {
const child = Object.assign(new EventEmitter(), {
stdout: new PassThrough(),
stderr: new PassThrough(),
kill: vi.fn(),
})
// The production code uses only the event and stream subset supplied by this test double.
mockSpawn.mockReturnValue(child as unknown as ReturnType<typeof spawn>)
const extraction = extractSingleBinary("/tmp/dcg.tar.xz", tempDir, DCG_ARCHIVES["linux-x64"])
child.emit("close", 0)
await expect(extraction).resolves.toBeUndefined()
expect(mockSpawn).toHaveBeenCalledTimes(1)
const expectedArgs = ["-xJf", "/tmp/dcg.tar.xz", "-C", tempDir, "--no-same-owner"]
if (process.platform === "linux") expectedArgs.push("--no-overwrite-dir")
expect(mockSpawn).toHaveBeenCalledWith("tar", expectedArgs, { shell: false, stdio: ["ignore", "pipe", "pipe"] })
})
it("surfaces process failures during extraction", async () => {
const child = Object.assign(new EventEmitter(), {
stdout: new PassThrough(),
stderr: new PassThrough(),
kill: vi.fn(),
})
mockSpawn.mockReturnValue(child as unknown as ReturnType<typeof spawn>)
const extraction = extractSingleBinary("/tmp/dcg.tar.xz", tempDir, DCG_ARCHIVES["linux-x64"])
child.stderr.write("invalid archive")
child.emit("close", 2)
await expect(extraction).rejects.toThrow("invalid archive")
})
it("reuses an existing managed binary and restores its executable permissions", async () => {
const binaryPath = getDcgBinaryPath(tempDir)
expect(binaryPath).toBeDefined()
await mkdir(path.dirname(binaryPath!), { recursive: true })
await writeFile(binaryPath!, "existing binary")
await writeFile(path.join(path.dirname(binaryPath!), ".dcg-version"), DCG_VERSION)
if (process.platform !== "win32") {
await chmod(binaryPath!, 0o600)
}
await expect(ensureDcgInstalled(tempDir)).resolves.toBe(binaryPath)
expect(mockSpawn).not.toHaveBeenCalled()
if (process.platform !== "win32") {
expect((await stat(binaryPath!)).mode & 0o111).toBe(0o111)
}
})
it("warns when the current platform is unsupported", async () => {
const platformKey = `${process.platform}-${process.arch}`
const info = DCG_ARCHIVES[platformKey]
if (!info) return
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {})
Reflect.deleteProperty(DCG_ARCHIVES, platformKey)
try {
await expect(ensureDcgInstalled(tempDir)).resolves.toBeUndefined()
expect(warnSpy).toHaveBeenCalledWith(`[DCG] Unsupported platform: ${platformKey}`)
} finally {
Reflect.set(DCG_ARCHIVES, platformKey, info)
warnSpy.mockRestore()
}
})
it("downloads, verifies, extracts, and deduplicates a new installation", async () => {
const info = getDcgArchiveInfo()
expect(info).toBeDefined()
if (!info || info.archive.endsWith(".zip")) return
const archive = Buffer.from("test archive")
const originalChecksum = info.sha256
Object.defineProperty(info, "sha256", {
value: createHash("sha256").update(archive).digest("hex"),
configurable: true,
})
const response = Object.assign(new PassThrough(), {
statusCode: 200,
headers: { "content-length": String(archive.length) },
})
const request = Object.assign(new EventEmitter(), {
setTimeout: vi.fn(),
destroy: vi.fn(),
})
mockGet.mockImplementation(
(
_url: string | URL,
optionsOrCallback: RequestOptions | ((response: IncomingMessage) => void),
optionalCallback?: (response: IncomingMessage) => void,
) => {
const callback = typeof optionsOrCallback === "function" ? optionsOrCallback : optionalCallback
setImmediate(() => {
// The downloader uses only the response stream/status subset supplied here.
callback?.(response as unknown as IncomingMessage)
response.end(archive)
})
// The downloader uses only timeout/error handling from ClientRequest.
return request as unknown as ReturnType<typeof get>
},
)
mockSpawn.mockImplementation((executable, args) => {
const child = Object.assign(new EventEmitter(), {
stdout: new PassThrough(),
stderr: new PassThrough(),
kill: vi.fn(),
})
setImmediate(async () => {
if (executable === "tar") {
const stagingDir = args[args.indexOf("-C") + 1]
await writeFile(path.join(stagingDir, info.binary), "executable")
}
child.emit("close", 0)
})
// The process runner uses only the event and stream subset supplied here.
return child as unknown as ReturnType<typeof spawn>
})
try {
const firstInstallation = ensureDcgInstalled(tempDir)
const concurrentInstallation = ensureDcgInstalled(tempDir)
expect(concurrentInstallation).toBe(firstInstallation)
const binaryPath = await firstInstallation
if (!binaryPath) throw new Error("Expected DCG to be supported in this test")
expect(await readFile(binaryPath, "utf8")).toBe("executable")
expect(mockGet).toHaveBeenCalledTimes(1)
expect(mockSpawn).toHaveBeenCalledTimes(1)
await expect(access(path.join(tempDir, `${DCG_VERSION}-${info.archive}`))).rejects.toThrow()
expect(await readFile(path.join(tempDir, "destructive-command-guard", ".dcg-version"), "utf8")).toBe(
DCG_VERSION,
)
} finally {
Object.defineProperty(info, "sha256", { value: originalChecksum, configurable: true })
}
})
it.skipIf(!getDcgArchiveInfo()?.archive.endsWith(".zip"))(
"downloads, verifies, extracts, and installs a ZIP archive",
async () => {
const info = getDcgArchiveInfo()
if (!info) throw new Error("Expected a ZIP archive in this test")
const archive = Buffer.from("test ZIP archive")
const originalChecksum = info.sha256
Object.defineProperty(info, "sha256", {
value: createHash("sha256").update(archive).digest("hex"),
configurable: true,
})
const response = Object.assign(new PassThrough(), {
statusCode: 200,
headers: { "content-length": String(archive.length) },
})
const request = Object.assign(new EventEmitter(), { setTimeout: vi.fn(), destroy: vi.fn() })
mockGet.mockImplementation((_url, optionsOrCallback, optionalCallback) => {
const callback = typeof optionsOrCallback === "function" ? optionsOrCallback : optionalCallback
setImmediate(() => {
callback?.(response as unknown as IncomingMessage)
response.end(archive)
})
return request as unknown as ReturnType<typeof get>
})
mockSpawn.mockImplementation((_executable, args) => {
const child = Object.assign(new EventEmitter(), {
stdout: new PassThrough(),
stderr: new PassThrough(),
kill: vi.fn(),
})
setImmediate(async () => {
const destinationIndex = args.indexOf(
"$archivePath = $args[0]; $destination = $args[1]; Expand-Archive -LiteralPath $archivePath -DestinationPath $destination -Force",
)
const stagingDir = args[destinationIndex + 2]
await writeFile(path.join(stagingDir, info.binary), "ZIP executable")
child.emit("close", 0)
})
return child as unknown as ReturnType<typeof spawn>
})
try {
const binaryPath = await ensureDcgInstalled(tempDir)
if (!binaryPath) throw new Error("Expected DCG to be supported in this test")
expect(await readFile(binaryPath, "utf8")).toBe("ZIP executable")
expect(await readFile(path.join(tempDir, "destructive-command-guard", ".dcg-version"), "utf8")).toBe(
DCG_VERSION,
)
await expect(access(path.join(tempDir, `${DCG_VERSION}-${info.archive}`))).rejects.toThrow()
} finally {
Object.defineProperty(info, "sha256", { value: originalChecksum, configurable: true })
}
},
)
})