fix(studio): keep a host's sandbox-origin seal on the preview frame (#7886)

This commit is contained in:
Michael Wang
2026-11-16 01:01:13 +00:00
committed by GitHub
parent 082d63683f
commit b650d0d198
2 changed files with 12 additions and 2 deletions
@@ -18,12 +18,14 @@ Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
let root: Root | null = null;
let lifecycleLog: string[] = [];
const playersMade: HTMLElement[] = [];
class TestHyperframesPlayer extends HTMLElement {
readonly iframeElement = document.createElement("iframe");
constructor() {
super();
playersMade.push(this);
this.attachShadow({ mode: "open" });
const addIframeListener = this.iframeElement.addEventListener.bind(this.iframeElement);
@@ -243,6 +245,14 @@ describe("a host's sandbox origin", () => {
expect(player.getAttribute("src")).toBe("/api/projects/demo/preview");
});
it("keeps the seal a host puts on the player from its ref", async () => {
const seal = (iframe: HTMLIFrameElement | null) =>
iframe && playersMade.at(-1)?.setAttribute("sandbox-origin", "opaque");
const { player } = await mountPlayer({ ref: seal });
await settle();
expect(player.getAttribute("sandbox-origin")).toBe("opaque");
});
it("leaves a same-origin preview on its plain path without asking for a token", async () => {
const fetch = vi.spyOn(globalThis, "fetch");
const { player } = await mountPlayer();
@@ -341,7 +341,8 @@ export const Player = forwardRef<HTMLIFrameElement, PlayerProps>(
player.addEventListener("painted", handlePainted);
player.addEventListener("error", handleError);
// Bridge the inner iframe to the forwarded ref for useTimelinePlayer.
// Sealed before the ref (useTimelinePlayer's bridge) sees the frame, never unsealed: a host may seal it too.
if (opaque) player.toggleAttribute("sandbox-origin", true);
if (typeof ref === "function") {
ref(iframe);
} else if (ref) {
@@ -352,7 +353,6 @@ export const Player = forwardRef<HTMLIFrameElement, PlayerProps>(
player.setAttribute("shader-loading", "player");
player.setAttribute("low-power-idle", "");
player.toggleAttribute("no-load-timeout", noLoadTimeout === true);
player.toggleAttribute("sandbox-origin", opaque);
player.setAttribute("width", String(portrait ? 1080 : 1920));
player.setAttribute("height", String(portrait ? 1920 : 1080));
player.style.width = "100%";