Skip to content

Commit

Permalink
http: handle multi-value content-disposition header
Browse files Browse the repository at this point in the history
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 <paolo@cowtech.it>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
ArsalanDotMe authored and richardlau committed Mar 25, 2024
1 parent c7350c2 commit 4e38dee
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,14 @@ function processHeader(self, state, key, value, validate) {
// https://www.rfc-editor.org/rfc/rfc6266#section-4.3
// Refs: https://github.com/nodejs/node/pull/46528
if (isContentDispositionField(key) && self._contentLength) {
value = Buffer.from(value, 'latin1');
// The value could be an array here
if (ArrayIsArray(value)) {
for (let i = 0; i < value.length; i++) {
value[i] = Buffer.from(value[i], 'latin1');
}
} else {
value = Buffer.from(value, 'latin1');
}
}

if (ArrayIsArray(value)) {
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-http-server-non-utf8-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ const nonUtf8ToLatin1 = Buffer.from(nonUtf8Header).toString('latin1');
}));
}

{
// Test multi-value header
const server = http.createServer(common.mustCall((req, res) => {
res.writeHead(200, [
'content-disposition',
[Buffer.from(nonUtf8Header).toString('binary')],
]);
res.end('hello');
}));

server.listen(0, common.mustCall(() => {
http.get({ port: server.address().port }, (res) => {
assert.strictEqual(res.statusCode, 200);
assert.strictEqual(res.headers['content-disposition'], nonUtf8ToLatin1);
res.resume().on('end', common.mustCall(() => {
server.close();
}));
});
}));
}

{
const server = http.createServer(common.mustCall((req, res) => {
res.writeHead(200, [
Expand Down

0 comments on commit 4e38dee

Please sign in to comment.