mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 16:08:56 +00:00
fix(studio-server): a sealed preview's wait for a video's proxy copy passes its preflight (#7856)
This commit is contained in:
@@ -1224,6 +1224,25 @@ jobs:
|
||||
STUDIO_PROJECT_DIR="$PWD/packages/studio/data/projects/preview-video-regression" \
|
||||
OUT_DIR=/tmp/preview-video-regression \
|
||||
node packages/studio/tests/e2e/preview-video-regression.mjs
|
||||
- name: Verify a sealed preview's proxy wait gets past its preflight
|
||||
if: success() || failure()
|
||||
run: |
|
||||
set -euo pipefail
|
||||
project=packages/studio/data/projects/sealed-proxy
|
||||
mkdir -p "$project"
|
||||
cp packages/cli/src/templates/blank/index.html "$project/index.html"
|
||||
ffmpeg -hide_banner -loglevel error -y -f lavfi -i testsrc=size=320x180:rate=30 -t 1 \
|
||||
-c:v libx264 -pix_fmt yuv420p "$project/clip.mov"
|
||||
VITE_STUDIO_OPAQUE_PREVIEW_ENABLED=1 NODE_ENV=production \
|
||||
bun run --cwd packages/studio dev -- --port 5319 --strictPort &
|
||||
SERVER_PID=$!
|
||||
trap 'kill "$SERVER_PID" 2>/dev/null || true' EXIT
|
||||
for i in $(seq 1 60); do
|
||||
if curl -sf "http://localhost:5319/" >/dev/null 2>&1; then break; fi
|
||||
sleep 1
|
||||
done
|
||||
STUDIO_URL="http://localhost:5319/#project/sealed-proxy" \
|
||||
node packages/studio/tests/e2e/sealed-preview-proxy.mjs
|
||||
- name: Upload gate evidence
|
||||
# The gate's whole output is machine-readable evidence, and a red run is
|
||||
# exactly when someone needs to read it. Keep it on failure too.
|
||||
|
||||
@@ -12,6 +12,11 @@ const GRANTED_TYPES = new Set(["Font", "Script", "Stylesheet", "Image", "Media",
|
||||
const PROXY_PARAM = "hf-proxy";
|
||||
const VIDEO_FILE = /\.(mp4|webm|mov|mkv|avi|m4v|mxf|mts|m2ts)$/i;
|
||||
|
||||
export const isProxyWait = (url: URL) =>
|
||||
url.searchParams.has(PROXY_PARAM) && VIDEO_FILE.test(url.pathname);
|
||||
export const PROXY_WAIT_ALLOWED_HEADERS = `Range, ${PREVIEW_PROXY_PRIORITY_HEADER}`;
|
||||
export const PREVIEW_EXPOSED_HEADERS = `Retry-After, ${PREVIEW_PROXY_PROGRESS_HEADER}, Content-Range`;
|
||||
|
||||
function capturePreviewPrefix(captureUrl: string): string | null {
|
||||
let url: URL;
|
||||
try {
|
||||
@@ -63,9 +68,7 @@ export function captureLoadGrant(
|
||||
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);
|
||||
(load.resourceType === "XHR" || load.resourceType === "Fetch") && isProxyWait(url);
|
||||
if (load.method === "OPTIONS") return proxyWait ? "preflight" : null;
|
||||
return load.method === "GET" ? "read" : null;
|
||||
}
|
||||
@@ -91,13 +94,13 @@ const OPAQUE_CALLER: Header[] = [
|
||||
{ name: "X-Content-Type-Options", value: "nosniff" },
|
||||
{
|
||||
name: "Access-Control-Expose-Headers",
|
||||
value: `Retry-After, ${PREVIEW_PROXY_PROGRESS_HEADER}, Content-Range`,
|
||||
value: PREVIEW_EXPOSED_HEADERS,
|
||||
},
|
||||
];
|
||||
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}` },
|
||||
{ name: "Access-Control-Allow-Headers", value: PROXY_WAIT_ALLOWED_HEADERS },
|
||||
];
|
||||
|
||||
/** Call on the capture page's own session before it navigates; keep the session for the page's life. */
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
import { createHmac, randomBytes, timingSafeEqual } from "node:crypto";
|
||||
import type { Context, Hono, Next } from "hono";
|
||||
import { getMimeType } from "./mime.js";
|
||||
import {
|
||||
isProxyWait,
|
||||
PREVIEW_EXPOSED_HEADERS,
|
||||
PROXY_WAIT_ALLOWED_HEADERS,
|
||||
} from "./captureLoads.js";
|
||||
|
||||
const ANY_FILE_THE_PREVIEW_SERVES = /./;
|
||||
const GRANTED_TYPES = new Map([
|
||||
@@ -55,11 +60,25 @@ function tokenPathHeaders(c: Context, res: Response, pathname: string): Headers
|
||||
if (granted) {
|
||||
headers.set("Access-Control-Allow-Origin", "null");
|
||||
headers.set("X-Content-Type-Options", "nosniff");
|
||||
headers.set("Access-Control-Expose-Headers", PREVIEW_EXPOSED_HEADERS);
|
||||
headers.append("Vary", "Origin");
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
||||
const answersProxyPreflight = (c: Context, url: URL) =>
|
||||
c.req.method === "OPTIONS" &&
|
||||
c.req.header("origin") === "null" &&
|
||||
c.req.header("access-control-request-method") === "GET" &&
|
||||
isProxyWait(url);
|
||||
|
||||
const PROXY_PREFLIGHT_ANSWER = {
|
||||
"Access-Control-Allow-Origin": "null",
|
||||
"Access-Control-Allow-Methods": "GET",
|
||||
"Access-Control-Allow-Headers": PROXY_WAIT_ALLOWED_HEADERS,
|
||||
Vary: "Origin",
|
||||
};
|
||||
|
||||
async function underToken(res: Response, headers: Headers, projectId: string, token: string) {
|
||||
if (res.status !== 200 || !headers.get("content-type")?.startsWith("text/html"))
|
||||
return new Response(res.body, { status: res.status, headers });
|
||||
@@ -85,6 +104,8 @@ export function serveTokenPreviewPaths(
|
||||
if (!projectId || !tokens.matches(projectId, token)) return c.text("not found", 404);
|
||||
segments.splice(4, 1);
|
||||
url.pathname = segments.join("/");
|
||||
if (answersProxyPreflight(c, url))
|
||||
return new Response(null, { status: 204, headers: PROXY_PREFLIGHT_ANSWER });
|
||||
const req = new Request(url, { method: c.req.method, headers: c.req.raw.headers });
|
||||
const res = await api.fetch(req);
|
||||
return underToken(res, tokenPathHeaders(c, res, url.pathname), projectId, token);
|
||||
|
||||
@@ -265,3 +265,56 @@ describe("a frame without the project's token", () => {
|
||||
expectNotGranted(res);
|
||||
});
|
||||
});
|
||||
|
||||
describe("an opaque preview frame's wait for a video's proxy copy", () => {
|
||||
const preflight = (path: string, headers: Record<string, string> = {}, prefix = `~${token}/`) =>
|
||||
api.request(`http://localhost/projects/demo/preview/${prefix}${path}`, {
|
||||
method: "OPTIONS",
|
||||
headers: {
|
||||
origin: "null",
|
||||
"sec-fetch-site": "cross-site",
|
||||
"sec-fetch-mode": "cors",
|
||||
"access-control-request-method": "GET",
|
||||
"access-control-request-headers": "range,x-hf-proxy-priority",
|
||||
...headers,
|
||||
},
|
||||
});
|
||||
|
||||
it("answers its preflight with the proxy GET's method and headers", async () => {
|
||||
const res = await preflight("clip.mp4?hf-proxy=1");
|
||||
expect(res.status).toBe(204);
|
||||
expect(res.headers.get("access-control-allow-origin")).toBe("null");
|
||||
expect(res.headers.get("access-control-allow-methods")).toBe("GET");
|
||||
expect(res.headers.get("access-control-allow-headers")?.toLowerCase()).toBe(
|
||||
"range, x-hf-proxy-priority",
|
||||
);
|
||||
expect(res.headers.get("vary")).toBe("Origin");
|
||||
});
|
||||
|
||||
it.each([
|
||||
["a file that is not a video", "data.json?hf-proxy=1", {}],
|
||||
["a video without the proxy query", "clip.mp4", {}],
|
||||
["a caller that is not an opaque frame", "clip.mp4?hf-proxy=1", { origin: "https://a.test" }],
|
||||
["a method other than GET", "clip.mp4?hf-proxy=1", { "access-control-request-method": "PUT" }],
|
||||
])("leaves the preflight of %s unanswered", async (_name, path, headers) => {
|
||||
const res = await preflight(path, headers);
|
||||
expect(res.status).not.toBe(204);
|
||||
expect(res.headers.get("access-control-allow-origin")).toBeNull();
|
||||
});
|
||||
|
||||
it("leaves a preflight without the project's token unanswered", async () => {
|
||||
const res = await preflight("clip.mp4?hf-proxy=1", {}, `~${"A".repeat(43)}/`);
|
||||
expect(res.status).toBe(404);
|
||||
expect(res.headers.get("access-control-allow-origin")).toBeNull();
|
||||
});
|
||||
|
||||
it("lets the frame read the proxy wait's headers on a granted load", async () => {
|
||||
const res = await load("clip.mp4", { origin: "null", "sec-fetch-dest": "empty" });
|
||||
expectGranted(res);
|
||||
expect(res.headers.get("access-control-expose-headers")?.split(/,\s*/)).toEqual([
|
||||
"Retry-After",
|
||||
"X-HF-Proxy-Progress",
|
||||
"Content-Range",
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
// A sealed preview asks for a video's proxy copy the way the runtime does (proxyWait.ts) and must get past
|
||||
// the request's CORS preflight. STUDIO_URL: a Studio started with VITE_STUDIO_OPAQUE_PREVIEW_ENABLED=1 on a
|
||||
// project holding clip.mov.
|
||||
import assert from "node:assert/strict";
|
||||
import { launchStudioChrome } from "./chrome-executable.mjs";
|
||||
|
||||
const url = process.env.STUDIO_URL;
|
||||
assert(url, "STUDIO_URL must point at a sealed Studio's project that holds clip.mov");
|
||||
const { browser } = await launchStudioChrome();
|
||||
try {
|
||||
const page = await browser.newPage();
|
||||
page.setDefaultTimeout(90_000);
|
||||
await page.goto(url, { waitUntil: "domcontentloaded" });
|
||||
await page.waitForFunction(() => document.querySelector("hyperframes-player")?.painted === true);
|
||||
const frame = page.frames().find((f) => /\/preview\/~[\w-]{43}/.test(f.url()));
|
||||
assert.ok(frame, "the preview loads under its frame token");
|
||||
const answer = await frame.evaluate(async () => {
|
||||
const origin = String(window.origin);
|
||||
try {
|
||||
const res = await fetch("clip.mov?hf-proxy=1", {
|
||||
headers: { Range: "bytes=0-0", "X-HF-Proxy-Priority": "1" },
|
||||
cache: "no-store",
|
||||
});
|
||||
void res.body?.cancel();
|
||||
return { origin, status: res.status, retryAfter: res.headers.get("Retry-After") };
|
||||
} catch (err) {
|
||||
return { origin, error: String(err) };
|
||||
}
|
||||
});
|
||||
console.log(JSON.stringify(answer));
|
||||
assert.equal(answer.origin, "null", "the preview frame is sealed");
|
||||
assert.equal(answer.error, undefined, "the proxy wait gets past its preflight");
|
||||
assert.equal(typeof answer.status, "number", "the frame reads the answer");
|
||||
if (answer.status === 202)
|
||||
assert.ok(answer.retryAfter, "a pending copy's Retry-After is readable");
|
||||
} finally {
|
||||
await browser.close();
|
||||
}
|
||||
Reference in New Issue
Block a user