Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a crash in multipartBodyParser if mapping files array #1819 #1820

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/plugins/multipartBodyParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,30 @@ function multipartBodyParser(options) {
if (req.params[f] && !override) {
return;
}
if (Array.isArray(files[f])) {
req.params[f] = [];
files[f].forEach(function forEach2(file) {
barrier.start('fs' + file.name);
fs.readFile(file.path, function readFile(
ex,
data
) {
barrier.done('fs' + file.name);
if (ex) {
return next(
new errors.InternalError(
ex,
'unable to read file' +
file.name
)
);
}
req.params[f].push(data);
return true;
});
});
}

barrier.start('fs' + f);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution @guoyibj!

I'm not super familiar with this piece of code. But it looks like we are trying to flatten the array here. This if condition doesn't look like it returns and the following barrier is still run against f. Is that intentional?

Would it be possible to flatten files first? I.E. convert [f1, f2, [f3, [f4]]] to [f1, f2, f3, f4] to avoid the conditional logic here?

fs.readFile(files[f].path, function readFile(ex, data) {
barrier.done('fs' + f);
Expand Down
8 changes: 8 additions & 0 deletions test/plugins/multipart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ describe('multipart parser', function() {
client.write(echoes + '\r\n');
client.write('--huff--');

client.write(
'Content-Disposition: form-data; name="echoes"; ' +
'filename="echoes2.txt"\r\n'
);
client.write('Content-Type: text/plain\r\n\r\n');
client.write(echoes + '\r\n');
client.write('--huff--');

client.end();
});

Expand Down