From c4d607c7f866c61c1dce18c2edd84e9b4c2c9f34 Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Mon, 16 Nov 2026 22:53:30 +0000 Subject: [PATCH] fix(core): a page marked for another site gives that site only playback (#7980) * fix(core): a page marked for another site gives that site only playback The runtime control bridge trusted window.parent alone. In a document the preview server marked as framed by another site, that parent is the other site, so it could turn on pick mode, flash elements, set runtime data and colour grading, and it received the timeline, diagnostics and picked elements, which describe the composition. In a marked page the bridge now takes only playback controls (play, pause, seek, volume, mute, rate) and posts only the ready, state and stage-size messages. Pages served to their own site are unchanged. * fix(core): keep the bridge's handler lookup in the shape code scanning accepts --- .../src/runtime/bridge.anotherSite.test.ts | 124 ++++++++++++++++++ packages/core/src/runtime/bridge.ts | 28 +++- 2 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 packages/core/src/runtime/bridge.anotherSite.test.ts diff --git a/packages/core/src/runtime/bridge.anotherSite.test.ts b/packages/core/src/runtime/bridge.anotherSite.test.ts new file mode 100644 index 0000000000..3baa2391e7 --- /dev/null +++ b/packages/core/src/runtime/bridge.anotherSite.test.ts @@ -0,0 +1,124 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { PREVIEW_FOR_ANOTHER_SITE_META } from "../studioPreviewMark"; + +const deps = () => ({ + onPlay: vi.fn(), + onPause: vi.fn(), + onStopMedia: vi.fn(), + onSeek: vi.fn(), + onTick: vi.fn(), + onSetMuted: vi.fn(), + onSetVolume: vi.fn(), + onSetMediaOutputMuted: vi.fn(), + onSetNativeMediaSyncDisabled: vi.fn(), + onSetWebAudioMediaDisabled: vi.fn(), + onSetPlaybackRate: vi.fn(), + onSetIdleHeartbeat: vi.fn(), + onSetColorGrading: vi.fn(), + onSetColorGradingCompare: vi.fn(), + onSetRootDuration: vi.fn(), + onSetPlayRange: vi.fn(), + onEnablePickMode: vi.fn(), + onDisablePickMode: vi.fn(), + onSetRuntimeData: vi.fn(), + onClearRuntimeData: vi.fn(), + getCanonicalFps: vi.fn(() => 30), +}); + +const control = (action: string, extra?: Record) => + new MessageEvent("message", { + source: window.parent, + data: { source: "hf-parent", type: "control", action, ...extra }, + }); + +// A fresh bridge module per test, so whether the page is marked is read from this test's document. +async function bridgeIn(marked: boolean) { + vi.resetModules(); + if (marked) { + const meta = document.createElement("meta"); + meta.name = PREVIEW_FOR_ANOTHER_SITE_META; + document.head.prepend(meta); + } + const posted: { type: string }[] = []; + vi.spyOn(window.parent, "postMessage").mockImplementation((message: unknown) => { + posted.push(message as { type: string }); + }); + const bridge = await import("./bridge"); + return { bridge, posted }; +} + +let handler: ((event: MessageEvent) => void) | null = null; + +beforeEach(() => { + document.head.innerHTML = ""; + document.body.innerHTML = '
Unpublished line
'; +}); + +afterEach(() => { + if (handler) window.removeEventListener("message", handler); + handler = null; + vi.restoreAllMocks(); + document.getElementById("__hf-flash-styles")?.remove(); +}); + +describe("runtime bridge in a page marked for another site", () => { + it("takes playback controls but no inspection or document-changing ones", async () => { + const { bridge } = await bridgeIn(true); + const calls = deps(); + handler = bridge.installRuntimeControlBridge(calls); + + for (const action of ["play", "pause", "seek", "set-muted", "set-volume"]) + handler(control(action, { timeSeconds: 1, volume: 0.5 })); + handler(control("enable-pick-mode")); + handler(control("flash-elements", { selectors: ["#secret"], duration: 500 })); + handler(control("set-runtime-data", { channel: "x", payload: 1 })); + handler(control("clear-runtime-data", { channel: "x" })); + handler(control("set-color-grading", { target: null, grading: {} })); + handler(control("set-play-range", { startSeconds: 0, endSeconds: 1 })); + + expect(calls.onPlay).toHaveBeenCalledOnce(); + expect(calls.onPause).toHaveBeenCalledOnce(); + expect(calls.onSeek).toHaveBeenCalledWith(1, "commit"); + expect(calls.onSetVolume).toHaveBeenCalledWith(0.5); + expect(calls.onEnablePickMode).not.toHaveBeenCalled(); + expect(calls.onSetRuntimeData).not.toHaveBeenCalled(); + expect(calls.onClearRuntimeData).not.toHaveBeenCalled(); + expect(calls.onSetColorGrading).not.toHaveBeenCalled(); + expect(calls.onSetPlayRange).not.toHaveBeenCalled(); + expect(document.getElementById("__hf-flash-styles")).toBeNull(); + expect(document.getElementById("secret")?.classList.contains("__hf-flash")).toBe(false); + }); + + it("sends that site only bare playback state", async () => { + const { bridge, posted } = await bridgeIn(true); + handler = bridge.installRuntimeControlBridge(deps()); + for (const type of ["timeline", "diagnostic", "element-hovered", "element-picked", "analytics"]) + bridge.postRuntimeMessage({ source: "hf-preview", type } as never); + bridge.postRuntimeMessage({ + source: "hf-preview", + type: "state", + frame: 0, + currentTime: 0, + ended: false, + isPlaying: false, + buffering: false, + muted: false, + playbackRate: 1, + }); + + expect(posted.map((message) => message.type)).toEqual(["ready", "state"]); + }); + + it("still serves every control and message to a page served to its own site", async () => { + const { bridge, posted } = await bridgeIn(false); + const calls = deps(); + handler = bridge.installRuntimeControlBridge(calls); + handler(control("enable-pick-mode")); + handler(control("set-runtime-data", { channel: "x", payload: 1 })); + bridge.postRuntimeMessage({ source: "hf-preview", type: "timeline" } as never); + + expect(calls.onEnablePickMode).toHaveBeenCalledOnce(); + expect(calls.onSetRuntimeData).toHaveBeenCalledOnce(); + expect(posted.map((message) => message.type)).toEqual(["ready", "timeline"]); + }); +}); diff --git a/packages/core/src/runtime/bridge.ts b/packages/core/src/runtime/bridge.ts index 1c4e0497d5..76332a745a 100644 --- a/packages/core/src/runtime/bridge.ts +++ b/packages/core/src/runtime/bridge.ts @@ -1,6 +1,7 @@ import { wallSetTimeout } from "./wallClock"; import { swallow } from "./diagnostics"; import { isStylableElement } from "./domRealm"; +import { servedToAnotherSite } from "../studioPreviewMark"; import type { HfColorGradingTarget } from "../colorGrading"; import type { RuntimeBridgeControlMessage, RuntimeOutboundMessage } from "./types"; import { @@ -48,7 +49,28 @@ export function setRuntimeProtocolFps(fps: number): void { runtimeProtocolFps = Number.isFinite(fps) && fps > 0 ? fps : 30; } +// Another site framing the page learns only how playback stands: the timeline and the picker describe the composition. +const ANOTHER_SITE_SEES = new Set(["ready", "state", "stage-size"]); +const ANOTHER_SITE_DRIVES = new Set([ + "play", + "pause", + "stop-media", + "seek", + "tick", + "set-muted", + "set-volume", + "set-media-output-muted", + "set-playback-rate", + "set-idle-heartbeat", + "set-display-scale", +]); + +let markedForAnotherSite = false; +const forAnotherSite = (): boolean => + (markedForAnotherSite ||= typeof document !== "undefined" && servedToAnotherSite(document)); + export function postRuntimeMessage(payload: RuntimeOutboundMessage): void { + if (forAnotherSite() && !ANOTHER_SITE_SEES.has(payload.type)) return; try { window.parent.postMessage({ ...payload, ...runtimeProtocolMetadata(runtimeProtocolFps) }, "*"); } catch (err) { @@ -215,6 +237,10 @@ function handleFlashElements(data: BridgeControlData): void { flashElements(selectors, duration); } +function isActionTakenHere(action: unknown): action is string { + return typeof action === "string" && (!forAnotherSite() || ANOTHER_SITE_DRIVES.has(action)); +} + export function installRuntimeControlBridge(deps: BridgeDeps): (event: MessageEvent) => void { const handler = (event: MessageEvent) => { if (event.source !== window.parent && event.source !== window) return; @@ -222,7 +248,7 @@ export function installRuntimeControlBridge(deps: BridgeDeps): (event: MessageEv if (!data || data.source !== "hf-parent" || data.type !== "control") return; if (rejectUnsupportedProtocol(data)) return; const action = data.action; - if (typeof action !== "string") return; + if (!isActionTakenHere(action)) return; const fn = CONTROL_HANDLERS.get(action); if (fn) fn(data, deps); };