mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 16:08:56 +00:00
fix(cli): refuse media-proxy copies another site asks for in play and the static server (#8058)
A `?hf-proxy=` request makes this machine run ffmpeg. Studio already refuses one that comes from another site; the `play` composition route and the static project server (used by check, snapshot and capture) still served it. Both now answer 403 to a cross-site or same-site `?hf-proxy=` request before any probe or transcode starts. Their own pages and plain asset requests are unchanged. — Rames Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
4721739ee7
commit
f97e289058
@@ -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 </body> loads after that script,
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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, "<html></html>");
|
||||
|
||||
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, "<html></html>");
|
||||
|
||||
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 },
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user