mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-23 11:29:54 +00:00
fix(core): attribute-value escapes write '<' so style text can't end early (#8250)
* fix(core): selector alias values escape '<' so style text can't end early Attribute values that the compiler copies into selector text, such as an SVG reference alias's rewritten value or a composition or root id in a scope selector, are written inside a <style> element. A '<' in one could close that element early. These values now write '<' as the CSS escape \3c , which reads back as the same character, so each selector still matches the same elements. * test(core): name the '<' escape tests for what the escapes guarantee The selector escapes write '<' as \3c themselves, so selector text cannot end its style element even when the CSS is serialized without postcss's own </style escaping. The test names and comments now say that.
This commit is contained in:
@@ -252,7 +252,7 @@ window.__timelines.main = gsap.timeline({ paused: true });</script><script type=
|
||||
function R(e) {
|
||||
return Array.from(e, (n) => {
|
||||
let t = n.charCodeAt(0);
|
||||
return t === 0 ? "\uFFFD" : t < 32 || t === 127 ? `\\${t.toString(16)} ` : n === '"' || n === "\\" ? `\\${n}` : n;
|
||||
return t === 0 ? "\uFFFD" : t < 32 || t === 127 || n === "<" ? `\\${t.toString(16)} ` : n === '"' || n === "\\" ? `\\${n}` : n;
|
||||
}).join("");
|
||||
}
|
||||
function H(e, n) {
|
||||
@@ -842,7 +842,7 @@ window.__timelines.main = gsap.timeline({ paused: true });</script><script type=
|
||||
function R(e) {
|
||||
return Array.from(e, (n) => {
|
||||
let t = n.charCodeAt(0);
|
||||
return t === 0 ? "\uFFFD" : t < 32 || t === 127 ? `\\${t.toString(16)} ` : n === '"' || n === "\\" ? `\\${n}` : n;
|
||||
return t === 0 ? "\uFFFD" : t < 32 || t === 127 || n === "<" ? `\\${t.toString(16)} ` : n === '"' || n === "\\" ? `\\${n}` : n;
|
||||
}).join("");
|
||||
}
|
||||
function H(e, n) {
|
||||
@@ -1432,7 +1432,7 @@ window.__timelines.main = gsap.timeline({ paused: true });</script><script type=
|
||||
function R(e) {
|
||||
return Array.from(e, (n) => {
|
||||
let t = n.charCodeAt(0);
|
||||
return t === 0 ? "\uFFFD" : t < 32 || t === 127 ? `\\${t.toString(16)} ` : n === '"' || n === "\\" ? `\\${n}` : n;
|
||||
return t === 0 ? "\uFFFD" : t < 32 || t === 127 || n === "<" ? `\\${t.toString(16)} ` : n === '"' || n === "\\" ? `\\${n}` : n;
|
||||
}).join("");
|
||||
}
|
||||
function H(e, n) {
|
||||
|
||||
@@ -279,7 +279,7 @@ window.__timelines.main = gsap.timeline({ paused: true });
|
||||
function R(e) {
|
||||
return Array.from(e, (n) => {
|
||||
let t = n.charCodeAt(0);
|
||||
return t === 0 ? "\uFFFD" : t < 32 || t === 127 ? `\\${t.toString(16)} ` : n === '"' || n === "\\" ? `\\${n}` : n;
|
||||
return t === 0 ? "\uFFFD" : t < 32 || t === 127 || n === "<" ? `\\${t.toString(16)} ` : n === '"' || n === "\\" ? `\\${n}` : n;
|
||||
}).join("");
|
||||
}
|
||||
function H(e, n) {
|
||||
@@ -870,7 +870,7 @@ window.__timelines.main = gsap.timeline({ paused: true });
|
||||
function R(e) {
|
||||
return Array.from(e, (n) => {
|
||||
let t = n.charCodeAt(0);
|
||||
return t === 0 ? "\uFFFD" : t < 32 || t === 127 ? `\\${t.toString(16)} ` : n === '"' || n === "\\" ? `\\${n}` : n;
|
||||
return t === 0 ? "\uFFFD" : t < 32 || t === 127 || n === "<" ? `\\${t.toString(16)} ` : n === '"' || n === "\\" ? `\\${n}` : n;
|
||||
}).join("");
|
||||
}
|
||||
function H(e, n) {
|
||||
@@ -1461,7 +1461,7 @@ window.__timelines.main = gsap.timeline({ paused: true });
|
||||
function R(e) {
|
||||
return Array.from(e, (n) => {
|
||||
let t = n.charCodeAt(0);
|
||||
return t === 0 ? "\uFFFD" : t < 32 || t === 127 ? `\\${t.toString(16)} ` : n === '"' || n === "\\" ? `\\${n}` : n;
|
||||
return t === 0 ? "\uFFFD" : t < 32 || t === 127 || n === "<" ? `\\${t.toString(16)} ` : n === '"' || n === "\\" ? `\\${n}` : n;
|
||||
}).join("");
|
||||
}
|
||||
function H(e, n) {
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
scopedModulePrelude,
|
||||
wrapScopedCompositionScript,
|
||||
} from "./compositionScoping";
|
||||
import { escapeCssIdentifier } from "./selectorIdTokens";
|
||||
|
||||
describe("composition scoping", () => {
|
||||
it("reports the child source before authored code and finishes after a thrown script", () => {
|
||||
@@ -1618,3 +1619,33 @@ it.each(["a$'b", "a$`b", "a$&b"])(
|
||||
expect(scopeCssToComposition(css(id), id)).toBe(plain.split("aZZb").join(id));
|
||||
},
|
||||
);
|
||||
|
||||
// Scoping escapes `<` in ids itself, so a selector can't end its style element even without postcss.
|
||||
it.each([false, true])(
|
||||
"writes `<` in scoped ids as a CSS escape itself, and the selector still matches (compound root: %s)",
|
||||
(compoundAuthoredRoot) => {
|
||||
const id = "scene</style><script>window.ran = 1</script>";
|
||||
const rootId = "root</style><script>window.ran = 2</script>";
|
||||
const css = scopeCssToComposition(
|
||||
`#${escapeCssIdentifier(rootId)} .title { color: red; }`,
|
||||
id,
|
||||
undefined,
|
||||
rootId,
|
||||
{ compoundAuthoredRoot },
|
||||
);
|
||||
|
||||
expect(css).not.toMatch(/<\/style|<script/i);
|
||||
const { document } = new JSDOM(
|
||||
`<style>${css}</style><div><div><p class="title"></p></div></div>`,
|
||||
).window;
|
||||
const host = document.querySelector("body > div")!;
|
||||
host.setAttribute("data-composition-id", id);
|
||||
(compoundAuthoredRoot ? host : host.firstElementChild!).setAttribute(
|
||||
"data-hf-authored-id",
|
||||
rootId,
|
||||
);
|
||||
expect(document.querySelectorAll("style, script").length).toBe(1);
|
||||
const rule = document.styleSheets[0]!.cssRules[0] as CSSStyleRule;
|
||||
expect(document.querySelector(".title")!.matches(rule.selectorText)).toBe(true);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -11,7 +11,7 @@ function escapeRegExp(value: string): string {
|
||||
}
|
||||
|
||||
function escapeCssAttributeValue(value: string): string {
|
||||
return value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
|
||||
return value.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/</g, "\\3c ");
|
||||
}
|
||||
|
||||
function getAuthoredRootIdSelectorForms(authoredRootId: string): string[] {
|
||||
|
||||
@@ -1328,6 +1328,25 @@ it("charges a mount's selector aliases while they are built, and each byte once"
|
||||
expect(perSrc.get("art.html")).toBeLessThan(growth + 1000);
|
||||
});
|
||||
|
||||
// The default scope escapes `<` itself, so it can't end its style element even without postcss.
|
||||
it("writes `<` in a host id as a CSS escape itself in the default scope selector", () => {
|
||||
const id = "scene</style><script>window.ran = true</script>";
|
||||
const { document } = parseHTML(`<!DOCTYPE html><html><body><div data-composition-id="main">
|
||||
<div data-composition-src="scene.html" data-start="0" data-duration="2"></div>
|
||||
</div></body></html>`);
|
||||
const host = document.querySelector("[data-composition-src]")!;
|
||||
host.setAttribute("data-composition-id", id);
|
||||
const { styles } = inlineSubCompositions(document, [host], {
|
||||
resolveHtml: () =>
|
||||
`<template><div data-composition-id="scene"><style>.title { color: red; }</style></div></template>`,
|
||||
parseHtml: (html) => parseHTML(html).document,
|
||||
});
|
||||
|
||||
expect(styles.map((style) => style.css)).toEqual([
|
||||
String.raw`[data-composition-id="scene\3c /style>\3c script>window.ran = true\3c /script>"] .title { color: red; }`,
|
||||
]);
|
||||
});
|
||||
|
||||
// fails without the fix: nested templates were walked per copy, linkedom copied each, and SVG ones rescanned.
|
||||
describe("dropAuthoredSvgReferenceAliases", () => {
|
||||
const planted = `data-hf-svg-reference-aliases="[]"`;
|
||||
|
||||
@@ -371,7 +371,7 @@ export interface InlineSubCompositionsResult {
|
||||
const isDocument = (node: Node): node is Document => node.nodeType === 9;
|
||||
|
||||
function defaultBuildScopeSelector(compId: string): string {
|
||||
const escaped = compId.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
|
||||
const escaped = compId.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/</g, "\\3c ");
|
||||
return `[data-composition-id="${escaped}"]`;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
decodeCssIdentifierAt,
|
||||
escapeCssAttributeValue,
|
||||
escapeCssIdentifier,
|
||||
parseAttributeSelector,
|
||||
replaceSelectorAttributeTokens,
|
||||
replaceSelectorIdTokens,
|
||||
} from "./selectorIdTokens";
|
||||
@@ -100,6 +101,15 @@ it("serializes control characters inside CSS attribute string values", () => {
|
||||
expect(escapeCssAttributeValue('a\nb\rc\fd"e\\f')).toBe('a\\a b\\d c\\c d\\"e\\\\f');
|
||||
});
|
||||
|
||||
// The escape writes `<` as `\3c `, so selector text can't end its style element even without postcss.
|
||||
it("hex-escapes `<` in an attribute string value, which still reads back as the same value", () => {
|
||||
const value = "a</style><script>alert(1)</script>";
|
||||
const escaped = escapeCssAttributeValue(value);
|
||||
|
||||
expect(escaped).toBe(String.raw`a\3c /style>\3c script>alert(1)\3c /script>`);
|
||||
expect(parseAttributeSelector(`[src="${escaped}"]`)?.value).toBe(value);
|
||||
});
|
||||
|
||||
it("keeps escaped closing brackets inside unquoted attribute tokens", () => {
|
||||
expect(
|
||||
replaceSelectorAttributeTokens(String.raw`use[href=\#sym\]bol]`, () => '[href="#renamed"]'),
|
||||
|
||||
@@ -271,7 +271,7 @@ export function escapeCssAttributeValue(value: string): string {
|
||||
return Array.from(value, (char) => {
|
||||
const code = char.charCodeAt(0);
|
||||
if (code === 0) return "\uFFFD";
|
||||
if (code < 0x20 || code === 0x7f) return `\\${code.toString(16)} `;
|
||||
if (code < 0x20 || code === 0x7f || char === "<") return `\\${code.toString(16)} `;
|
||||
return char === '"' || char === "\\" ? `\\${char}` : char;
|
||||
}).join("");
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { JSDOM } from "jsdom";
|
||||
import { parseHTML } from "linkedom";
|
||||
|
||||
const hashCalls = vi.hoisted(() => [] as string[]);
|
||||
@@ -553,6 +554,28 @@ it("rewrites attribute selectors in linear time however many fragment references
|
||||
expect(work(800, sameSymbol) / work(400, sameSymbol)).toBeLessThan(2.6);
|
||||
});
|
||||
|
||||
// An alias escapes `<` itself, so its rule can't end its style element even without postcss.
|
||||
it("writes `<` in an aliased attribute value as a CSS escape itself, and the rule still matches it", () => {
|
||||
const tail = "</style><script>window.ran = true</script>";
|
||||
const after = `clip.mp4#url(#m--c)${tail}`;
|
||||
const references = [
|
||||
{ name: "src", localName: "src", namespaceURI: null, before: `clip.mp4#url(#c)${tail}`, after },
|
||||
];
|
||||
const css = rewriteSvgIdReferencesInCss(
|
||||
`video[src*="url(#c)"] { opacity: 0.5; }`,
|
||||
new Map(),
|
||||
references,
|
||||
);
|
||||
|
||||
expect(css).not.toMatch(/<\/style|<script/i);
|
||||
const { document } = new JSDOM(`<style>${css}</style><video></video>`).window;
|
||||
const video = document.querySelector("video")!;
|
||||
video.setAttribute("src", after);
|
||||
expect(document.querySelectorAll("style, script").length).toBe(1);
|
||||
const rule = document.styleSheets[0]!.cssRules[0] as CSSStyleRule;
|
||||
expect(video.matches(rule.selectorText)).toBe(true);
|
||||
});
|
||||
|
||||
// fails without the fix: the whole namespace, however long, prefixed every renamed id.
|
||||
it("caps a long namespace's prefix, each mount's renamed ids still distinct", () => {
|
||||
const long = (end: string) => `${"x".repeat(5000)}${end}`;
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -340,6 +340,29 @@ describe("variable-driven composition styles and media sources", () => {
|
||||
expect(await srcFor("https://example.com/my clip.mp4")).toBe("https://example.com/my clip.mp4");
|
||||
expect(await srcFor("https://example.com/x.mp4\n")).toBe("https://example.com/x.mp4");
|
||||
});
|
||||
|
||||
// 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 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),
|
||||
});
|
||||
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);
|
||||
const src = document.getElementById("v1")!.getAttribute("src")!;
|
||||
expect(src).toBe(clip.replace("#c)", "#scene--c)"));
|
||||
const scripts = [...document.querySelectorAll("script")].map((script) => script.textContent);
|
||||
expect(scripts.filter((text) => text?.includes("window.ran"))).toEqual([]);
|
||||
const sheets = [...document.querySelectorAll("style")].map((style) => style.textContent);
|
||||
expect(sheets).toContainEqual(expect.stringContaining(`[src="${src.replace(/</g, "\\3c ")}"]`));
|
||||
});
|
||||
});
|
||||
|
||||
describe("local font embedding", () => {
|
||||
|
||||
Reference in New Issue
Block a user