From 75367b4c1a7d94adca4237508dfc5bd280c1bdb6 Mon Sep 17 00:00:00 2001 From: Will Date: Tue, 24 Nov 2026 21:57:33 +0800 Subject: [PATCH] fix(files): encode apostrophes and parentheses in PDF filenames The inline Content-Disposition value used encodeURIComponent, which leaves "'", "(", ")" and "*" unescaped. Those are not RFC 5987 attr-chars, and an apostrophe is the value delimiter in filename*=UTF-8''..., so a name like "O'Brien (final).pdf" produced a malformed header. Use the same attr-char escaping the attachment encoder already applies, and cover ASCII, punctuation and non-ASCII names. --- apps/server/src/app.ts | 4 +-- apps/server/src/files.ts | 10 +++++++ tests/file-disposition.test.ts | 53 ++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 tests/file-disposition.test.ts diff --git a/apps/server/src/app.ts b/apps/server/src/app.ts index 811a5f520..e17feb93b 100644 --- a/apps/server/src/app.ts +++ b/apps/server/src/app.ts @@ -22,7 +22,7 @@ import type { Store } from "./db.ts"; import { agentRoutes } from "./engine/routes.ts"; import { AgentService } from "./engine/service.ts"; import { AppError } from "./errors.ts"; -import { Files } from "./files.ts"; +import { encodeDispositionName, Files } from "./files.ts"; import { GoogleAuth } from "./google-auth.ts"; import { rateLimit } from "./rate-limit.ts"; import { WorkspaceService } from "./workspace.ts"; @@ -264,7 +264,7 @@ export async function createApp( app.get("/api/files/:id/content", async (c) => { const file = await files.get(c.get("owner"), c.req.param("id")); c.header("Content-Type", "application/pdf"); - c.header("Content-Disposition", `inline; filename*=UTF-8''${encodeURIComponent(file.name)}`); + c.header("Content-Disposition", `inline; filename*=UTF-8''${encodeDispositionName(file.name)}`); return c.body(await files.bytes(c.get("owner"), file.id)); }); app.post("/api/files/:id/fill", async (c) => { diff --git a/apps/server/src/files.ts b/apps/server/src/files.ts index 14453455c..3564c5fa6 100644 --- a/apps/server/src/files.ts +++ b/apps/server/src/files.ts @@ -20,6 +20,16 @@ export function sanitizeFileName(name: string): string { return cleaned || "document.pdf"; } +/** RFC 5987 attr-char encoding for a Content-Disposition filename value. */ +export function encodeDispositionName(name: string): string { + // encodeURIComponent leaves these sub-delimiters alone, but attr-char does not + // include them and the apostrophe is the value delimiter in filename*=UTF-8''. + return encodeURIComponent(name).replace( + /['()*]/g, + (character) => `%${character.charCodeAt(0).toString(16).toUpperCase()}`, + ); +} + function assertFileId(id: string): void { if (!FILE_ID.test(id)) throw new AppError("File not found", 404); } diff --git a/tests/file-disposition.test.ts b/tests/file-disposition.test.ts new file mode 100644 index 000000000..421e41d2f --- /dev/null +++ b/tests/file-disposition.test.ts @@ -0,0 +1,53 @@ +import assert from "node:assert/strict"; +import { mkdtemp, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import test, { type TestContext } from "node:test"; +import { createApp } from "../apps/server/src/app.ts"; +import type { Config } from "../apps/server/src/config.ts"; +import { createStore } from "../apps/server/src/db.ts"; +import { createSamplePdf } from "../packages/integrations/src/pdf.ts"; + +async function fixture(t: TestContext) { + const directory = await mkdtemp(join(tmpdir(), "openfuse-disposition-")); + const db = await createStore({ dataDir: join(directory, "db") }); + const config: Config = { + mode: "sample", + port: 8787, + host: "127.0.0.1", + publicUrl: "http://localhost:8787", + dataDir: directory, + agentBackend: "sample", + intelligenceApiKey: "test-project-key-never-sent", + googleRedirectUri: "http://localhost:8787/api/google/callback", + allowedOrigins: [], + }; + const server = await createApp(db, config); + t.after(async () => { + await server.agent.stop(); + await db.close(); + await rm(directory, { recursive: true, force: true }); + }); + return server; +} + +for (const [name, encoded] of [ + ["O'Brien (final).pdf", "O%27Brien%20%28final%29.pdf"], + ["report.pdf", "report.pdf"], + ["报告 2026.pdf", "%E6%8A%A5%E5%91%8A%202026.pdf"], +] as const) { + test(`content disposition encodes ${JSON.stringify(name)} for RFC 5987`, async (t) => { + const server = await fixture(t); + const owner = "disposition-user"; + const file = await server.files.import(owner, name, await createSamplePdf(), "Test file"); + const url = new URL(file.url); + + const response = await server.app.request(`${url.pathname}${url.search}`); + assert.equal(response.status, 200); + assert.equal( + response.headers.get("content-disposition"), + `inline; filename*=UTF-8''${encoded}`, + ); + await response.arrayBuffer(); + }); +}