Skip to content

Commit

Permalink
http: split set-cookie when using setHeaders
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-ippolito committed Feb 2, 2024
1 parent 68885d5 commit ff72f2f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -728,8 +728,23 @@ OutgoingMessage.prototype.setHeaders = function setHeaders(headers) {
throw new ERR_INVALID_ARG_TYPE('headers', ['Headers', 'Map'], headers);
}

for (const key of headers.keys()) {
this.setHeader(key, headers.get(key));
// Headers object joins multiple cookies with a comma when using
// the getter to retrieve the value,
// unless iterating over the headers directly.
// We also cannot safely split by comma.
// To avoid setHeader owerwriting the previous value we push
// set-cookie values in array and set them all at once.
const cookies = [];

for (const { 0: key, 1: value } of headers.entries()) {
if (key === 'set-cookie') {
cookies.push(value);
continue;
}
this.setHeader(key, value);
}
if (cookies.length) {
this.setHeader('set-cookie', cookies);
}

return this;
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-http-response-setheaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,25 @@ const assert = require('assert');
});
}));
}

{
const server = http.createServer({ requireHostHeader: false }, common.mustCall((req, res) => {
const headers = new Headers();
headers.append('Set-Cookie', 'a=b');
headers.append('Set-Cookie', 'c=d');
res.setHeaders(headers);
res.end();
}));

server.listen(0, common.mustCall(() => {
http.get({ port: server.address().port }, (res) => {
assert(Array.isArray(res.headers['set-cookie']));
assert.strictEqual(res.headers['set-cookie'].length, 2);
assert.strictEqual(res.headers['set-cookie'][0], 'a=b');
assert.strictEqual(res.headers['set-cookie'][1], 'c=d');
res.resume().on('end', common.mustCall(() => {
server.close();
}));
});
}));
}

0 comments on commit ff72f2f

Please sign in to comment.