From ded4c1bf15f8b08bc5564e92d3dcb555c2345e9c Mon Sep 17 00:00:00 2001 From: Danila Katalshov <56929384+ADanMan@users.noreply.github.com> Date: Fri, 4 Dec 2026 03:49:11 +0300 Subject: [PATCH] fix(server): encode Unicode asset response filenames (#22892) ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - People open uploaded files and company logos from their browser. > - Two routes put the stored filename directly into an HTTP header. > - Node rejects some valid Unicode names used by screenshot tools and people. > - This change formats both filenames with the same Express API used for issue attachments. ## Linked Issues or Issue Description Fixes: #7493 Thanks to dosthcpp for the report and the earlier proposal in #7494. The issue attachment route already uses Express to format the filename. The asset content and invite logo routes still wrote it directly. Related #8716 and #9570 cover attachment display policy and ZIP downloads. ## What Changed - Format the filename with Express in the asset content and invite logo responses. Keep the current content type and inline or attachment choice. - Exercise a screenshot with a narrow non-breaking space and a logo with Japanese characters. ## Verification - On the preceding master `f1dd9288d`, both Unicode cases returned 500 before the fix. - On `3fbf872d9` with the fix, 57 tests passed across the assets, invite logo, and issue attachment suites. The new checks verify status, Unicode filename encoding, and inline disposition. - Full server typecheck, test budget, and `git diff --check` passed. PR CI is pending. ## Risks - Both routes still set the content type from storage metadata after formatting the filename. The existing disposition, range, and content policy behavior remain covered by the route tests. ## Model Used - OpenAI Codex (GPT-6 family) with terminal and Vitest access. Exact model identifier and context window were not exposed in this session. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [ ] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --- server/src/__tests__/assets.test.ts | 27 +++++++++++++++++++ .../src/__tests__/invite-logo-route.test.ts | 8 ++++-- server/src/routes/access.ts | 5 ++-- server/src/routes/assets.ts | 10 +++---- 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/server/src/__tests__/assets.test.ts b/server/src/__tests__/assets.test.ts index 5825e47e1b1..84fd65974f1 100644 --- a/server/src/__tests__/assets.test.ts +++ b/server/src/__tests__/assets.test.ts @@ -494,6 +494,33 @@ describe("GET /api/assets/:assetId/content", () => { expect(res.headers["content-security-policy"]).toBe("sandbox; default-src 'none'"); }); + it("serves screenshots with Unicode filenames inline", async () => { + const bytes = Buffer.from("png-bytes"); + const filename = "Screenshot 10.32.55\u202fPM.png"; + const storage = createStorageService("image/png"); + getAssetByIdMock.mockResolvedValue({ + ...createAsset(), + originalFilename: filename, + byteSize: bytes.byteLength, + }); + vi.mocked(storage.getObject).mockResolvedValue({ + stream: Readable.from(bytes), + contentType: "image/png", + contentLength: bytes.byteLength, + }); + + const response = await requestApp(await createApp(storage), (baseUrl) => + request(baseUrl).get("/api/assets/asset-1/content"), + ); + + expect(response.status).toBe(200); + expect(response.headers["content-type"]).toBe("image/png"); + expect(response.headers["content-disposition"]).toBe( + `inline; filename="Screenshot 10.32.55?PM.png"; filename*=UTF-8''${encodeURIComponent(filename)}`, + ); + expect(response.body).toEqual(bytes); + }); + it("keeps curated image types inline", async () => { const image = Buffer.from("png-bytes"); const storage = createStorageService("image/png"); diff --git a/server/src/__tests__/invite-logo-route.test.ts b/server/src/__tests__/invite-logo-route.test.ts index a18137c3ab4..75e8591a89a 100644 --- a/server/src/__tests__/invite-logo-route.test.ts +++ b/server/src/__tests__/invite-logo-route.test.ts @@ -68,7 +68,10 @@ describe("GET /invites/:token/logo", () => { mockStorage.headObject.mockReset(); }); - it("serves the company logo for an active invite without company auth", async () => { + it.each([ + { filename: "logo.png", disposition: 'inline; filename="logo.png"' }, + { filename: "ロゴ.png", disposition: `inline; filename="??.png"; filename*=UTF-8''${encodeURIComponent("ロゴ.png")}` }, + ])("serves $filename for an active invite without company auth", async ({ filename, disposition }) => { const invite = { id: "invite-1", companyId: "company-1", @@ -99,7 +102,7 @@ describe("GET /invites/:token/logo", () => { objectKey: "assets/companies/logo-1", contentType: "image/png", byteSize: 3, - originalFilename: "logo.png", + originalFilename: filename, }]), ); @@ -107,6 +110,7 @@ describe("GET /invites/:token/logo", () => { expect(res.status).toBe(200); expect(res.headers["content-type"]).toContain("image/png"); + expect(res.headers["content-disposition"]).toBe(disposition); expect(mockStorage.headObject).toHaveBeenCalledWith("company-1", "assets/companies/logo-1"); expect(mockStorage.getObject).toHaveBeenCalledWith("company-1", "assets/companies/logo-1"); }); diff --git a/server/src/routes/access.ts b/server/src/routes/access.ts index bfea3bfb084..cb64905d0ac 100644 --- a/server/src/routes/access.ts +++ b/server/src/routes/access.ts @@ -3613,6 +3613,8 @@ export function accessRoutes( logoHead.contentType || object.contentType || "application/octet-stream"; + res.attachment(logoAsset.originalFilename || "company-logo"); + res.setHeader("Content-Disposition", String(res.getHeader("Content-Disposition")).replace(/^attachment;/, "inline;")); res.setHeader("Content-Type", responseContentType); res.setHeader( "Content-Length", @@ -3623,9 +3625,6 @@ export function accessRoutes( if (responseContentType === "image/svg+xml") { res.setHeader("Content-Security-Policy", "sandbox; default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'"); } - const filename = logoAsset.originalFilename ?? "company-logo"; - res.setHeader("Content-Disposition", `inline; filename=\"${filename.replaceAll("\"", "")}\"`); - object.stream.on("error", (err) => { next(err); }); diff --git a/server/src/routes/assets.ts b/server/src/routes/assets.ts index 5f48886baa3..cd0e6c801d0 100644 --- a/server/src/routes/assets.ts +++ b/server/src/routes/assets.ts @@ -352,6 +352,10 @@ export function assetRoutes(db: Db, storage: StorageService) { const mediaType = responseContentType.split(";", 1)[0]?.trim().toLowerCase() ?? ""; const inlineSafe = mediaType !== SVG_CONTENT_TYPE && isInlineAttachmentContentType(mediaType); + res.attachment(asset.originalFilename || "asset"); + if (inlineSafe) { + res.setHeader("Content-Disposition", String(res.getHeader("Content-Disposition")).replace(/^attachment;/, "inline;")); + } res.setHeader("Content-Type", responseContentType); res.setHeader("Content-Length", String(range ? range.end - range.start + 1 : asset.byteSize || object.contentLength || 0)); if (range) { @@ -363,12 +367,6 @@ export function assetRoutes(db: Db, storage: StorageService) { if (!inlineSafe) { res.setHeader("Content-Security-Policy", "sandbox; default-src 'none'"); } - const filename = asset.originalFilename ?? "asset"; - const disposition = inlineSafe - ? "inline" - : "attachment"; - res.setHeader("Content-Disposition", `${disposition}; filename=\"${filename.replaceAll("\"", "")}\"`); - object.stream.on("error", (err) => { next(err); });