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); });