From 9e783536701da7f837c6902c737ddf25ab90994b Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Thu, 19 Nov 2026 19:27:40 +0000 Subject: [PATCH] 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 "; + const rootId = "root"; + const css = scopeCssToComposition( + `#${escapeCssIdentifier(rootId)} .title { color: red; }`, + id, + undefined, + rootId, + { compoundAuthoredRoot }, + ); + + expect(css).not.toMatch(/<\/style|"; + const { document } = parseHTML(`
+
+
`); + const host = document.querySelector("[data-composition-src]")!; + host.setAttribute("data-composition-id", id); + const { styles } = inlineSubCompositions(document, [host], { + resolveHtml: () => + ``, + 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="[]"`; diff --git a/packages/core/src/compiler/inlineSubCompositions.ts b/packages/core/src/compiler/inlineSubCompositions.ts index 53580c16d3..7152fd8ea2 100644 --- a/packages/core/src/compiler/inlineSubCompositions.ts +++ b/packages/core/src/compiler/inlineSubCompositions.ts @@ -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(/ { 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"; + 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"]'), diff --git a/packages/core/src/compiler/selectorIdTokens.ts b/packages/core/src/compiler/selectorIdTokens.ts index 0f6e834c0e..df8301ba09 100644 --- a/packages/core/src/compiler/selectorIdTokens.ts +++ b/packages/core/src/compiler/selectorIdTokens.ts @@ -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(""); } diff --git a/packages/core/src/compiler/svgIdNamespacing.test.ts b/packages/core/src/compiler/svgIdNamespacing.test.ts index 0943d4f103..7a9e822e87 100644 --- a/packages/core/src/compiler/svgIdNamespacing.test.ts +++ b/packages/core/src/compiler/svgIdNamespacing.test.ts @@ -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 = ""; + 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|"; + 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(/ {