diff --git a/packages/cli/src/commands/play.test.ts b/packages/cli/src/commands/play.test.ts index 8a8eb71e25..831b27d185 100644 --- a/packages/cli/src/commands/play.test.ts +++ b/packages/cli/src/commands/play.test.ts @@ -375,6 +375,45 @@ describe("registerCompositionRoute", () => { 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"])( + "refuses ?hf-proxy for a request from another site (%s) without starting a transcode", + async (site) => { + 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 }, + }); + + expect(res.status).toBe(403); + expect(mocks.resolveProxy).not.toHaveBeenCalled(); + expect(mediaMocks.probeAssetCodec).not.toHaveBeenCalled(); + }, + ); + + it("still serves ?hf-proxy to the play page itself and plain assets to another site", async () => { + const project = tmpProject(); + writeFileSync(join(project.dir, "clip.mp4"), "original-hevc-bytes"); + const proxyPath = join(project.dir, "proxy.mp4"); + writeFileSync(proxyPath, "transcoded-h264-bytes"); + mocks.resolveProxy.mockResolvedValue(proxyPath); + const app = await buildApp(project, true); + + const own = await app.request("/composition/clip.mp4?hf-proxy=h264", { + headers: { "Sec-Fetch-Site": "same-origin" }, + }); + expect(own.status).toBe(200); + expect(await own.text()).toBe("transcoded-h264-bytes"); + + const plain = await app.request("/composition/clip.mp4", { + headers: { "Sec-Fetch-Site": "cross-site" }, + }); + expect(plain.status).toBe(200); + expect(await plain.text()).toBe("original-hevc-bytes"); + }); + it("serves the runtime script ahead of every author script", async () => { // Compositions read `window.__hyperframes.getVariables()` from an inline // script at init. A runtime injected before loads after that script, diff --git a/packages/cli/src/commands/play.ts b/packages/cli/src/commands/play.ts index 3c481422a3..281a884438 100644 --- a/packages/cli/src/commands/play.ts +++ b/packages/cli/src/commands/play.ts @@ -38,6 +38,7 @@ import { listenOnFreePort, refuseUnknownHosts, sandboxForAnotherSite, + refuseProxyWorkForAnotherSite, injectRuntime, injectMediaCodecMap, buildRangeResponse, @@ -225,6 +226,7 @@ export async function registerCompositionRoute( const { isSafePath } = await import("@hyperframes/core/studio-api"); sandboxForAnotherSite(app, "/composition/*"); + refuseProxyWorkForAnotherSite(app, "/composition/*"); // fallow-ignore-next-line complexity app.get("/composition/*", async (ctx) => { const reqPath = requestSubPath(ctx.req.url, "composition"); diff --git a/packages/cli/src/utils/compositionServer.ts b/packages/cli/src/utils/compositionServer.ts index 106f6a671c..a2a7cc4d3c 100644 --- a/packages/cli/src/utils/compositionServer.ts +++ b/packages/cli/src/utils/compositionServer.ts @@ -14,6 +14,7 @@ import { PREVIEW_ALLOWED_HOSTS_ENV, previewAllowedHostsFromEnv, documentFromAnotherSite, + requestFromAnotherSite, wildcardBindHostAllowed, } from "@hyperframes/studio-server/request-identity"; import { SAME_ORIGIN_FRAMING_HEADERS } from "@hyperframes/engine"; @@ -244,6 +245,20 @@ 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({ + fetchSite: c.req.header("sec-fetch-site"), + host: c.req.header("host") ?? new URL(c.req.url).host, + referer: c.req.header("referer"), + }); + if (anotherSite && c.req.query("hf-proxy") !== undefined) + return c.text("Forbidden: media proxy requested by another site", 403); + await next(); + }); +} + /** The runtime hands an editing port to its parent frame, so only this server's own pages may frame what it serves. */ export function refuseFramingByOtherSites(app: Hono): void { app.use(async function sameOriginFramingOnly(c, next) { diff --git a/packages/cli/src/utils/staticProjectServer.test.ts b/packages/cli/src/utils/staticProjectServer.test.ts index 0ad82a45d1..68f3b8f561 100644 --- a/packages/cli/src/utils/staticProjectServer.test.ts +++ b/packages/cli/src/utils/staticProjectServer.test.ts @@ -631,6 +631,43 @@ describe("serveStaticProjectHtml transparent media proxies", () => { 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"])( + "refuses ?hf-proxy for a request from another site (%s) without starting a transcode", + async (site) => { + const projectDir = mk(); + writeFileSync(join(projectDir, "clip.mp4"), "original-hevc-bytes"); + server = await serveStaticProjectHtml(projectDir, ""); + + const res = await fetch(`${server.url}clip.mp4?hf-proxy=h264`, { + headers: { "Sec-Fetch-Site": site }, + }); + expect(res.status).toBe(403); + expect(mocks.resolveProxy).not.toHaveBeenCalled(); + }, + ); + + it("still serves ?hf-proxy to its own page and plain assets to another site", async () => { + const projectDir = mk(); + writeFileSync(join(projectDir, "clip.mp4"), "original-hevc-bytes"); + const proxyPath = join(projectDir, "proxy.mp4"); + writeFileSync(proxyPath, "transcoded-h264-bytes"); + mocks.resolveProxy.mockResolvedValue(proxyPath); + server = await serveStaticProjectHtml(projectDir, ""); + + const own = await fetch(`${server.url}clip.mp4?hf-proxy=h264`, { + headers: { "Sec-Fetch-Site": "same-origin" }, + }); + expect(own.status).toBe(200); + expect(await own.text()).toBe("transcoded-h264-bytes"); + + const plain = await fetch(`${server.url}clip.mp4`, { + headers: { "Sec-Fetch-Site": "cross-site" }, + }); + expect(plain.status).toBe(200); + expect(await plain.text()).toBe("original-hevc-bytes"); + }); + it("stops the copies it started, pre-warmed or asked for, when it closes", async () => { mocks.scanProjectMediaCodecMap.mockResolvedValue({ "/clip.mp4": { codecName: "hevc", browserHostile: true, representativeMime: null }, diff --git a/packages/cli/src/utils/staticProjectServer.ts b/packages/cli/src/utils/staticProjectServer.ts index d3eded3010..a6888e7b54 100644 --- a/packages/cli/src/utils/staticProjectServer.ts +++ b/packages/cli/src/utils/staticProjectServer.ts @@ -19,7 +19,10 @@ import { } from "@hyperframes/core"; import { getMimeType } from "@hyperframes/core/studio-api"; import { decodeWellFormedEscapes, openRangeEnd } from "@hyperframes/studio-server"; -import { documentFromAnotherSite } from "@hyperframes/studio-server/request-identity"; +import { + documentFromAnotherSite, + requestFromAnotherSite, +} from "@hyperframes/studio-server/request-identity"; import { findStageProxyBox } from "./compositionViewport.js"; import { resolveAutoProxy } from "./projectConfig.js"; import { injectMediaCodecMap } from "./compositionServer.js"; @@ -295,6 +298,15 @@ 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({ + fetchSite: req.headers["sec-fetch-site"] as string | undefined, + host: req.headers.host, + referer: req.headers.referer, + }); +} + function loadedByAnotherSite(req: IncomingMessage, res: ServerResponse): boolean { const anotherSite = documentFromAnotherSite({ fetchSite: req.headers["sec-fetch-site"] as string | undefined, @@ -384,6 +396,11 @@ export async function serveStaticProjectHtml( res.end(); return; } + if (proxyParam !== null && proxyRequestedByAnotherSite(req)) { + res.writeHead(403); + res.end("Forbidden: media proxy requested by another site"); + return; + } const requestPath = decodeWellFormedEscapes(pathOnly).replace(/^\//, ""); for (const root of roots) {