mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-04 06:54:28 +00:00
fix: use renamed asset name in download Content-Disposition (#286)
* fix(core): use renamed asset name in download Content-Disposition
Downloads of renamed assets (e.g. test.png renamed to foo.png) previously
saved under the storage-key basename because the presigned URLs / local
file handler only set Content-Disposition: attachment without a filename,
and the HTML download attribute is ignored for cross-origin URLs.
- Add buildContentDisposition() helper that emits a sanitized
'attachment; filename=...; filename*=UTF-8...' header (RFC 5987) so
non-ASCII names survive.
- Thread an optional filename through S3Service.presign():
* S3 backend passes it via contentDisposition (response-content-disposition)
* Local backend carries it as a ?filename= query param consumed by the
/files serveStatic onFound handler
- getDownloadLinks / getDownloadUrl now pass the renamed asset name from
the DB, so all download flows (file browser, viewers, breadcrumb, share)
save files under the current asset name.
* fix(core): keep Content-Disposition header ASCII for non-ASCII names
Bun's Headers API rejects header values containing non-ASCII bytes, so a
download of a renamed file with a non-ASCII name (e.g. 幻境边界.pptx) threw
'TypeError: Header Content-Disposition has invalid value' on the local
storage backend.
Per RFC 5987, non-ASCII names belong only in the percent-encoded
filename* parameter. buildContentDisposition now emits the quoted
filename="..." fallback only when the name is pure ASCII; otherwise the
header carries just 'attachment; filename*=UTF-8''...' (all-ASCII, accepted
by Bun/Node Headers and all modern browsers).
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { LocalStorageService, S3StorageService } from './s3'
|
||||
import { buildContentDisposition, LocalStorageService, S3StorageService } from './s3'
|
||||
|
||||
// Mock Bun S3Client
|
||||
const s3ClientConstructorSpy = vi.fn()
|
||||
@@ -140,6 +140,32 @@ describe('S3Service implementations', () => {
|
||||
)
|
||||
})
|
||||
|
||||
it('should set contentDisposition with the filename when download filename is provided', async () => {
|
||||
const s3 = new S3StorageService('http://localhost:9000', 'key', 'secret', 'test-bucket')
|
||||
await s3.presign('bucket', 'key', 'GET', true, 'foo.png')
|
||||
|
||||
expect(s3PresignSpy).toHaveBeenCalledWith(
|
||||
'key',
|
||||
expect.objectContaining({
|
||||
contentDisposition: 'attachment; filename="foo.png"; filename*=UTF-8\'\'foo.png',
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('should include an RFC 5987 filename* for non-ASCII filenames', async () => {
|
||||
const s3 = new S3StorageService('http://localhost:9000', 'key', 'secret', 'test-bucket')
|
||||
await s3.presign('bucket', 'key', 'GET', true, '报告.png')
|
||||
|
||||
expect(s3PresignSpy).toHaveBeenCalledWith(
|
||||
'key',
|
||||
expect.objectContaining({
|
||||
// Non-ASCII names are conveyed only via filename* (percent-encoded),
|
||||
// keeping the header value pure ASCII (RFC 7230).
|
||||
contentDisposition: "attachment; filename*=UTF-8''%E6%8A%A5%E5%91%8A.png",
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('should not cache download presign URLs', async () => {
|
||||
const s3 = new S3StorageService('http://localhost:9000', 'key', 'secret', 'test-bucket')
|
||||
|
||||
@@ -248,6 +274,18 @@ describe('S3Service implementations', () => {
|
||||
expect(url).toBe('http://localhost:3000/files/my-bucket/dir/file.txt?download=1')
|
||||
})
|
||||
|
||||
it('should include an encoded filename in the local download URL', async () => {
|
||||
const url = await localS3.presign('my-bucket', 'dir/file.txt', 'GET', true, 'foo bar.png')
|
||||
expect(url).toBe(
|
||||
'http://localhost:3000/files/my-bucket/dir/file.txt?download=1&filename=foo%20bar.png',
|
||||
)
|
||||
})
|
||||
|
||||
it('should not append a filename param when none is provided', async () => {
|
||||
const url = await localS3.presign('my-bucket', 'dir/file.txt', 'GET', true)
|
||||
expect(url).not.toContain('filename=')
|
||||
})
|
||||
|
||||
it('should throw an error for unsupported presign methods', async () => {
|
||||
await expect(localS3.presign('b', 'k', 'POST')).rejects.toThrow()
|
||||
})
|
||||
@@ -287,4 +325,46 @@ describe('S3Service implementations', () => {
|
||||
expect(url).not.toContain('4567')
|
||||
})
|
||||
})
|
||||
|
||||
describe('buildContentDisposition', () => {
|
||||
it('returns plain attachment when no filename is provided', () => {
|
||||
expect(buildContentDisposition()).toBe('attachment')
|
||||
expect(buildContentDisposition('')).toBe('attachment')
|
||||
expect(buildContentDisposition(null)).toBe('attachment')
|
||||
})
|
||||
|
||||
it('builds a disposition with quoted filename and RFC 5987 filename*', () => {
|
||||
expect(buildContentDisposition('foo.png')).toBe(
|
||||
'attachment; filename="foo.png"; filename*=UTF-8\'\'foo.png',
|
||||
)
|
||||
})
|
||||
|
||||
it('strips control characters and quotes to prevent header injection', () => {
|
||||
expect(buildContentDisposition('a\r\nb"c\\d')).toBe(
|
||||
'attachment; filename="abcd"; filename*=UTF-8\'\'abcd',
|
||||
)
|
||||
})
|
||||
|
||||
it('percent-encodes reserved RFC 5987 characters', () => {
|
||||
expect(buildContentDisposition("it's (final).png")).toBe(
|
||||
"attachment; filename=\"it's (final).png\"; filename*=UTF-8''it%27s%20%28final%29.png",
|
||||
)
|
||||
})
|
||||
|
||||
it('percent-encodes the * character in filename*', () => {
|
||||
expect(buildContentDisposition('foo*bar.png')).toBe(
|
||||
'attachment; filename="foo*bar.png"; filename*=UTF-8\'\'foo%2Abar.png',
|
||||
)
|
||||
})
|
||||
|
||||
it('omits the quoted filename fallback for non-ASCII names to keep the header ASCII', () => {
|
||||
expect(buildContentDisposition('幻境边界.pptx')).toBe(
|
||||
"attachment; filename*=UTF-8''%E5%B9%BB%E5%A2%83%E8%BE%B9%E7%95%8C.pptx",
|
||||
)
|
||||
// The whole header value must remain pure ASCII so Bun/Node Headers accept it
|
||||
expect(
|
||||
[...buildContentDisposition('幻境边界.pptx')].every((ch) => ch.charCodeAt(0) <= 0x7f),
|
||||
).toBe(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user