diff --git a/app/src/stream/asset.ts b/app/src/stream/asset.ts index 1b97319..a3d9676 100644 --- a/app/src/stream/asset.ts +++ b/app/src/stream/asset.ts @@ -12,6 +12,7 @@ import { isVideoAsset, resolveDownloadEndpoint, resolveImageEndpoint } from '../ import { pipeline } from 'stream/promises' import { readableFromWeb } from '../utils/webStream' import { log } from '../utils/log' +import { attachmentDisposition } from './download' /** * Stream an asset from Immich back to the client. @@ -104,8 +105,7 @@ export async function assetBuffer (req: IncomingShareRequest, res: Response, ass const playbackMime = useVideoPlayback ? (data.headers.get('content-type') || '').split(';')[0].trim() || undefined : undefined - const filename = encodeURI(getFilename(asset, servedSize, playbackMime)) - res.setHeader('Content-Disposition', `attachment; filename*=UTF-8''${filename}`) + res.setHeader('Content-Disposition', attachmentDisposition(getFilename(asset, servedSize, playbackMime))) } } headerList.forEach(header => { diff --git a/app/src/stream/download.ts b/app/src/stream/download.ts index 09f7591..40ca581 100644 --- a/app/src/stream/download.ts +++ b/app/src/stream/download.ts @@ -131,9 +131,7 @@ export async function downloadAssets (res: Response, share: SharedLink, assets: function startZipResponse (res: Response, share: SharedLink, archive: Archiver) { res.setHeader('Content-Type', 'application/zip') - let filename = (sanitize(title(share)) || 'photos') + '.zip' - filename = encodeURI(filename) - res.setHeader('Content-Disposition', `attachment; filename*=UTF-8''${filename}`) + res.setHeader('Content-Disposition', attachmentDisposition((sanitize(title(share)) || 'photos') + '.zip')) // Hint to intermediate proxies (Nginx, etc.) not to buffer this response. res.setHeader('X-Accel-Buffering', 'no') res.setHeader('Cache-Control', 'no-store') @@ -246,6 +244,14 @@ function enrichFromHeaders (asset: Asset, response: globalThis.Response): Asset } } +export function attachmentDisposition (filename: string): string { + // encodeURI leaves `,` and `;` raw, which split the header: Chrome reads a + // comma as a second Content-Disposition and refuses the download. + const encoded = encodeURIComponent(filename) + .replace(/['()*]/g, c => '%' + c.charCodeAt(0).toString(16).toUpperCase()) + return `attachment; filename*=UTF-8''${encoded}` +} + /** * Extract a filename from a `Content-Disposition` header. Prefers the RFC 5987 * `filename*=UTF-8''...` form (percent-decoded) over the plain `filename=`. diff --git a/app/tests/contentDisposition.test.ts b/app/tests/contentDisposition.test.ts index 53803fc..78b9547 100644 --- a/app/tests/contentDisposition.test.ts +++ b/app/tests/contentDisposition.test.ts @@ -1,5 +1,5 @@ import { describe, it, expect } from 'vitest' -import { filenameFromContentDisposition } from '../src/stream/download' +import { attachmentDisposition, filenameFromContentDisposition } from '../src/stream/download' // Used by the zip download path to recover the real filename for album grid // assets (which arrive without originalFileName) from the /original response. @@ -23,3 +23,19 @@ describe('filenameFromContentDisposition', () => { expect(filenameFromContentDisposition(null)).toBeUndefined() }) }) + +describe('attachmentDisposition', () => { + it('percent-encodes commas and semicolons so the header stays a single value', () => { + const header = attachmentDisposition('Yellow Aster Butte, WA; day 1.zip') + expect(header).toBe("attachment; filename*=UTF-8''Yellow%20Aster%20Butte%2C%20WA%3B%20day%201.zip") + }) + + it('percent-encodes the characters RFC 5987 excludes from attr-char', () => { + expect(attachmentDisposition("it's (1)*.jpg")).toBe("attachment; filename*=UTF-8''it%27s%20%281%29%2A.jpg") + }) + + it('round-trips through filenameFromContentDisposition', () => { + const name = "Photo été, #2 & 'friends' (100%).jpg" + expect(filenameFromContentDisposition(attachmentDisposition(name))).toBe(name) + }) +})