fix(producer): a render variable bound to a media src can't add markup to the page (#8678)

This commit is contained in:
Miguel Ángel
2026-11-24 09:29:18 +00:00
committed by GitHub
parent bef7b44bee
commit fb1b15b941
3 changed files with 118 additions and 10 deletions
@@ -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 = `<svg><clipPath id="c"><rect/></clipPath></svg><div clip-path="url(#c)"></div>`;
const clip = "clip.mp4#url(#c)</style><script>window.ran = true</script>";
it("refuses a bound media source holding a raw `<`, keeping the authored one", async () => {
const dir = project({
"index.html": withBody(
`<style>video[src*="url(#c)"] { opacity: 0.5; }</style>${svg}<video id="v1" data-var-src="clip" src="fallback.mp4"></video>`,
),
"scene.html": SCENE.replace("<h1>Scene</h1>", svg),
"index.html": withBody(`<video id="v1" data-var-src="clip" src="fallback.mp4"></video>`),
"scene.html": SCENE,
});
const clip = "clip.mp4#url(#c)</style><script>window.ran = true</script>";
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 = `<svg><clipPath id="c"><rect/></clipPath></svg><div clip-path="url(#c)"></div>`;
const dir = project({
"index.html": withBody(
`<style>video[src*="url(#c)"] { opacity: 0.5; }</style>${svg}<video id="v1" src="${clip.replace(/</g, "&lt;")}"></video>`,
),
"scene.html": SCENE.replace("<h1>Scene</h1>", 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)"));
+11 -3
View File
@@ -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,
"</style>".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,
});
@@ -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<typeof import("../utils/urlDownloader.js")>()),
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\\"><script>window.__fromSheet = 1;</script>"); }`,
}));
const PAGE =
`<!doctype html><html data-composition-variables='[{"id":"clip","type":"string","default":"clip.mp4"}]'>` +
`<head><style>@font-face { font-family: "Brand"; src: url(${FONT_URL}); }</style></head>` +
`<body><main data-composition-id="root" data-start="0" data-duration="1" data-width="16" data-height="16">` +
`<video id="v" src="clip.mp4" data-var-src="clip" data-start="0" data-duration="1" data-track-index="0"></video>` +
`<script>window.__authored = 1;</script></main></body></html>`;
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",
`<link rel='stylesheet' href='https://fonts.example.com/brand.css'><script>window.__injected = 1;</script>`,
],
[
"a planted style block naming a project font",
`<style>@font-face{font-family:B;src:url(fonts/local.woff2)}</style><script>window.__injected = 1;</script>`,
],
["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})`);
});
});