Skip to content

fix: accept files exactly at fileSize limit#1382

Open
abhu85 wants to merge 1 commit intoexpressjs:mainfrom
abhu85:fix/accept-files-at-exact-limit
Open

fix: accept files exactly at fileSize limit#1382
abhu85 wants to merge 1 commit intoexpressjs:mainfrom
abhu85:fix/accept-files-at-exact-limit

Conversation

@abhu85
Copy link
Copy Markdown

@abhu85 abhu85 commented Mar 18, 2026

Summary

Fixes #1348

Busboy emits the 'limit' event when fileSize equals limits.fileSize, causing multer to reject files that are exactly at the configured limit. This is an off-by-one error - files at the limit should be accepted; only files exceeding the limit should be rejected.

Root Cause

In busboy's multipart.js:

if (fileSize === fileSizeLimit) {
  this._fileStream.emit('limit');
  // ...
}

The 'limit' event fires when the file size equals the limit, not when it exceeds it. This is arguably correct behavior for busboy (it can't know if more bytes are coming), but multer should handle this case gracefully.

Solution

Pass fileSize + 1 to busboy so that files at the exact limit don't trigger the limit event. Files 1 byte over the configured limit will still trigger the event correctly.

var busboyLimits = limits
if (limits && typeof limits.fileSize === 'number') {
  busboyLimits = Object.assign({}, limits, { fileSize: limits.fileSize + 1 })
}

Test Plan

  • Added test: file exactly at limit is accepted
  • Added test: file 1 byte over limit is rejected
  • Added test: empty file accepted when limit is 0
  • Added test: non-empty file rejected when limit is 0
  • All existing tests pass (76 total)

Busboy emits the 'limit' event when fileSize equals limits.fileSize,
causing multer to reject files that are exactly at the limit. Files
at the limit should be accepted; only files exceeding the limit should
be rejected.

The fix passes fileSize + 1 to busboy so that files at the exact limit
don't trigger the limit event. Files 1 byte over the limit will still
trigger the event correctly.

Added tests verifying:
- File exactly at limit is accepted
- File 1 byte over limit is rejected
- Empty file accepted when limit is 0
- Non-empty file rejected when limit is 0

Fixes expressjs#1348
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 throws "File too large" error if file size of exact "limits.fileSize" bytes provided.

1 participant