Skip to content

Commit 201a79f

Browse files
vanceingallsclaude
andcommitted
fix(core): escape < in the compiler-emitted variables script
`<script>` is a raw-text element: HTML serialization does not escape its content, and the tokenizer ends it at the first `</script`. The statement `buildVariablesByCompScript` emits embeds composition variables via `JSON.stringify`, which escapes `"` and `\` but not `/` — so a variable value, key, or composition id containing `</script>` terminated the element early and the remainder was parsed as markup, corrupting the compiled document. Rewrite `<` to its JSON unicode escape. This is transparent to JSON.parse, so the table the runtime reads is unchanged. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent f75ca07 commit 201a79f

2 files changed

Lines changed: 68 additions & 1 deletion

File tree

‎packages/core/src/compiler/compositionScoping.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, it, vi } from "vitest";
22
import { parseHTML } from "linkedom";
33
import {
4+
buildVariablesByCompScript,
45
scopeCssToComposition,
56
wrapInlineScriptWithErrorBoundary,
67
wrapScopedCompositionScript,
@@ -886,3 +887,60 @@ window.__timelines['intro'] = tl;
886887
expect(gsapTargets).toEqual([["HELLO"]]);
887888
});
888889
});
890+
891+
/**
892+
* The emitted statement is placed inside a `<script>` element, and `<script>` is a
893+
* RAW TEXT element: HTML serialization does not escape its content and the tokenizer
894+
* closes it at the first `</script`. `JSON.stringify` escapes `"` and `\` but not `/`,
895+
* so an unescaped variable value could close the element and have the remainder parsed
896+
* as markup — turning composition data into executable script.
897+
*/
898+
describe("buildVariablesByCompScript — <script> breakout", () => {
899+
/** Serialize into a document the way the compilers do, then re-parse it. */
900+
function scriptsAfterRoundTrip(body: string): string[] {
901+
const { document } = parseHTML("<!doctype html><html><head></head><body></body></html>");
902+
const el = document.createElement("script");
903+
el.textContent = body;
904+
document.body.appendChild(el);
905+
const { document: reparsed } = parseHTML(document.toString());
906+
return [...reparsed.querySelectorAll("script")].map((s) => s.textContent ?? "");
907+
}
908+
909+
it("does not let a variable VALUE close the script element", () => {
910+
const body = buildVariablesByCompScript({
911+
"comp-a": { greeting: "</script><script>window.__pwned=1//" },
912+
});
913+
expect(body).not.toBeNull();
914+
expect(body).not.toContain("</script");
915+
expect(scriptsAfterRoundTrip(body ?? "")).toHaveLength(1);
916+
});
917+
918+
it("does not let a variable KEY close the script element", () => {
919+
const body = buildVariablesByCompScript({
920+
"comp-a": { "</script><script>window.__pwned=1//": "x" },
921+
});
922+
expect(body).not.toContain("</script");
923+
expect(scriptsAfterRoundTrip(body ?? "")).toHaveLength(1);
924+
});
925+
926+
it("does not let a COMP ID close the script element", () => {
927+
const body = buildVariablesByCompScript({
928+
"</script><script>window.__pwned=1//": { a: "x" },
929+
});
930+
expect(body).not.toContain("</script");
931+
expect(scriptsAfterRoundTrip(body ?? "")).toHaveLength(1);
932+
});
933+
934+
it("keeps the value byte-identical once executed — the escape is transparent", () => {
935+
// Run the statement the way the browser does rather than string-slicing it.
936+
const variables = { "comp-a": { greeting: "a </script> b <em>c</em>" } };
937+
const body = buildVariablesByCompScript(variables) ?? "";
938+
const fakeWindow: Record<string, unknown> = {};
939+
new Function("window", body)(fakeWindow);
940+
expect(fakeWindow.__hfVariablesByComp).toEqual(variables);
941+
});
942+
943+
it("returns null when there are no per-instance values", () => {
944+
expect(buildVariablesByCompScript({})).toBeNull();
945+
});
946+
});

‎packages/core/src/compiler/compositionScoping.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,10 +601,19 @@ export function wrapInlineScriptWithErrorBoundary(source: string, errorLabel: st
601601
* `getVariables()` returned `{}` only during render — parametrized sub-comps
602602
* silently shipped blank/default text in the final MP4 while snapshot QA passed
603603
* (issue #2064). Both callers now share this one builder so they can't drift.
604+
*
605+
* Every `<` is rewritten to its JSON unicode escape because this body is
606+
* emitted into a `<script>` element, and `<script>` is a RAW TEXT element:
607+
* HTML serialization does not escape its content, and the tokenizer ends it at
608+
* the first `</script`. `JSON.stringify` escapes `"` and `\` but NOT `/`, so a
609+
* variable value or key containing `</script>` would otherwise close the
610+
* element early and the remainder would parse as markup. The escape is
611+
* transparent to `JSON.parse`, so the value the runtime reads is unchanged.
604612
*/
605613
export function buildVariablesByCompScript(
606614
variablesByComp: Record<string, Record<string, unknown>>,
607615
): string | null {
608616
if (!variablesByComp || Object.keys(variablesByComp).length === 0) return null;
609-
return `window.__hfVariablesByComp = Object.assign({}, window.__hfVariablesByComp || {}, ${JSON.stringify(variablesByComp)});`;
617+
const json = JSON.stringify(variablesByComp).replace(/</g, "\\u003c");
618+
return `window.__hfVariablesByComp = Object.assign({}, window.__hfVariablesByComp || {}, ${json});`;
610619
}

0 commit comments

Comments
 (0)