From d4d0122a6c969a3a40d5e7c8bcb3ec7e689d31fc Mon Sep 17 00:00:00 2001 From: Khafra Date: Wed, 13 Nov 2024 14:54:19 -0500 Subject: [PATCH 1/4] fixup! contructor --- lib/web/fetch/headers.js | 2 +- test/fetch/headers.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/web/fetch/headers.js b/lib/web/fetch/headers.js index cc4d138140b..d782d2a2793 100644 --- a/lib/web/fetch/headers.js +++ b/lib/web/fetch/headers.js @@ -451,7 +451,7 @@ class Headers { // 2. If init is given, then fill this with init. if (init !== undefined) { - init = webidl.converters.HeadersInit(init, 'Headers contructor', 'init') + init = webidl.converters.HeadersInit(init, 'Headers constructor', 'init') fill(this, init) } } diff --git a/test/fetch/headers.js b/test/fetch/headers.js index 38dd6b16886..e9d3d497732 100644 --- a/test/fetch/headers.js +++ b/test/fetch/headers.js @@ -26,7 +26,7 @@ test('Headers initialization', async (t) => { throws(() => new Headers(['undici', 'fetch', 'fetch']), TypeError) throws( () => new Headers([0, 1, 2]), - TypeError('Headers contructor: init[0] (0) is not iterable.') + TypeError('Headers constructor: init[0] (0) is not iterable.') ) }) @@ -41,7 +41,7 @@ test('Headers initialization', async (t) => { const init = ['undici', 'fetch', 'fetch', 'undici'] throws( () => new Headers(init), - TypeError('Headers contructor: init[0] ("undici") is not iterable.') + TypeError('Headers constructor: init[0] ("undici") is not iterable.') ) }) }) From d245b29c47199b4e6a96d8dfa75a5e5b7714b726 Mon Sep 17 00:00:00 2001 From: Khafra Date: Wed, 13 Nov 2024 15:07:14 -0500 Subject: [PATCH 2/4] better error message --- lib/web/fetch/webidl.js | 6 ++++-- test/fetch/headers.js | 13 +++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/web/fetch/webidl.js b/lib/web/fetch/webidl.js index 23ba8f57df2..d8c184137bd 100644 --- a/lib/web/fetch/webidl.js +++ b/lib/web/fetch/webidl.js @@ -345,12 +345,14 @@ webidl.recordConverter = function (keyConverter, valueConverter) { const keys = [...Object.getOwnPropertyNames(O), ...Object.getOwnPropertySymbols(O)] for (const key of keys) { + const keyName = webidl.util.Stringify(key) + // 1. Let typedKey be key converted to an IDL value of type K. - const typedKey = keyConverter(key, prefix, argument) + const typedKey = keyConverter(key, prefix, `Key ${keyName} in ${argument}`) // 2. Let value be ? Get(O, key). // 3. Let typedValue be value converted to an IDL value of type V. - const typedValue = valueConverter(O[key], prefix, argument) + const typedValue = valueConverter(O[key], prefix, `${argument}[${keyName}]`) // 4. Set result[typedKey] to typedValue. result[typedKey] = typedValue diff --git a/test/fetch/headers.js b/test/fetch/headers.js index e9d3d497732..25223e8be86 100644 --- a/test/fetch/headers.js +++ b/test/fetch/headers.js @@ -767,3 +767,16 @@ test('Invalid Symbol.iterators', (t) => { new Headers(obj) // eslint-disable-line no-new }, TypeError) }) + +// https://github.com/nodejs/undici/issues/3829 +test('Invalid key/value records passed to constructor (issue #3829)', (t) => { + assert.throws( + () => new Headers({ [Symbol('x-fake-header')]: '??' }), + new TypeError('Headers constructor: Key Symbol(x-fake-header) in init is a symbol, which cannot be converted to a DOMString.') + ) + + assert.throws( + () => new Headers({ 'x-fake-header': Symbol('why is this here?') }), + new TypeError('Headers constructor: init["x-fake-header"] is a symbol, which cannot be converted to a DOMString.') + ) +}) From 55a100a2e3b2f8c1ebed4412f87023368160e5b0 Mon Sep 17 00:00:00 2001 From: Khafra Date: Wed, 13 Nov 2024 15:13:28 -0500 Subject: [PATCH 3/4] fixup! rm DOMString converter from ByteString --- lib/web/fetch/webidl.js | 10 ++++++++-- test/fetch/headers.js | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/web/fetch/webidl.js b/lib/web/fetch/webidl.js index d8c184137bd..46850e16f05 100644 --- a/lib/web/fetch/webidl.js +++ b/lib/web/fetch/webidl.js @@ -503,8 +503,14 @@ webidl.converters.DOMString = function (V, prefix, argument, opts) { // https://webidl.spec.whatwg.org/#es-ByteString webidl.converters.ByteString = function (V, prefix, argument) { // 1. Let x be ? ToString(V). - // Note: DOMString converter perform ? ToString(V) - const x = webidl.converters.DOMString(V, prefix, argument) + if (typeof V === 'symbol') { + throw webidl.errors.exception({ + header: prefix, + message: `${argument} is a symbol, which cannot be converted to a ByteString.` + }) + } + + const x = String(V) // 2. If the value of any element of x is greater than // 255, then throw a TypeError. diff --git a/test/fetch/headers.js b/test/fetch/headers.js index 25223e8be86..02d500d6f80 100644 --- a/test/fetch/headers.js +++ b/test/fetch/headers.js @@ -772,11 +772,11 @@ test('Invalid Symbol.iterators', (t) => { test('Invalid key/value records passed to constructor (issue #3829)', (t) => { assert.throws( () => new Headers({ [Symbol('x-fake-header')]: '??' }), - new TypeError('Headers constructor: Key Symbol(x-fake-header) in init is a symbol, which cannot be converted to a DOMString.') + new TypeError('Headers constructor: Key Symbol(x-fake-header) in init is a symbol, which cannot be converted to a ByteString.') ) assert.throws( () => new Headers({ 'x-fake-header': Symbol('why is this here?') }), - new TypeError('Headers constructor: init["x-fake-header"] is a symbol, which cannot be converted to a DOMString.') + new TypeError('Headers constructor: init["x-fake-header"] is a symbol, which cannot be converted to a ByteString.') ) }) From 646e7ad36ad7cd85108db6b30863d462536d4a6b Mon Sep 17 00:00:00 2001 From: Khafra Date: Wed, 13 Nov 2024 15:20:49 -0500 Subject: [PATCH 4/4] fixup --- test/webidl/errors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/webidl/errors.js b/test/webidl/errors.js index c5406c35325..a1e0873a2f3 100644 --- a/test/webidl/errors.js +++ b/test/webidl/errors.js @@ -17,7 +17,7 @@ test('ByteString', (t) => { ]) { assert.throws( () => new Headers()[method](name, value), - new TypeError(`Headers.${method}: name is a symbol, which cannot be converted to a DOMString.`) + new TypeError(`Headers.${method}: name is a symbol, which cannot be converted to a ByteString.`) ) } })