diff --git a/packages/studio/src/player/lib/bridgePlayback.ts b/packages/studio/src/player/lib/bridgePlayback.ts index d98f2a9c65..0c27f09dbd 100644 --- a/packages/studio/src/player/lib/bridgePlayback.ts +++ b/packages/studio/src/player/lib/bridgePlayback.ts @@ -3,6 +3,7 @@ // because a runtime posts its first `timeline` while it boots. import type { PlaybackAdapter } from "./playbackTypes"; import { inspectStudioRuntimeMessage, postRuntimeControlMessage } from "./runtimeProtocol"; +import { previewFrameOpaque } from "./previewChannel"; interface PlaybackMirror { iframe: HTMLIFrameElement; @@ -56,10 +57,15 @@ export function previewStageSize( return (frame && mirrors.get(frame)?.stageSize) ?? null; } -/** The origins a post from this frame can carry, given its sandbox and what it loads. */ +/** The origins a post from this frame can carry; a page its server sandboxed posts as `null` without the attribute. */ export function frameOrigins(iframe: HTMLIFrameElement): string[] { const sandbox = iframe.getAttribute("sandbox")?.toLowerCase().split(/\s+/); if (sandbox && !sandbox.includes("allow-same-origin")) return ["null"]; + const loaded = loadedOrigins(iframe); + return previewFrameOpaque(iframe) && !loaded.includes("null") ? [...loaded, "null"] : loaded; +} + +function loadedOrigins(iframe: HTMLIFrameElement): string[] { const src = iframe.getAttribute("src"); if (iframe.hasAttribute("srcdoc") || !src) return [window.location.origin]; try { diff --git a/packages/studio/src/player/lib/previewChannel.test.ts b/packages/studio/src/player/lib/previewChannel.test.ts index f93227b2f0..c7ed51c0d2 100644 --- a/packages/studio/src/player/lib/previewChannel.test.ts +++ b/packages/studio/src/player/lib/previewChannel.test.ts @@ -219,6 +219,19 @@ describe("bridge playback for a frame Studio cannot script", () => { expect(own.getTime()).toBe(5); }); + it("accepts a frame its server sandboxed, which posts as null though its frame has no sandbox", () => { + const { iframe, frame } = opaqueFrame(); + iframe.removeAttribute("sandbox"); + iframe.setAttribute("src", "/api/projects/demo/preview"); + watchPreviewFrame(iframe); + const adapter = bridgePlaybackAdapter(iframe)!; + fromFrame(frame, { type: "state", currentTime: 6, isPlaying: false }, "https://example.com"); + fromFrame({}, { type: "state", currentTime: 7, isPlaying: false }, "null"); + expect(adapter.getTime()).toBe(0); + fromFrame(frame, { type: "state", currentTime: 6, isPlaying: false }, "null"); + expect(adapter.getTime()).toBe(6); + }); + it.each([ ["srcdoc", (f: HTMLIFrameElement) => f.setAttribute("srcdoc", "

x

"), location.origin], [