-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.spec.ts
More file actions
617 lines (539 loc) · 18.7 KB
/
Copy pathtest.spec.ts
File metadata and controls
617 lines (539 loc) · 18.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
import { describe, expect, it } from "vitest";
import AdmZip from "adm-zip";
import * as crypto from "node:crypto";
import { runArtifacts } from "./src/runner";
function response(bytes: Buffer, contentType: string) {
const headers = new Map<string, string>([
["content-type", contentType],
["content-length", String(bytes.length)],
]);
return {
ok: true,
status: 200,
statusText: "OK",
headers: { get: (k: string) => headers.get(k.toLowerCase()) ?? null },
body: null,
arrayBuffer: async () => bytes,
text: async () => bytes.toString("utf8"),
json: async () => JSON.parse(bytes.toString("utf8")),
} as unknown as Response;
}
function bytesResponse(bytes: Buffer) {
return response(bytes, "application/zip");
}
function jsonResponse(body: unknown) {
return response(
Buffer.from(JSON.stringify(body), "utf8"),
"application/json",
);
}
function mockFetch(
zipBytes: Buffer,
metadata: unknown,
manifest: unknown = {},
) {
return async (url: string) => {
const u = String(url);
if (u.endsWith(".zip") || u.endsWith(".xpi"))
return bytesResponse(zipBytes);
return jsonResponse(u.includes("artifact-manifest") ? manifest : metadata);
};
}
function sampleZip() {
const zip = new AdmZip();
zip.addFile("manifest.json", Buffer.from('{"manifest_version":3}', "utf8"));
return zip.toBuffer();
}
describe("extension-artifact-integrity", () => {
it("rejects an unsupported browser instead of building a URL from it", async () => {
await expect(
runArtifacts({
artifactsBaseUrl: "https://artifacts.extension.land",
owner: "o",
repo: "r",
sha: "s",
browser: "../../../secret" as unknown as "chrome",
timeoutMs: 1000,
}),
).rejects.toThrow(/Unsupported browser/);
});
it("fails gracefully when remote is unreachable", async () => {
const res = await runArtifacts({
artifactsBaseUrl: "https://invalid.example.local",
owner: "o",
repo: "r",
sha: "s",
browser: "chrome",
timeoutMs: 250,
});
expect(res.ok).toBe(false);
expect(res.checks.find((c) => c.id === "download-package")?.ok).toBe(false);
});
it("sends Authorization: Bearer when a token is provided", async () => {
const seenHeaders: Array<Record<string, string>> = [];
const zipBytes = sampleZip();
const originalFetch = globalThis.fetch;
(globalThis as any).fetch = async (url: string, init?: RequestInit) => {
seenHeaders.push((init?.headers as Record<string, string>) || {});
return String(url).endsWith(".zip")
? bytesResponse(zipBytes)
: jsonResponse({});
};
await runArtifacts({
artifactsBaseUrl: "https://artifacts.extension.land",
owner: "o",
repo: "r",
sha: "s",
browser: "chrome",
timeoutMs: 1000,
token: "tok_abc",
});
(globalThis as any).fetch = originalFetch;
expect(seenHeaders.length).toBeGreaterThan(0);
for (const h of seenHeaders) {
expect(h.Authorization).toBe("Bearer tok_abc");
}
});
it("omits Authorization when no token is provided", async () => {
const seenHeaders: Array<Record<string, string>> = [];
const zipBytes = sampleZip();
const originalFetch = globalThis.fetch;
(globalThis as any).fetch = async (url: string, init?: RequestInit) => {
seenHeaders.push((init?.headers as Record<string, string>) || {});
return String(url).endsWith(".zip")
? bytesResponse(zipBytes)
: jsonResponse({});
};
await runArtifacts({
artifactsBaseUrl: "https://artifacts.extension.land",
owner: "o",
repo: "r",
sha: "s",
browser: "chrome",
timeoutMs: 1000,
});
(globalThis as any).fetch = originalFetch;
for (const h of seenHeaders) {
expect(h.Authorization).toBeUndefined();
}
});
it("points a 401 at the refusal instead of at a missing artifact", async () => {
const originalFetch = globalThis.fetch;
(globalThis as any).fetch = async () =>
({
ok: false,
status: 401,
statusText: "Unauthorized",
headers: { get: () => null },
body: null,
arrayBuffer: async () => Buffer.alloc(0),
}) as unknown as Response;
const out = await runArtifacts({
artifactsBaseUrl: "https://artifacts.extension.land",
owner: "o",
repo: "r",
sha: "s",
browser: "chrome",
timeoutMs: 1000,
});
(globalThis as any).fetch = originalFetch;
const c = out.checks.find((x) => x.id === "download-package");
expect(c?.ok).toBe(false);
expect(c?.detail).toMatch(/401/);
expect(c?.remediation).toMatch(/declared public/i);
expect(c?.remediation).toMatch(/reserves/i);
});
it("refuses to send a bearer token over a non-HTTPS URL", async () => {
const zipBytes = sampleZip();
const originalFetch = globalThis.fetch;
(globalThis as any).fetch = mockFetch(zipBytes, { ok: true });
const out = await runArtifacts({
artifactsBaseUrl: "http://artifacts.extension.land",
owner: "o",
repo: "r",
sha: "s",
browser: "chrome",
timeoutMs: 1000,
token: "tok_abc",
});
(globalThis as any).fetch = originalFetch;
const c = out.checks.find((x) => x.id === "download-package");
expect(c?.ok).toBe(false);
expect(c?.detail).toMatch(/non-HTTPS/i);
});
it("caps an oversized download instead of buffering it", async () => {
const zipBytes = sampleZip();
const originalFetch = globalThis.fetch;
(globalThis as any).fetch = mockFetch(zipBytes, { ok: true });
const out = await runArtifacts({
artifactsBaseUrl: "https://artifacts.extension.land",
owner: "o",
repo: "r",
sha: "s",
browser: "chrome",
timeoutMs: 1000,
maxBytes: 1,
});
(globalThis as any).fetch = originalFetch;
const c = out.checks.find((x) => x.id === "download-package");
expect(c?.ok).toBe(false);
expect(c?.detail).toMatch(/cap/i);
});
it("reports a friendly error when metadata is not JSON", async () => {
const zipBytes = sampleZip();
const originalFetch = globalThis.fetch;
(globalThis as any).fetch = async (url: string) => {
const u = String(url);
if (u.endsWith(".zip")) return bytesResponse(zipBytes);
return response(
Buffer.from("<html>Not Found</html>", "utf8"),
"text/html",
);
};
const out = await runArtifacts({
artifactsBaseUrl: "https://artifacts.extension.land",
owner: "o",
repo: "r",
sha: "s",
browser: "chrome",
timeoutMs: 1000,
});
(globalThis as any).fetch = originalFetch;
const c = out.checks.find((x) => x.id === "download-metadata");
expect(c?.ok).toBe(false);
expect(c?.detail).toMatch(/Expected JSON|soft-404/i);
});
it("validates a zip structure from a mocked fetch", async () => {
const zip = new AdmZip();
zip.addFile("manifest.json", Buffer.from('{"manifest_version":3}', "utf8"));
const zipBytes = zip.toBuffer();
const originalFetch = globalThis.fetch;
(globalThis as any).fetch = mockFetch(zipBytes, { ok: true });
const out = await runArtifacts({
artifactsBaseUrl: "https://artifacts.extension.land",
owner: "o",
repo: "r",
sha: "s",
browser: "chrome",
timeoutMs: 1000,
});
(globalThis as any).fetch = originalFetch;
expect(out.checks.find((c) => c.id === "zip-structure")?.ok).toBe(true);
expect(out.checks.find((c) => c.id === "manifest-present")?.ok).toBe(true);
expect(out.checks.find((c) => c.id === "download-metadata")?.ok).toBe(true);
});
it("fails manifest-present when manifest.json is missing from the zip", async () => {
const zip = new AdmZip();
zip.addFile("popup.js", Buffer.from("console.log(1)", "utf8"));
const zipBytes = zip.toBuffer();
const originalFetch = globalThis.fetch;
(globalThis as any).fetch = mockFetch(zipBytes, { ok: true });
const out = await runArtifacts({
artifactsBaseUrl: "https://artifacts.extension.land",
owner: "o",
repo: "r",
sha: "s",
browser: "chrome",
timeoutMs: 1000,
});
(globalThis as any).fetch = originalFetch;
const c = out.checks.find((x) => x.id === "manifest-present");
expect(c?.ok).toBe(false);
expect(out.checks.find((x) => x.id === "zip-structure")?.ok).toBe(true);
});
it("fails manifest-present when manifest.json is not valid JSON", async () => {
const zip = new AdmZip();
zip.addFile("manifest.json", Buffer.from("{ not json", "utf8"));
const zipBytes = zip.toBuffer();
const originalFetch = globalThis.fetch;
(globalThis as any).fetch = mockFetch(zipBytes, { ok: true });
const out = await runArtifacts({
artifactsBaseUrl: "https://artifacts.extension.land",
owner: "o",
repo: "r",
sha: "s",
browser: "chrome",
timeoutMs: 1000,
});
(globalThis as any).fetch = originalFetch;
expect(out.checks.find((x) => x.id === "manifest-present")?.ok).toBe(false);
});
it("fails manifest-present when manifest_version is not 2 or 3", async () => {
const zip = new AdmZip();
zip.addFile("manifest.json", Buffer.from('{"manifest_version":1}', "utf8"));
const zipBytes = zip.toBuffer();
const originalFetch = globalThis.fetch;
(globalThis as any).fetch = mockFetch(zipBytes, { ok: true });
const out = await runArtifacts({
artifactsBaseUrl: "https://artifacts.extension.land",
owner: "o",
repo: "r",
sha: "s",
browser: "chrome",
timeoutMs: 1000,
});
(globalThis as any).fetch = originalFetch;
expect(out.checks.find((x) => x.id === "manifest-present")?.ok).toBe(false);
});
it("passes content-integrity when metadata declares a matching sha256", async () => {
const zipBytes = sampleZip();
const digest = crypto.createHash("sha256").update(zipBytes).digest("hex");
const originalFetch = globalThis.fetch;
(globalThis as any).fetch = mockFetch(zipBytes, { sha256: digest });
const out = await runArtifacts({
artifactsBaseUrl: "https://artifacts.extension.land",
owner: "o",
repo: "r",
sha: "s",
browser: "chrome",
timeoutMs: 1000,
});
(globalThis as any).fetch = originalFetch;
const c = out.checks.find((x) => x.id === "package-integrity");
expect(c?.ok).toBe(true);
expect(out.sha256).toBe(digest);
expect(out.ok).toBe(true);
});
it("accepts an SRI sha256-<base64> integrity field", async () => {
const zipBytes = sampleZip();
const b64 = crypto.createHash("sha256").update(zipBytes).digest("base64");
const originalFetch = globalThis.fetch;
(globalThis as any).fetch = mockFetch(zipBytes, {
integrity: `sha256-${b64}`,
});
const out = await runArtifacts({
artifactsBaseUrl: "https://artifacts.extension.land",
owner: "o",
repo: "r",
sha: "s",
browser: "chrome",
timeoutMs: 1000,
});
(globalThis as any).fetch = originalFetch;
expect(out.checks.find((x) => x.id === "package-integrity")?.ok).toBe(true);
});
it("FAILS content-integrity when the declared sha256 does not match", async () => {
const zipBytes = sampleZip();
const originalFetch = globalThis.fetch;
(globalThis as any).fetch = mockFetch(zipBytes, { sha256: "0".repeat(64) });
const out = await runArtifacts({
artifactsBaseUrl: "https://artifacts.extension.land",
owner: "o",
repo: "r",
sha: "s",
browser: "chrome",
timeoutMs: 1000,
});
(globalThis as any).fetch = originalFetch;
expect(out.checks.find((x) => x.id === "package-integrity")?.ok).toBe(
false,
);
expect(out.ok).toBe(false);
});
it("reports the computed sha256 as info when no digest is declared", async () => {
const zipBytes = sampleZip();
const originalFetch = globalThis.fetch;
(globalThis as any).fetch = mockFetch(zipBytes, { ok: true });
const out = await runArtifacts({
artifactsBaseUrl: "https://artifacts.extension.land",
owner: "o",
repo: "r",
sha: "s",
browser: "chrome",
timeoutMs: 1000,
});
(globalThis as any).fetch = originalFetch;
const c = out.checks.find((x) => x.id === "package-integrity");
expect(c?.ok).toBe(true);
expect(c?.level).toBe("info");
expect(out.sha256).toBeDefined();
expect(out.ok).toBe(true);
});
it("verifies against the artifact manifest files.zip.sha256", async () => {
const zipBytes = sampleZip();
const digest = crypto.createHash("sha256").update(zipBytes).digest("hex");
const originalFetch = globalThis.fetch;
(globalThis as any).fetch = mockFetch(
zipBytes,
{ ok: true },
{ files: { zip: { sha256: digest } } },
);
const out = await runArtifacts({
artifactsBaseUrl: "https://artifacts.extension.land",
owner: "o",
repo: "r",
sha: "s",
browser: "chrome",
timeoutMs: 1000,
});
(globalThis as any).fetch = originalFetch;
expect(out.checks.find((x) => x.id === "package-integrity")?.ok).toBe(true);
expect(out.urls.manifest).toContain("artifact-manifest");
});
it("manifest digest takes priority over a metadata digest", async () => {
const zipBytes = sampleZip();
const realDigest = crypto
.createHash("sha256")
.update(zipBytes)
.digest("hex");
const originalFetch = globalThis.fetch;
(globalThis as any).fetch = mockFetch(
zipBytes,
{ sha256: realDigest },
{ files: { zip: { sha256: "0".repeat(64) } } },
);
const out = await runArtifacts({
artifactsBaseUrl: "https://artifacts.extension.land",
owner: "o",
repo: "r",
sha: "s",
browser: "chrome",
timeoutMs: 1000,
});
(globalThis as any).fetch = originalFetch;
expect(out.checks.find((x) => x.id === "package-integrity")?.ok).toBe(
false,
);
});
it("records a warn check with the error when the manifest fetch fails", async () => {
const zipBytes = sampleZip();
const digest = crypto.createHash("sha256").update(zipBytes).digest("hex");
const originalFetch = globalThis.fetch;
(globalThis as any).fetch = async (url: string) => {
const u = String(url);
if (u.includes("artifact-manifest"))
throw new Error("manifest origin down");
if (u.endsWith(".zip")) return bytesResponse(zipBytes);
return jsonResponse({ sha256: digest });
};
const out = await runArtifacts({
artifactsBaseUrl: "https://artifacts.extension.land",
owner: "o",
repo: "r",
sha: "s",
browser: "chrome",
timeoutMs: 1000,
});
(globalThis as any).fetch = originalFetch;
const c = out.checks.find((x) => x.id === "download-manifest");
expect(c?.ok).toBe(false);
expect(c?.level).toBe("warn");
expect(c?.detail).toMatch(/manifest origin down/);
expect(c?.detail).toMatch(/weaker/i);
expect(out.checks.find((x) => x.id === "package-integrity")?.ok).toBe(true);
expect(out.ok).toBe(true);
});
it("reports the manifest fetch as a passing check when it succeeds", async () => {
const zipBytes = sampleZip();
const digest = crypto.createHash("sha256").update(zipBytes).digest("hex");
const originalFetch = globalThis.fetch;
(globalThis as any).fetch = mockFetch(
zipBytes,
{ ok: true },
{ files: { zip: { sha256: digest } } },
);
const out = await runArtifacts({
artifactsBaseUrl: "https://artifacts.extension.land",
owner: "o",
repo: "r",
sha: "s",
browser: "chrome",
timeoutMs: 1000,
});
(globalThis as any).fetch = originalFetch;
const c = out.checks.find((x) => x.id === "download-manifest");
expect(c?.ok).toBe(true);
expect(c?.level).toBe("warn");
});
it("keeps requireDigest failing closed when the manifest fetch fails", async () => {
const zipBytes = sampleZip();
const originalFetch = globalThis.fetch;
(globalThis as any).fetch = async (url: string) => {
const u = String(url);
if (u.includes("artifact-manifest"))
throw new Error("manifest origin down");
if (u.endsWith(".zip")) return bytesResponse(zipBytes);
return jsonResponse({ ok: true });
};
const out = await runArtifacts({
artifactsBaseUrl: "https://artifacts.extension.land",
owner: "o",
repo: "r",
sha: "s",
browser: "chrome",
timeoutMs: 1000,
requireDigest: true,
});
(globalThis as any).fetch = originalFetch;
expect(out.checks.find((x) => x.id === "download-manifest")?.ok).toBe(
false,
);
const c = out.checks.find((x) => x.id === "package-integrity");
expect(c?.ok).toBe(false);
expect(c?.level).toBe("fail");
expect(out.ok).toBe(false);
});
it("fails content-integrity when requireDigest is set and none is declared", async () => {
const zipBytes = sampleZip();
const originalFetch = globalThis.fetch;
(globalThis as any).fetch = mockFetch(zipBytes, { ok: true });
const out = await runArtifacts({
artifactsBaseUrl: "https://artifacts.extension.land",
owner: "o",
repo: "r",
sha: "s",
browser: "chrome",
timeoutMs: 1000,
requireDigest: true,
});
(globalThis as any).fetch = originalFetch;
const c = out.checks.find((x) => x.id === "package-integrity");
expect(c?.ok).toBe(false);
expect(c?.level).toBe("fail");
expect(out.ok).toBe(false);
});
it("throws on a malformed expectedSha256 instead of falling back", async () => {
const zipBytes = sampleZip();
const realDigest = crypto
.createHash("sha256")
.update(zipBytes)
.digest("hex");
const originalFetch = globalThis.fetch;
(globalThis as any).fetch = mockFetch(zipBytes, { sha256: realDigest });
await expect(
runArtifacts({
artifactsBaseUrl: "https://artifacts.extension.land",
owner: "o",
repo: "r",
sha: "s",
browser: "chrome",
timeoutMs: 1000,
expectedSha256: "a".repeat(63),
}),
).rejects.toThrow(/expectedSha256/);
(globalThis as any).fetch = originalFetch;
});
it("enforces expectedSha256 over a (correct) metadata digest", async () => {
const zipBytes = sampleZip();
const realDigest = crypto
.createHash("sha256")
.update(zipBytes)
.digest("hex");
const originalFetch = globalThis.fetch;
(globalThis as any).fetch = mockFetch(zipBytes, { sha256: realDigest });
const out = await runArtifacts({
artifactsBaseUrl: "https://artifacts.extension.land",
owner: "o",
repo: "r",
sha: "s",
browser: "chrome",
timeoutMs: 1000,
expectedSha256: "f".repeat(64),
});
(globalThis as any).fetch = originalFetch;
expect(out.checks.find((x) => x.id === "package-integrity")?.ok).toBe(
false,
);
expect(out.ok).toBe(false);
});
});