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
This commit is contained in:
Danila Katalshov authored and GitHub committed 2026-12-03 16:49:11 -08:00
1 parent c5f5d3163d
commit ded4c1bf15
4 files changed
+39 -11

No files matched your search

+27
View File
@@ -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");
@@ -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");
});
+2 -3
View File
@@ -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);
});
+4 -6
View File
@@ -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);
});