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 <[email protected]>
Reviewed-By: Marco Ippolito <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
ArsalanDotMe authored and RafaelGSS committed Dec 15, 2023
1 parent 3fb7fc9 commit 7f95926
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 @@ -600,7 +600,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 7f95926

Please sign in to comment.