From fb1b15b941a486fce0170c738045a5bd36af42c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Tue, 24 Nov 2026 09:29:18 +0000 Subject: [PATCH] fix(producer): a render variable bound to a media src can't add markup to the page (#8678) --- .../services/htmlCompiler.sizeLimit.test.ts | 28 ++++-- .../producer/src/services/htmlCompiler.ts | 14 ++- .../htmlCompiler.variableMediaText.test.ts | 86 +++++++++++++++++++ 3 files changed, 118 insertions(+), 10 deletions(-) create mode 100644 packages/producer/src/services/htmlCompiler.variableMediaText.test.ts diff --git a/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts b/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts index d4bca9950b..102bf4c592 100644 --- a/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts +++ b/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts @@ -362,19 +362,33 @@ describe("variable-driven composition styles and media sources", () => { }); // The `[src]` alias escapes `<` itself, so it can't end its style element even without postcss. - it("writes `<` in a bound media source's selector alias as a CSS escape itself", async () => { - const svg = `
`; + const clip = "clip.mp4#url(#c)"; + + it("refuses a bound media source holding a raw `<`, keeping the authored one", async () => { const dir = project({ - "index.html": withBody( - `${svg}`, - ), - "scene.html": SCENE.replace("

Scene

", svg), + "index.html": withBody(``), + "scene.html": SCENE, }); - const clip = "clip.mp4#url(#c)"; const compiled = await compileForRender(dir, join(dir, "index.html"), dir, { variables: { clip }, }); + const { document } = parseHTML(compiled.html); + expect(document.getElementById("v1")!.getAttribute("src")).toBe("fallback.mp4"); + const scripts = [...document.querySelectorAll("script")].map((script) => script.textContent); + expect(scripts.filter((text) => text?.includes("window.ran"))).toEqual([]); + }); + + it("writes `<` in a media source's selector alias as a CSS escape itself", async () => { + const svg = `
`; + const dir = project({ + "index.html": withBody( + `${svg}`, + ), + "scene.html": SCENE.replace("

Scene

", svg), + }); + const compiled = await compileForRender(dir, join(dir, "index.html"), dir); + const { document } = parseHTML(compiled.html); const src = document.getElementById("v1")!.getAttribute("src")!; expect(src).toBe(clip.replace("#c)", "#scene--c)")); diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts index c5985c4808..80ee34ded5 100644 --- a/packages/producer/src/services/htmlCompiler.ts +++ b/packages/producer/src/services/htmlCompiler.ts @@ -2370,7 +2370,15 @@ function localizeFontFaceUrls( "Remote font download failed", "Localized remote font face(s)", assets, - (h, url, relPath) => h.replaceAll(`url(${url})`, `url("${relPath}")`), + (h, url, relPath) => + replaceMatches(h, styleBlockMatches(h), (style, css) => + replaceInner( + style, + css, + "".length, + css.replaceAll(`url(${url})`, `url("${relPath}")`), + ), + ), ); } @@ -2659,7 +2667,7 @@ function rebaseDirectEntryAssetPaths( export const MAX_VARIABLE_MEDIA_SRC_BYTES = 8 * 1024; // oxlint-disable-next-line no-control-regex -const MEDIA_SRC_CONTROL_CHARS = /[\u0000-\u001f\u007f]/; +const MEDIA_SRC_NON_URL_CHARS = /[\u0000-\u001f\u007f<>]/; /** * Point each `data-var-src` video/audio at the clip its variable names, so the length and sound read below @@ -2684,7 +2692,7 @@ function bindVariableMediaSources( const bound = boundMediaSrc(el, values); if (bound === null) continue; const src = bound.trim(); - if (src.length > MAX_VARIABLE_MEDIA_SRC_BYTES || MEDIA_SRC_CONTROL_CHARS.test(src)) { + if (src.length > MAX_VARIABLE_MEDIA_SRC_BYTES || MEDIA_SRC_NON_URL_CHARS.test(src)) { log?.warn("[Compiler] Refused a variable media source that cannot be a URL", { length: src.length, }); diff --git a/packages/producer/src/services/htmlCompiler.variableMediaText.test.ts b/packages/producer/src/services/htmlCompiler.variableMediaText.test.ts new file mode 100644 index 0000000000..d893dbc776 --- /dev/null +++ b/packages/producer/src/services/htmlCompiler.variableMediaText.test.ts @@ -0,0 +1,86 @@ +// @vitest-environment node +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { copyFileSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { parseHTML } from "linkedom"; +import { compileForRender } from "./htmlCompiler.js"; + +const FONT_URL = "https://fonts.example.com/brand.woff2"; +const FONT_FILE = join( + __dirname, + "../../tests/heygen-promo-preview-assets/src/TT_Norms_Pro_Medium.woff2", +); + +vi.mock("../utils/urlDownloader.js", async (importOriginal) => ({ + ...(await importOriginal()), + downloadToTemp: async (url: string, destDir: string) => { + if (url !== FONT_URL) throw new Error("offline test"); + mkdirSync(destDir, { recursive: true }); + const local = join(destDir, "download_brand.woff2"); + copyFileSync(FONT_FILE, local); + return local; + }, + fetchPublicHttpsText: async () => + `@font-face { font-family: "Brand"; src: url("x\\">"); }`, +})); + +const PAGE = + `` + + `` + + `
` + + `` + + `
`; + +describe("compileForRender with a variable bound to data-var-src", () => { + let projectDir: string; + + beforeEach(() => { + projectDir = mkdtempSync(join(tmpdir(), "hf-var-media-text-")); + mkdirSync(join(projectDir, "fonts")); + copyFileSync(FONT_FILE, join(projectDir, "fonts/local.woff2")); + writeFileSync(join(projectDir, "index.html"), PAGE); + vi.stubGlobal( + "fetch", + vi.fn(async () => new Response("", { status: 400 })), + ); + }); + + afterEach(() => { + vi.unstubAllGlobals(); + rmSync(projectDir, { recursive: true, force: true }); + }); + + const compile = async (clip: string) => { + const { html } = await compileForRender( + projectDir, + join(projectDir, "index.html"), + join(projectDir, ".dl"), + { allowSystemFontCapture: false, variables: { clip } }, + ); + return parseHTML(html).document; + }; + + it.each([ + [ + "a planted stylesheet link", + ``, + ], + [ + "a planted style block naming a project font", + ``, + ], + ["a planted remote font url", `x.mp4?url(${FONT_URL})/onerror=window.__injected=1//`], + ])("adds no element or attribute from %s", async (_route, clip) => { + const document = await compile(clip); + const scripts = [...document.querySelectorAll("script")].map((s) => s.textContent ?? ""); + expect( + scripts.some((text) => text.includes("__injected") || text.includes("__fromSheet")), + ).toBe(false); + expect(scripts.some((text) => text.includes("__authored"))).toBe(true); + const names = document.getElementById("v")!.getAttributeNames(); + expect(names.filter((name) => /^on|["'()/]/.test(name))).toEqual([]); + const styles = [...document.querySelectorAll("style")].map((s) => s.textContent ?? ""); + expect(styles.join("\n")).not.toContain(`url(${FONT_URL})`); + }); +});