Skip to content

fix: remove aborted uploads through the objects in req.files - #1502

Open
yxshee wants to merge 1 commit into
expressjs:mainfrom
yxshee:fix/stale-req-files-after-abort
Open

yxshee wants to merge 1 commit into
expressjs:mainfrom
yxshee:fix/stale-req-files-after-abort

Conversation

@yxshee

@yxshee yxshee commented Nov 2, 2026

Copy link
Copy Markdown

When an upload fails after some files were already stored (a later file hits LIMIT_FILE_SIZE, LIMIT_UNEXPECTED_FILE, a fileFilter error and so on), multer removes the stored files before calling next(err). With single() the error handler then sees req.file without its path (disk) or buffer (memory), because req.file is the same object multer passes to _removeFile.

With array(), fields() and any() that is not the case. replacePlaceholder copies the file's properties onto the placeholder in req.files, but uploadedFiles keeps the original object. So on error:

  • disk storage: req.files[i].path still points at a file that has just been unlinked
  • memory storage: req.files[i].buffer still holds the whole file, although _removeFile already "freed" it
  • err.storageErrors[i].file is a different object from the entry in req.files (the existing test for single() asserts err.storageErrors[0].file === req.file)

Repro (disk storage):

const upload = multer({ dest: 'uploads/' }).fields([{ name: 'a', maxCount: 1 }])
// send a file in `a`, then a file in `b`
// err.code === 'LIMIT_UNEXPECTED_FILE', uploads/ is empty,
// but req.files.a[0].path is still 'uploads/<name>'

The fix has replacePlaceholder return the object it stored in req.file / req.files, and the middleware tracks that object in uploadedFiles. Removal then goes through the same object the error handler sees, so all strategies behave like single() already does. The object passed to _removeFile still carries the info returned by _handleFile, so the existing _removeFile tests are unaffected.

Tests:

  • disk storage: fields() + LIMIT_UNEXPECTED_FILE leaves req.files.tiny0[0] without path/filename/destination, and the upload dir empty
  • memory storage: array(name, 1) + a second file leaves req.files[0].buffer undefined
  • error handling: err.storageErrors[0].file === req.files.tiny0[0] for fields()

All three fail on main and pass with the change; the full suite and standard pass.

When an upload fails after some files were stored, multer removes those
files, but array(), fields() and any() kept a copy of each file in
req.files. The copy kept its path (disk) or buffer (memory) after the file
was deleted, and err.storageErrors[].file pointed at a different object.
single() did not have this problem because req.file is the tracked object.

Track the object that req.files holds instead of a copy.
@NgoQuocViet2001

Copy link
Copy Markdown

I checked 1b2ce38 on Windows x64 / Node.js 24.18.0 with real HTTP multipart requests, covering .array(), .fields() and .any() with both disk and memory storage.

For each case, I sent the first file and waited for its storage callback to complete before sending a second file that fileFilter rejects. I checked both successful cleanup and a simulated error returned by _removeFile after calling the real cleanup implementation (12 cases total).

On base 2c380d0, the first file's entry in req.files retained its path or buffer in all 12 cases. On this head, those properties were cleared, _removeFile received the same object exposed in req.files, and storageErrors[0].file was that same object in the injected-error cases. Disk directories were empty, the original filter error was preserved, and removal and next(err) each ran once.

The full suite also passed: 174 passing, 2 pending; lint was clean.

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.

2 participants