|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { test } from "node:test"; |
| 6 | +import { fetchHostedFiles } from "./catalog-hosted-files.js"; |
| 7 | + |
| 8 | +function fixture(url = "https://static.heygen.ai/asset", path = "asset.bin") { |
| 9 | + const root = mkdtempSync(join(tmpdir(), "hf-hosted-test-")); |
| 10 | + writeFileSync(join(root, "registry-item.json"), JSON.stringify({ files: [{ url, path }] })); |
| 11 | + return root; |
| 12 | +} |
| 13 | + |
| 14 | +test("downloads exact bytes through a relative CDN redirect", async (t) => { |
| 15 | + const root = fixture(); |
| 16 | + const calls: string[] = []; |
| 17 | + t.mock.method( |
| 18 | + globalThis, |
| 19 | + "fetch", |
| 20 | + async (url: Parameters<typeof fetch>[0], options?: RequestInit) => { |
| 21 | + calls.push(String(url)); |
| 22 | + assert.equal(options?.redirect, "manual"); |
| 23 | + assert.ok(options?.signal); |
| 24 | + return calls.length === 1 |
| 25 | + ? new Response(null, { status: 302, headers: { location: "/final" } }) |
| 26 | + : new Response(new Uint8Array([0, 255, 42])); |
| 27 | + }, |
| 28 | + ); |
| 29 | + try { |
| 30 | + await fetchHostedFiles(root); |
| 31 | + assert.deepEqual(calls, ["https://static.heygen.ai/asset", "https://static.heygen.ai/final"]); |
| 32 | + assert.deepEqual(readFileSync(join(root, "asset.bin")), Buffer.from([0, 255, 42])); |
| 33 | + } finally { |
| 34 | + rmSync(root, { recursive: true, force: true }); |
| 35 | + } |
| 36 | +}); |
| 37 | + |
| 38 | +for (const location of [ |
| 39 | + "http://169.254.169.254/meta", |
| 40 | + "https://localhost/x", |
| 41 | + "https://static.heygen.ai.evil.test/x", |
| 42 | + "http://static.heygen.ai/x", |
| 43 | +]) { |
| 44 | + test(`rejects redirect to ${location} before a second request`, async (t) => { |
| 45 | + const root = fixture(); |
| 46 | + const mock = t.mock.method( |
| 47 | + globalThis, |
| 48 | + "fetch", |
| 49 | + async () => new Response(null, { status: 302, headers: { location } }), |
| 50 | + ); |
| 51 | + try { |
| 52 | + await assert.rejects(fetchHostedFiles(root), /HTTPS CDN/); |
| 53 | + assert.equal(mock.mock.callCount(), 1); |
| 54 | + assert.deepEqual(readdirSync(root), ["registry-item.json"]); |
| 55 | + } finally { |
| 56 | + rmSync(root, { recursive: true, force: true }); |
| 57 | + } |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +for (const url of [ |
| 62 | + "https://127.0.0.1/x", |
| 63 | + "https://user:[email protected]/x", |
| 64 | + "https://static.heygen.ai:8443/x", |
| 65 | +]) { |
| 66 | + test(`rejects disallowed initial URL ${url} without a request`, async (t) => { |
| 67 | + const root = fixture(url); |
| 68 | + const mock = t.mock.method(globalThis, "fetch", async () => assert.fail("unexpected request")); |
| 69 | + try { |
| 70 | + await assert.rejects(fetchHostedFiles(root), /HTTPS CDN/); |
| 71 | + assert.equal(mock.mock.callCount(), 0); |
| 72 | + } finally { |
| 73 | + rmSync(root, { recursive: true, force: true }); |
| 74 | + } |
| 75 | + }); |
| 76 | +} |
| 77 | + |
| 78 | +test("enforces actual streaming bytes even when Content-Length understates them", async (t) => { |
| 79 | + const root = fixture(); |
| 80 | + let cancelled = false; |
| 81 | + let chunks = 0; |
| 82 | + const chunk = new Uint8Array(129 * 1024 * 1024); |
| 83 | + t.mock.method( |
| 84 | + globalThis, |
| 85 | + "fetch", |
| 86 | + async () => |
| 87 | + new Response( |
| 88 | + new ReadableStream({ |
| 89 | + pull(controller) { |
| 90 | + if (chunks++ < 3) controller.enqueue(chunk); |
| 91 | + else controller.close(); |
| 92 | + }, |
| 93 | + cancel() { |
| 94 | + cancelled = true; |
| 95 | + }, |
| 96 | + }), |
| 97 | + { headers: { "content-length": "1" } }, |
| 98 | + ), |
| 99 | + ); |
| 100 | + try { |
| 101 | + await assert.rejects(fetchHostedFiles(root), /256 MiB/); |
| 102 | + assert.equal(cancelled, true); |
| 103 | + assert.deepEqual(readdirSync(root), ["registry-item.json"]); |
| 104 | + } finally { |
| 105 | + rmSync(root, { recursive: true, force: true }); |
| 106 | + } |
| 107 | +}); |
| 108 | + |
| 109 | +for (const mode of ["declared oversize", "loop", "HTTP error", "stream error"]) { |
| 110 | + test(`${mode} preserves the existing asset`, async (t) => { |
| 111 | + const root = fixture(); |
| 112 | + writeFileSync(join(root, "asset.bin"), "previous"); |
| 113 | + let calls = 0; |
| 114 | + let cancelled = false; |
| 115 | + t.mock.method(globalThis, "fetch", async () => { |
| 116 | + calls++; |
| 117 | + const body = new ReadableStream({ |
| 118 | + start(controller) { |
| 119 | + if (mode === "stream error") controller.error(new Error("broken stream")); |
| 120 | + }, |
| 121 | + cancel() { |
| 122 | + cancelled = true; |
| 123 | + }, |
| 124 | + }); |
| 125 | + if (mode === "loop") |
| 126 | + return new Response(body, { status: 302, headers: { location: "/asset" } }); |
| 127 | + if (mode === "HTTP error") return new Response(body, { status: 500 }); |
| 128 | + return new Response(body, { |
| 129 | + headers: { |
| 130 | + "content-length": mode === "declared oversize" ? String(257 * 1024 * 1024) : "1", |
| 131 | + }, |
| 132 | + }); |
| 133 | + }); |
| 134 | + try { |
| 135 | + await assert.rejects(fetchHostedFiles(root), /256 MiB|redirect limit|HTTP 500|broken stream/); |
| 136 | + assert.equal(readFileSync(join(root, "asset.bin"), "utf8"), "previous"); |
| 137 | + assert.equal(calls, mode === "loop" ? 6 : 1); |
| 138 | + if (mode !== "stream error") assert.equal(cancelled, true); |
| 139 | + } finally { |
| 140 | + rmSync(root, { recursive: true, force: true }); |
| 141 | + } |
| 142 | + }); |
| 143 | +} |
| 144 | + |
| 145 | +test("skips manifest paths outside the copied project before fetching", async (t) => { |
| 146 | + const root = fixture("https://static.heygen.ai/asset", "../escape.bin"); |
| 147 | + const mock = t.mock.method(globalThis, "fetch", async () => assert.fail("unexpected request")); |
| 148 | + try { |
| 149 | + await fetchHostedFiles(root); |
| 150 | + assert.equal(mock.mock.callCount(), 0); |
| 151 | + assert.deepEqual(readdirSync(root), ["registry-item.json"]); |
| 152 | + } finally { |
| 153 | + rmSync(root, { recursive: true, force: true }); |
| 154 | + } |
| 155 | +}); |
0 commit comments