Skip to content

Commit 2c2b658

Browse files
jinwoogibfahn
authored andcommitted
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: #16452 PR-URL: #16458 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
1 parent 568d614 commit 2c2b658

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/internal/http2/util.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -403,13 +403,14 @@ function mapToHeaders(map,
403403
if (typeof key === 'symbol' || value === undefined || !key)
404404
continue;
405405
key = String(key).toLowerCase();
406-
const isArray = Array.isArray(value);
406+
let isArray = Array.isArray(value);
407407
if (isArray) {
408408
switch (value.length) {
409409
case 0:
410410
continue;
411411
case 1:
412412
value = String(value[0]);
413+
isArray = false;
413414
break;
414415
default:
415416
if (kSingleValueHeaders.has(key))

test/parallel/test-http2-util-headers-list.js

+12
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,18 @@ const {
154154
);
155155
}
156156

157+
{
158+
// Arrays containing a single set-cookie value are handled correctly
159+
// (https://github.com/nodejs/node/issues/16452)
160+
const headers = {
161+
'set-cookie': 'foo=bar'
162+
};
163+
assert.deepStrictEqual(
164+
mapToHeaders(headers),
165+
[ [ 'set-cookie', 'foo=bar', '' ].join('\0'), 1 ]
166+
);
167+
}
168+
157169
// The following are not allowed to have multiple values
158170
[
159171
HTTP2_HEADER_STATUS,

0 commit comments

Comments
 (0)