diff --git a/packages/core/src/utils/htmlAttrSafety.test.ts b/packages/core/src/utils/htmlAttrSafety.test.ts new file mode 100644 index 0000000000..a665d6cb05 --- /dev/null +++ b/packages/core/src/utils/htmlAttrSafety.test.ts @@ -0,0 +1,53 @@ +// @vitest-environment happy-dom +import { describe, expect, it } from "vitest"; +import { isSafeAttributeValue, unsafeMarkup } from "./htmlAttrSafety"; + +const root = (html: string) => { + const holder = document.createElement("template"); + holder.innerHTML = html; + return holder.content.firstElementChild!; +}; + +describe("unsafeMarkup", () => { + it("names each handler and script link anywhere in the tree", () => { + const tree = root( + `
`, + ); + expect(unsafeMarkup(tree)).toEqual(["div[onclick]", "a[href]", "img[src]", "img[onerror]"]); + }); + + it("names elements that carry code, wherever they sit, a template's content included", () => { + const tree = root( + ``, + ); + expect(unsafeMarkup(tree)).toEqual(["script", "iframe[srcdoc]", "object", "embed", "script"]); + }); + + it("names an SVG animation that sets a script link", () => { + const tree = root( + ``, + ); + expect(unsafeMarkup(tree)).toEqual(["set[to]", "animate[values]"]); + }); + + it("names nothing in ordinary markup", () => { + const tree = root( + ``, + ); + expect(unsafeMarkup(tree)).toEqual([]); + }); +}); + +describe("isSafeAttributeValue", () => { + it("reads a link the way a browser does: no tab, newline or leading control character hides its scheme", () => { + for (const link of [ + "java\tscript:a()", + "\njavascript:a()", + "\u0001 javascript:a()", + "JAVA\r\nSCRIPT:a()", + ]) + expect(isSafeAttributeValue("href", link)).toBe(false); + expect(isSafeAttributeValue("data", "javascript:a()")).toBe(false); + expect(isSafeAttributeValue("href", "https://example.com/java\tscript")).toBe(true); + }); +}); diff --git a/packages/core/src/utils/htmlAttrSafety.ts b/packages/core/src/utils/htmlAttrSafety.ts index 59bc764e7e..9166ede430 100644 --- a/packages/core/src/utils/htmlAttrSafety.ts +++ b/packages/core/src/utils/htmlAttrSafety.ts @@ -66,6 +66,7 @@ export const ALLOWED_HTML_ATTRS = new Set([ export const URI_BEARING_ATTRS = new Set([ "src", + "data", "href", "action", "formaction", @@ -86,11 +87,72 @@ export function isAllowedHtmlAttribute(name: string): boolean { return false; } +// The URL spec's own clean-up before it reads a scheme: no C0 control or space at either end, no tab or newline at all. +function linkAsBrowsersReadIt(value: string): string { + let start = 0; + let end = value.length; + while (start < end && value.charCodeAt(start) <= 0x20) start++; + while (end > start && value.charCodeAt(end - 1) <= 0x20) end--; + return value.slice(start, end).replace(/[\t\n\r]/g, ""); +} + export function isSafeAttributeValue(name: string, value: string): boolean { if (URI_BEARING_ATTRS.has(name.toLowerCase())) { - const trimmed = value.trim(); + const trimmed = linkAsBrowsersReadIt(value); if (DANGEROUS_URI_SCHEMES.test(trimmed)) return false; if (DANGEROUS_DATA_URI.test(trimmed)) return false; } return true; } + +const CODE_ELEMENTS = new Set(["script", "object", "embed"]); +const INLINE_DOCUMENT_FRAMES = new Set(["iframe", "frame"]); +const LINK_ANIMATIONS = new Set(["set", "animate"]); +const ANIMATED_LINKS = new Set(["href", "xlink:href"]); +const ANIMATED_VALUES = ["to", "from", "by", "values"]; + +function kindFindings(element: Element, tag: string): string[] { + if (CODE_ELEMENTS.has(tag)) return [tag]; + return INLINE_DOCUMENT_FRAMES.has(tag) && element.hasAttribute("srcdoc") + ? [`${tag}[srcdoc]`] + : []; +} + +const runsCode = (name: string, value: string) => + name.toLowerCase().startsWith("on") || !isSafeAttributeValue(name, value); + +function attributeFindings(element: Element, tag: string): string[] { + return Array.from(element.attributes) + .filter(({ name, value }) => runsCode(name, value)) + .map(({ name }) => `${tag}[${name}]`); +} + +const animatesLink = (element: Element, tag: string) => + LINK_ANIMATIONS.has(tag) && + ANIMATED_LINKS.has(element.getAttribute("attributeName")?.toLowerCase() ?? ""); + +function animationFindings(element: Element, tag: string): string[] { + if (!animatesLink(element, tag)) return []; + return ANIMATED_VALUES.filter((name) => + (element.getAttribute(name)?.split(";") ?? []).some((v) => !isSafeAttributeValue("href", v)), + ).map((name) => `${tag}[${name}]`); +} + +function childrenOf(element: Element, tag: string): Element[] { + const content = tag === "template" ? (element as HTMLTemplateElement).content : null; + return [...Array.from(element.children), ...Array.from(content?.children ?? [])]; +} + +function unsafeIn(element: Element, found: string[]): void { + const tag = element.localName; + found.push(...kindFindings(element, tag)); + found.push(...attributeFindings(element, tag)); + found.push(...animationFindings(element, tag)); + for (const child of childrenOf(element, tag)) unsafeIn(child, found); +} + +export function unsafeMarkup(root: Element): string[] { + const found: string[] = []; + unsafeIn(root, found); + return found; +} diff --git a/packages/studio/src/hooks/useClipboard.sealed.test.ts b/packages/studio/src/hooks/useClipboard.sealed.test.ts index e732699d6a..3042ffd768 100644 --- a/packages/studio/src/hooks/useClipboard.sealed.test.ts +++ b/packages/studio/src/hooks/useClipboard.sealed.test.ts @@ -26,17 +26,19 @@ afterEach(() => { }); /** A sealed preview of SAVED, with the runtime's hide on the clip, and the saved file served. */ -function sealedClipboard(domSelection: DomEditSelection | null) { - const preview = new DOMParser().parseFromString(ensureHfIds(SAVED), "text/html"); +function sealedClipboard(domSelection: DomEditSelection | null, scriptMade = "", saved = SAVED) { + const preview = new DOMParser().parseFromString(ensureHfIds(saved), "text/html"); document.body.innerHTML = preview.body.innerHTML; + document.querySelector("[data-composition-id]")!.insertAdjacentHTML("beforeend", scriptMade); document.getElementById("title")!.style.cssText = "visibility: hidden; display: none"; vi.stubGlobal( "fetch", - vi.fn(async () => new Response(JSON.stringify({ content: SAVED }))), + vi.fn(async () => new Response(JSON.stringify({ content: saved }))), ); const { iframe } = sealedPreviewFrame(); const writes: string[] = []; const deleted: string[] = []; + const toasts: string[] = []; const view = { projectId: "p" }; let api: ReturnType