mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
fix(cli,studio): refuse ?hf-proxy when a request carries no same-site signal (#8066)
A media-proxy request with neither Sec-Fetch-Site nor a Referer naming the host was treated as same-site. Play, the static project server and Studio now require a positive signal and refuse the request otherwise. The check command's own re-request of failed media sends a Referer for its origin so it keeps working. — Rames Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
a56fa2caf3
commit
4bea3e79c2
@@ -53,6 +53,9 @@ const mocks = vi.hoisted(() => {
|
||||
});
|
||||
const FakeProxyTranscodeError = mocks.ProxyTranscodeError;
|
||||
|
||||
/** What the page itself sends for its media; a proxy copy needs a same-site signal. */
|
||||
const OWN_PAGE = { headers: { "Sec-Fetch-Site": "same-origin" } };
|
||||
|
||||
const mediaMocks = vi.hoisted(() => ({
|
||||
probeAssetCodec: vi.fn(async () => ({
|
||||
codecName: "hevc",
|
||||
@@ -135,7 +138,7 @@ it("serves direct VP8 proxy requests for alpha sources", async () => {
|
||||
mocks.resolveProxy.mockResolvedValueOnce(proxyPath);
|
||||
const app = await buildApp(project, true);
|
||||
|
||||
const res = await app.request("/composition/clip.mov?hf-proxy=auto");
|
||||
const res = await app.request("/composition/clip.mov?hf-proxy=auto", OWN_PAGE);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.headers.get("content-type")).toBe("video/webm");
|
||||
@@ -296,7 +299,7 @@ describe("registerCompositionRoute", () => {
|
||||
mocks.resolveProxy.mockResolvedValue(proxyPath);
|
||||
const app = await buildApp(project, true);
|
||||
|
||||
const res = await app.request("/composition/clip.mp4?hf-proxy=h264");
|
||||
const res = await app.request("/composition/clip.mp4?hf-proxy=h264", OWN_PAGE);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(await res.text()).toBe("transcoded-h264-bytes");
|
||||
@@ -315,7 +318,7 @@ describe("registerCompositionRoute", () => {
|
||||
mocks.resolveProxy.mockResolvedValue(proxyPath);
|
||||
const app = await buildApp(project, true);
|
||||
|
||||
const res = await app.request("/composition/clip.mov?hf-proxy=h264");
|
||||
const res = await app.request("/composition/clip.mov?hf-proxy=h264", OWN_PAGE);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.headers.get("content-type")).toBe("video/mp4");
|
||||
@@ -332,7 +335,7 @@ describe("registerCompositionRoute", () => {
|
||||
mocks.resolveProxy.mockResolvedValue(proxyPath);
|
||||
const app = await buildApp(project, true);
|
||||
|
||||
const res = await app.request(`/composition/clip.${extension}?hf-proxy=h264`);
|
||||
const res = await app.request(`/composition/clip.${extension}?hf-proxy=h264`, OWN_PAGE);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(await res.text()).toBe("transcoded-h264-bytes");
|
||||
@@ -347,7 +350,7 @@ describe("registerCompositionRoute", () => {
|
||||
);
|
||||
const app = await buildApp(project, true);
|
||||
|
||||
const res = await app.request("/composition/clip.mp4?hf-proxy=h264");
|
||||
const res = await app.request("/composition/clip.mp4?hf-proxy=h264", OWN_PAGE);
|
||||
|
||||
expect(res.status).toBe(502);
|
||||
});
|
||||
@@ -358,7 +361,7 @@ describe("registerCompositionRoute", () => {
|
||||
mocks.resolveProxy.mockRejectedValue(new mocks.ProxyCapacityError());
|
||||
const app = await buildApp(project, true);
|
||||
|
||||
const res = await app.request("/composition/clip.mp4?hf-proxy=h264");
|
||||
const res = await app.request("/composition/clip.mp4?hf-proxy=h264", OWN_PAGE);
|
||||
|
||||
expect(res.status).toBe(503);
|
||||
expect(res.headers.get("retry-after")).toBe("1");
|
||||
@@ -369,23 +372,26 @@ describe("registerCompositionRoute", () => {
|
||||
writeFileSync(join(project.dir, "image.png"), "not-a-video");
|
||||
const app = await buildApp(project, true);
|
||||
|
||||
const res = await app.request("/composition/image.png?hf-proxy=h264");
|
||||
const res = await app.request("/composition/image.png?hf-proxy=h264", OWN_PAGE);
|
||||
|
||||
expect(res.status).toBe(404);
|
||||
expect(mocks.resolveProxy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
// fails without the fix: another site's page could make this machine transcode the asset.
|
||||
it.each(["cross-site", "same-site"])(
|
||||
// fails without the fix: another site's page could make this machine transcode the asset,
|
||||
// and a request carrying neither fetch metadata nor a Referer counted as this site's.
|
||||
it.each([
|
||||
["cross-site", { "Sec-Fetch-Site": "cross-site" }],
|
||||
["same-site", { "Sec-Fetch-Site": "same-site" }],
|
||||
["no fetch metadata and no Referer", {}],
|
||||
])(
|
||||
"refuses ?hf-proxy for a request from another site (%s) without starting a transcode",
|
||||
async (site) => {
|
||||
async (_label, headers: Record<string, string>) => {
|
||||
const project = tmpProject();
|
||||
writeFileSync(join(project.dir, "clip.mp4"), "original-hevc-bytes");
|
||||
const app = await buildApp(project, true);
|
||||
|
||||
const res = await app.request("/composition/clip.mp4?hf-proxy=h264", {
|
||||
headers: { "Sec-Fetch-Site": site },
|
||||
});
|
||||
const res = await app.request("/composition/clip.mp4?hf-proxy=h264", { headers });
|
||||
|
||||
expect(res.status).toBe(403);
|
||||
expect(mocks.resolveProxy).not.toHaveBeenCalled();
|
||||
@@ -407,6 +413,11 @@ describe("registerCompositionRoute", () => {
|
||||
expect(own.status).toBe(200);
|
||||
expect(await own.text()).toBe("transcoded-h264-bytes");
|
||||
|
||||
const referred = await app.request("http://localhost/composition/clip.mp4?hf-proxy=h264", {
|
||||
headers: { Referer: "http://localhost/" },
|
||||
});
|
||||
expect(referred.status).toBe(200);
|
||||
|
||||
const plain = await app.request("/composition/clip.mp4", {
|
||||
headers: { "Sec-Fetch-Site": "cross-site" },
|
||||
});
|
||||
@@ -471,7 +482,7 @@ describe("registerCompositionRoute", () => {
|
||||
expect(await htmlRes.text()).not.toContain("__HF_MEDIA_CODEC_MAP__");
|
||||
expect(mocks.scanProjectMediaCodecMap).not.toHaveBeenCalled();
|
||||
|
||||
const proxyRes = await app.request("/composition/clip.mp4?hf-proxy=h264");
|
||||
const proxyRes = await app.request("/composition/clip.mp4?hf-proxy=h264", OWN_PAGE);
|
||||
expect(proxyRes.status).toBe(404);
|
||||
expect(mocks.resolveProxy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -43,7 +43,9 @@ const html = '<html><head></head><body><video src="clip.mov"></video></body></ht
|
||||
describe("serveCapturePage", () => {
|
||||
it("starts no transcode of a hostile clip by default, whatever hyperframes.json says", async () => {
|
||||
server = await serveCapturePage(hostileClipProject(), html, undefined);
|
||||
const proxy = await fetch(`${server.url}clip.mov?hf-proxy=h264`);
|
||||
const proxy = await fetch(`${server.url}clip.mov?hf-proxy=h264`, {
|
||||
headers: { "Sec-Fetch-Site": "same-origin" },
|
||||
});
|
||||
|
||||
expect(proxy.status).toBe(404);
|
||||
expect(mocks.injectMediaCodecMap).not.toHaveBeenCalled();
|
||||
|
||||
@@ -33,4 +33,29 @@ describe("networkChangesStillFailing", () => {
|
||||
`${base}/clip.mp4`,
|
||||
]);
|
||||
});
|
||||
|
||||
it("rechecks as the page itself, so a server that wants a same-site signal answers", async () => {
|
||||
const server = createServer((req, res) => {
|
||||
res.statusCode = req.headers.referer === `${base}/` ? 206 : 403;
|
||||
res.end("x");
|
||||
});
|
||||
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
|
||||
const base = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
|
||||
try {
|
||||
const url = `${base}/clip.mp4?hf-proxy=h264`;
|
||||
const failing = await networkChangesStillFailing([
|
||||
{
|
||||
code: "request_failed",
|
||||
severity: "error",
|
||||
message: url,
|
||||
time: 1,
|
||||
url,
|
||||
networkChanged: true,
|
||||
},
|
||||
]);
|
||||
expect([...failing]).toEqual([]);
|
||||
} finally {
|
||||
server.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -987,7 +987,7 @@ export async function networkChangesStillFailing(drafts: RuntimeDraft[]): Promis
|
||||
urls.map(async (url) => {
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
headers: { Range: "bytes=0-0" },
|
||||
headers: { Range: "bytes=0-0", Referer: `${new URL(url).origin}/` },
|
||||
signal: AbortSignal.timeout(5000),
|
||||
});
|
||||
await response.body?.cancel();
|
||||
|
||||
@@ -17,7 +17,7 @@ import {
|
||||
PREVIEW_ALLOWED_HOSTS_ENV,
|
||||
previewAllowedHostsFromEnv,
|
||||
documentFromAnotherSite,
|
||||
requestFromAnotherSite,
|
||||
proxyWorkFromAnotherSite,
|
||||
wildcardBindHostAllowed,
|
||||
} from "@hyperframes/studio-server/request-identity";
|
||||
import { SAME_ORIGIN_FRAMING_HEADERS } from "@hyperframes/engine";
|
||||
@@ -252,7 +252,7 @@ export function sandboxForAnotherSite(app: Hono, path: string): void {
|
||||
/** A `?hf-proxy=` copy runs ffmpeg on this machine, so another site may not ask for one. */
|
||||
export function refuseProxyWorkForAnotherSite(app: Hono, path: string): void {
|
||||
app.use(path, async function refuseAnotherSiteProxyWork(c, next) {
|
||||
const anotherSite = requestFromAnotherSite({
|
||||
const anotherSite = proxyWorkFromAnotherSite({
|
||||
fetchSite: c.req.header("sec-fetch-site"),
|
||||
host: c.req.header("host") ?? new URL(c.req.url).host,
|
||||
referer: c.req.header("referer"),
|
||||
|
||||
@@ -84,6 +84,9 @@ const mocks = vi.hoisted(() => {
|
||||
});
|
||||
const FakeProxyTranscodeError = mocks.ProxyTranscodeError;
|
||||
|
||||
/** What the page itself sends for its media; a proxy copy needs a same-site signal. */
|
||||
const OWN_PAGE = { headers: { "Sec-Fetch-Site": "same-origin" } };
|
||||
|
||||
vi.mock("@hyperframes/studio-server/proxy-transcoder", () => ({
|
||||
resolveProxy: mocks.resolveProxy,
|
||||
ProxyTranscodeError: mocks.ProxyTranscodeError,
|
||||
@@ -312,7 +315,7 @@ describe("serveStaticProjectHtml asset stream lifetime", () => {
|
||||
const status = await new Promise<number | undefined>((resolve, reject) => {
|
||||
const req = get(
|
||||
`${server?.url}large.mp4${proxy ? "?hf-proxy=h264" : ""}`,
|
||||
{ headers: range ? { Range: range } : {} },
|
||||
{ headers: { ...(range ? { Range: range } : {}), ...(proxy ? OWN_PAGE.headers : {}) } },
|
||||
(response) => {
|
||||
response.on("data", () => {
|
||||
response.destroy();
|
||||
@@ -486,7 +489,7 @@ describe("serveStaticProjectHtml transparent media proxies", () => {
|
||||
mocks.resolveProxy.mockResolvedValue(proxyPath);
|
||||
server = await serveStaticProjectHtml(projectDir, "<html></html>");
|
||||
|
||||
const res = await fetch(`${server.url}clip.mp4?hf-proxy=h264`);
|
||||
const res = await fetch(`${server.url}clip.mp4?hf-proxy=h264`, OWN_PAGE);
|
||||
expect(res.status).toBe(200);
|
||||
expect(await res.text()).toBe("transcoded-h264-bytes");
|
||||
expect(mocks.resolveProxy).toHaveBeenCalledWith(
|
||||
@@ -526,7 +529,7 @@ describe("serveStaticProjectHtml transparent media proxies", () => {
|
||||
mocks.resolveProxy.mockResolvedValue(proxyPath);
|
||||
server = await serveStaticProjectHtml(projectDir, "<html></html>");
|
||||
|
||||
const res = await fetch(`${server.url}clip.mp4?hf-proxy=h264&hf-proxy-box=2048x1448`);
|
||||
const res = await fetch(`${server.url}clip.mp4?hf-proxy=h264&hf-proxy-box=2048x1448`, OWN_PAGE);
|
||||
|
||||
expect(await res.text()).toBe("boxed-h264-bytes");
|
||||
expect(mocks.resolveProxy).toHaveBeenCalledWith(
|
||||
@@ -561,7 +564,7 @@ describe("serveStaticProjectHtml transparent media proxies", () => {
|
||||
mocks.resolveProxy.mockResolvedValue(proxyPath);
|
||||
server = await serveStaticProjectHtml(projectDir, "<html></html>");
|
||||
|
||||
const res = await fetch(`${server.url}clip.${extension}?hf-proxy=h264`);
|
||||
const res = await fetch(`${server.url}clip.${extension}?hf-proxy=h264`, OWN_PAGE);
|
||||
expect(res.status).toBe(200);
|
||||
expect(await res.text()).toBe("transcoded-h264-bytes");
|
||||
expect(mocks.resolveProxy).toHaveBeenCalledWith(projectDir, sourcePath, "h264", undefined, {
|
||||
@@ -584,7 +587,7 @@ describe("serveStaticProjectHtml transparent media proxies", () => {
|
||||
mocks.resolveProxy.mockResolvedValueOnce(proxyPath);
|
||||
server = await serveStaticProjectHtml(projectDir, "<html></html>");
|
||||
|
||||
const res = await fetch(`${server.url}clip.mov?hf-proxy=auto`);
|
||||
const res = await fetch(`${server.url}clip.mov?hf-proxy=auto`, OWN_PAGE);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.headers.get("content-type")).toBe("video/webm");
|
||||
@@ -606,7 +609,7 @@ describe("serveStaticProjectHtml transparent media proxies", () => {
|
||||
);
|
||||
server = await serveStaticProjectHtml(projectDir, "<html></html>");
|
||||
|
||||
const res = await fetch(`${server.url}clip.mp4?hf-proxy=h264`);
|
||||
const res = await fetch(`${server.url}clip.mp4?hf-proxy=h264`, OWN_PAGE);
|
||||
expect(res.status).toBe(502);
|
||||
});
|
||||
|
||||
@@ -616,7 +619,7 @@ describe("serveStaticProjectHtml transparent media proxies", () => {
|
||||
mocks.resolveProxy.mockRejectedValue(new mocks.ProxyCapacityError());
|
||||
server = await serveStaticProjectHtml(projectDir, "<html></html>");
|
||||
|
||||
const res = await fetch(`${server.url}clip.mp4?hf-proxy=h264`);
|
||||
const res = await fetch(`${server.url}clip.mp4?hf-proxy=h264`, OWN_PAGE);
|
||||
expect(res.status).toBe(503);
|
||||
expect(res.headers.get("retry-after")).toBe("1");
|
||||
});
|
||||
@@ -626,22 +629,25 @@ describe("serveStaticProjectHtml transparent media proxies", () => {
|
||||
writeFileSync(join(projectDir, "image.png"), "not-a-video");
|
||||
server = await serveStaticProjectHtml(projectDir, "<html></html>");
|
||||
|
||||
const res = await fetch(`${server.url}image.png?hf-proxy=h264`);
|
||||
const res = await fetch(`${server.url}image.png?hf-proxy=h264`, OWN_PAGE);
|
||||
expect(res.status).toBe(404);
|
||||
expect(mocks.resolveProxy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
// fails without the fix: another site's page could make this machine transcode the asset.
|
||||
it.each(["cross-site", "same-site"])(
|
||||
// fails without the fix: another site's page could make this machine transcode the asset,
|
||||
// and a request carrying neither fetch metadata nor a Referer counted as this site's.
|
||||
it.each([
|
||||
["cross-site", { "Sec-Fetch-Site": "cross-site" }],
|
||||
["same-site", { "Sec-Fetch-Site": "same-site" }],
|
||||
["no fetch metadata and no Referer", {}],
|
||||
])(
|
||||
"refuses ?hf-proxy for a request from another site (%s) without starting a transcode",
|
||||
async (site) => {
|
||||
async (_label, headers: Record<string, string>) => {
|
||||
const projectDir = mk();
|
||||
writeFileSync(join(projectDir, "clip.mp4"), "original-hevc-bytes");
|
||||
server = await serveStaticProjectHtml(projectDir, "<html></html>");
|
||||
|
||||
const res = await fetch(`${server.url}clip.mp4?hf-proxy=h264`, {
|
||||
headers: { "Sec-Fetch-Site": site },
|
||||
});
|
||||
const res = await fetch(`${server.url}clip.mp4?hf-proxy=h264`, { headers });
|
||||
expect(res.status).toBe(403);
|
||||
expect(mocks.resolveProxy).not.toHaveBeenCalled();
|
||||
},
|
||||
@@ -661,6 +667,11 @@ describe("serveStaticProjectHtml transparent media proxies", () => {
|
||||
expect(own.status).toBe(200);
|
||||
expect(await own.text()).toBe("transcoded-h264-bytes");
|
||||
|
||||
const referred = await fetch(`${server.url}clip.mp4?hf-proxy=h264`, {
|
||||
headers: { Referer: server.url },
|
||||
});
|
||||
expect(referred.status).toBe(200);
|
||||
|
||||
const plain = await fetch(`${server.url}clip.mp4`, {
|
||||
headers: { "Sec-Fetch-Site": "cross-site" },
|
||||
});
|
||||
@@ -683,7 +694,7 @@ describe("serveStaticProjectHtml transparent media proxies", () => {
|
||||
}),
|
||||
);
|
||||
const closing = await serveStaticProjectHtml(projectDir, "<html><head></head></html>");
|
||||
const held = fetch(`${closing.url}clip.mp4?hf-proxy=h264`);
|
||||
const held = fetch(`${closing.url}clip.mp4?hf-proxy=h264`, OWN_PAGE);
|
||||
await vi.waitFor(() => expect(asked).toBeDefined());
|
||||
const injected = vi.mocked(injectMediaCodecMapIntoHtml).mock.calls.at(-1) as unknown[];
|
||||
const prewarms = injected[5] as AbortSignal | undefined;
|
||||
|
||||
@@ -25,7 +25,7 @@ import {
|
||||
} from "@hyperframes/studio-server";
|
||||
import {
|
||||
documentFromAnotherSite,
|
||||
requestFromAnotherSite,
|
||||
proxyWorkFromAnotherSite,
|
||||
} from "@hyperframes/studio-server/request-identity";
|
||||
import { findStageProxyBox } from "./compositionViewport.js";
|
||||
import { resolveAutoProxy } from "./projectConfig.js";
|
||||
@@ -304,7 +304,7 @@ function markForAnotherSite(html: string): string {
|
||||
/** A load by another site is sandboxed, and the documents it gets are marked so their runtime offers it no editing port. */
|
||||
/** A `?hf-proxy=` copy runs ffmpeg on this machine, so another site may not ask for one. */
|
||||
function proxyRequestedByAnotherSite(req: IncomingMessage): boolean {
|
||||
return requestFromAnotherSite({
|
||||
return proxyWorkFromAnotherSite({
|
||||
fetchSite: req.headers["sec-fetch-site"] as string | undefined,
|
||||
host: req.headers.host,
|
||||
referer: req.headers.referer,
|
||||
|
||||
@@ -30,6 +30,7 @@ import {
|
||||
PREVIEW_ALLOWED_HOSTS_ENV,
|
||||
documentFromAnotherSite,
|
||||
requestFromAnotherSite,
|
||||
proxyWorkFromAnotherSite,
|
||||
requestHost,
|
||||
sameOriginPost,
|
||||
} from "./helpers/requestIdentity.js";
|
||||
@@ -104,7 +105,7 @@ const sandboxPreviewForAnotherSiteOrCapture =
|
||||
|
||||
async function refuseProxyWorkForAnotherSite(c: Context, next: Next) {
|
||||
const proxyWork = c.req.query(PREVIEW_PROXY_PARAM) !== undefined;
|
||||
if (proxyWork && fromAnotherSite(c) && !loadedThroughFrameToken(c.req.raw))
|
||||
if (proxyWork && proxyWorkFromAnotherSite(siteHeaders(c)) && !loadedThroughFrameToken(c.req.raw))
|
||||
return c.text("forbidden: media proxy requested by another site", 403);
|
||||
return next();
|
||||
}
|
||||
|
||||
@@ -125,6 +125,16 @@ export function requestFromAnotherSite(headers: {
|
||||
return !refererNamesHost(headers.referer, host);
|
||||
}
|
||||
|
||||
export function proxyWorkFromAnotherSite(headers: {
|
||||
fetchSite?: string;
|
||||
host?: string;
|
||||
referer?: string;
|
||||
}): boolean {
|
||||
const site = headers.fetchSite;
|
||||
if (site !== undefined) return site !== "same-origin" && site !== "none";
|
||||
return !headers.host || !refererNamesHost(headers.referer, headers.host);
|
||||
}
|
||||
|
||||
export function documentFromAnotherSite(headers: {
|
||||
fetchSite?: string;
|
||||
host?: string;
|
||||
|
||||
@@ -334,6 +334,12 @@ describe("a media proxy asked for by another site", () => {
|
||||
},
|
||||
);
|
||||
|
||||
// fails without the fix: a request carrying neither fetch metadata nor a Referer counted as the Studio's.
|
||||
it("is refused on the plain path to a request with no fetch metadata and no Referer", async () => {
|
||||
const res = await api.request("http://localhost/projects/demo/preview/clip.mp4?hf-proxy=h264");
|
||||
expect(res.status).toBe(403);
|
||||
});
|
||||
|
||||
it("is refused on the plain path even with a still query", async () => {
|
||||
const res = await api.request(
|
||||
"http://localhost/projects/demo/preview/clip.mp4?hf-proxy=h264&hf-proxy-still=1",
|
||||
@@ -345,6 +351,11 @@ describe("a media proxy asked for by another site", () => {
|
||||
it("goes through under the project's frame token, and from the Studio itself", async () => {
|
||||
expect((await ask(`~${token}/`, "cross-site")).status).not.toBe(403);
|
||||
expect((await ask("", "same-origin")).status).not.toBe(403);
|
||||
const referred = await api.request(
|
||||
"http://localhost/projects/demo/preview/clip.mp4?hf-proxy=h264",
|
||||
{ headers: { referer: "http://localhost/projects/demo/preview/index.html" } },
|
||||
);
|
||||
expect(referred.status).not.toBe(403);
|
||||
});
|
||||
|
||||
it("leaves another site's plain load of a video without the proxy query alone", async () => {
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
// @vitest-environment node
|
||||
// Node's Request keeps the Sec-Fetch-Site header the Studio API needs before it starts proxy work;
|
||||
// happy-dom's drops it.
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { mkdtempSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import type { StudioApiAdapter } from "../types";
|
||||
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
afterEach(() => {
|
||||
vi.resetModules();
|
||||
vi.doUnmock("../helpers/mediaMetadata.js");
|
||||
for (const dir of tempDirs.splice(0)) rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
function createProjectDir(): string {
|
||||
const projectDir = mkdtempSync(join(tmpdir(), "hf-preview-test-"));
|
||||
tempDirs.push(projectDir);
|
||||
writeFileSync(join(projectDir, "index.html"), "<html><head></head><body>Preview</body></html>");
|
||||
return projectDir;
|
||||
}
|
||||
|
||||
function createAdapter(projectDir: string): StudioApiAdapter {
|
||||
return {
|
||||
listProjects: () => [],
|
||||
resolveProject: async (id: string) => ({ id, dir: projectDir }),
|
||||
bundle: async () => null,
|
||||
lint: async () => ({ findings: [] }),
|
||||
runtimeUrl: "/api/runtime.js",
|
||||
rendersDir: () => "/tmp/renders",
|
||||
startRender: () => ({
|
||||
id: "job-1",
|
||||
status: "rendering",
|
||||
progress: 0,
|
||||
outputPath: "/tmp/out.mp4",
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
describe("an hf-proxy request whose project folder goes away", () => {
|
||||
it("answers that the project folder is gone when it is renamed while a proxy is requested", async () => {
|
||||
const projectDir = createProjectDir();
|
||||
writeFileSync(join(projectDir, "clip.mp4"), "original-hevc-bytes");
|
||||
vi.resetModules();
|
||||
vi.doMock("../helpers/mediaMetadata.js", async () => ({
|
||||
...(await vi.importActual<typeof import("../helpers/mediaMetadata.js")>(
|
||||
"../helpers/mediaMetadata.js",
|
||||
)),
|
||||
probeMediaMetadata: async () => {
|
||||
tempDirs.push(`${projectDir}-renamed`);
|
||||
renameSync(projectDir, `${projectDir}-renamed`);
|
||||
return { kind: "video" as const, color: { codecName: "hevc", pixelFormat: "yuv420p" } };
|
||||
},
|
||||
}));
|
||||
const { createStudioApi: create } = await import("../createStudioApi.js");
|
||||
const api = create(createAdapter(projectDir));
|
||||
|
||||
const response = await api.request(
|
||||
"http://localhost/projects/demo/preview/clip.mp4?hf-proxy=h264",
|
||||
{ headers: { "sec-fetch-site": "same-origin" } },
|
||||
);
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
expect(await response.json()).toMatchObject({ why: "project_dir_missing" });
|
||||
});
|
||||
|
||||
it("answers that the project folder is gone when a rename makes the codec probe fail", async () => {
|
||||
const projectDir = createProjectDir();
|
||||
writeFileSync(join(projectDir, "clip.mp4"), "original-hevc-bytes");
|
||||
vi.resetModules();
|
||||
vi.doMock("../helpers/mediaMetadata.js", async () => ({
|
||||
...(await vi.importActual<typeof import("../helpers/mediaMetadata.js")>(
|
||||
"../helpers/mediaMetadata.js",
|
||||
)),
|
||||
probeMediaMetadata: async () => {
|
||||
tempDirs.push(`${projectDir}-renamed`);
|
||||
renameSync(projectDir, `${projectDir}-renamed`);
|
||||
return { kind: "video" as const, color: {}, probeError: "ffprobe failed" };
|
||||
},
|
||||
}));
|
||||
const { createStudioApi: create } = await import("../createStudioApi.js");
|
||||
const api = create(createAdapter(projectDir));
|
||||
|
||||
const response = await api.request(
|
||||
"http://localhost/projects/demo/preview/clip.mp4?hf-proxy=h264",
|
||||
{ headers: { "sec-fetch-site": "same-origin" } },
|
||||
);
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
expect(await response.json()).toMatchObject({ why: "project_dir_missing" });
|
||||
});
|
||||
});
|
||||
@@ -10,7 +10,6 @@ import {
|
||||
openSync,
|
||||
readFileSync,
|
||||
realpathSync,
|
||||
renameSync,
|
||||
rmSync,
|
||||
statSync,
|
||||
symlinkSync,
|
||||
@@ -3039,56 +3038,6 @@ describe("hf-proxy codec probe", () => {
|
||||
return { proxy, probeMediaMetadata };
|
||||
}
|
||||
|
||||
it("answers that the project folder is gone when it is renamed while a proxy is requested", async () => {
|
||||
const projectDir = createProjectDir();
|
||||
writeFileSync(join(projectDir, "clip.mp4"), "original-hevc-bytes");
|
||||
vi.resetModules();
|
||||
vi.doMock("../helpers/mediaMetadata.js", async () => ({
|
||||
...(await vi.importActual<typeof import("../helpers/mediaMetadata.js")>(
|
||||
"../helpers/mediaMetadata.js",
|
||||
)),
|
||||
probeMediaMetadata: async () => {
|
||||
tempDirs.push(`${projectDir}-renamed`);
|
||||
renameSync(projectDir, `${projectDir}-renamed`);
|
||||
return { kind: "video" as const, color: { codecName: "hevc", pixelFormat: "yuv420p" } };
|
||||
},
|
||||
}));
|
||||
const { createStudioApi: create } = await import("../createStudioApi.js");
|
||||
const api = create(createAdapter(projectDir));
|
||||
|
||||
const response = await api.request(
|
||||
"http://localhost/projects/demo/preview/clip.mp4?hf-proxy=h264",
|
||||
);
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
expect(await response.json()).toMatchObject({ why: "project_dir_missing" });
|
||||
});
|
||||
|
||||
it("answers that the project folder is gone when a rename makes the codec probe fail", async () => {
|
||||
const projectDir = createProjectDir();
|
||||
writeFileSync(join(projectDir, "clip.mp4"), "original-hevc-bytes");
|
||||
vi.resetModules();
|
||||
vi.doMock("../helpers/mediaMetadata.js", async () => ({
|
||||
...(await vi.importActual<typeof import("../helpers/mediaMetadata.js")>(
|
||||
"../helpers/mediaMetadata.js",
|
||||
)),
|
||||
probeMediaMetadata: async () => {
|
||||
tempDirs.push(`${projectDir}-renamed`);
|
||||
renameSync(projectDir, `${projectDir}-renamed`);
|
||||
return { kind: "video" as const, color: {}, probeError: "ffprobe failed" };
|
||||
},
|
||||
}));
|
||||
const { createStudioApi: create } = await import("../createStudioApi.js");
|
||||
const api = create(createAdapter(projectDir));
|
||||
|
||||
const response = await api.request(
|
||||
"http://localhost/projects/demo/preview/clip.mp4?hf-proxy=h264",
|
||||
);
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
expect(await response.json()).toMatchObject({ why: "project_dir_missing" });
|
||||
});
|
||||
|
||||
it("runs ffprobe once for repeated proxy requests of the same unchanged clip", async () => {
|
||||
const projectDir = createProjectDir();
|
||||
writeFileSync(join(projectDir, "clip.mp4"), "original-hevc-bytes");
|
||||
|
||||
Reference in New Issue
Block a user