Skip to content

Commit 6b2cef6

Browse files
committed
http: append Cookie header values with semicolon
Previously, separate incoming Cookie headers would be concatenated with a comma, which can cause confusion in userland code when it comes to parsing the final Cookie header value. This commit concatenates using a semicolon instead. Fixes: nodejs#11256 PR-URL: nodejs#11259 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 8243ca0 commit 6b2cef6

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

lib/_http_incoming.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ function matchKnownFields(field) {
194194
case 'Set-Cookie':
195195
case 'set-cookie':
196196
return '\u0001';
197+
case 'Cookie':
198+
case 'cookie':
199+
return '\u0002cookie';
197200
// The fields below are not used in _addHeaderLine(), but they are common
198201
// headers where we can avoid toLowerCase() if the mixed or lower case
199202
// versions match the first time through.
@@ -215,9 +218,6 @@ function matchKnownFields(field) {
215218
case 'Content-Encoding':
216219
case 'content-encoding':
217220
return '\u0000content-encoding';
218-
case 'Cookie':
219-
case 'cookie':
220-
return '\u0000cookie';
221221
case 'Origin':
222222
case 'origin':
223223
return '\u0000origin';
@@ -263,18 +263,20 @@ function matchKnownFields(field) {
263263
//
264264
// Per RFC2616, section 4.2 it is acceptable to join multiple instances of the
265265
// same header with a ', ' if the header in question supports specification of
266-
// multiple values this way. If not, we declare the first instance the winner
267-
// and drop the second. Extended header fields (those beginning with 'x-') are
268-
// always joined.
266+
// multiple values this way. The one exception to this is the Cookie header,
267+
// which has multiple values joined with a '; ' instead. If a header's values
268+
// cannot be joined in either of these ways, we declare the first instance the
269+
// winner and drop the second. Extended header fields (those beginning with
270+
// 'x-') are always joined.
269271
IncomingMessage.prototype._addHeaderLine = _addHeaderLine;
270272
function _addHeaderLine(field, value, dest) {
271273
field = matchKnownFields(field);
272274
var flag = field.charCodeAt(0);
273-
if (flag === 0) {
275+
if (flag === 0 || flag === 2) {
274276
field = field.slice(1);
275-
// Make comma-separated list
277+
// Make a delimited list
276278
if (typeof dest[field] === 'string') {
277-
dest[field] += ', ' + value;
279+
dest[field] += (flag === 0 ? ', ' : '; ') + value;
278280
} else {
279281
dest[field] = value;
280282
}

test/parallel/test-http-incoming-matchKnownFields.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ const IncomingMessage = require('http').IncomingMessage;
66
function checkDest(field, result, value) {
77
const dest = {};
88

9-
if (value) dest[field] = 'test';
109
const incomingMessage = new IncomingMessage(field);
1110
// dest is changed by IncomingMessage._addHeaderLine
11+
if (value)
12+
incomingMessage._addHeaderLine(field, 'test', dest);
1213
incomingMessage._addHeaderLine(field, value, dest);
1314
assert.deepStrictEqual(dest, result);
1415
}
@@ -49,7 +50,7 @@ checkDest('age', {age: 'test'}, 'value');
4950
checkDest('Expires', {expires: undefined});
5051
checkDest('expires', {expires: 'test'}, 'value');
5152
checkDest('Set-Cookie', {'set-cookie': [undefined]});
52-
checkDest('set-cookie', {'set-cookie': [undefined]});
53+
checkDest('set-cookie', {'set-cookie': ['test', 'value']}, 'value');
5354
checkDest('Transfer-Encoding', {'transfer-encoding': undefined});
5455
checkDest('transfer-encoding', {'transfer-encoding': 'test, value'}, 'value');
5556
checkDest('Date', {date: undefined});
@@ -64,8 +65,8 @@ checkDest('Vary', {vary: undefined});
6465
checkDest('vary', {vary: 'test, value'}, 'value');
6566
checkDest('Content-Encoding', {'content-encoding': undefined}, undefined);
6667
checkDest('content-encoding', {'content-encoding': 'test, value'}, 'value');
67-
checkDest('Cookies', {cookies: undefined});
68-
checkDest('cookies', {cookies: 'test, value'}, 'value');
68+
checkDest('Cookie', {cookie: undefined});
69+
checkDest('cookie', {cookie: 'test; value'}, 'value');
6970
checkDest('Origin', {origin: undefined});
7071
checkDest('origin', {origin: 'test, value'}, 'value');
7172
checkDest('Upgrade', {upgrade: undefined});
@@ -88,3 +89,5 @@ checkDest('X-Forwarded-Host', {'x-forwarded-host': undefined});
8889
checkDest('x-forwarded-host', {'x-forwarded-host': 'test, value'}, 'value');
8990
checkDest('X-Forwarded-Proto', {'x-forwarded-proto': undefined});
9091
checkDest('x-forwarded-proto', {'x-forwarded-proto': 'test, value'}, 'value');
92+
checkDest('X-Foo', {'x-foo': undefined});
93+
checkDest('x-foo', {'x-foo': 'test, value'}, 'value');

test/parallel/test-http-server-multiheaders2.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ const srv = http.createServer(function(req, res) {
5454
'foo', 'header parsed incorrectly: ' + header);
5555
});
5656
multipleAllowed.forEach(function(header) {
57+
const sep = (header.toLowerCase() === 'cookie' ? '; ' : ', ');
5758
assert.strictEqual(req.headers[header.toLowerCase()],
58-
'foo, bar', 'header parsed incorrectly: ' + header);
59+
'foo' + sep + 'bar',
60+
'header parsed incorrectly: ' + header);
5961
});
6062

6163
res.writeHead(200, {'Content-Type': 'text/plain'});

0 commit comments

Comments
 (0)