Skip to content

fix: do not consume maxCount for files skipped by fileFilter#1426

Open
Sagargupta16 wants to merge 1 commit into
expressjs:mainfrom
Sagargupta16:fix/filefilter-skipped-maxcount
Open

fix: do not consume maxCount for files skipped by fileFilter#1426
Sagargupta16 wants to merge 1 commit into
expressjs:mainfrom
Sagargupta16:fix/filefilter-skipped-maxcount

Conversation

@Sagargupta16

Copy link
Copy Markdown

Description

wrappedFileFilter decrements the field's remaining-count before the user's fileFilter runs:

filesLeft[file.fieldname] -= 1
fileFilter(req, file, cb)

So a file the user skips with cb(null, false) still consumes a slot. With .array('docs', 1), uploading a skipped file followed by an accepted file on the same field makes the second file fail with LIMIT_UNEXPECTED_FILE — even though zero files were stored. Per the README, cb(null, false) means the file is silently skipped (never added to req.files), so it must not count against maxCount. Fixes #1419.

Fix

Move the decrement inside the fileFilter callback and only decrement when the file is actually accepted:

fileFilter(req, file, function (err, includeFile) {
  if (!err && includeFile) filesLeft[file.fieldname] -= 1
  cb(err, includeFile)
})

This routes all callers (.single / .array / .fields) through one guard. Truly unexpected fields and accepted files beyond maxCount still error as before.

Test

Added a regression test in test/file-filter.js: a .array('docs', 1) upload where the filter skips the first same-field file and accepts the second — asserts exactly one file (tiny1.dat) is stored with no error. The test fails on main without the fix (LIMIT_UNEXPECTED_FILE) and passes with it; existing fileFilter tests remain green.

wrappedFileFilter decremented the field's remaining-count before the user
fileFilter ran, so a file skipped via cb(null, false) still consumed a
slot. With e.g. .array('docs', 1), a skipped file made the next accepted
file on the same field fail with LIMIT_UNEXPECTED_FILE, even though zero
files had been stored. Decrement only after the filter accepts the file.

Closes expressjs#1419
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.

fileFilter-skipped files consume same-field maxCount

1 participant