From eb2af57c8f3fb79b08d7858e01b3ff5967a42656 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Tue, 24 Oct 2017 13:34:25 -0700 Subject: [PATCH 1/2] http2: fix mapToHeaders() with single string value This is for issue 16452. When 'set-cookie' header is set with an array that has only one string value, it's split into its individual characters. Fix by resetting `isArray` to false when the value is converted from an array to a string. Fixes: https://github.com/nodejs/node/issues/16452 --- lib/internal/http2/util.js | 3 ++- test/parallel/test-http2-util-headers-list.js | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/internal/http2/util.js b/lib/internal/http2/util.js index 4610be7e4d376e..11931b35f46451 100644 --- a/lib/internal/http2/util.js +++ b/lib/internal/http2/util.js @@ -401,13 +401,14 @@ function mapToHeaders(map, if (typeof key === 'symbol' || value === undefined || !key) continue; key = String(key).toLowerCase(); - const isArray = Array.isArray(value); + let isArray = Array.isArray(value); if (isArray) { switch (value.length) { case 0: continue; case 1: value = String(value[0]); + isArray = false; break; default: if (kSingleValueHeaders.has(key)) diff --git a/test/parallel/test-http2-util-headers-list.js b/test/parallel/test-http2-util-headers-list.js index 1884a22894d280..46803fe800083a 100644 --- a/test/parallel/test-http2-util-headers-list.js +++ b/test/parallel/test-http2-util-headers-list.js @@ -154,6 +154,17 @@ const { ); } +{ + // Arrays containing a single set-cookie value are handled correctly + // (issue #16452) + const headers = Object.create({}); + headers['set-cookie'] = ['foo=bar']; + assert.deepStrictEqual( + mapToHeaders(headers), + [ [ 'set-cookie', 'foo=bar', '' ].join('\0'), 1 ] + ); +} + // The following are not allowed to have multiple values [ HTTP2_HEADER_STATUS, From 4f6b1bc8f21d7902f8f01a910226d7f82615200c Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Wed, 25 Oct 2017 09:09:22 -0700 Subject: [PATCH 2/2] address review comments. - full url for the github issue - simplify the header creation in test --- test/parallel/test-http2-util-headers-list.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/parallel/test-http2-util-headers-list.js b/test/parallel/test-http2-util-headers-list.js index 46803fe800083a..f5e202538502eb 100644 --- a/test/parallel/test-http2-util-headers-list.js +++ b/test/parallel/test-http2-util-headers-list.js @@ -156,9 +156,10 @@ const { { // Arrays containing a single set-cookie value are handled correctly - // (issue #16452) - const headers = Object.create({}); - headers['set-cookie'] = ['foo=bar']; + // (https://github.com/nodejs/node/issues/16452) + const headers = { + 'set-cookie': 'foo=bar' + }; assert.deepStrictEqual( mapToHeaders(headers), [ [ 'set-cookie', 'foo=bar', '' ].join('\0'), 1 ]