Skip to content

Commit

Permalink
Handle missing field names (expressjs#913)
Browse files Browse the repository at this point in the history
Fixes expressjs#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 <linus@folkdatorn.se>
  • Loading branch information
2 people authored and himanshiLt committed Apr 26, 2022
1 parent a526617 commit 4d6f4ca
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/make-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ function makeMiddleware (setup) {

// handle text field data
busboy.on('field', function (fieldname, value, fieldnameTruncated, valueTruncated) {
if (fieldname == null) return abortWithCode('MISSING_FIELD_NAME')
if (fieldnameTruncated) return abortWithCode('LIMIT_FIELD_KEY')
if (valueTruncated) return abortWithCode('LIMIT_FIELD_VALUE', fieldname)

Expand Down
3 changes: 2 additions & 1 deletion lib/multer-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
27 changes: 27 additions & 0 deletions test/error-handling.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 4d6f4ca

Please sign in to comment.