| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { PART_SIZE, R2Sink } from "../../src/lib/r2-sink.js"; |
| 3 | import { importHostAllowed, withoutCredentials } from "../../src/lib/import-hosts.js"; |
| 4 | |
| 5 | /** An in-memory stand-in for the bits of R2Bucket the sink uses; it enforces R2's part-size rules. */ |
| 6 | function fakeBucket() { |
| 7 | const objects = new Map<string, Uint8Array>(); |
| 8 | const log: string[] = []; |
| 9 | const bucket = { |
| 10 | async put(key: string, value: Uint8Array) { |
| 11 | log.push(`put ${value.length}`); |
| 12 | objects.set(key, value); |
| 13 | return {}; |
| 14 | }, |
| 15 | async createMultipartUpload(key: string) { |
| 16 | const parts = new Map<number, Uint8Array>(); |
| 17 | log.push("create"); |
| 18 | return { |
| 19 | async uploadPart(n: number, data: Uint8Array) { |
| 20 | log.push(`part ${n} ${data.length}`); |
| 21 | parts.set(n, data.slice()); |
| 22 | return { partNumber: n, etag: `e${n}` }; |
| 23 | }, |
| 24 | async complete(list: Array<{ partNumber: number }>) { |
| 25 | const sizes = list.map((p) => parts.get(p.partNumber)!.length); |
| 26 | // R2: every part but the last has the same size, at least 5 MiB. |
| 27 | for (const s of sizes.slice(0, -1)) { |
| 28 | if (s !== sizes[0] || s < 5 * 1024 * 1024) throw new Error("bad part sizes"); |
| 29 | } |
| 30 | const all = new Uint8Array(sizes.reduce((a, b) => a + b, 0)); |
| 31 | let o = 0; |
| 32 | for (const p of list) { |
| 33 | all.set(parts.get(p.partNumber)!, o); |
| 34 | o += parts.get(p.partNumber)!.length; |
| 35 | } |
| 36 | objects.set(key, all); |
| 37 | log.push("complete"); |
| 38 | return {}; |
| 39 | }, |
| 40 | async abort() { |
| 41 | log.push("abort"); |
| 42 | }, |
| 43 | }; |
| 44 | }, |
| 45 | }; |
| 46 | return { bucket: bucket as unknown as R2Bucket, objects, log }; |
| 47 | } |
| 48 | |
| 49 | const chunk = (n: number, fill: number) => new Uint8Array(n).fill(fill); |
| 50 | |
| 51 | describe("R2Sink", () => { |
| 52 | it("uses a single put for small objects", async () => { |
| 53 | const { bucket, objects, log } = fakeBucket(); |
| 54 | const sink = new R2Sink(bucket, "k", "text/plain", 1024); |
| 55 | await sink.write(chunk(10, 1)); |
| 56 | await sink.write(chunk(20, 2)); |
| 57 | expect(await sink.close()).toBe(30); |
| 58 | expect(log).toEqual(["put 30"]); |
| 59 | expect(objects.get("k")!.length).toBe(30); |
| 60 | }); |
| 61 | |
| 62 | it("uploads big objects as equal parts, whatever the chunk sizes", async () => { |
| 63 | const { bucket, objects, log } = fakeBucket(); |
| 64 | const sink = new R2Sink(bucket, "k", "application/gzip", 64 * 1024 * 1024); |
| 65 | const sizes = [3_000_000, 7_777_777, 1, 5_000_000, 2_500_000]; |
| 66 | let i = 0; |
| 67 | for (const s of sizes) await sink.write(chunk(s, i++)); |
| 68 | const total = sizes.reduce((a, b) => a + b, 0); |
| 69 | expect(await sink.close()).toBe(total); |
| 70 | expect(log.filter((l) => l.startsWith("part")).slice(0, -1).every((l) => l.endsWith(` ${PART_SIZE}`))).toBe(true); |
| 71 | expect(log.at(-1)).toBe("complete"); |
| 72 | // The bytes come out in order. |
| 73 | const out = objects.get("k")!; |
| 74 | expect(out.length).toBe(total); |
| 75 | expect(out[0]).toBe(0); |
| 76 | expect(out[3_000_000]).toBe(1); |
| 77 | expect(out[total - 1]).toBe(4); |
| 78 | }); |
| 79 | |
| 80 | it("stops with a 413 past the limit and aborts the upload", async () => { |
| 81 | const { bucket, log } = fakeBucket(); |
| 82 | const sink = new R2Sink(bucket, "k", "application/zip", PART_SIZE + 10, "too big"); |
| 83 | await sink.write(chunk(PART_SIZE, 0)); |
| 84 | await expect(sink.write(chunk(11, 0))).rejects.toMatchObject({ status: 413 }); |
| 85 | await sink.abort(); |
| 86 | expect(log).toContain("abort"); |
| 87 | }); |
| 88 | }); |
| 89 | |
| 90 | describe("importer hosts", () => { |
| 91 | it("allows GitHub's hosts and the configured enterprise host only", () => { |
| 92 | for (const h of ["github.com", "api.github.com", "objects.githubusercontent.com", "release-assets.githubusercontent.com", "GitHub.com"]) expect(importHostAllowed(h)).toBe(true); |
| 93 | for (const h of ["example.com", "evil.github.com.example", "githubusercontent.com", "raw.githubusercontent.com", "localhost"]) expect(importHostAllowed(h)).toBe(false); |
| 94 | expect(importHostAllowed("ghe.example.com", "GHE.example.com")).toBe(true); |
| 95 | expect(importHostAllowed("ghe.example.com")).toBe(false); |
| 96 | }); |
| 97 | |
| 98 | it("drops every credential header on a cross-host redirect", () => { |
| 99 | const h = withoutCredentials({ Authorization: "token x", Cookie: "a=b", "X-GitHub-Token": "y", "Proxy-Authorization": "z", Accept: "application/octet-stream", "User-Agent": "diffinery" }); |
| 100 | expect(h).toEqual({ Accept: "application/octet-stream", "User-Agent": "diffinery" }); |
| 101 | }); |
| 102 | }); |