Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b8e7a87

Browse filesBrowse files
marco-ippolitotargos
authored andcommitted
http: split set-cookie when using setHeaders
PR-URL: #51649 Fixes: #51599 Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Akhil Marsonya <akhil.marsonya27@gmail.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
1 parent e5c52a2 commit b8e7a87
Copy full SHA for b8e7a87

File tree

Expand file treeCollapse file tree

2 files changed

+64
-2
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+64
-2
lines changed
Open diff view settings
Collapse file

‎lib/_http_outgoing.js‎

Copy file name to clipboardExpand all lines: lib/_http_outgoing.js
+21-2Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -728,8 +728,27 @@ OutgoingMessage.prototype.setHeaders = function setHeaders(headers) {
728728
throw new ERR_INVALID_ARG_TYPE('headers', ['Headers', 'Map'], headers);
729729
}
730730

731-
for (const key of headers.keys()) {
732-
this.setHeader(key, headers.get(key));
731+
// Headers object joins multiple cookies with a comma when using
732+
// the getter to retrieve the value,
733+
// unless iterating over the headers directly.
734+
// We also cannot safely split by comma.
735+
// To avoid setHeader overwriting the previous value we push
736+
// set-cookie values in array and set them all at once.
737+
const cookies = [];
738+
739+
for (const { 0: key, 1: value } of headers) {
740+
if (key === 'set-cookie') {
741+
if (ArrayIsArray(value)) {
742+
cookies.push(...value);
743+
} else {
744+
cookies.push(value);
745+
}
746+
continue;
747+
}
748+
this.setHeader(key, value);
749+
}
750+
if (cookies.length) {
751+
this.setHeader('set-cookie', cookies);
733752
}
734753

735754
return this;
Collapse file

‎test/parallel/test-http-response-setheaders.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-http-response-setheaders.js
+43Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,46 @@ const assert = require('assert');
129129
});
130130
}));
131131
}
132+
133+
{
134+
const server = http.createServer({ requireHostHeader: false }, common.mustCall((req, res) => {
135+
const headers = new Headers();
136+
headers.append('Set-Cookie', 'a=b');
137+
headers.append('Set-Cookie', 'c=d');
138+
res.setHeaders(headers);
139+
res.end();
140+
}));
141+
142+
server.listen(0, common.mustCall(() => {
143+
http.get({ port: server.address().port }, (res) => {
144+
assert(Array.isArray(res.headers['set-cookie']));
145+
assert.strictEqual(res.headers['set-cookie'].length, 2);
146+
assert.strictEqual(res.headers['set-cookie'][0], 'a=b');
147+
assert.strictEqual(res.headers['set-cookie'][1], 'c=d');
148+
res.resume().on('end', common.mustCall(() => {
149+
server.close();
150+
}));
151+
});
152+
}));
153+
}
154+
155+
{
156+
const server = http.createServer({ requireHostHeader: false }, common.mustCall((req, res) => {
157+
const headers = new Map();
158+
headers.set('Set-Cookie', ['a=b', 'c=d']);
159+
res.setHeaders(headers);
160+
res.end();
161+
}));
162+
163+
server.listen(0, common.mustCall(() => {
164+
http.get({ port: server.address().port }, (res) => {
165+
assert(Array.isArray(res.headers['set-cookie']));
166+
assert.strictEqual(res.headers['set-cookie'].length, 2);
167+
assert.strictEqual(res.headers['set-cookie'][0], 'a=b');
168+
assert.strictEqual(res.headers['set-cookie'][1], 'c=d');
169+
res.resume().on('end', common.mustCall(() => {
170+
server.close();
171+
}));
172+
});
173+
}));
174+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.