mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 16:08:56 +00:00
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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> * refactor(studio-server): keep capture-load comments within the package share Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> * fix(studio-server): leave a video with another host's <source> out of capture CORS A video whose <source> 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 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
3ad7611f9f
commit
80cda85d76
@@ -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"), '<div data-composition-id="main"></div>');
|
||||
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();
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -328,8 +328,13 @@ function makeSnapshotKey(cacheKey: string, sampleIndex: number, side: "from" | "
|
||||
|
||||
function openSnapshotDb(): Promise<IDBDatabase | null> {
|
||||
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)) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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<Parameters<CaptureCdpSession["on"]>[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> = {}): 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<Record<string, string>> };
|
||||
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();
|
||||
});
|
||||
});
|
||||
@@ -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<unknown>;
|
||||
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<void> {
|
||||
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",
|
||||
})),
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { corsProjectVideos } from "./captureVideos.js";
|
||||
|
||||
const doc = (body: string) => `<!DOCTYPE html><html><head></head><body>${body}</body></html>`;
|
||||
|
||||
describe("corsProjectVideos", () => {
|
||||
it("loads the project's own videos with CORS and leaves the rest alone", () => {
|
||||
const html = corsProjectVideos(
|
||||
doc(
|
||||
'<video id="a" src="assets/a.mp4"></video>' +
|
||||
'<video id="b" src="/api/projects/p/preview/b.mp4"></video>' +
|
||||
'<video id="c"><source src="c.mp4"></video>' +
|
||||
'<video id="d" src="https://cdn.example/d.mp4"></video>' +
|
||||
'<video id="e" src="//cdn.example/e.mp4"></video>' +
|
||||
'<video id="f" src="blob:x"></video>' +
|
||||
'<video id="g" crossorigin="use-credentials" src="g.mp4"></video>' +
|
||||
'<video id="h"><source src="h.webm"><source src="https://cdn.example/h.mp4"></video>',
|
||||
),
|
||||
);
|
||||
const marked = [...html.matchAll(/<video crossorigin="anonymous" id="(\w)"/g)].map((m) => m[1]);
|
||||
expect(marked).toEqual(["a", "b", "c"]);
|
||||
expect(html).toContain('<video id="g" crossorigin="use-credentials"');
|
||||
});
|
||||
|
||||
it("returns a document without videos unchanged", () => {
|
||||
const html = doc('<img src="a.png">');
|
||||
expect(corsProjectVideos(html)).toBe(html);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
import { findStartTags } from "@hyperframes/core/compiler/html-document";
|
||||
import { parseHTML } from "linkedom";
|
||||
|
||||
const OTHER_HOST = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i;
|
||||
|
||||
const isOtherHost = (el: Element) => OTHER_HOST.test(el.getAttribute("src")?.trim() ?? "");
|
||||
|
||||
const isProjectVideo = (video: Element) =>
|
||||
!video.hasAttribute("crossorigin") &&
|
||||
!isOtherHost(video) &&
|
||||
![...video.querySelectorAll("source")].some(isOtherHost);
|
||||
|
||||
/** An opaque capture page reads a video's pixels (color grading, shader textures) only from a CORS load. */
|
||||
export function corsProjectVideos(html: string): string {
|
||||
const tags = findStartTags(html, "video");
|
||||
if (tags.length === 0) return html;
|
||||
const videos = [...parseHTML(html).document.querySelectorAll("video")];
|
||||
if (tags.length !== videos.length) return html;
|
||||
const parts: string[] = [];
|
||||
let from = 0;
|
||||
videos.forEach((video, i) => {
|
||||
if (!isProjectVideo(video)) return;
|
||||
const at = (tags[i] ?? 0) + "<video".length;
|
||||
parts.push(html.slice(from, at), ' crossorigin="anonymous"');
|
||||
from = at;
|
||||
});
|
||||
parts.push(html.slice(from));
|
||||
return parts.join("");
|
||||
}
|
||||
@@ -59,6 +59,7 @@ export {
|
||||
type FileWriteReceipt,
|
||||
} from "./helpers/fileVersion.js";
|
||||
export { buildSubCompositionHtml } from "./helpers/subComposition.js";
|
||||
export { captureLoadGrant, grantCaptureLoads } from "./helpers/captureLoads.js";
|
||||
export {
|
||||
clearElementScreenshotIsolation,
|
||||
getElementScreenshotClip,
|
||||
|
||||
@@ -16,6 +16,10 @@ writeFileSync(
|
||||
join(projectDir, "icon.svg"),
|
||||
'<svg xmlns="http://www.w3.org/2000/svg"><script>void 0</script></svg>',
|
||||
);
|
||||
writeFileSync(
|
||||
join(projectDir, "clip.html"),
|
||||
'<video id="local" src="clip.mp4"></video><video id="remote" src="https://cdn.example/a.mp4"></video>',
|
||||
);
|
||||
afterAll(() => rmSync(projectDir, { recursive: true, force: true }));
|
||||
|
||||
const adapter: StudioApiAdapter = {
|
||||
@@ -76,3 +80,48 @@ describe("a revalidated preview", () => {
|
||||
expect(res.headers.get("content-security-policy")).toBe(SANDBOX);
|
||||
});
|
||||
});
|
||||
|
||||
describe("a thumbnail capture document", () => {
|
||||
const captures = [
|
||||
["the bundled preview", "/projects/demo/preview?hf-capture=1"],
|
||||
["a sub-composition preview", "/projects/demo/preview/comp/page.html?hf-capture=1"],
|
||||
] as const;
|
||||
const sandboxing = createStudioApi(adapter, { sandboxThumbnailCaptures: true });
|
||||
const request = (app: typeof api, path: string, headers: Record<string, string> = {}) =>
|
||||
app.request(`http://localhost${path}`, { headers });
|
||||
|
||||
describe.each(captures)("%s", (_name, path) => {
|
||||
it.each([["same-origin"], ["none"], [undefined]])(
|
||||
"is sandboxed when the app opts in, even when Sec-Fetch-Site is %s",
|
||||
async (site) => {
|
||||
const res = await request(sandboxing, path, site ? { "sec-fetch-site": site } : {});
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.headers.get("content-security-policy")).toBe(SANDBOX);
|
||||
},
|
||||
);
|
||||
|
||||
it("keeps its origin when the app has not opted in", async () => {
|
||||
const res = await request(api, path, { "sec-fetch-site": "none" });
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.headers.get("content-security-policy")).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
it("carries the sandbox on a 304", async () => {
|
||||
const path = "/projects/demo/preview?hf-capture=1";
|
||||
const etag = (await request(sandboxing, path)).headers.get("etag");
|
||||
expect(etag).toBeTruthy();
|
||||
const res = await request(sandboxing, path, { "if-none-match": etag! });
|
||||
expect(res.status).toBe(304);
|
||||
expect(res.headers.get("content-security-policy")).toBe(SANDBOX);
|
||||
});
|
||||
|
||||
it("loads the project's videos with CORS only when the app opts in", async () => {
|
||||
const path = "/projects/demo/preview/comp/clip.html?hf-capture=1";
|
||||
const sandboxed = await (await request(sandboxing, path)).text();
|
||||
expect(sandboxed).toMatch(/<video crossorigin="anonymous"[^>]*id="local"/);
|
||||
expect(sandboxed).toMatch(/<video (?![^>]*crossorigin)[^>]*id="remote"/);
|
||||
const plain = await (await request(api, path)).text();
|
||||
expect(plain).not.toContain('crossorigin="anonymous"');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -102,6 +102,7 @@ import {
|
||||
} from "../helpers/mediaProxyPreview.js";
|
||||
import { requestSubPath } from "../helpers/requestSubPath.js";
|
||||
import { lazyPreviewImages } from "../helpers/lazyPreviewImages.js";
|
||||
import { corsProjectVideos } from "../helpers/captureVideos.js";
|
||||
|
||||
const PROJECT_SIGNATURE_META = "hyperframes-project-signature";
|
||||
function escapeGsapUrl(url: string): string {
|
||||
@@ -372,11 +373,11 @@ function injectStudioPreviewAugmentations(
|
||||
signature: string,
|
||||
projectDir: string,
|
||||
activeCompositionPath: string,
|
||||
options: { capture: boolean; gsapBaseUrl: string },
|
||||
options: { capture: boolean; gsapBaseUrl: string; sandboxedCapture: boolean },
|
||||
): string {
|
||||
const marked = options.capture
|
||||
? injectTagsAtHeadStart(
|
||||
html,
|
||||
options.sandboxedCapture ? corsProjectVideos(html) : html,
|
||||
`<meta name="${STUDIO_PREVIEW_MARK_META}" content="${STUDIO_PREVIEW_CAPTURE_MODE}">`,
|
||||
)
|
||||
: injectTagsAtHeadStart(
|
||||
@@ -482,7 +483,11 @@ function resolvePreviewGsapBaseUrl(adapter: PreviewApiAdapter): string {
|
||||
return normalizeGsapLocalRoot(adapter.gsapLocalRoot);
|
||||
}
|
||||
|
||||
export function registerPreviewRoutes(api: Hono, adapter: PreviewApiAdapter): void {
|
||||
export function registerPreviewRoutes(
|
||||
api: Hono,
|
||||
adapter: PreviewApiAdapter,
|
||||
{ sandboxThumbnailCaptures = false }: { sandboxThumbnailCaptures?: boolean } = {},
|
||||
): void {
|
||||
const gsapBaseUrl = resolvePreviewGsapBaseUrl(adapter);
|
||||
const gsapEtagSalt = `:gsap:${createHash("sha1").update(gsapBaseUrl).digest("hex").slice(0, 12)}`;
|
||||
const previewCacheHeaders = (etag: string) => ({
|
||||
@@ -540,7 +545,7 @@ export function registerPreviewRoutes(api: Hono, adapter: PreviewApiAdapter): vo
|
||||
signature,
|
||||
project.dir,
|
||||
mainCompositionPath,
|
||||
{ capture, gsapBaseUrl },
|
||||
{ capture, gsapBaseUrl, sandboxedCapture: sandboxThumbnailCaptures },
|
||||
);
|
||||
if (previewVariables) bundled = injectPreviewVariables(bundled, previewVariables);
|
||||
bundled = await injectMediaCodecMap(
|
||||
@@ -571,7 +576,7 @@ export function registerPreviewRoutes(api: Hono, adapter: PreviewApiAdapter): vo
|
||||
signature,
|
||||
project.dir,
|
||||
fallback.compositionPath,
|
||||
{ capture, gsapBaseUrl },
|
||||
{ capture, gsapBaseUrl, sandboxedCapture: sandboxThumbnailCaptures },
|
||||
);
|
||||
if (previewVariables) {
|
||||
fallbackAugmented = injectPreviewVariables(fallbackAugmented, previewVariables);
|
||||
@@ -695,6 +700,7 @@ export function registerPreviewRoutes(api: Hono, adapter: PreviewApiAdapter): vo
|
||||
html = injectStudioPreviewAugmentations(html, adapter, signature, project.dir, compPath, {
|
||||
capture,
|
||||
gsapBaseUrl,
|
||||
sandboxedCapture: sandboxThumbnailCaptures,
|
||||
});
|
||||
if (previewVariables) html = injectPreviewVariables(html, previewVariables);
|
||||
html = await injectMediaCodecMap(html, adapter, project.dir, compPath, mediaCodecProbeCache);
|
||||
|
||||
@@ -2,7 +2,9 @@ import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi }
|
||||
import { join } from "node:path";
|
||||
|
||||
const browserMocks = vi.hoisted(() => {
|
||||
const cdp = { send: vi.fn(async (_method: string) => undefined), on: vi.fn() };
|
||||
const page = {
|
||||
createCDPSession: vi.fn(async () => cdp),
|
||||
setViewport: vi.fn(async () => {}),
|
||||
goto: vi.fn(async () => {}),
|
||||
evaluate: vi.fn(async () => undefined),
|
||||
@@ -15,7 +17,7 @@ const browserMocks = vi.hoisted(() => {
|
||||
const sharedBrowser = { connected: true as boolean, newPage };
|
||||
const launch = vi.fn(async () => sharedBrowser);
|
||||
const versionProbe = vi.fn(() => "150.0.0.0");
|
||||
return { launch, newPage, page, sharedBrowser, versionProbe };
|
||||
return { cdp, launch, newPage, page, sharedBrowser, versionProbe };
|
||||
});
|
||||
|
||||
vi.mock("puppeteer-core", () => ({ default: { launch: browserMocks.launch } }));
|
||||
@@ -63,6 +65,7 @@ describe("generateThumbnail", () => {
|
||||
browserMocks.newPage.mockClear();
|
||||
browserMocks.versionProbe.mockClear();
|
||||
for (const mock of Object.values(browserMocks.page)) mock.mockClear();
|
||||
browserMocks.cdp.send.mockClear();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -105,6 +108,21 @@ describe("generateThumbnail", () => {
|
||||
warn.mockRestore();
|
||||
});
|
||||
|
||||
it("grants the capture page's loads on its own session before it navigates", async () => {
|
||||
const previewUrl = "http://127.0.0.1:5190/api/projects/demo/preview?hf-capture=1";
|
||||
await expect(generateThumbnail({ ...options(), previewUrl })).resolves.toEqual(
|
||||
Buffer.from("thumbnail"),
|
||||
);
|
||||
const enable = browserMocks.cdp.send.mock.calls.findIndex(
|
||||
([method]) => method === "Fetch.enable",
|
||||
);
|
||||
expect(enable).toBeGreaterThanOrEqual(0);
|
||||
expect(browserMocks.cdp.send.mock.invocationCallOrder[enable]).toBeLessThan(
|
||||
browserMocks.page.goto.mock.invocationCallOrder[0]!,
|
||||
);
|
||||
expect(browserMocks.page.goto).toHaveBeenCalledWith(previewUrl, expect.anything());
|
||||
});
|
||||
|
||||
it("captures at the route-provided physical output size", async () => {
|
||||
await expect(generateThumbnail(options())).resolves.toEqual(Buffer.from("thumbnail"));
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import { homedir } from "node:os";
|
||||
import { join, win32 as pathWin32 } from "node:path";
|
||||
import {
|
||||
getElementScreenshotClip,
|
||||
grantCaptureLoads,
|
||||
thumbnailDeviceScaleFactor,
|
||||
type ScreenshotClip,
|
||||
} from "@hyperframes/studio-server";
|
||||
@@ -227,6 +228,7 @@ async function prepareThumbnailPage(
|
||||
height: opts.height,
|
||||
deviceScaleFactor: thumbnailDeviceScaleFactor(opts),
|
||||
});
|
||||
await grantCaptureLoads(await page.createCDPSession(), opts.previewUrl);
|
||||
await page.goto(opts.previewUrl, { waitUntil: "domcontentloaded", timeout: 10000 });
|
||||
await page.evaluate(() => {
|
||||
document.documentElement.style.background = "#1c2028";
|
||||
|
||||
@@ -209,6 +209,7 @@ function devProjectApi(): Plugin {
|
||||
// writes the API accepts (Codespaces, tunnels).
|
||||
_api = mod.createStudioApi(adapter, {
|
||||
allowedHosts: mod.previewAllowedHostsFromEnv(),
|
||||
sandboxThumbnailCaptures: true,
|
||||
});
|
||||
}
|
||||
return _api;
|
||||
|
||||
Reference in New Issue
Block a user