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

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

‎lib/internal/http2/util.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,13 +403,14 @@ function mapToHeaders(map,
403403
if(typeofkey==='symbol'||value===undefined||!key)
404404
continue;
405405
key=String(key).toLowerCase();
406-
constisArray=Array.isArray(value);
406+
letisArray=Array.isArray(value);
407407
if(isArray){
408408
switch(value.length){
409409
case0:
410410
continue;
411411
case1:
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‎

Lines changed: 12 additions & 0 deletions
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+
constheaders={
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)