Skip to content

Commit

Permalink
Use the on method instead of once to listen for busboy parser `…
Browse files Browse the repository at this point in the history
…error` events.
  • Loading branch information
krasivyy3954 committed May 27, 2022
1 parent 3ac5535 commit 7e100bf
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
### Patch

- Updated the [`typescript`](https://npm.im/typescript) dev dependency.
- In the function `processRequest` use the `on` method instead of `once` to listen for `error` events on the [`busboy`](https://npm.im/busboy) parser, as in edge cases the same parser could have multiple `error` events and all must be handled to prevent the Node.js process exiting with an error.
- Added a test for the function `processRequest` with a maliciously malformed multipart request.

## 14.0.0
Expand Down
10 changes: 5 additions & 5 deletions processRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ function processRequest(
* @param {Error} error Error instance.
*/
const exit = (error) => {
// None of the tested scenarios cause multiple calls of this function, but
// it’s still good to guard against it happening in case it’s possible now
// or in the future.
// coverage ignore next line
if (exitError) return;

exitError = error;
Expand Down Expand Up @@ -331,7 +327,11 @@ function processRequest(
upload.reject(createError(400, "File missing in the request."));
});

parser.once("error", exit);
// Use the `on` method instead of `once` as in edge cases the same parser
// could have multiple `error` events and all must be handled to prevent the
// Node.js process exiting with an error. One edge case is if there is a
// malformed part header as well as an unexpected end of the form.
parser.on("error", exit);

response.once("close", () => {
released = true;
Expand Down

0 comments on commit 7e100bf

Please sign in to comment.