Skip to content

fix: a fileFilter or filename function that throws crashes the server - #1503

Open
yxshee wants to merge 1 commit into
expressjs:mainfrom
yxshee:fix/thrown-callback-crash
Open

yxshee wants to merge 1 commit into
expressjs:mainfrom
yxshee:fix/thrown-callback-crash

Conversation

@yxshee

@yxshee yxshee commented Nov 3, 2026

Copy link
Copy Markdown

Closes #904

fileFilter and storage._handleFile are called from busboy's 'file' event, which fires while busboy is processing a chunk piped from req. On Node 22 and earlier nothing on that stack catches exceptions, so if the filter, or a DiskStorage destination / filename function, throws instead of calling back with an error, the exception is uncaught and the process exits. Express can't help, because by then the middleware call has already returned.

Node 24 changed Readable.prototype.pipe() to catch a throw from dest.write() and destroy the destination with it (nodejs/node#55270), so there the error already reaches next(err) through busboy's 'error' event. multer supports Node >= 10.16 and Node 22 is still an LTS line, so this fixes the crash there and gives every supported version the same behavior instead of depending on the Node version.

The usual way to hit it is code that trusts the client-supplied name:

const storage = multer.diskStorage({
  destination: 'uploads/',
  filename (req, file, cb) {
    // TypeError for a file named "README" with no extension
    cb(null, file.originalname.match(/\.(\w+)$/)[1] + '-' + Date.now())
  }
})

On Node 22, one upload of a file without an extension brings the server down. In #904 @jonchurch suggested catching it in the 'file' handler and aborting with the error, and that is what this does. A throwing limits function already goes to next(err) the same way (test/limits-function.js).

The change:

  • The fileFilter call and the storage._handleFile call are each wrapped in try/catch. A synchronous throw is handled as if the error had been passed to the callback, so it goes through the existing paths: the placeholder is removed, pending writes settle, files already stored for the request are removed, and next(err) is called once.
  • If the middleware has already called next() when the exception arrives, it is rethrown unchanged. At that point it can come from the application's own handler (a storage engine that calls back synchronously), and multer should not swallow it or clean up files the route is using.
  • The two callbacks became named functions (onFileFiltered, onFileStored) so they could be called from the catch. Their bodies are unchanged, which keeps the diff to the call sites.

A destination function that calls back asynchronously and then has filename throw is still outside multer's stack, so it is not covered; that has to be caught in the application.

Tests, all failing on main under Node 22 with an uncaught exception and passing with the change (on Node 24 and later they already pass on main, because of the pipe() change):

  • test/file-filter.js: a filter that throws reaches the callback with its error and req.file stays undefined
  • test/disk-storage.js: a throwing destination function, and a filename function that throws a TypeError on a name with no extension (the upload dir stays empty)
  • test/disk-storage.js: with .any(), a filter that throws on the second file still removes the first file, which was already written to disk

npm test passes on Node 22.23.4 and 26.7.0, and npm run lint passes.

This touches the same fileFilter callback as #1491, but not the same lines; whichever lands second needs a trivial rebase.

fileFilter and storage._handleFile run inside busboy's 'file' event, so
an exception thrown there (or in a DiskStorage destination/filename
function) was never caught and crashed the process. Treat a synchronous
throw like an error passed to the callback, the same way a throwing
limits function is already handled.

Refs expressjs#904
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Multer doesn't seem to be forwarding error to Express

1 participant