Skip to content

Commit 0092358

Browse files
ArsalanDotMerichardlau
authored andcommitted
http: handle multi-value content-disposition header
Headers in nodejs can be arrays and current workaround for content-disposition header do not take this into account. This change fixes that and makes sure array values are handled properly. PR-URL: #50977 Reviewed-By: Paolo Insogna <[email protected]> Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent c2e6a8f commit 0092358

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/_http_outgoing.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,14 @@ function processHeader(self, state, key, value, validate) {
552552
// https://www.rfc-editor.org/rfc/rfc6266#section-4.3
553553
// Refs: https://github.com/nodejs/node/pull/46528
554554
if (isContentDispositionField(key) && self._contentLength) {
555-
value = Buffer.from(value, 'latin1');
555+
// The value could be an array here
556+
if (ArrayIsArray(value)) {
557+
for (let i = 0; i < value.length; i++) {
558+
value[i] = Buffer.from(value[i], 'latin1');
559+
}
560+
} else {
561+
value = Buffer.from(value, 'latin1');
562+
}
556563
}
557564

558565
if (ArrayIsArray(value)) {

test/parallel/test-http-server-non-utf8-header.js

+21
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,27 @@ const nonUtf8ToLatin1 = Buffer.from(nonUtf8Header).toString('latin1');
2626
}));
2727
}
2828

29+
{
30+
// Test multi-value header
31+
const server = http.createServer(common.mustCall((req, res) => {
32+
res.writeHead(200, [
33+
'content-disposition',
34+
[Buffer.from(nonUtf8Header).toString('binary')],
35+
]);
36+
res.end('hello');
37+
}));
38+
39+
server.listen(0, common.mustCall(() => {
40+
http.get({ port: server.address().port }, (res) => {
41+
assert.strictEqual(res.statusCode, 200);
42+
assert.strictEqual(res.headers['content-disposition'], nonUtf8ToLatin1);
43+
res.resume().on('end', common.mustCall(() => {
44+
server.close();
45+
}));
46+
});
47+
}));
48+
}
49+
2950
{
3051
const server = http.createServer(common.mustCall((req, res) => {
3152
res.writeHead(200, [

0 commit comments

Comments
 (0)