From ba11876308a8abad6f2b1011c256541c44394c03 Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Thu, 25 Apr 2024 06:40:07 +0900 Subject: [PATCH 1/4] fix header cloning bug --- lib/web/fetch/headers.js | 20 +++++++++++++++++++- test/fetch/headers.js | 6 ++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/web/fetch/headers.js b/lib/web/fetch/headers.js index a1dab1050a8..5e38725d2f8 100644 --- a/lib/web/fetch/headers.js +++ b/lib/web/fetch/headers.js @@ -259,6 +259,24 @@ class HeadersList { return headers } + get entriesList () { + const headers = [] + + if (this[kHeadersMap].size !== 0) { + for (const { name, value } of this[kHeadersMap].values()) { + if (name === 'set-cookie') { + for (const cookie of this.cookies) { + headers.push(['set-cookie', cookie]) + } + } else { + headers.push([name, value]) + } + } + } + + return headers + } + // https://fetch.spec.whatwg.org/#convert-header-names-to-a-sorted-lowercase-set toSortedArray () { const size = this[kHeadersMap].size @@ -605,7 +623,7 @@ webidl.converters.HeadersInit = function (V) { // A work-around to ensure we send the properly-cased Headers when V is a Headers object. // Read https://github.com/nodejs/undici/pull/3159#issuecomment-2075537226 before touching, please. if (!util.types.isProxy(V) && kHeadersList in V && iterator === Headers.prototype.entries) { // Headers object - return V[kHeadersList].entries + return V[kHeadersList].entriesList } if (typeof iterator === 'function') { diff --git a/test/fetch/headers.js b/test/fetch/headers.js index 00798559e90..ac1183eeded 100644 --- a/test/fetch/headers.js +++ b/test/fetch/headers.js @@ -725,6 +725,12 @@ test('Headers.prototype.getSetCookie', async (t) => { assert.deepStrictEqual(res.headers.getSetCookie(), ['test=onetwo', 'test=onetwothree']) assert.ok('set-cookie' in entries) }) + + await t.test('When Headers are cloned, so are the cookies (Headers constructor)', () => { + const headers = new Headers([['set-cookie', 'a'], ['set-cookie', 'b']]) + + assert.deepStrictEqual([...headers], [...new Headers(headers)]) + }) }) test('When the value is updated, update the cache', (t) => { From 8f58c099c4a7cdfb47892d9cf3fb630098360f76 Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Thu, 25 Apr 2024 06:46:21 +0900 Subject: [PATCH 2/4] Apply suggestions from code review --- lib/web/fetch/headers.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/web/fetch/headers.js b/lib/web/fetch/headers.js index 5e38725d2f8..fed61acf485 100644 --- a/lib/web/fetch/headers.js +++ b/lib/web/fetch/headers.js @@ -263,8 +263,8 @@ class HeadersList { const headers = [] if (this[kHeadersMap].size !== 0) { - for (const { name, value } of this[kHeadersMap].values()) { - if (name === 'set-cookie') { + for (const { 0: lowerName, 1: { name, value } } of this[kHeadersMap].values()) { + if (lowerName === 'set-cookie') { for (const cookie of this.cookies) { headers.push(['set-cookie', cookie]) } From 154b105e045fe3d45d317785a1c53ed413254718 Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Thu, 25 Apr 2024 06:48:18 +0900 Subject: [PATCH 3/4] Apply suggestions from code review --- lib/web/fetch/headers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/web/fetch/headers.js b/lib/web/fetch/headers.js index fed61acf485..b82f5c1ee77 100644 --- a/lib/web/fetch/headers.js +++ b/lib/web/fetch/headers.js @@ -263,7 +263,7 @@ class HeadersList { const headers = [] if (this[kHeadersMap].size !== 0) { - for (const { 0: lowerName, 1: { name, value } } of this[kHeadersMap].values()) { + for (const { 0: lowerName, 1: { name, value } } of this[kHeadersMap]) { if (lowerName === 'set-cookie') { for (const cookie of this.cookies) { headers.push(['set-cookie', cookie]) From 9493e114fdb7a1fa08b3c43dce4e051c24e9ce23 Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Thu, 25 Apr 2024 06:50:55 +0900 Subject: [PATCH 4/4] Update headers.js --- lib/web/fetch/headers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/web/fetch/headers.js b/lib/web/fetch/headers.js index b82f5c1ee77..7f2f526a9cb 100644 --- a/lib/web/fetch/headers.js +++ b/lib/web/fetch/headers.js @@ -266,7 +266,7 @@ class HeadersList { for (const { 0: lowerName, 1: { name, value } } of this[kHeadersMap]) { if (lowerName === 'set-cookie') { for (const cookie of this.cookies) { - headers.push(['set-cookie', cookie]) + headers.push([name, cookie]) } } else { headers.push([name, value])