Skip to content

fix: prevent process crash when fileFilter calls its callback more than once - #1427

Merged
UlisesGascon merged 3 commits into
expressjs:mainfrom
spokodev:fix/fields-double-callback-crash
Aug 25, 2026
Merged

UlisesGascon merged 3 commits into
expressjs:mainfrom
spokodev:fix/fields-double-callback-crash

Conversation

@spokodev

@spokodev spokodev commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Fixes #950.

With upload.fields(), if a fileFilter invokes its callback more than once and does so asynchronously, Multer crashes the whole Node process with an uncaught exception:

TypeError: Cannot read properties of undefined (reading 'length')
    at FileAppender.removePlaceholder (lib/file-appender.js)

A placeholder is inserted synchronously when the file event fires, then removed asynchronously from the fileFilter callback. When the callback runs twice, removePlaceholder runs twice for the same field: the first call sees length 1 and deletes req.files[fieldname]; the second call reads .length of 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 OBJECT strategy (upload.fields()) is affected; the ARRAY strategy (upload.array()) already tolerates a double callback because arrayRemove no-ops on a missing entry.

Fix

Break out of the OBJECT branch when the field entry is already gone, matching the null-safety the ARRAY branch already has. The single-callback path and behavior are unchanged.

Testing

Added a test to test/file-filter.js: a .fields() parser whose fileFilter calls the callback twice via setImmediate (the asynchronous case). It crashes the process on the current code and passes with the fix. Full suite stays green (81 passing).

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.
@MohammedAlkindi

Copy link
Copy Markdown
Contributor

The guard stops the crash, but the same double callback still silently drops an accepted file.

removePlaceholder is not idempotent under the OBJECT strategy. With .fields([{ name: 'docs', maxCount: 2 }]), two files on docs, and a filter that calls cb(null, false) twice for the second one, it runs twice for a single placeholder. The first call takes the arrayRemove branch and leaves one real file behind. The second sees length === 1 and runs delete this.req.files[placeholder.fieldname] (lib/file-appender.js:47-48), dropping that stored file. Nothing is raised, so the caller just sees the field gone.

The length === 1 branch deletes the whole field without checking that the placeholder being removed is the element still in it. Deleting only once the array is actually empty covers both cases:

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>
@spokodev

Copy link
Copy Markdown
Contributor Author

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: .fields([{ name: 'docs', maxCount: 2 }]) with a duplicate cb(null, false) on the second file, asserting the first accepted file survives. It failed on the previous commit and passes now; full suite is green (82 passing) and standard is clean.

@UlisesGascon UlisesGascon self-assigned this Aug 24, 2026
@UlisesGascon UlisesGascon changed the title fix: prevent process crash when fileFilter calls its callback more than once (fixes #950) fix: prevent process crash when fileFilter calls its callback more than once Aug 25, 2026
@UlisesGascon
UlisesGascon merged commit f8e759f into expressjs:main Aug 25, 2026
28 of 40 checks passed
@UlisesGascon UlisesGascon mentioned this pull request Aug 25, 2026
YasharF added a commit to sahat/hackathon-starter that referenced this pull request Aug 28, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Length is undefined in "if (this.req.files[placeholder.fieldname].length === 1)" when uploading file with invalid extension

3 participants