From 80cda85d761128e121a1cef0f1378ad342472411 Mon Sep 17 00:00:00 2001 From: James Russo Date: Wed, 11 Nov 2026 07:50:59 +0000 Subject: [PATCH] fix(studio-server): run thumbnail captures in an opaque origin (#7528) * fix(studio-server): run thumbnail captures in an opaque origin The thumbnail renderer loads each composition as a top-level page, so its scripts ran on the Studio's origin. The capture document (`?hf-capture`) now carries `Content-Security-Policy: sandbox allow-scripts`, so those scripts get an opaque origin. An opaque page's CORS-mode loads fail without a CORS header. The renderer that opened the page is trusted, so it adds `Access-Control-Allow-Origin: null` itself, over that page's own DevTools session, to the loads a frame needs: - fonts, module scripts and images; - the `.html` scenes, `.json` and `.cube` files the runtime fetches. Only GETs of the project's preview files qualify, matched by file type. Both thumbnail renderers do this: the CLI's and the Vite dev server's. Co-Authored-By: Claude Opus 5.5 * fix(studio-server): let an opaque capture wait for a video's preview copy A video the browser cannot decode is drawn from its `?hf-proxy=` copy. The runtime waits for that copy with a ranged fetch, preflighted when it carries the priority header. In the opaque capture page both failed, so each such thumbnail fell back to a still after about five seconds of retries. The renderer now answers that preflight itself. It also exposes the copy's status headers to the wait, for video files under the preview path only. Co-Authored-By: Claude Opus 5.5 * refactor(studio-server): keep capture-load comments within the package share Co-Authored-By: Claude Opus 5.5 * fix(studio-server): keep video pixels and project fetches in opaque thumbnail captures - Media loads: the renderer now grants them, and a capture document loads the project's own videos with `crossorigin="anonymous"`. Color grading and shader textures can then read their pixels. - Fetches: every GET of the project's preview files is granted, except a dotfile or dot-folder path such as `.env` or `.git/`. This covers the models and fonts that registry blocks fetch at runtime. - Opt-in: the capture sandbox is now a `createStudioApi` option, `sandboxThumbnailCaptures`, off by default. The CLI Studio server and the Vite dev server turn it on, because their thumbnail renderers install the grant. Another app keeps its current capture documents until its renderer does the same. Co-Authored-By: Claude Opus 5.5 * fix(shader-transitions): prepare transitions when IndexedDB is denied An opaque-origin document, such as a sandboxed preview or thumbnail capture, has no IndexedDB, and opening it throws. The snapshot cache now treats that as no cache, so the transitions still prepare and draw. Thumbnail captures also grant a CORS read of a missing preview file, so a fetch() sees its status as a same-origin page would. Co-Authored-By: Claude Opus 5.5 * fix(studio-server): leave a video with another host's out of capture CORS A video whose child points to another host now keeps its plain load in a thumbnail capture. Adding crossorigin to it would make a CDN that sends no CORS headers refuse the video, leaving the clip blank. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- packages/cli/src/server/studioServer.test.ts | 47 +++++ packages/cli/src/server/studioServer.ts | 4 +- .../src/hyper-shader.preview.test.ts | 28 +++ .../shader-transitions/src/hyper-shader.ts | 7 +- packages/studio-server/src/createStudioApi.ts | 27 ++- .../src/helpers/captureLoads.test.ts | 186 ++++++++++++++++++ .../studio-server/src/helpers/captureLoads.ts | 138 +++++++++++++ .../src/helpers/captureVideos.test.ts | 29 +++ .../src/helpers/captureVideos.ts | 29 +++ packages/studio-server/src/index.ts | 1 + .../src/routes/preview.sandbox.test.ts | 49 +++++ packages/studio-server/src/routes/preview.ts | 16 +- packages/studio/vite.browser.test.ts | 20 +- packages/studio/vite.browser.ts | 2 + packages/studio/vite.config.ts | 1 + 15 files changed, 567 insertions(+), 17 deletions(-) create mode 100644 packages/studio-server/src/helpers/captureLoads.test.ts create mode 100644 packages/studio-server/src/helpers/captureLoads.ts create mode 100644 packages/studio-server/src/helpers/captureVideos.test.ts create mode 100644 packages/studio-server/src/helpers/captureVideos.ts diff --git a/packages/cli/src/server/studioServer.test.ts b/packages/cli/src/server/studioServer.test.ts index 6d08dad7a7..6e8046b7cc 100644 --- a/packages/cli/src/server/studioServer.test.ts +++ b/packages/cli/src/server/studioServer.test.ts @@ -761,6 +761,53 @@ describe("Studio thumbnail capture", () => { expect(clears[0]).toBeGreaterThan(screenshot.mock.invocationCallOrder[0]!); }); + it("serves thumbnail capture documents in an opaque origin", async () => { + const dir = tmpProject(); + writeFileSync(join(dir, "index.html"), '
'); + server = createStudioServer({ projectDir: dir, projectName: "demo" }); + const capture = await server.app.request( + "http://localhost/api/projects/demo/preview?hf-capture=1", + ); + expect(capture.headers.get("content-security-policy")).toBe("sandbox allow-scripts"); + const preview = await server.app.request("http://localhost/api/projects/demo/preview"); + expect(preview.headers.get("content-security-policy")).toBeNull(); + }); + + it("grants the capture page's loads on its own session before it navigates", async () => { + const calls: string[] = []; + const session = { + send: vi.fn(async (method: string) => void calls.push(method)), + on: vi.fn(), + }; + const page = new Proxy( + { + createCDPSession: async () => session, + goto: async () => void calls.push("goto"), + screenshot: async () => Buffer.from("jpeg"), + }, + { + get: (target, key) => + key === "then" + ? undefined + : key in target + ? target[key as keyof typeof target] + : async () => {}, + }, + ); + engineState.acquireBrowser = async () => ({ + browser: { connected: true, newPage: async () => page, on: () => {} }, + release: async () => {}, + }); + const dir = tmpProject(); + server = createStudioServer({ projectDir: dir }); + const previewUrl = "http://127.0.0.1:3002/api/projects/demo/preview?hf-capture=1"; + await expect( + server.adapter.generateThumbnail?.({ ...opts(dir), previewUrl }), + ).resolves.toBeInstanceOf(Buffer); + expect(calls.slice(0, 2)).toEqual(["Fetch.enable", "goto"]); + expect(session.on).toHaveBeenCalledWith("Fetch.requestPaused", expect.any(Function)); + }); + it("reuses the cached project signature instead of walking the project per thumbnail", async () => { fakePageBrowser(); const dir = tmpProject(); diff --git a/packages/cli/src/server/studioServer.ts b/packages/cli/src/server/studioServer.ts index de9ce8eae3..77b8c8082d 100644 --- a/packages/cli/src/server/studioServer.ts +++ b/packages/cli/src/server/studioServer.ts @@ -64,6 +64,7 @@ import { PREVIEW_BUNDLE_OPTIONS, createPreviewDocumentStore, thumbnailDeviceScaleFactor, + grantCaptureLoads, type ResolvedProject, type RenderJobState, type BackgroundRemovalRender, @@ -805,6 +806,7 @@ export function createStudioServer(options: StudioServerOptions): StudioServer { opts.seekTime, async (page) => { await page.setViewport(viewport); + await grantCaptureLoads(await page.createCDPSession(), opts.previewUrl); await page.goto(opts.previewUrl, { waitUntil: "domcontentloaded", timeout: 10000 }); await assertWebGpuAdapterAvailable( page, @@ -1119,7 +1121,7 @@ export function createStudioServer(options: StudioServerOptions): StudioServer { // Mount the shared studio API at /api. // Use fetch() forwarding (not .route()) so the sub-app sees paths without // the /api prefix — the shared module's path extraction uses c.req.path. - const api = createStudioApi(adapter, { allowedHosts }); + const api = createStudioApi(adapter, { allowedHosts, sandboxThumbnailCaptures: true }); app.all("/api/*", async (c) => { const url = new URL(c.req.url); url.pathname = url.pathname.slice(4); // Strip "/api" prefix diff --git a/packages/shader-transitions/src/hyper-shader.preview.test.ts b/packages/shader-transitions/src/hyper-shader.preview.test.ts index b5c354312b..903b9f6be0 100644 --- a/packages/shader-transitions/src/hyper-shader.preview.test.ts +++ b/packages/shader-transitions/src/hyper-shader.preview.test.ts @@ -354,3 +354,31 @@ describe("a runtime seek while the prewarm runs", () => { expect(tl.totalTime()).toBe(1); }); }); + +describe("a document without IndexedDB access", () => { + it("prepares its transitions without the snapshot cache", async () => { + stubGsap(); + stubWebGl(); + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + const denied = new DOMException( + "access to the Indexed Database API is denied", + "SecurityError", + ); + const open = vi.fn(() => { + throw denied; + }); + vi.stubGlobal("indexedDB", { open }); + // The cache's connection is module state, so a fresh module opens it under this stub. + vi.resetModules(); + const { init: initFresh } = await import("./hyper-shader.js"); + mountScenes(["s1", "s2"]); + initFresh({ + bgColor: "#000", + scenes: ["s1", "s2"], + transitions: [{ time: 2, duration: 0.8, shader: "domain-warp" }], + }); + await prewarmDone(); + expect(open).toHaveBeenCalled(); + expect(warn.mock.calls.flat()).not.toContain(denied); + }); +}); diff --git a/packages/shader-transitions/src/hyper-shader.ts b/packages/shader-transitions/src/hyper-shader.ts index f696533965..d1f41a205e 100644 --- a/packages/shader-transitions/src/hyper-shader.ts +++ b/packages/shader-transitions/src/hyper-shader.ts @@ -328,8 +328,13 @@ function makeSnapshotKey(cacheKey: string, sampleIndex: number, side: "from" | " function openSnapshotDb(): Promise { if (typeof indexedDB === "undefined") return Promise.resolve(null); + let request: IDBOpenDBRequest; + try { + request = indexedDB.open(SNAPSHOT_CACHE_DB, SNAPSHOT_CACHE_VERSION); + } catch { + return Promise.resolve(null); + } return new Promise((resolve) => { - const request = indexedDB.open(SNAPSHOT_CACHE_DB, SNAPSHOT_CACHE_VERSION); request.onupgradeneeded = () => { const db = request.result; if (!db.objectStoreNames.contains(SNAPSHOT_CACHE_STORE)) { diff --git a/packages/studio-server/src/createStudioApi.ts b/packages/studio-server/src/createStudioApi.ts index b8ae0b71ac..05b8a7520d 100644 --- a/packages/studio-server/src/createStudioApi.ts +++ b/packages/studio-server/src/createStudioApi.ts @@ -2,7 +2,7 @@ import { Hono, type Context, type Next } from "hono"; import type { StudioApiAdapter } from "./types.js"; import { registerProjectRoutes } from "./routes/projects.js"; import { registerFileRoutes } from "./routes/files.js"; -import { registerPreviewRoutes } from "./routes/preview.js"; +import { PREVIEW_CAPTURE_PARAM, registerPreviewRoutes } from "./routes/preview.js"; import { registerLintRoutes } from "./routes/lint.js"; import { registerRenderRoutes } from "./routes/render.js"; import { registerImageThumbnailRoutes } from "./routes/imageThumbnail.js"; @@ -25,6 +25,7 @@ import { sameOriginPost, } from "./helpers/requestIdentity.js"; import { previewErrorPage } from "./helpers/previewErrorPage.js"; +import type { CaptureSandboxOption } from "./helpers/captureLoads.js"; async function answerRefusedFrameWithPage(c: Context, next: Next) { await next(); @@ -43,11 +44,15 @@ const fromAnotherSite = (c: Context) => { return site === "cross-site" || site === "same-site"; }; -async function sandboxPreviewOpenedFromAnotherSite(c: Context, next: Next) { - await next(); - c.header("Vary", "Sec-Fetch-Site", { append: true }); - if (fromAnotherSite(c)) c.header("Content-Security-Policy", "sandbox allow-scripts"); -} +const isThumbnailCapture = (c: Context) => c.req.query(PREVIEW_CAPTURE_PARAM) !== undefined; + +const sandboxPreviewForAnotherSiteOrCapture = + (sandboxCaptures: boolean) => async (c: Context, next: Next) => { + await next(); + c.header("Vary", "Sec-Fetch-Site", { append: true }); + if (fromAnotherSite(c) || (sandboxCaptures && isThumbnailCapture(c))) + c.header("Content-Security-Policy", "sandbox allow-scripts"); + }; async function refuseThumbnailForAnotherSite(c: Context, next: Next) { if (fromAnotherSite(c)) @@ -71,13 +76,17 @@ function urlScheme(url: string): string | undefined { } } -export interface StudioApiOptions { +export interface StudioApiOptions extends CaptureSandboxOption { allowedHosts?: readonly string[]; } export function createStudioApi(adapter: StudioApiAdapter, options: StudioApiOptions = {}): Hono { const api = new Hono(); - api.use("/projects/:id/preview/*", sandboxPreviewOpenedFromAnotherSite); + const sandboxThumbnailCaptures = options.sandboxThumbnailCaptures ?? false; + api.use( + "/projects/:id/preview/*", + sandboxPreviewForAnotherSiteOrCapture(sandboxThumbnailCaptures), + ); api.use("/projects/:id/thumbnail/*", refuseThumbnailForAnotherSite); // This API is unauthenticated and simple cross-origin POSTs need no // preflight, so the guard rejects writes the shared predicate refuses. @@ -151,7 +160,7 @@ export function createStudioApi(adapter: StudioApiAdapter, options: StudioApiOpt registerProjectRoutes(api, adapter); registerFileRoutes(api, adapter); - registerPreviewRoutes(api, adapter); + registerPreviewRoutes(api, adapter, { sandboxThumbnailCaptures }); registerLintRoutes(api, adapter); registerRenderRoutes(api, adapter); registerThumbnailRoutes(api, adapter); diff --git a/packages/studio-server/src/helpers/captureLoads.test.ts b/packages/studio-server/src/helpers/captureLoads.test.ts new file mode 100644 index 0000000000..2a35c4c1f2 --- /dev/null +++ b/packages/studio-server/src/helpers/captureLoads.test.ts @@ -0,0 +1,186 @@ +import { describe, expect, it, vi } from "vitest"; +import { captureLoadGrant, grantCaptureLoads, type CaptureCdpSession } from "./captureLoads"; + +const ORIGIN = "http://127.0.0.1:3002"; +const PREVIEW = `${ORIGIN}/api/projects/demo/preview`; +const CAPTURE = `${PREVIEW}/comp/compositions/intro.html?hf-capture=1`; + +describe("captureLoadGrant", () => { + it.each([ + ["Font", `${PREVIEW}/assets/brand.woff2`], + ["Script", `${PREVIEW}/assets/scene.mjs`], + ["Stylesheet", `${PREVIEW}/style.css`], + ["Image", `${PREVIEW}/assets/logo.png`], + ["Media", `${PREVIEW}/assets/clip.mp4`], + ["Media", `${PREVIEW}/assets/clip.mov?hf-proxy=h264`], + ["XHR", `${PREVIEW}/compositions/scene.html`], + ["Fetch", `${PREVIEW}/models/device.glb`], + ["XHR", `${PREVIEW}/assets/fonts/Geist-Bold.ttf`], + ["XHR", `${PREVIEW}/notes.txt`], + ["XHR", `${PREVIEW}/assets/clip.mov?hf-proxy=h264&hf-proxy-box=1448x724`], + ])("lets the frame read a %s load of %s", (resourceType, url) => { + expect(captureLoadGrant({ url, method: "GET", resourceType }, CAPTURE)).toBe("read"); + }); + + it("answers the preflight of the runtime's wait for a video's preview copy", () => { + const url = `${PREVIEW}/assets/clip.mov?hf-proxy=h264&hf-proxy-box=1448x724`; + expect(captureLoadGrant({ url, method: "OPTIONS", resourceType: "XHR" }, CAPTURE)).toBe( + "preflight", + ); + }); + + it.each([ + ["XHR", `${PREVIEW}/.env`, "GET"], + ["XHR", `${PREVIEW}/.git/config`, "GET"], + ["XHR", `${PREVIEW}/assets/.npmrc`, "GET"], + ["XHR", `${PREVIEW}/%2Eenv`, "GET"], + ["XHR", `${PREVIEW}/%E0%A4%A.html`, "GET"], + ["Script", `${PREVIEW}/.env`, "GET"], + ["Image", `${PREVIEW}/.git/logo.png`, "GET"], + ["Document", `${PREVIEW}/compositions/scene.html`, "GET"], + ["XHR", `${ORIGIN}/api/projects/demo/files/index.html`, "GET"], + ["XHR", `${ORIGIN}/api/projects/other/preview/index.html`, "GET"], + ["XHR", `http://127.0.0.1:9999/api/projects/demo/preview/index.html`, "GET"], + ["XHR", `${PREVIEW}/compositions/scene.html`, "PUT"], + ["XHR", `${PREVIEW}/compositions/scene.html`, "POST"], + ["XHR", `${PREVIEW}/compositions/scene.html`, "OPTIONS"], + ["XHR", `${PREVIEW}/.env?hf-proxy=h264`, "OPTIONS"], + ["XHR", `${PREVIEW}/notes.txt?hf-proxy=h264`, "OPTIONS"], + ["XHR", `${PREVIEW}/assets/clip.mov?hf-proxy=h264`, "PUT"], + ])("refuses a %s load of %s (%s)", (resourceType, url, method) => { + expect(captureLoadGrant({ url, method, resourceType }, CAPTURE)).toBeNull(); + }); + + it("grants nothing for a URL that is not a preview document", () => { + const load = { url: `${PREVIEW}/assets/brand.woff2`, method: "GET", resourceType: "Font" }; + expect(captureLoadGrant(load, `${ORIGIN}/api/projects/demo/files/index.html`)).toBeNull(); + expect(captureLoadGrant(load, "not a url")).toBeNull(); + }); +}); + +type Paused = Parameters[1]>[0]; + +function fakeSession() { + let paused: ((event: Paused) => void) | undefined; + const send = vi.fn(async (_method: string, _params?: object) => undefined); + const session: CaptureCdpSession = { + send, + on: (_event, handler) => { + paused = handler; + }, + }; + return { session, send, pause: (event: Paused) => paused?.(event) }; +} + +const response = (url: string, resourceType: string, extra: Partial = {}): Paused => ({ + requestId: "r1", + request: { url, method: "GET" }, + resourceType, + responseStatusCode: 200, + responseHeaders: [{ name: "Content-Type", value: "font/woff2" }], + ...extra, +}); + +describe("grantCaptureLoads", () => { + it("intercepts responses under the project's preview path only", async () => { + const { session, send } = fakeSession(); + await grantCaptureLoads(session, CAPTURE); + const enable = send.mock.calls.find(([method]) => method === "Fetch.enable"); + expect(enable).toBeDefined(); + const { patterns } = enable![1] as { patterns: Array> }; + expect(patterns.map((pattern) => pattern.resourceType)).toEqual( + expect.arrayContaining(["Font", "Image", "Media", "XHR"]), + ); + for (const pattern of patterns) { + expect(pattern.urlPattern).toBe(`${PREVIEW}/*`); + expect(pattern.requestStage).toBe("Response"); + } + }); + + it("adds the CORS header to a granted load and keeps its own headers", async () => { + const { session, send, pause } = fakeSession(); + await grantCaptureLoads(session, CAPTURE); + pause(response(`${PREVIEW}/assets/brand.woff2`, "Font")); + expect(send).toHaveBeenLastCalledWith("Fetch.continueResponse", { + requestId: "r1", + responseCode: 200, + responseHeaders: [ + { name: "Content-Type", value: "font/woff2" }, + { name: "Access-Control-Allow-Origin", value: "null" }, + { name: "X-Content-Type-Options", value: "nosniff" }, + expect.objectContaining({ name: "Access-Control-Expose-Headers" }), + ], + }); + }); + + it("lets the frame read a missing file's status, as a same-origin page would", async () => { + const { session, send, pause } = fakeSession(); + await grantCaptureLoads(session, CAPTURE); + pause(response(`${PREVIEW}/assets/fonts/Missing.ttf`, "XHR", { responseStatusCode: 404 })); + expect(send).toHaveBeenLastCalledWith( + "Fetch.continueResponse", + expect.objectContaining({ responseCode: 404 }), + ); + }); + + it("exposes the preview copy's progress headers to the runtime's wait", async () => { + const { session, send, pause } = fakeSession(); + await grantCaptureLoads(session, CAPTURE); + pause( + response(`${PREVIEW}/assets/clip.mov?hf-proxy=h264`, "XHR", { + responseStatusCode: 202, + responseHeaders: [{ name: "Retry-After", value: "2" }], + }), + ); + expect(send).toHaveBeenLastCalledWith("Fetch.continueResponse", { + requestId: "r1", + responseCode: 202, + responseHeaders: expect.arrayContaining([ + { name: "Access-Control-Allow-Origin", value: "null" }, + { name: "Access-Control-Expose-Headers", value: expect.stringContaining("Retry-After") }, + ]), + }); + }); + + it("answers the wait's preflight itself, whatever the server said", async () => { + const { session, send, pause } = fakeSession(); + await grantCaptureLoads(session, CAPTURE); + pause({ + ...response(`${PREVIEW}/assets/clip.mov?hf-proxy=h264`, "XHR", { responseStatusCode: 404 }), + request: { url: `${PREVIEW}/assets/clip.mov?hf-proxy=h264`, method: "OPTIONS" }, + }); + expect(send).toHaveBeenLastCalledWith("Fetch.fulfillRequest", { + requestId: "r1", + responseCode: 204, + responseHeaders: expect.arrayContaining([ + { name: "Access-Control-Allow-Origin", value: "null" }, + { name: "Access-Control-Allow-Methods", value: "GET" }, + ]), + }); + }); + + it.each([ + ["a file fetch() may not read", response(`${PREVIEW}/.env`, "XHR")], + [ + "a request paused before its response", + response(`${PREVIEW}/assets/brand.woff2`, "Font", { responseStatusCode: undefined }), + ], + [ + "a load that already answers CORS", + response(`${PREVIEW}/assets/brand.woff2`, "Font", { + responseHeaders: [{ name: "access-control-allow-origin", value: "*" }], + }), + ], + ])("passes %s through unchanged", async (_name, event) => { + const { session, send, pause } = fakeSession(); + await grantCaptureLoads(session, CAPTURE); + pause(event); + expect(send).toHaveBeenLastCalledWith("Fetch.continueRequest", { requestId: "r1" }); + }); + + it("does nothing for a URL that is not a preview document", async () => { + const { session, send } = fakeSession(); + await grantCaptureLoads(session, `${ORIGIN}/api/projects/demo/files/index.html`); + expect(send).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/studio-server/src/helpers/captureLoads.ts b/packages/studio-server/src/helpers/captureLoads.ts new file mode 100644 index 0000000000..3e40e908df --- /dev/null +++ b/packages/studio-server/src/helpers/captureLoads.ts @@ -0,0 +1,138 @@ +import { PREVIEW_PROXY_PRIORITY_HEADER, PREVIEW_PROXY_PROGRESS_HEADER } from "@hyperframes/core"; + +/** + * A sandboxed capture document's CORS-mode loads fail, so the trusted renderer adds CORS headers for + * its own page: every GET of the project's preview files except a dotfile or dot-folder (`.env`, + * `.git/`), and the preflight of the runtime's wait for a video's `?hf-proxy=` copy. + */ +// Chrome reports a page's `fetch()`, and its preflight, as `XHR`. +const GRANTED_TYPES = new Set(["Font", "Script", "Stylesheet", "Image", "Media", "Fetch", "XHR"]); + +const PROXY_PARAM = "hf-proxy"; +const VIDEO_FILE = /\.(mp4|webm|mov|mkv|avi|m4v|mxf|mts|m2ts)$/i; + +function capturePreviewPrefix(captureUrl: string): string | null { + let url: URL; + try { + url = new URL(captureUrl); + } catch { + return null; + } + const path = /^\/api\/projects\/[^/]+\/preview(?=\/|$)/.exec(url.pathname)?.[0]; + return path ? `${url.origin}${path}/` : null; +} + +export interface CaptureSandboxOption { + /** + * Serve thumbnail capture documents (`?hf-capture`) in an opaque origin. Default off. Turn it on + * only with a thumbnail renderer that calls `grantCaptureLoads` on each capture page before loading + * it; without that grant its captures lose fonts, module scripts, nested scenes and video pixels. + */ + sandboxThumbnailCaptures?: boolean; +} + +export interface CaptureLoad { + url: string; + method: string; + resourceType: string; +} + +const decodedSegment = (segment: string) => { + try { + return decodeURIComponent(segment); + } catch { + return "."; + } +}; + +const isDotPath = (url: URL, prefix: string) => + url.pathname + .slice(new URL(prefix).pathname.length) + .split("/") + .some((segment) => decodedSegment(segment).startsWith(".")); + +export function captureLoadGrant( + load: CaptureLoad, + captureUrl: string, +): "read" | "preflight" | null { + const prefix = capturePreviewPrefix(captureUrl); + if (!prefix || !load.url.startsWith(prefix)) return null; + const url = new URL(load.url); + if (isDotPath(url, prefix) || !GRANTED_TYPES.has(load.resourceType)) return null; + const proxyWait = + (load.resourceType === "XHR" || load.resourceType === "Fetch") && + url.searchParams.has(PROXY_PARAM) && + VIDEO_FILE.test(url.pathname); + if (load.method === "OPTIONS") return proxyWait ? "preflight" : null; + return load.method === "GET" ? "read" : null; +} + +type Header = { name: string; value: string }; + +export interface CaptureCdpSession { + send(method: string, params?: object): Promise; + on( + event: "Fetch.requestPaused", + handler: (event: { + requestId: string; + request: { url: string; method: string }; + resourceType: string; + responseStatusCode?: number; + responseHeaders?: Header[]; + }) => void, + ): unknown; +} + +const OPAQUE_CALLER: Header[] = [ + { name: "Access-Control-Allow-Origin", value: "null" }, + { name: "X-Content-Type-Options", value: "nosniff" }, + { + name: "Access-Control-Expose-Headers", + value: `Retry-After, ${PREVIEW_PROXY_PROGRESS_HEADER}, Content-Range`, + }, +]; +const PREFLIGHT_ANSWER: Header[] = [ + { name: "Access-Control-Allow-Origin", value: "null" }, + { name: "Access-Control-Allow-Methods", value: "GET" }, + { name: "Access-Control-Allow-Headers", value: `Range, ${PREVIEW_PROXY_PRIORITY_HEADER}` }, +]; + +/** Call on the capture page's own session before it navigates; keep the session for the page's life. */ +export async function grantCaptureLoads(cdp: CaptureCdpSession, captureUrl: string): Promise { + const prefix = capturePreviewPrefix(captureUrl); + if (!prefix) return; + cdp.on("Fetch.requestPaused", (event) => { + const { requestId } = event; + const headers = event.responseHeaders ?? []; + const status = event.responseStatusCode; + const grant = captureLoadGrant( + { url: event.request.url, method: event.request.method, resourceType: event.resourceType }, + captureUrl, + ); + const answered = + status !== undefined && + !headers.some((header) => header.name.toLowerCase() === "access-control-allow-origin"); + const sent = + grant === "preflight" + ? cdp.send("Fetch.fulfillRequest", { + requestId, + responseCode: 204, + responseHeaders: PREFLIGHT_ANSWER, + }) + : grant && answered + ? cdp.send("Fetch.continueResponse", { + requestId, + responseCode: status, + responseHeaders: [...headers, ...OPAQUE_CALLER], + }) + : cdp.send("Fetch.continueRequest", { requestId }); + void sent.catch(() => undefined); + }); + await cdp.send("Fetch.enable", { + patterns: [...GRANTED_TYPES].map((resourceType) => ({ + urlPattern: `${prefix}*`, + resourceType, + requestStage: "Response", + })), + }); +} diff --git a/packages/studio-server/src/helpers/captureVideos.test.ts b/packages/studio-server/src/helpers/captureVideos.test.ts new file mode 100644 index 0000000000..d0cb2786fa --- /dev/null +++ b/packages/studio-server/src/helpers/captureVideos.test.ts @@ -0,0 +1,29 @@ +import { describe, expect, it } from "vitest"; +import { corsProjectVideos } from "./captureVideos.js"; + +const doc = (body: string) => `${body}`; + +describe("corsProjectVideos", () => { + it("loads the project's own videos with CORS and leaves the rest alone", () => { + const html = corsProjectVideos( + doc( + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '', + ), + ); + const marked = [...html.matchAll(/