fix(core): pasted or written markup never carries a composition source from elsewhere (#8520)

This commit is contained in:
Miguel Ángel
2026-11-22 17:16:16 +00:00
committed by GitHub
parent 675e3a876e
commit 2425df5f4d
7 changed files with 34 additions and 4 deletions
@@ -56,6 +56,7 @@ import {
} from "./compositionAssembly";
import { SCENE_NO_SWAP_ATTR, SCENE_PART_ATTR } from "../sceneParts";
import { terminalText } from "../terminalText";
import { COMPOSITION_SRC_ATTR } from "../compositionSource";
// Anything a scene script can leave running, pending or registered outside its timeline, or that
// throws when run again: only the timeline is torn down when a scene is swapped, so when unsure, refuse.
@@ -481,7 +482,7 @@ export function inlineSubCompositions(
}));
for (let queueIndex = 0; queueIndex < queue.length; queueIndex += 1) {
const { element: hostEl, ancestry, scene: parentScene } = queue[queueIndex]!;
const src = hostEl.getAttribute("data-composition-src");
const src = hostEl.getAttribute(COMPOSITION_SRC_ATTR);
if (!src) continue;
const compHtml = resolveHtml(src);
@@ -812,7 +813,7 @@ export function inlineSubCompositions(
hostEl.insertAdjacentHTML("beforeend", el.outerHTML);
hostEl.setAttribute("data-composition-file", src);
hostEl.removeAttribute("data-composition-src");
hostEl.removeAttribute(COMPOSITION_SRC_ATTR);
const nestedAncestry = [...ancestry, src];
const nested = enumerateNestedCompositionHosts(hostEl, nestedAncestry);
+3
View File
@@ -0,0 +1,3 @@
export const COMPOSITION_SRC_ATTR = "data-composition-src";
export const CODE_LOADING_ATTRS: ReadonlySet<string> = new Set([COMPOSITION_SRC_ATTR]);
@@ -43,6 +43,7 @@ import {
readRenderOverrides,
} from "./getVariables";
import { isElementNode, isHtmlElement, isLinkElement, isStyleElement } from "./domRealm";
import { COMPOSITION_SRC_ATTR } from "../compositionSource";
type LoadCompositionsParams = {
injectedStyles: HTMLStyleElement[];
@@ -745,11 +746,11 @@ async function mountExternalCompositions(
cleanupDetachedScopedVariables();
if (trackedHosts.length === 0) return [];
const hostIdentityByElement = assignRuntimeCompositionIds(trackedHosts, new Set());
const hosts = trackedHosts.filter((host) => host.hasAttribute("data-composition-src"));
const hosts = trackedHosts.filter((host) => host.hasAttribute(COMPOSITION_SRC_ATTR));
const mounted = await Promise.all(
hosts.map(async (host): Promise<MountedComposition | null> => {
const src = host.getAttribute("data-composition-src");
const src = host.getAttribute(COMPOSITION_SRC_ATTR);
if (!src) return null;
const hostIdentity = hostIdentityByElement.get(host);
const authoredCompositionId = hostIdentity?.authoredCompositionId || null;
@@ -27,6 +27,7 @@ const ALLOWED = [
];
// fails without the fix: each of these passed the old denylist, and the first runs a handler once scripting is on.
// The last three passed the allowlist too: a composition source makes the runtime fetch and run another page.
const REFUSED = [
'<div class="clip"><noscript><p title="</noscript><img src=x onerror=alert(1)>"></p></noscript></div>',
'<div class="clip"><style>p{}</style><p>Made</p></div>',
@@ -36,6 +37,9 @@ const REFUSED = [
'<div class="clip"><link rel="stylesheet" href="https://elsewhere.example/a.css"></div>',
'<div class="clip"><template><img src=x onerror=alert(1)></template></div>',
'<div class="clip"><form action="https://elsewhere.example/"><button>Go</button></form></div>',
'<div class="clip" data-composition-src="https://elsewhere.example/scene.html"></div>',
'<div class="clip"><div data-composition-src="//elsewhere.example/scene.html"></div></div>',
'<div class="clip"><template><div data-composition-src="scenes/b.html"></div></template></div>',
];
describe("markupOutsideAllowlist", () => {
@@ -6,6 +6,7 @@
*/
import { SVG_ATTRIBUTES, SVG_ELEMENTS } from "./svgAllowlist";
import { CODE_LOADING_ATTRS } from "../compositionSource";
export const ALLOWED_HTML_ATTRS = new Set([
"id",
@@ -99,6 +100,7 @@ function linkAsBrowsersReadIt(value: string): string {
}
export function isSafeAttributeValue(name: string, value: string): boolean {
if (CODE_LOADING_ATTRS.has(name.toLowerCase())) return false;
if (URI_BEARING_ATTRS.has(name.toLowerCase())) {
const trimmed = linkAsBrowsersReadIt(value);
if (DANGEROUS_URI_SCHEMES.test(trimmed)) return false;
@@ -347,6 +347,24 @@ describe("patchElementInHtml", () => {
expect(result).not.toContain("font-size");
});
// fails without the fix: either op wrote a composition source, which the runtime fetches and runs.
it("never writes a composition source, by either attribute op", () => {
for (const op of [
{
type: "html-attribute",
property: "data-composition-src",
value: "https://elsewhere.example/a.html",
},
{ type: "attribute", property: "composition-src", value: "https://elsewhere.example/a.html" },
{ type: "attribute", property: "data-composition-src", value: "scenes/b.html" },
] as const) {
const { html: result } = patchElementInHtml(FIXTURE, { id: "hero" }, [op]);
expect(result.match(/data-composition-src/g)?.length ?? 0).toBe(
FIXTURE.match(/data-composition-src/g)?.length ?? 0,
);
}
});
it("removes attribute when value is null", () => {
const { html: result } = patchElementInHtml(
FIXTURE,
@@ -342,6 +342,7 @@ export function patchElementInHtml(
{
const fullAttr = op.property.startsWith("data-") ? op.property : `data-${op.property}`;
if (op.value != null) {
if (!isSafeAttributeValue(fullAttr, op.value)) break;
opTarget.setAttribute(fullAttr, op.value);
} else {
opTarget.removeAttribute(fullAttr);