Skip to content
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

fix formdata arg validation #3529

Merged
merged 1 commit into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
44 changes: 20 additions & 24 deletions lib/web/fetch/formdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,20 @@ class FormData {
const prefix = 'FormData.append'
webidl.argumentLengthCheck(arguments, 2, prefix)

if (arguments.length === 3 && !(value instanceof Blob)) {
throw new TypeError(
"Failed to execute 'append' on 'FormData': parameter 2 is not of type 'Blob'"
)
name = webidl.converters.USVString(name)

if (arguments.length === 3 || value instanceof Blob) {
value = webidl.converters.Blob(value, prefix, 'value')

if (filename !== undefined) {
filename = webidl.converters.USVString(filename)
}
} else {
value = webidl.converters.USVString(value)
}

// 1. Let value be value if given; otherwise blobValue.

name = webidl.converters.USVString(name)
value = value instanceof Blob
? webidl.converters.Blob(value, prefix, 'value')
: webidl.converters.USVString(value)
filename = arguments.length === 3
? webidl.converters.USVString(filename)
: undefined

// 2. Let entry be the result of creating an entry with
// name, value, and filename if given.
const entry = makeEntry(name, value, filename)
Expand Down Expand Up @@ -123,25 +121,23 @@ class FormData {
const prefix = 'FormData.set'
webidl.argumentLengthCheck(arguments, 2, prefix)

if (arguments.length === 3 && !(value instanceof Blob)) {
throw new TypeError(
"Failed to execute 'set' on 'FormData': parameter 2 is not of type 'Blob'"
)
name = webidl.converters.USVString(name)

if (arguments.length === 3 || value instanceof Blob) {
value = webidl.converters.Blob(value, prefix, 'value')

if (filename !== undefined) {
filename = webidl.converters.USVString(filename)
}
} else {
value = webidl.converters.USVString(value)
Comment on lines +124 to +133
Copy link
Contributor

Choose a reason for hiding this comment

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

cant this be at line 140?

Copy link
Member Author

Choose a reason for hiding this comment

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

No. The webidl stuff occurs before the steps, but the step is also not needed because js doesn't have method overrides (hence the weird webidl here).

The set(name, value) and set(name, blobValue, filename) method steps are:

The first step is telling us to set value to "blobValue" if there were 3 arguments passed, otherwise do nothing. "value" is only ever "value" here.

}

// The set(name, value) and set(name, blobValue, filename) method steps
// are:

// 1. Let value be value if given; otherwise blobValue.

name = webidl.converters.USVString(name)
value = value instanceof Blob
? webidl.converters.Blob(value, prefix, 'name')
: webidl.converters.USVString(value)
filename = arguments.length === 3
? webidl.converters.USVString(filename)
: undefined

// 2. Let entry be the result of creating an entry with name, value, and
// filename if given.
const entry = makeEntry(name, value, filename)
Expand Down
16 changes: 16 additions & 0 deletions test/fetch/formdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ test('arg validation', () => {
})
})

test('set blob', () => {
const form = new FormData()

form.set('key', new Blob([]), undefined)
assert.strictEqual(form.get('key').name, 'blob')

form.set('key1', new Blob([]), null)
assert.strictEqual(form.get('key1').name, 'null')
})

test('append file', () => {
const form = new FormData()
form.set('asd', new File([], 'asd1', { type: 'text/plain' }), 'asd2')
Expand All @@ -106,6 +116,12 @@ test('append blob', async () => {
assert.strictEqual(await form.get('asd').text(), 'asd1')
form.delete('asd')
assert.strictEqual(form.get('asd'), null)

form.append('key', new Blob([]), undefined)
assert.strictEqual(form.get('key').name, 'blob')

form.append('key1', new Blob([]), null)
assert.strictEqual(form.get('key1').name, 'null')
})

test('append string', () => {
Expand Down
Loading