From cd55f059c36340fd99b9f6001480ba3521c6904d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sat, 14 Nov 2026 18:01:01 -0500 Subject: [PATCH] fix(producer): compile downloads composition scripts through the shared asset downloader (#7763) * fix(producer): compile downloads composition scripts through the shared asset downloader * fix(producer): compile downloads composition scripts through the shared asset downloader * fix(producer): compile downloads composition scripts through the shared asset downloader --- packages/engine/src/index.ts | 1 + packages/engine/src/utils/urlDownloader.ts | 17 +- .../src/services/bundledScripts.test.ts | 15 +- .../services/htmlCompiler.scriptOrder.test.ts | 6 +- .../src/services/htmlCompiler.test.ts | 201 ++++++++++++------ .../producer/src/services/htmlCompiler.ts | 32 ++- packages/producer/src/utils/urlDownloader.ts | 1 + 7 files changed, 191 insertions(+), 82 deletions(-) diff --git a/packages/engine/src/index.ts b/packages/engine/src/index.ts index 47bec6f3e7..184e358efd 100644 --- a/packages/engine/src/index.ts +++ b/packages/engine/src/index.ts @@ -417,6 +417,7 @@ export { _setDownloadFetchForTests, assertPublicHttpsUrl, downloadToTemp, + fetchPublicHttpsBytes, fetchPublicHttpsText, isHttpUrl, safeDownloadUrlIdentity, diff --git a/packages/engine/src/utils/urlDownloader.ts b/packages/engine/src/utils/urlDownloader.ts index f5427fe957..5eaa07dc13 100644 --- a/packages/engine/src/utils/urlDownloader.ts +++ b/packages/engine/src/utils/urlDownloader.ts @@ -539,12 +539,12 @@ async function fetchWithValidatedRedirects( } } -/** Fetch bounded UTF-8 text while applying the downloader's redirect and SSRF policy to every hop. */ +/** Fetch bounded bytes while applying the downloader's redirect and SSRF policy to every hop. */ // fallow-ignore-next-line complexity -export async function fetchPublicHttpsText( +export async function fetchPublicHttpsBytes( url: string, options: PublicHttpsTextOptions, -): Promise { +): Promise { const timeoutMs = options.timeoutMs ?? 15_000; if (!Number.isSafeInteger(options.maxBytes) || options.maxBytes <= 0) { throw new RangeError("maxBytes must be a positive safe integer"); @@ -573,7 +573,7 @@ export async function fetchPublicHttpsText( await cancelResponseBody(response); throw classifyHttpFailure(response.status); } - if (!response.body) return ""; + if (!response.body) return new Uint8Array(); let declaredLength: number | undefined; try { @@ -623,7 +623,7 @@ export async function fetchPublicHttpsText( { expectedBytes, receivedBytes }, ); } - return new TextDecoder().decode(Buffer.concat(chunks.map((chunk) => Buffer.from(chunk)))); + return Buffer.concat(chunks.map((chunk) => Buffer.from(chunk))); } catch (error) { if (callerAborted) { throw new UrlDownloadError("cancelled", false, "Text fetch cancelled"); @@ -639,6 +639,13 @@ export async function fetchPublicHttpsText( } } +export async function fetchPublicHttpsText( + url: string, + options: PublicHttpsTextOptions, +): Promise { + return new TextDecoder().decode(await fetchPublicHttpsBytes(url, options)); +} + interface PartialIntegrity { finalHost?: string; status: number; diff --git a/packages/producer/src/services/bundledScripts.test.ts b/packages/producer/src/services/bundledScripts.test.ts index 0890fcbeef..8b92f91473 100644 --- a/packages/producer/src/services/bundledScripts.test.ts +++ b/packages/producer/src/services/bundledScripts.test.ts @@ -4,6 +4,7 @@ import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { gsapCdnDist } from "@hyperframes/core/gsap-cdn"; +import { _setDownloadFetchForTests } from "@hyperframes/engine"; import { inlineExternalScripts } from "./htmlCompiler.js"; import { GSAP_VENDOR_DIR_ENV } from "./bundledScripts.js"; @@ -17,7 +18,6 @@ const failingFetch = () => }) as unknown as typeof fetch; let dir: string; -const realFetch = globalThis.fetch; const realEnv = process.env[GSAP_VENDOR_DIR_ENV]; beforeEach(() => { dir = mkdtempSync(join(tmpdir(), "gsap-vendor-")); @@ -25,7 +25,7 @@ beforeEach(() => { process.env[GSAP_VENDOR_DIR_ENV] = dir; }); afterEach(() => { - globalThis.fetch = realFetch; + _setDownloadFetchForTests(); if (realEnv === undefined) delete process.env[GSAP_VENDOR_DIR_ENV]; else process.env[GSAP_VENDOR_DIR_ENV] = realEnv; rmSync(dir, { recursive: true, force: true }); @@ -34,19 +34,20 @@ afterEach(() => { describe("the host's own GSAP needs no network", () => { // fails without the fix: with the network gone the script stayed an external tag and the render failed. it("inlines the default CDN's gsap.min.js from the host's copy when fetch fails", async () => { - globalThis.fetch = failingFetch(); + const fetch = failingFetch(); + _setDownloadFetchForTests(fetch); expect(await inlineExternalScripts(page(URL_))).toContain(BYTES); - expect(globalThis.fetch).not.toHaveBeenCalled(); + expect(fetch).not.toHaveBeenCalled(); }); it("downloads as before when the host ships no copy", async () => { delete process.env[GSAP_VENDOR_DIR_ENV]; - globalThis.fetch = mock(async () => new Response("var net = 1;")) as unknown as typeof fetch; + _setDownloadFetchForTests(mock(async () => new Response("var net = 1;"))); expect(await inlineExternalScripts(page(URL_))).toContain("var net = 1;"); }); it("leaves any other URL, including a mirror's, to the network", async () => { - globalThis.fetch = mock(async () => new Response("var net = 1;")) as unknown as typeof fetch; + _setDownloadFetchForTests(mock(async () => new Response("var net = 1;"))); for (const other of [ "https://cdn.jsdelivr.net/npm/gsap@3.14.2/dist/gsap.min.js", "https://mirror.example.com/gsap@3.15.0/dist/gsap.min.js", @@ -58,7 +59,7 @@ describe("the host's own GSAP needs no network", () => { it("downloads when the host's copy fails the tag's integrity metadata", async () => { const fresh = "var fresh = 2;"; const digest = `sha256-${createHash("sha256").update(fresh).digest("base64")}`; - globalThis.fetch = mock(async () => new Response(fresh)) as unknown as typeof fetch; + _setDownloadFetchForTests(mock(async () => new Response(fresh))); const result = await inlineExternalScripts(page(URL_, ` integrity="${digest}"`)); expect(result).toContain(fresh); expect(result).not.toContain(BYTES); diff --git a/packages/producer/src/services/htmlCompiler.scriptOrder.test.ts b/packages/producer/src/services/htmlCompiler.scriptOrder.test.ts index b5b3ba21b6..9ad1100af8 100644 --- a/packages/producer/src/services/htmlCompiler.scriptOrder.test.ts +++ b/packages/producer/src/services/htmlCompiler.scriptOrder.test.ts @@ -5,6 +5,7 @@ import { dirname, join } from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; import { parseHTML } from "linkedom"; import { AFTER_FONTS_SCRIPT_TYPE } from "@hyperframes/core/compiler"; +import { _setDownloadFetchForTests } from "@hyperframes/engine"; import { compileForRender } from "./htmlCompiler.js"; const tempDirs: string[] = []; @@ -12,6 +13,7 @@ const tempDirs: string[] = []; afterEach(() => { for (const dir of tempDirs.splice(0)) rmSync(dir, { recursive: true, force: true }); vi.unstubAllGlobals(); + _setDownloadFetchForTests(); }); function project(files: Record): string { @@ -78,7 +80,7 @@ describe("compileForRender script order", () => { }); it("still runs a mounted composition's CDN script before its inline scripts", async () => { - vi.stubGlobal("fetch", async () => new Response("window.CDN_LIB = 1;")); + _setDownloadFetchForTests(async () => new Response("window.CDN_LIB = 1;")); const dir = project({ "index.html": ` @@ -103,7 +105,7 @@ describe("compileForRender script order", () => { }); it("defers every body script it emits until fonts, the position edit script and CDN libraries too", async () => { - vi.stubGlobal("fetch", async () => new Response("window.CDN_LIB = 1;")); + _setDownloadFetchForTests(async () => new Response("window.CDN_LIB = 1;")); const dir = project({ "index.html": ` diff --git a/packages/producer/src/services/htmlCompiler.test.ts b/packages/producer/src/services/htmlCompiler.test.ts index dc14527659..518ea7cfb1 100644 --- a/packages/producer/src/services/htmlCompiler.test.ts +++ b/packages/producer/src/services/htmlCompiler.test.ts @@ -382,6 +382,71 @@ describe("collectExternalAssets", () => { // ── inlineExternalScripts ────────────────────────────────────────────────── describe("inlineExternalScripts", () => { + it("fetches composition scripts only from public HTTPS addresses", async () => { + const fetched: string[] = []; + const record = mock(async (url: string | URL | Request) => { + fetched.push(String(url)); + return new Response("window.fromLocalHost = 1;"); + }); + const originalFetch = globalThis.fetch; + globalThis.fetch = record as unknown as typeof fetch; + _setDownloadFetchForTests(record); + try { + const html = ``; + const result = await inlineExternalScripts(html); + expect({ fetched, inlined: result.includes("window.fromLocalHost") }).toEqual({ + fetched: [], + inlined: false, + }); + } finally { + globalThis.fetch = originalFetch; + _setDownloadFetchForTests(); + } + }); + + it("trusts only the operator's GSAP mirror, without following its redirects", async () => { + const asked: { url: string; redirect?: RequestRedirect }[] = []; + const originalFetch = globalThis.fetch; + globalThis.fetch = mock(async (url: string | URL | Request, init?: RequestInit) => { + asked.push({ url: String(url), redirect: init?.redirect }); + return new Response("window.fromMirror = 1;"); + }) as unknown as typeof fetch; + const previous = process.env.HYPERFRAMES_GSAP_BASE_URL; + process.env.HYPERFRAMES_GSAP_BASE_URL = "http://127.0.0.1:8080/vendor/npm"; + try { + const mirror = "http://127.0.0.1:8080/vendor/npm/gsap@3.15.0/dist/gsap.min.js"; + const escape = "http://127.0.0.1:8080/vendor/npm/../../admin.js"; + const result = await inlineExternalScripts( + ``, + ); + expect(asked).toEqual([{ url: mirror, redirect: "error" }]); + expect(result).toContain("window.fromMirror = 1;"); + } finally { + globalThis.fetch = originalFetch; + if (previous === undefined) delete process.env.HYPERFRAMES_GSAP_BASE_URL; + else process.env.HYPERFRAMES_GSAP_BASE_URL = previous; + } + }); + + it("checks every redirect of a CDN script before following it", async () => { + const asked: string[] = []; + _setDownloadFetchForTests(async (url) => { + asked.push(url); + return new Response(null, { + status: 302, + headers: { location: "https://169.254.169.254/latest/meta-data/" }, + }); + }); + try { + const html = ``; + const result = await inlineExternalScripts(html); + expect(result).toContain('src="https://cdn.example.com/r.js"'); + expect(asked).toEqual(["https://cdn.example.com/r.js"]); + } finally { + _setDownloadFetchForTests(); + } + }); + it("returns HTML unchanged when no external scripts exist", async () => { const html = ``; const result = await inlineExternalScripts(html); @@ -395,21 +460,21 @@ describe("inlineExternalScripts", () => { }); it("leaves a CDN runtime link for the file server to strip", async () => { - const originalFetch = globalThis.fetch; const fetchMock = mock(async () => new Response("window.__hyperframeRuntime = {};")); - globalThis.fetch = fetchMock as any; + _setDownloadFetchForTests(fetchMock as any); try { const html = ``; expect(await inlineExternalScripts(html)).toBe(html); expect(fetchMock).not.toHaveBeenCalled(); } finally { - globalThis.fetch = originalFetch; + _setDownloadFetchForTests(); } }); it("inlines a CDN script on successful fetch", async () => { - const originalFetch = globalThis.fetch; - globalThis.fetch = mock(async () => new Response("var gsap = {};", { status: 200 })) as any; + _setDownloadFetchForTests( + mock(async () => new Response("var gsap = {};", { status: 200 })) as any, + ); try { const html = ``; @@ -418,7 +483,7 @@ describe("inlineExternalScripts", () => { expect(result).toContain("var gsap = {};"); expect(result).not.toContain('src="https://cdn.example.com/gsap.min.js"'); } finally { - globalThis.fetch = originalFetch; + _setDownloadFetchForTests(); } }); @@ -445,8 +510,7 @@ describe("inlineExternalScripts", () => { { metadata: "sha384-YQ==", accepted: false }, { metadata: "sha1-unknown malformed", accepted: true }, ]; - const originalFetch = globalThis.fetch; - globalThis.fetch = mock(async () => new Response(bytes)) as any; + _setDownloadFetchForTests(mock(async () => new Response(bytes)) as any); try { for (const { metadata, accepted } of cases) { const html = ``; @@ -461,15 +525,14 @@ describe("inlineExternalScripts", () => { expect(result).not.toContain('src="https://cdn.example.com/script.js"'); } } finally { - globalThis.fetch = originalFetch; + _setDownloadFetchForTests(); } }); it("preserves non-src script attributes when inlining", async () => { - const originalFetch = globalThis.fetch; - globalThis.fetch = mock( - async () => new Response('console.log("module");', { status: 200 }), - ) as any; + _setDownloadFetchForTests( + mock(async () => new Response('console.log("module");', { status: 200 })) as any, + ); try { const html = @@ -481,15 +544,16 @@ describe("inlineExternalScripts", () => { expect(result).toContain('console.log("module");'); expect(result).not.toContain('src="https://cdn.example.com/module.js"'); } finally { - globalThis.fetch = originalFetch; + _setDownloadFetchForTests(); } }); it("escapes { - const originalFetch = globalThis.fetch; - globalThis.fetch = mock( - async () => new Response('var x = "";', { status: 200 }), - ) as any; + _setDownloadFetchForTests( + mock( + async () => new Response('var x = "";', { status: 200 }), + ) as any, + ); try { const html = ``; @@ -498,18 +562,19 @@ describe("inlineExternalScripts", () => { expect(result).not.toContain(""); expect(result).toContain("<\\/script"); } finally { - globalThis.fetch = originalFetch; + _setDownloadFetchForTests(); } }); it("preserves literal replacement tokens in downloaded script content", async () => { - const originalFetch = globalThis.fetch; - globalThis.fetch = mock( - async () => - new Response('const before = "$`"; const after = "$\'"; const both = "$&";', { - status: 200, - }), - ) as any; + _setDownloadFetchForTests( + mock( + async () => + new Response('const before = "$`"; const after = "$\'"; const both = "$&";', { + status: 200, + }), + ) as any, + ); try { const html = `
tail
`; @@ -520,13 +585,14 @@ describe("inlineExternalScripts", () => { expect(result).toContain('const both = "$&";'); expect(result.match(/
tail
'; @@ -537,15 +603,16 @@ describe("inlineExternalScripts", () => { expect(result).toContain("
tail
"); expect(result).not.toContain('src="https://cdn.example.com/d3.min.js"'); } finally { - globalThis.fetch = originalFetch; + _setDownloadFetchForTests(); } }); it("warns but keeps original tag when fetch fails", async () => { - const originalFetch = globalThis.fetch; - globalThis.fetch = mock(async () => { - throw new Error("Network error"); - }) as any; + _setDownloadFetchForTests( + mock(async () => { + throw new Error("Network error"); + }) as any, + ); try { const html = ``; @@ -553,18 +620,19 @@ describe("inlineExternalScripts", () => { // Original script tag should remain since download failed expect(result).toContain('src="https://cdn.example.com/gsap.min.js"'); } finally { - globalThis.fetch = originalFetch; + _setDownloadFetchForTests(); } }); it("handles multiple CDN scripts with mixed success/failure", async () => { - const originalFetch = globalThis.fetch; - globalThis.fetch = mock(async (url: string) => { - if (url.includes("gsap")) { - return new Response("var gsap = {};", { status: 200 }); - } - throw new Error("404"); - }) as any; + _setDownloadFetchForTests( + mock(async (url: string) => { + if (url.includes("gsap")) { + return new Response("var gsap = {};", { status: 200 }); + } + throw new Error("404"); + }) as any, + ); try { const html = ` @@ -577,17 +645,18 @@ describe("inlineExternalScripts", () => { // Lottie should remain as original tag expect(result).toContain('src="https://cdn.example.com/lottie.min.js"'); } finally { - globalThis.fetch = originalFetch; + _setDownloadFetchForTests(); } }); it("handles duplicate CDN URLs (same script referenced twice)", async () => { - const originalFetch = globalThis.fetch; let fetchCount = 0; - globalThis.fetch = mock(async () => { - fetchCount++; - return new Response("var gsap = {};", { status: 200 }); - }) as any; + _setDownloadFetchForTests( + mock(async () => { + fetchCount++; + return new Response("var gsap = {};", { status: 200 }); + }) as any, + ); try { const html = ` @@ -601,7 +670,7 @@ describe("inlineExternalScripts", () => { result.match(/\/\* inlined: https:\/\/cdn\.example\.com\/gsap\.min\.js \*\//g)?.length, ).toBe(2); } finally { - globalThis.fetch = originalFetch; + _setDownloadFetchForTests(); } }); }); @@ -847,13 +916,14 @@ describe("detectRenderModeHints", () => { `, ); - const originalFetch = globalThis.fetch; - globalThis.fetch = mock(async () => { - return new Response( - "window.gsap = { timeline: function() { return { paused: true }; } }; function __ticker(){ requestAnimationFrame(__ticker); }", - { status: 200 }, - ); - }) as any; + _setDownloadFetchForTests( + mock(async () => { + return new Response( + "window.gsap = { timeline: function() { return { paused: true }; } }; function __ticker(){ requestAnimationFrame(__ticker); }", + { status: 200 }, + ); + }) as any, + ); try { const result = await compileForRender(projectDir, join(projectDir, "index.html"), projectDir); @@ -861,7 +931,7 @@ describe("detectRenderModeHints", () => { expect(result.renderModeHints.recommendScreenshot).toBe(false); expect(result.renderModeHints.reasons).toEqual([]); } finally { - globalThis.fetch = originalFetch; + _setDownloadFetchForTests(); } }); @@ -3478,16 +3548,17 @@ describe("nested CDN integrity", () => { join(dir, "child.html"), `
Child
`, ); - const originalFetch = globalThis.fetch; try { - globalThis.fetch = mock(async () => new Response(bytes)) as any; + _setDownloadFetchForTests(mock(async () => new Response(bytes)) as any); await expect(compileForRender(dir, join(dir, "index.html"), dir)).resolves.toBeDefined(); - globalThis.fetch = mock(async () => new Response("window.compromised = true;")) as any; + _setDownloadFetchForTests( + mock(async () => new Response("window.compromised = true;")) as any, + ); await expect(compileForRender(dir, join(dir, "index.html"), dir)).rejects.toThrow( "Subresource integrity mismatch", ); } finally { - globalThis.fetch = originalFetch; + _setDownloadFetchForTests(); rmSync(dir, { recursive: true, force: true }); } }); @@ -3516,15 +3587,19 @@ describe("missing local GSAP in renders", () => { const previous = process.env.HYPERFRAMES_GSAP_BASE_URL; if (base === undefined) delete process.env.HYPERFRAMES_GSAP_BASE_URL; else process.env.HYPERFRAMES_GSAP_BASE_URL = base; - globalThis.fetch = mock(async (input: string | URL | Request) => { + const fetchMock = mock(async (input: string | URL | Request) => { fetched.push(String(input instanceof Request ? input.url : input)); return new Response("window.gsap = {};", { status: 200 }); - }) as unknown as typeof fetch; + }); + // The operator's mirror is fetched directly; every other URL goes through the checked transport. + globalThis.fetch = fetchMock as unknown as typeof fetch; + _setDownloadFetchForTests(fetchMock); try { const result = await compileForRender(projectDir, join(projectDir, "index.html"), projectDir); return { html: result.html, fetched }; } finally { globalThis.fetch = originalFetch; + _setDownloadFetchForTests(); if (previous === undefined) delete process.env.HYPERFRAMES_GSAP_BASE_URL; else process.env.HYPERFRAMES_GSAP_BASE_URL = previous; rmSync(projectDir, { recursive: true, force: true }); diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts index 635c3a0036..9c05aea584 100644 --- a/packages/producer/src/services/htmlCompiler.ts +++ b/packages/producer/src/services/htmlCompiler.ts @@ -103,6 +103,7 @@ import { } from "@hyperframes/engine"; import { downloadToTemp, + fetchPublicHttpsBytes, fetchPublicHttpsText, isHttpUrl, safeDownloadUrlIdentity, @@ -1333,6 +1334,30 @@ function matchesScriptIntegrity(bytes: Uint8Array, metadata: string): boolean { ); } +const MAX_SCRIPT_BYTES = 16 * 1024 * 1024; + +/** The operator's `HYPERFRAMES_GSAP_BASE_URL` may be on this machine; a composition's own URLs never are. */ +function operatorGsapMirror(): string | undefined { + const raw = process.env.HYPERFRAMES_GSAP_BASE_URL?.trim(); + if (!raw || raw.startsWith("/")) return undefined; + try { + return `${normalizeGsapBaseUrl(raw)}/`; + } catch { + return undefined; + } +} + +async function downloadScript(src: string, mirror: string | undefined): Promise { + if (mirror && new URL(src).href.startsWith(mirror)) { + const response = await fetch(src, { redirect: "error", signal: AbortSignal.timeout(15_000) }); + if (!response.ok) throw new Error(`HTTP ${response.status} for ${src}`); + const bytes = new Uint8Array(await response.arrayBuffer()); + if (bytes.byteLength > MAX_SCRIPT_BYTES) throw new Error(`Script over 16 MiB: ${src}`); + return bytes; + } + return fetchPublicHttpsBytes(src, { maxBytes: MAX_SCRIPT_BYTES, timeoutMs: 15_000 }); +} + /** * Download external CDN scripts and inline them into the HTML so rendering * works without network access (Docker, CI, restricted environments). @@ -1353,6 +1378,7 @@ export async function inlineExternalScripts(html: string): Promise { if (externalScripts.length === 0) return html; + const mirror = operatorGsapMirror(); const downloads = await Promise.allSettled( externalScripts.map(async ({ el, src }) => { const integrity = el.getAttribute("integrity") || ""; @@ -1361,11 +1387,7 @@ export async function inlineExternalScripts(html: string): Promise { if (bundled && matchesScriptIntegrity(bundled, integrity)) { return { src, text: new TextDecoder().decode(bundled) }; } - const response = await fetch(src, { - signal: AbortSignal.timeout(15_000), - }); - if (!response.ok) throw new Error(`HTTP ${response.status} for ${src}`); - const bytes = new Uint8Array(await response.arrayBuffer()); + const bytes = await downloadScript(src, mirror); if (!matchesScriptIntegrity(bytes, integrity)) { throw new ScriptIntegrityError(`Subresource integrity mismatch for ${src}`); } diff --git a/packages/producer/src/utils/urlDownloader.ts b/packages/producer/src/utils/urlDownloader.ts index 4dd34e8880..263ca8c45d 100644 --- a/packages/producer/src/utils/urlDownloader.ts +++ b/packages/producer/src/utils/urlDownloader.ts @@ -4,6 +4,7 @@ */ export { downloadToTemp, + fetchPublicHttpsBytes, fetchPublicHttpsText, isHttpUrl, safeDownloadUrlIdentity,