mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-25 16:37:32 +00:00
fix(studio): copy and duplicate refuse a file that is not one of the project's compositions (#8490)
* refactor(core): keep the SVG element and attribute allowlist in its own module * test(studio): clipboard writes refuse markup a scripting preview reads differently * fix(studio): paste and duplicate write only allowed markup unless it comes from the same file * test(studio): copy and duplicate use only files Studio knows in the project * fix(studio): copy and duplicate refuse a file that is not one of the project's compositions * test(core): inline SVG with its namespace declarations pastes from another file * fix(core): paste keeps SVG namespace declarations and checks xlink:href as a link * chore(cli): regenerate the media-use SVG sanitizer from its moved allowlist
This commit is contained in:
@@ -102,6 +102,18 @@ export function getCompositionSourceMapRevision(): number {
|
||||
return compositionSourceMapRevision;
|
||||
}
|
||||
|
||||
export function knownCompositionFile(
|
||||
claimed: string | undefined,
|
||||
activeCompositionPath: string | null,
|
||||
): string | null {
|
||||
const file = claimed || activeCompositionPath || "index.html";
|
||||
const known =
|
||||
file === "index.html" ||
|
||||
file === activeCompositionPath ||
|
||||
[...compositionSourceMap.values()].includes(file);
|
||||
return known ? file : null;
|
||||
}
|
||||
|
||||
/** The source file `source` names (see `EditingElementSource`), with this project's composition map. */
|
||||
export function resolveElementSourceFile(
|
||||
source: EditingElementSource,
|
||||
|
||||
@@ -7,6 +7,7 @@ import { useClipboard } from "./useClipboard";
|
||||
import { usePlayerStore, type TimelineElement } from "../player";
|
||||
import type { DomEditSelection } from "../components/editor/domEditing";
|
||||
import { sealedPreviewFrame } from "../player/lib/sealedPreviewFrame.test-helpers";
|
||||
import { setCompositionSourceMap } from "../components/editor/domEditingDom";
|
||||
|
||||
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
|
||||
|
||||
@@ -21,6 +22,7 @@ afterEach(() => {
|
||||
act(() => root?.unmount());
|
||||
root = null;
|
||||
vi.unstubAllGlobals();
|
||||
setCompositionSourceMap(new Map());
|
||||
usePlayerStore.getState().reset();
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
@@ -186,7 +188,8 @@ it("checks a saved element read from another file before pasting it into this on
|
||||
selector: "#go",
|
||||
sourceFile: "compositions/recv.html",
|
||||
} as DomEditSelection;
|
||||
// The frame shows one of the person's own clips under the received element's file and id.
|
||||
// A received sub-composition of this project, and one of the person's own clips shown under its file and id.
|
||||
setCompositionSourceMap(new Map([["recv", "compositions/recv.html"]]));
|
||||
const relabelled =
|
||||
'<div data-composition-file="compositions/recv.html"><button id="go" class="clip">Go</button></div>';
|
||||
const { clipboard, writes, toasts } = sealedClipboard(selection, relabelled, SAVED, {
|
||||
@@ -198,6 +201,31 @@ it("checks a saved element read from another file before pasting it into this on
|
||||
expect(toasts.at(-1)).toMatch(/^Couldn't paste/);
|
||||
});
|
||||
|
||||
// fails without the fix: the file the frame named for a clip or a selection was read and written as given.
|
||||
it("copies and duplicates nothing from a file Studio does not know as one of this project's compositions", async () => {
|
||||
const elsewhere = { ...clipOf("go", "button", 1, 2), sourceFile: "compositions/elsewhere.html" };
|
||||
const shown =
|
||||
'<div data-composition-file="compositions/elsewhere.html"><button id="go" class="clip">Go</button></div>';
|
||||
usePlayerStore.setState({ elements: [elsewhere] });
|
||||
selectClip("go");
|
||||
const duplicated = sealedClipboard(null, shown);
|
||||
expect(await duplicated.clipboard().handleDuplicate()).toBe(false);
|
||||
expect(duplicated.writes).toEqual([]);
|
||||
act(() => root?.unmount());
|
||||
root = null;
|
||||
|
||||
usePlayerStore.setState({ selectedElementId: null, selectedElementIds: new Set() });
|
||||
const selection = {
|
||||
id: "go",
|
||||
selector: "#go",
|
||||
sourceFile: "compositions/elsewhere.html",
|
||||
} as DomEditSelection;
|
||||
const copied = sealedClipboard(selection, shown);
|
||||
copied.clipboard().handleCopy();
|
||||
await copied.clipboard().handlePaste();
|
||||
expect(copied.writes).toEqual([]);
|
||||
});
|
||||
|
||||
it("pastes and duplicates a saved element with its own handler, as saved", async () => {
|
||||
const saved = SAVED.replace(
|
||||
"</div></body>",
|
||||
|
||||
@@ -2,11 +2,12 @@
|
||||
import React, { act } from "react";
|
||||
import { createRoot, type Root } from "react-dom/client";
|
||||
import { ensureHfIds } from "@hyperframes/parsers/hf-ids";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { useClipboard } from "./useClipboard";
|
||||
import { usePlayerStore, type TimelineElement } from "../player";
|
||||
import type { DomEditSelection } from "../components/editor/domEditing";
|
||||
import { serializeStudioFileMutations } from "../utils/studioFileMutationCoordinator";
|
||||
import { setCompositionSourceMap } from "../components/editor/domEditingDom";
|
||||
|
||||
// Saved without hf-ids and with no element id: the preview stamps ids only in memory.
|
||||
const SAVED = `<!doctype html><html><body>
|
||||
@@ -43,9 +44,12 @@ const TITLE: TimelineElement = {
|
||||
};
|
||||
|
||||
let root: Root | null = null;
|
||||
// What Studio reads from the project's index.html before any copy.
|
||||
beforeEach(() => setCompositionSourceMap(new Map([["sub", "compositions/sub.html"]])));
|
||||
afterEach(() => {
|
||||
act(() => root?.unmount());
|
||||
root = null;
|
||||
setCompositionSourceMap(new Map());
|
||||
vi.unstubAllGlobals();
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
|
||||
@@ -29,7 +29,8 @@ import {
|
||||
savedMarkupElseLive,
|
||||
} from "../utils/authoredSource";
|
||||
import { serializeStudioFileMutations } from "../utils/studioFileMutationCoordinator";
|
||||
import { refuseUnlistedMarkup } from "../utils/clipboardMarkup";
|
||||
import { copiedFile, refuseUnlistedMarkup } from "../utils/clipboardMarkup";
|
||||
import { knownCompositionFile } from "../components/editor/domEditingDom";
|
||||
|
||||
type InPlace = { inPlace?: boolean };
|
||||
|
||||
@@ -325,7 +326,7 @@ export function useClipboard({
|
||||
targets: { elements: TimelineElement[]; lives: Element[] },
|
||||
pid: string | null,
|
||||
): Promise<TimelineClipboardClip[]> => {
|
||||
const paths = targets.elements.map((el) => el.sourceFile || activeCompPath || "index.html");
|
||||
const paths = targets.elements.map((el) => copiedFile(el.sourceFile, activeCompPath));
|
||||
const markup = await readSavedMarkup((path) => readSaved(path, pid), paths, targets.lives);
|
||||
return targets.elements.map((element, index) => ({
|
||||
...(markup[index] as MarkedMarkup),
|
||||
@@ -348,13 +349,12 @@ export function useClipboard({
|
||||
findSelectedClips(selected, doc),
|
||||
);
|
||||
const copy = (targets: { elements: TimelineElement[]; lives: Element[] }) => {
|
||||
const sourceFile = targets.elements[0]?.sourceFile || activeCompPath || "index.html";
|
||||
return readClips(targets, pid).then((clips): ClipboardPayload => {
|
||||
showToast(clips.length > 1 ? `Copied ${clips.length} clips` : "Copied clip", "info");
|
||||
return {
|
||||
kind: "timeline-clip",
|
||||
clips,
|
||||
sourceFile,
|
||||
sourceFile: copiedFile(targets.elements[0]?.sourceFile, activeCompPath),
|
||||
projectId: pid ?? undefined,
|
||||
copiedInComposition: activeCompPath || "index.html",
|
||||
};
|
||||
@@ -370,17 +370,17 @@ export function useClipboard({
|
||||
const live = doc && findElementForSelection(doc, domSelection, activeCompPath);
|
||||
return live ?? { refused: "Unable to copy this element." };
|
||||
});
|
||||
const sourceFile = domSelection.sourceFile || activeCompPath || "index.html";
|
||||
const copy = (live: Element) =>
|
||||
readSaved(sourceFile, pid).then((content): ClipboardPayload => {
|
||||
showToast("Copied element", "info");
|
||||
return {
|
||||
kind: "dom-element",
|
||||
...savedMarkupElseLive(parseSavedSource(content), live, sourceFile),
|
||||
sourceFile,
|
||||
projectId: pid ?? undefined,
|
||||
};
|
||||
});
|
||||
const copy = async (live: Element): Promise<ClipboardPayload> => {
|
||||
const sourceFile = copiedFile(domSelection.sourceFile, activeCompPath);
|
||||
const content = await readSaved(sourceFile, pid);
|
||||
showToast("Copied element", "info");
|
||||
return {
|
||||
kind: "dom-element",
|
||||
...savedMarkupElseLive(parseSavedSource(content), live, sourceFile),
|
||||
sourceFile,
|
||||
projectId: pid ?? undefined,
|
||||
};
|
||||
};
|
||||
return copyFound(found, copy, showToast);
|
||||
},
|
||||
[activeCompPath, previewIframeRef, readSaved, showToast],
|
||||
@@ -492,8 +492,7 @@ export function useClipboard({
|
||||
if (!targets || !pid || projectIdRef.current !== pid) return false;
|
||||
|
||||
const { elements } = targets;
|
||||
const pathOf = (el: TimelineElement) => el.sourceFile || activeCompPath || "index.html";
|
||||
const targetPath = pathOf(elements[0]!);
|
||||
const pathOf = (el: TimelineElement) => knownCompositionFile(el.sourceFile, activeCompPath);
|
||||
// The copy lands in targetPath's own clock, so anchor and lane check are local to it.
|
||||
const anchorTime = toAuthoredStart(
|
||||
elements[0]!,
|
||||
@@ -504,6 +503,7 @@ export function useClipboard({
|
||||
|
||||
try {
|
||||
const clips = await readClips(targets, pid);
|
||||
const targetPath = copiedFile(elements[0]!.sourceFile, activeCompPath);
|
||||
let ids: string[] = [];
|
||||
const duplicate = (originalContent: string) => {
|
||||
const liveElements = usePlayerStore
|
||||
|
||||
@@ -1,4 +1,14 @@
|
||||
import { markupOutsideAllowlist } from "@hyperframes/core/html-attr-safety";
|
||||
import { knownCompositionFile } from "../components/editor/domEditingDom";
|
||||
|
||||
export function copiedFile(
|
||||
claimed: string | undefined,
|
||||
activeCompositionPath: string | null,
|
||||
): string {
|
||||
const file = knownCompositionFile(claimed, activeCompositionPath);
|
||||
if (file) return file;
|
||||
throw new Error("Couldn't use this element: its file isn't one of this project's compositions.");
|
||||
}
|
||||
|
||||
/** Every clipboard write: only markup Studio itself read from the file it writes to skips the allowlist. */
|
||||
export function refuseUnlistedMarkup(
|
||||
|
||||
Reference in New Issue
Block a user