Skip to content

Commit 216ec7b

Browse files
authored
Headers webidl errors (#3833)
* fixup! contructor * better error message * fixup! rm DOMString converter from ByteString * fixup
1 parent 28b10fa commit 216ec7b

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

lib/web/fetch/headers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ class Headers {
451451

452452
// 2. If init is given, then fill this with init.
453453
if (init !== undefined) {
454-
init = webidl.converters.HeadersInit(init, 'Headers contructor', 'init')
454+
init = webidl.converters.HeadersInit(init, 'Headers constructor', 'init')
455455
fill(this, init)
456456
}
457457
}

lib/web/fetch/webidl.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,12 +345,14 @@ webidl.recordConverter = function (keyConverter, valueConverter) {
345345
const keys = [...Object.getOwnPropertyNames(O), ...Object.getOwnPropertySymbols(O)]
346346

347347
for (const key of keys) {
348+
const keyName = webidl.util.Stringify(key)
349+
348350
// 1. Let typedKey be key converted to an IDL value of type K.
349-
const typedKey = keyConverter(key, prefix, argument)
351+
const typedKey = keyConverter(key, prefix, `Key ${keyName} in ${argument}`)
350352

351353
// 2. Let value be ? Get(O, key).
352354
// 3. Let typedValue be value converted to an IDL value of type V.
353-
const typedValue = valueConverter(O[key], prefix, argument)
355+
const typedValue = valueConverter(O[key], prefix, `${argument}[${keyName}]`)
354356

355357
// 4. Set result[typedKey] to typedValue.
356358
result[typedKey] = typedValue
@@ -501,8 +503,14 @@ webidl.converters.DOMString = function (V, prefix, argument, opts) {
501503
// https://webidl.spec.whatwg.org/#es-ByteString
502504
webidl.converters.ByteString = function (V, prefix, argument) {
503505
// 1. Let x be ? ToString(V).
504-
// Note: DOMString converter perform ? ToString(V)
505-
const x = webidl.converters.DOMString(V, prefix, argument)
506+
if (typeof V === 'symbol') {
507+
throw webidl.errors.exception({
508+
header: prefix,
509+
message: `${argument} is a symbol, which cannot be converted to a ByteString.`
510+
})
511+
}
512+
513+
const x = String(V)
506514

507515
// 2. If the value of any element of x is greater than
508516
// 255, then throw a TypeError.

test/fetch/headers.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ test('Headers initialization', async (t) => {
2626
throws(() => new Headers(['undici', 'fetch', 'fetch']), TypeError)
2727
throws(
2828
() => new Headers([0, 1, 2]),
29-
TypeError('Headers contructor: init[0] (0) is not iterable.')
29+
TypeError('Headers constructor: init[0] (0) is not iterable.')
3030
)
3131
})
3232

@@ -41,7 +41,7 @@ test('Headers initialization', async (t) => {
4141
const init = ['undici', 'fetch', 'fetch', 'undici']
4242
throws(
4343
() => new Headers(init),
44-
TypeError('Headers contructor: init[0] ("undici") is not iterable.')
44+
TypeError('Headers constructor: init[0] ("undici") is not iterable.')
4545
)
4646
})
4747
})
@@ -767,3 +767,16 @@ test('Invalid Symbol.iterators', (t) => {
767767
new Headers(obj) // eslint-disable-line no-new
768768
}, TypeError)
769769
})
770+
771+
// https://github.com/nodejs/undici/issues/3829
772+
test('Invalid key/value records passed to constructor (issue #3829)', (t) => {
773+
assert.throws(
774+
() => new Headers({ [Symbol('x-fake-header')]: '??' }),
775+
new TypeError('Headers constructor: Key Symbol(x-fake-header) in init is a symbol, which cannot be converted to a ByteString.')
776+
)
777+
778+
assert.throws(
779+
() => new Headers({ 'x-fake-header': Symbol('why is this here?') }),
780+
new TypeError('Headers constructor: init["x-fake-header"] is a symbol, which cannot be converted to a ByteString.')
781+
)
782+
})

test/webidl/errors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test('ByteString', (t) => {
1717
]) {
1818
assert.throws(
1919
() => new Headers()[method](name, value),
20-
new TypeError(`Headers.${method}: name is a symbol, which cannot be converted to a DOMString.`)
20+
new TypeError(`Headers.${method}: name is a symbol, which cannot be converted to a ByteString.`)
2121
)
2222
}
2323
})

0 commit comments

Comments
 (0)