Skip to content

Write HTML. Render video. Built for agents.

fix(studio-server): make render download headers ByteString-safe (#3984)

heygengenesis[bot]b599f031a929
diff --git a/packages/studio-server/src/routes/render.test.ts b/packages/studio-server/src/routes/render.test.ts
index 40183da199..b4853a2def 100644
--- a/packages/studio-server/src/routes/render.test.ts
+++ b/packages/studio-server/src/routes/render.test.ts
@@ -456,6 +456,41 @@ describe("GET /projects/:id/renders/file/* — path safety", () => {
tmpDirs.length = 0;
});
+ it("serves non-Latin-1 filenames with RFC 6266 content dispositions", async () => {
+ const filename = "測試.mp4";
+ const { app, rendersDir } = buildApp();
+ const outputPath = join(rendersDir, filename);
+ writeFileSync(outputPath, "render-bytes");
+
+ const listResponse = await app.request("http://localhost/projects/demo/renders");
+ expect(listResponse.status).toBe(200);
+ const jobId = encodeURIComponent(filename.replace(/\.mp4$/, ""));
+ const expectedFilename = `filename="__.mp4"; filename*=UTF-8''${encodeURIComponent(filename)}`;
+ const responses = [
+ {
+ response: await app.request(`http://localhost/render/${jobId}/view`),
+ disposition: "inline",
+ },
+ {
+ response: await app.request(`http://localhost/render/${jobId}/download`),
+ disposition: "attachment",
+ },
+ {
+ response: await app.request(
+ `http://localhost/projects/demo/renders/file/${encodeURIComponent(filename)}`,
+ ),
+ disposition: "inline",
+ },
+ ];
+
+ for (const { response, disposition } of responses) {
+ expect(response.status).toBe(200);
+ const header = response.headers.get("Content-Disposition");
+ expect(header).toBe(`${disposition}; ${expectedFilename}`);
+ expect(header).toMatch(/^[\x20-\x7e]+$/);
+ }
+ });
+
it("serves a render file that lives inside rendersDir", async () => {
const { app, rendersDir } = buildApp();
writeFileSync(join(rendersDir, "demo.mp4"), "render-bytes");
diff --git a/packages/studio-server/src/routes/render.ts b/packages/studio-server/src/routes/render.ts
index bc0c6de270..871437cb36 100644
--- a/packages/studio-server/src/routes/render.ts
+++ b/packages/studio-server/src/routes/render.ts
@@ -10,6 +10,15 @@ import { isVariablesPayload, VARIABLES_PAYLOAD_ERROR } from "../helpers/variable
const VALID_RESOLUTIONS = new Set<string>(VALID_CANVAS_RESOLUTIONS);
+function contentDispositionHeader(disposition: "inline" | "attachment", filename: string): string {
+ const fallback = filename.replace(/[^\x20-\x7e]|["\\]/g, "_");
+ const encoded = encodeURIComponent(filename).replace(
+ /[!'()*]/g,
+ (character) => `%${character.charCodeAt(0).toString(16).toUpperCase()}`,
+ );
+ return `${disposition}; filename="${fallback}"; filename*=UTF-8''${encoded}`;
+}
+
export function registerRenderRoutes(api: Hono, adapter: StudioApiAdapter): void {
// Scoped job store — not shared across createStudioApi() calls
const renderJobs = new Map<string, RenderJobState & { createdAt: number }>();
@@ -204,7 +213,7 @@ export function registerRenderRoutes(api: Hono, adapter: StudioApiAdapter): void
return new Response(content, {
headers: {
"Content-Type": contentType,
- "Content-Disposition": `inline; filename="${filename}"`,
+ "Content-Disposition": contentDispositionHeader("inline", filename),
"Accept-Ranges": "bytes",
"Content-Length": String(content.length),
},
@@ -225,7 +234,7 @@ export function registerRenderRoutes(api: Hono, adapter: StudioApiAdapter): void
return new Response(content, {
headers: {
"Content-Type": contentType,
- "Content-Disposition": `attachment; filename="${filename}"`,
+ "Content-Disposition": contentDispositionHeader("attachment", filename),
},
});
});
@@ -267,7 +276,7 @@ export function registerRenderRoutes(api: Hono, adapter: StudioApiAdapter): void
return new Response(content, {
headers: {
"Content-Type": contentType,
- "Content-Disposition": `inline; filename="${filename}"`,
+ "Content-Disposition": contentDispositionHeader("inline", filename),
"Accept-Ranges": "bytes",
"Content-Length": String(content.length),
},