Repository navigation
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #904
fileFilterandstorage._handleFileare called from busboy's'file'event, which fires while busboy is processing a chunk piped fromreq. On Node 22 and earlier nothing on that stack catches exceptions, so if the filter, or aDiskStoragedestination/filenamefunction, 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 fromdest.write()and destroy the destination with it (nodejs/node#55270), so there the error already reachesnext(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:
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 throwinglimitsfunction already goes tonext(err)the same way (test/limits-function.js).The change:
fileFiltercall and thestorage._handleFilecall are each wrapped intry/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, andnext(err)is called once.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.onFileFiltered,onFileStored) so they could be called from thecatch. Their bodies are unchanged, which keeps the diff to the call sites.A
destinationfunction that calls back asynchronously and then hasfilenamethrow is still outside multer's stack, so it is not covered; that has to be caught in the application.Tests, all failing on
mainunder Node 22 with an uncaught exception and passing with the change (on Node 24 and later they already pass onmain, because of thepipe()change):test/file-filter.js: a filter that throws reaches the callback with its error andreq.filestays undefinedtest/disk-storage.js: a throwingdestinationfunction, and afilenamefunction that throws aTypeErroron 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 disknpm testpasses on Node 22.23.4 and 26.7.0, andnpm run lintpasses.This touches the same
fileFiltercallback as #1491, but not the same lines; whichever lands second needs a trivial rebase.