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
This commit is contained in:
Miguel Ángel
2026-11-14 18:01:01 -05:00
committed by GitHub
parent 89bca002ef
commit cd55f059c3
7 changed files with 191 additions and 82 deletions
+1
View File
@@ -417,6 +417,7 @@ export {
_setDownloadFetchForTests,
assertPublicHttpsUrl,
downloadToTemp,
fetchPublicHttpsBytes,
fetchPublicHttpsText,
isHttpUrl,
safeDownloadUrlIdentity,
+12 -5
View File
@@ -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<string> {
): Promise<Uint8Array> {
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<string> {
return new TextDecoder().decode(await fetchPublicHttpsBytes(url, options));
}
interface PartialIntegrity {
finalHost?: string;
status: number;
@@ -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);
@@ -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, string>): 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": `<!doctype html>
<html><body>
@@ -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": `<!doctype html>
<html><body>
@@ -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 = `<html><body><script src="http://127.0.0.1:8080/a.js"></script><script src="https://127.0.0.1:8443/b.js"></script><script src="https://10.0.0.5/c.js"></script></body></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(
`<script src="${mirror}"></script><script src="${escape}"></script>`,
);
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 = `<html><body><script src="https://cdn.example.com/r.js"></script></body></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 = `<html><body><script>var x = 1;</script></body></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 = `<html><head><script src="https://cdn.example.com/hyperframe.runtime.iife.js"></script></head><body></body></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 = `<html><body><script src="https://cdn.example.com/gsap.min.js"></script></body></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 = `<script src="https://cdn.example.com/script.js" integrity="${metadata}" crossorigin="anonymous"></script>`;
@@ -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 </script in downloaded content", async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = mock(
async () => new Response('var x = "</script><script>alert(1)</script>";', { status: 200 }),
) as any;
_setDownloadFetchForTests(
mock(
async () => new Response('var x = "</script><script>alert(1)</script>";', { status: 200 }),
) as any,
);
try {
const html = `<html><body><script src="https://cdn.example.com/evil.js"></script></body></html>`;
@@ -498,18 +562,19 @@ describe("inlineExternalScripts", () => {
expect(result).not.toContain("</script><script>alert(1)</script>");
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 = `<html><body><script src="https://cdn.example.com/d3.min.js"></script><div>tail</div></body></html>`;
@@ -520,13 +585,14 @@ describe("inlineExternalScripts", () => {
expect(result).toContain('const both = "$&";');
expect(result.match(/<script>/g)?.length).toBe(1);
} finally {
globalThis.fetch = originalFetch;
_setDownloadFetchForTests();
}
});
it("returns a fragment when the input has no html/body wrapper", async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = mock(async () => new Response("var d3 = {};", { status: 200 })) as any;
_setDownloadFetchForTests(
mock(async () => new Response("var d3 = {};", { status: 200 })) as any,
);
try {
const html = '<script src="https://cdn.example.com/d3.min.js"></script><div>tail</div>';
@@ -537,15 +603,16 @@ describe("inlineExternalScripts", () => {
expect(result).toContain("<div>tail</div>");
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 = `<html><body><script src="https://cdn.example.com/gsap.min.js"></script></body></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 = `<html><body>
@@ -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 = `<html><body>
@@ -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", () => {
</template>`,
);
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"),
`<html><head><script src="${src}" integrity="${integrity}" crossorigin="anonymous"></script></head><body><div data-composition-id="child" data-width="320" data-height="180" data-duration="1">Child</div></body></html>`,
);
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 });
+27 -5
View File
@@ -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<Uint8Array> {
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<string> {
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<string> {
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}`);
}
@@ -4,6 +4,7 @@
*/
export {
downloadToTemp,
fetchPublicHttpsBytes,
fetchPublicHttpsText,
isHttpUrl,
safeDownloadUrlIdentity,