From 6eb29e9fc7c85d2346ce376be89eee7c39877bea Mon Sep 17 00:00:00 2001 From: Evgeny Soynov Date: Tue, 7 Dec 2021 12:36:08 +0100 Subject: [PATCH] Handle missing field names (#913) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes expressjs/multer#553 Without this fix fields without a name result in a "TypeError: Cannot read property 'length' of undefined" in the underlying append-field library. The current change allows getting an error from Multer that makes it possible to handle it in servers. Co-authored-by: Linus Unnebäck --- lib/multer-error.js | 3 ++- test/error-handling.js | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/multer-error.js b/lib/multer-error.js index 4c91d295..d56b00e8 100644 --- a/lib/multer-error.js +++ b/lib/multer-error.js @@ -7,7 +7,8 @@ var errorMessages = { LIMIT_FIELD_KEY: 'Field name too long', LIMIT_FIELD_VALUE: 'Field value too long', LIMIT_FIELD_COUNT: 'Too many fields', - LIMIT_UNEXPECTED_FILE: 'Unexpected field' + LIMIT_UNEXPECTED_FILE: 'Unexpected field', + MISSING_FIELD_NAME: 'Field name missing' } function MulterError (code, field) { diff --git a/test/error-handling.js b/test/error-handling.js index 450173fb..b462e16e 100644 --- a/test/error-handling.js +++ b/test/error-handling.js @@ -148,6 +148,33 @@ describe('Error Handling', function () { }) }) + it('should notify of missing field name', function (done) { + var req = new stream.PassThrough() + var storage = multer.memoryStorage() + var upload = multer({ storage: storage }).single('tiny0') + var boundary = 'AaB03x' + var body = [ + '--' + boundary, + 'Content-Disposition: form-data', + '', + 'test content', + '--' + boundary, + '' + ].join('\r\n') + + req.headers = { + 'content-type': 'multipart/form-data; boundary=' + boundary, + 'content-length': body.length + } + + req.end(body) + + upload(req, null, function (err) { + assert.strictEqual(err.code, 'MISSING_FIELD_NAME') + done() + }) + }) + it('should report errors from storage engines', function (done) { var storage = multer.memoryStorage()