Skip to content

fix(content-disposition): allow empty filename without throwing error #466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/content-disposition/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ function format({
function createParams(filename?: string, fallback?: string | boolean) {
if (filename === undefined) return {}

if (filename === '') return { filename }

const params: Partial<
Record<string, string> & {
filename: string
Expand Down Expand Up @@ -138,6 +140,7 @@ export function contentDisposition(
}

function decodefield(str: string) {
if (str === '') return '' // empty string
const match = EXT_VALUE_REGEXP.exec(str)
if (!match) throw new TypeError('invalid extended field value')

Expand Down Expand Up @@ -198,11 +201,15 @@ export function parse(header: string): ContentDisposition {
names.push(key)

if (key.indexOf('*') + 1 === key.length) {
// decode extended value
key = key.slice(0, -1)

if (!value) {
params[key] = ''
continue
}

value = decodefield(value)

// overwrite existing value
params[key] = value
continue
}
Expand Down
1 change: 1 addition & 0 deletions packages/req/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"dependencies": {
"@tinyhttp/accepts": "workspace:*",
"@tinyhttp/req": "workspace:^",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you add the package to itself?

"@tinyhttp/type-is": "workspace:*",
"@tinyhttp/url": "workspace:*",
"header-range-parser": "^1.1.3"
Expand Down
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/modules/content-disposition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,3 +611,9 @@ describe('parse(string)', () => {
})
})
})

it('should work when file name is empty', () => {
const encoded = contentDisposition('', { type: 'inline' })
const parsed = parse(encoded)
expect(parsed.parameters.filename).toBe('')
})