diff --git a/packages/core/src/compositionSource.ts b/packages/core/src/compositionSource.ts index 72c6b6b609..d506c51162 100644 --- a/packages/core/src/compositionSource.ts +++ b/packages/core/src/compositionSource.ts @@ -1,3 +1,9 @@ export const COMPOSITION_SRC_ATTR = "data-composition-src"; export const CODE_LOADING_ATTRS: ReadonlySet = new Set([COMPOSITION_SRC_ATTR]); + +export const SOURCE_HOST_ATTRS = ["data-composition-file", COMPOSITION_SRC_ATTR] as const; +export const COMPOSITION_ROOT_ATTRS = [ + "data-composition-id", + "data-hf-original-composition-id", +] as const; diff --git a/packages/core/src/runtime/editingElement.ts b/packages/core/src/runtime/editingElement.ts index 50fb86640e..d12e0404c5 100644 --- a/packages/core/src/runtime/editingElement.ts +++ b/packages/core/src/runtime/editingElement.ts @@ -1,4 +1,5 @@ // How an element is addressed in source: one rule for Studio and the runtime's editing agent. +import { COMPOSITION_ROOT_ATTRS, SOURCE_HOST_ATTRS } from "../compositionSource"; import { isHtmlElement } from "./domRealm"; /** Names an element's source file. The parent resolves it: `file`, else its composition map. */ @@ -193,14 +194,13 @@ function closestWithAny(el: Element, names: readonly string[]): Element | null { } export function readElementSource(el: Element): EditingElementSource { - const host = closestWithAny(el, ["data-composition-file", "data-composition-src"]); - const root = closestWithAny(el, ["data-composition-id"]); + const [fileAttr, srcAttr] = SOURCE_HOST_ATTRS; + const [idAttr, originalIdAttr] = COMPOSITION_ROOT_ATTRS; + const host = closestWithAny(el, SOURCE_HOST_ATTRS); + const root = closestWithAny(el, [idAttr]); return { - file: - host?.getAttribute("data-composition-file") ?? - host?.getAttribute("data-composition-src") ?? - null, - compositionId: root?.getAttribute("data-composition-id") ?? null, - originalCompositionId: root?.getAttribute("data-hf-original-composition-id") ?? null, + file: host?.getAttribute(fileAttr) ?? host?.getAttribute(srcAttr) ?? null, + compositionId: root?.getAttribute(idAttr) ?? null, + originalCompositionId: root?.getAttribute(originalIdAttr) ?? null, }; } diff --git a/packages/core/src/utils/htmlAttrSafety.allowlist.test.ts b/packages/core/src/utils/htmlAttrSafety.allowlist.test.ts index bc091bb234..87f2b6a4bf 100644 --- a/packages/core/src/utils/htmlAttrSafety.allowlist.test.ts +++ b/packages/core/src/utils/htmlAttrSafety.allowlist.test.ts @@ -58,6 +58,19 @@ describe("markupOutsideAllowlist", () => { expect(markupOutsideAllowlist(parse(html, false).documentElement), html).not.toEqual([]); }); + // fails without the fix: a copy kept the marks Studio reads as its file, so later edits went to that other file. + it.each(["data-composition-file", "data-composition-id", "data-hf-original-composition-id"])( + "refuses %s at any depth and inside templates", + (mark) => { + for (const html of [ + `
`, + `

x

`, + `
`, + ]) + expect(markupOutsideAllowlist(parse(html, false).documentElement), html).not.toEqual([]); + }, + ); + it("shows why: a noscript the check reads as inert runs a handler once scripting is on", () => { const html = REFUSED[0]!; expect(runsCode(parse(html, false))).toBe(false); diff --git a/packages/core/src/utils/htmlAttrSafety.ts b/packages/core/src/utils/htmlAttrSafety.ts index 01719b5fde..879a74fc75 100644 --- a/packages/core/src/utils/htmlAttrSafety.ts +++ b/packages/core/src/utils/htmlAttrSafety.ts @@ -6,7 +6,11 @@ */ import { SVG_ATTRIBUTES, SVG_ELEMENTS } from "./svgAllowlist"; -import { CODE_LOADING_ATTRS } from "../compositionSource"; +import { + CODE_LOADING_ATTRS, + COMPOSITION_ROOT_ATTRS, + SOURCE_HOST_ATTRS, +} from "../compositionSource"; export const ALLOWED_HTML_ATTRS = new Set([ "id", @@ -134,8 +138,14 @@ function elementAllowed(element: Element, tag: string): boolean { return ELEMENTS_PARSED_ALIKE_WITH_SCRIPTING_ON.has(tag) || /^hf-[a-z0-9-]+$/.test(tag); } +const MARKS_STUDIO_EDITS_THE_FILE_OF: ReadonlySet = new Set([ + ...SOURCE_HOST_ATTRS, + ...COMPOSITION_ROOT_ATTRS, +]); + function attributeAllowed(element: Element, name: string, value: string): boolean { if (!isSafeAttributeValue(name, value) || name.toLowerCase().startsWith("on")) return false; + if (MARKS_STUDIO_EDITS_THE_FILE_OF.has(name.toLowerCase())) return false; if (isAllowedHtmlAttribute(name)) return true; if (!isSvg(element)) return false; const namespace = FIXED_NAMESPACES.get(name); diff --git a/packages/studio/src/hooks/useClipboard.written.test.ts b/packages/studio/src/hooks/useClipboard.written.test.ts index 27d78b6c57..366155d1af 100644 --- a/packages/studio/src/hooks/useClipboard.written.test.ts +++ b/packages/studio/src/hooks/useClipboard.written.test.ts @@ -253,3 +253,28 @@ it("compares each pasted copy with only the elements that could be it", () => { expect(compared.mock.calls.length).toBeLessThanOrEqual(clips.length * 2); compared.mockRestore(); }); + +// fails without the fix: a copy kept data-composition-file, and Studio then wrote this element's edits to that file. +describe("a copy from another file cannot choose which file its edits go to", () => { + const marked = (mark: string) => + `

x

`; + const MARKS = ["data-composition-file", "data-composition-id", "data-hf-original-composition-id"]; + + it.each(MARKS)("refuses a copy carrying %s, as an element or a clip", (mark) => { + const html = marked(mark); + const readFrom = "compositions/recv.html"; + expect(() => pasteElementHtml(FILE, { html, readFrom }, false, "index.html")).toThrow( + /Couldn't paste/, + ); + const clip = { html, readFrom, start: 0, duration: 1, track: 1 }; + expect(() => pasteTimelineClips(FILE, [clip], 0, [], false, "index.html")).toThrow( + /Couldn't paste/, + ); + }); + + it.each(MARKS)("keeps %s on a copy pasted into the file it was read from", (mark) => { + const html = marked(mark); + const pasted = pasteElementHtml(FILE, { html, readFrom: "index.html" }, true, "index.html"); + expect(pasted).toContain(`${mark}="compositions/b.html"`); + }); +});