Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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) => {
Expand Down
10 changes: 10 additions & 0 deletions apps/server/src/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
53 changes: 53 additions & 0 deletions tests/file-disposition.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
}