diff --git a/packages/core/src/compiler/inlineSubCompositions.ts b/packages/core/src/compiler/inlineSubCompositions.ts index beeebaf798..33de331537 100644 --- a/packages/core/src/compiler/inlineSubCompositions.ts +++ b/packages/core/src/compiler/inlineSubCompositions.ts @@ -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); diff --git a/packages/core/src/compositionSource.ts b/packages/core/src/compositionSource.ts new file mode 100644 index 0000000000..72c6b6b609 --- /dev/null +++ b/packages/core/src/compositionSource.ts @@ -0,0 +1,3 @@ +export const COMPOSITION_SRC_ATTR = "data-composition-src"; + +export const CODE_LOADING_ATTRS: ReadonlySet = new Set([COMPOSITION_SRC_ATTR]); diff --git a/packages/core/src/runtime/compositionLoader.ts b/packages/core/src/runtime/compositionLoader.ts index 9d8a1da248..c92721e7ca 100644 --- a/packages/core/src/runtime/compositionLoader.ts +++ b/packages/core/src/runtime/compositionLoader.ts @@ -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 => { - 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; diff --git a/packages/core/src/utils/htmlAttrSafety.allowlist.test.ts b/packages/core/src/utils/htmlAttrSafety.allowlist.test.ts index 3a9108d696..bc091bb234 100644 --- a/packages/core/src/utils/htmlAttrSafety.allowlist.test.ts +++ b/packages/core/src/utils/htmlAttrSafety.allowlist.test.ts @@ -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 = [ '
', '

Made

', @@ -36,6 +37,9 @@ const REFUSED = [ '
', '
', '
', + '
', + '
', + '
', ]; describe("markupOutsideAllowlist", () => { diff --git a/packages/core/src/utils/htmlAttrSafety.ts b/packages/core/src/utils/htmlAttrSafety.ts index 04fb423033..01719b5fde 100644 --- a/packages/core/src/utils/htmlAttrSafety.ts +++ b/packages/core/src/utils/htmlAttrSafety.ts @@ -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; diff --git a/packages/studio-server/src/helpers/sourceMutation.test.ts b/packages/studio-server/src/helpers/sourceMutation.test.ts index 7c5eef124f..5d3ac7d212 100644 --- a/packages/studio-server/src/helpers/sourceMutation.test.ts +++ b/packages/studio-server/src/helpers/sourceMutation.test.ts @@ -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, diff --git a/packages/studio-server/src/helpers/sourceMutation.ts b/packages/studio-server/src/helpers/sourceMutation.ts index 4c5321522d..94d05034aa 100644 --- a/packages/studio-server/src/helpers/sourceMutation.ts +++ b/packages/studio-server/src/helpers/sourceMutation.ts @@ -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);