Skip to content

Commit

Permalink
http2: fix mapToHeaders() with single string value
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Anatoli Papirovski <[email protected]>
  • Loading branch information
jinwoo authored and gibfahn committed Oct 31, 2017
1 parent 568d614 commit 2c2b658
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/internal/http2/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,13 +403,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))
Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-http2-util-headers-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ const {
);
}

{
// Arrays containing a single set-cookie value are handled correctly
// (https://github.com/nodejs/node/issues/16452)
const 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,
Expand Down

0 comments on commit 2c2b658

Please sign in to comment.