Skip to content

Commit b599f03

Browse files
fix(studio-server): make render download headers ByteString-safe (heygen-com#3984)
Build RFC 6266 Content-Disposition values with an ASCII filename fallback and UTF-8 filename* parameter for each render file response. This preserves inline and attachment disposition semantics while preventing non-Latin-1 filenames from crossing the HTTP ByteString boundary. Co-authored-by: heygengenesis[bot] <262951085+heygengenesis[bot]@users.noreply.github.com> Co-authored-by: miguel.sierra <[email protected]>
1 parent edb6e87 commit b599f03

2 files changed

Lines changed: 47 additions & 3 deletions

File tree

‎packages/studio-server/src/routes/render.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,41 @@ describe("GET /projects/:id/renders/file/* — path safety", () => {
456456
tmpDirs.length = 0;
457457
});
458458

459+
it("serves non-Latin-1 filenames with RFC 6266 content dispositions", async () => {
460+
const filename = "測試.mp4";
461+
const { app, rendersDir } = buildApp();
462+
const outputPath = join(rendersDir, filename);
463+
writeFileSync(outputPath, "render-bytes");
464+
465+
const listResponse = await app.request("http://localhost/projects/demo/renders");
466+
expect(listResponse.status).toBe(200);
467+
const jobId = encodeURIComponent(filename.replace(/\.mp4$/, ""));
468+
const expectedFilename = `filename="__.mp4"; filename*=UTF-8''${encodeURIComponent(filename)}`;
469+
const responses = [
470+
{
471+
response: await app.request(`http://localhost/render/${jobId}/view`),
472+
disposition: "inline",
473+
},
474+
{
475+
response: await app.request(`http://localhost/render/${jobId}/download`),
476+
disposition: "attachment",
477+
},
478+
{
479+
response: await app.request(
480+
`http://localhost/projects/demo/renders/file/${encodeURIComponent(filename)}`,
481+
),
482+
disposition: "inline",
483+
},
484+
];
485+
486+
for (const { response, disposition } of responses) {
487+
expect(response.status).toBe(200);
488+
const header = response.headers.get("Content-Disposition");
489+
expect(header).toBe(`${disposition}; ${expectedFilename}`);
490+
expect(header).toMatch(/^[\x20-\x7e]+$/);
491+
}
492+
});
493+
459494
it("serves a render file that lives inside rendersDir", async () => {
460495
const { app, rendersDir } = buildApp();
461496
writeFileSync(join(rendersDir, "demo.mp4"), "render-bytes");

‎packages/studio-server/src/routes/render.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ import { isVariablesPayload, VARIABLES_PAYLOAD_ERROR } from "../helpers/variable
1010

1111
const VALID_RESOLUTIONS = new Set<string>(VALID_CANVAS_RESOLUTIONS);
1212

13+
function contentDispositionHeader(disposition: "inline" | "attachment", filename: string): string {
14+
const fallback = filename.replace(/[^\x20-\x7e]|["\\]/g, "_");
15+
const encoded = encodeURIComponent(filename).replace(
16+
/[!'()*]/g,
17+
(character) => `%${character.charCodeAt(0).toString(16).toUpperCase()}`,
18+
);
19+
return `${disposition}; filename="${fallback}"; filename*=UTF-8''${encoded}`;
20+
}
21+
1322
export function registerRenderRoutes(api: Hono, adapter: StudioApiAdapter): void {
1423
// Scoped job store — not shared across createStudioApi() calls
1524
const renderJobs = new Map<string, RenderJobState & { createdAt: number }>();
@@ -204,7 +213,7 @@ export function registerRenderRoutes(api: Hono, adapter: StudioApiAdapter): void
204213
return new Response(content, {
205214
headers: {
206215
"Content-Type": contentType,
207-
"Content-Disposition": `inline; filename="${filename}"`,
216+
"Content-Disposition": contentDispositionHeader("inline", filename),
208217
"Accept-Ranges": "bytes",
209218
"Content-Length": String(content.length),
210219
},
@@ -225,7 +234,7 @@ export function registerRenderRoutes(api: Hono, adapter: StudioApiAdapter): void
225234
return new Response(content, {
226235
headers: {
227236
"Content-Type": contentType,
228-
"Content-Disposition": `attachment; filename="${filename}"`,
237+
"Content-Disposition": contentDispositionHeader("attachment", filename),
229238
},
230239
});
231240
});
@@ -267,7 +276,7 @@ export function registerRenderRoutes(api: Hono, adapter: StudioApiAdapter): void
267276
return new Response(content, {
268277
headers: {
269278
"Content-Type": contentType,
270-
"Content-Disposition": `inline; filename="${filename}"`,
279+
"Content-Disposition": contentDispositionHeader("inline", filename),
271280
"Accept-Ranges": "bytes",
272281
"Content-Length": String(content.length),
273282
},

0 commit comments

Comments
 (0)