Skip to content

Commit

Permalink
fix formdata arg validation (#3529)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev authored Sep 1, 2024
1 parent 9639f18 commit e071406
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
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)
}

// 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

0 comments on commit e071406

Please sign in to comment.