Repository navigation
fix: prevent process crash when fileFilter calls its callback more than once - #1427
Conversation
With upload.fields(), if a fileFilter invokes its callback more than once asynchronously, removePlaceholder runs twice for the same field: the first call deletes req.files[fieldname], the second reads .length of undefined and throws an uncaught TypeError that crashes the process. Break out when the field entry is already gone, matching the array strategy. Fixes expressjs#950.
|
The guard stops the crash, but the same double callback still silently drops an accepted file.
The case 'OBJECT': {
var files = this.req.files[placeholder.fieldname]
if (!files) break
arrayRemove(files, placeholder)
if (files.length === 0) delete this.req.files[placeholder.fieldname]
break
}Your new test still passes with that change, and the suite's failing set is unchanged either way: 67 passing / 13 failing on both sides, the 13 being pre-existing Windows path failures on my box rather than anything this PR touches. |
…twice Under the OBJECT strategy removePlaceholder deleted the whole field once its array reached length 1, assuming the single remaining element was the placeholder being removed. A duplicate fileFilter callback runs removePlaceholder twice for one placeholder, so the second call dropped a real accepted sibling file. Remove the element first and delete the field only when the array is actually empty. Co-authored-by: MohammedAlkindi <175210364+MohammedAlkindi@users.noreply.github.com>
|
Great catch, thanks @MohammedAlkindi. You're right that the crash guard left the data-loss in place. I applied your idempotent version of the OBJECT branch (arrayRemove first, delete the field only when the array is empty) and credited you as co-author on the commit. I also added a regression test: |
config/morgan.js: colors moved out of the token and into the format string, where morgan doesn't touch them Dependency Release Notes: Multer v2.3.0 - Fix [CVE-2026-77078](https://www.cve.org/CVERecord?id=CVE-2026-77078) ([GHSA-wc9g-mqfw-jrwm](GHSA-wc9g-mqfw-jrwm)) - Fix [CVE-2026-77037](https://www.cve.org/CVERecord?id=CVE-2026-77037) ([GHSA-qfvm-cv95-jqjf](GHSA-qfvm-cv95-jqjf)) - Fix [CVE-2026-77063](https://www.cve.org/CVERecord?id=CVE-2026-77063) ([GHSA-qvfw-j98x-7q72](GHSA-qvfw-j98x-7q72)) - Fix [CVE-2026-82333](https://www.cve.org/CVERecord?id=CVE-2026-82333) ([GHSA-535w-7cp7-47q4](GHSA-535w-7cp7-47q4)) - Add `MulterError` codes `INVALID_FIELD_NAME` and `STREAM_DESTROYED` - Add opt-in `limits.fieldArrayIndexLimit` to bound numeric array indexes in field names ([#1438](expressjs/multer#1438)) - Accept files whose size is exactly `limits.fileSize` ([#1407](expressjs/multer#1407)) - Preserve the caller's async context (`AsyncLocalStorage`) when calling `next()` ([#1124](expressjs/multer#1124)) - Decode WHATWG-escaped characters (`%0A`, `%0D`, `%22`) in `file.originalname` ([#1421](expressjs/multer#1421)) - Do not crash when `fileFilter` invokes its callback more than once ([#1427](expressjs/multer#1427)) - Use a fallback message for `MulterError` codes without a mapping ([#1448](expressjs/multer#1448)) - Docs: clarify `preservePath` and `parts`, use `crypto.randomBytes` in the `DiskStorage` example ([#1414](expressjs/multer#1414), [#1430](expressjs/multer#1430), [#1436](expressjs/multer#1436)) - Docs: add Indonesian, Japanese and Tamil translations and refresh all translations from the current README ([#1431](expressjs/multer#1431), [#1354](expressjs/multer#1354), [#1462](expressjs/multer#1462)) - Internal: run the test suite on Windows ([#1334](expressjs/multer#1334)) Morgan 1.12.0 - Security fix for [CVE-2026-15603](https://www.cve.org/CVERecord?id=CVE-2026-15603)([GHSA-jxfw-x594-9x9m](https://github.com/expressjs/morgan/security/advisories/GHSA-jxfw-x594-9x9m)) - Allow format functions to return objects for streams in `objectMode` - Respect the `NO_COLOR` environment variable in the `dev` format
Fixes #950.
With
upload.fields(), if afileFilterinvokes its callback more than once and does so asynchronously, Multer crashes the whole Node process with an uncaught exception:A placeholder is inserted synchronously when the file event fires, then removed asynchronously from the
fileFiltercallback. When the callback runs twice,removePlaceholderruns twice for the same field: the first call sees length 1 and deletesreq.files[fieldname]; the second call reads.lengthof the now-missing entry and throws on a later tick, where there is no surrounding try/catch, so the process dies. This has been reported repeatedly (#950, #1093, #1205).Only the
OBJECTstrategy (upload.fields()) is affected; theARRAYstrategy (upload.array()) already tolerates a double callback becausearrayRemoveno-ops on a missing entry.Fix
Break out of the
OBJECTbranch when the field entry is already gone, matching the null-safety theARRAYbranch already has. The single-callback path and behavior are unchanged.Testing
Added a test to
test/file-filter.js: a.fields()parser whosefileFiltercalls the callback twice viasetImmediate(the asynchronous case). It crashes the process on the current code and passes with the fix. Full suite stays green (81 passing).