Skip to content

Commit 8f6279b

Browse files
committed
fix: handle two busboy error events
1 parent 8ec534f commit 8f6279b

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

lib/make-middleware.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function makeMiddleware (setup) {
4141
if (isDone) return
4242
isDone = true
4343
req.unpipe(busboy)
44-
process.nextTick(() => {
44+
setImmediate(() => {
4545
busboy.removeAllListeners()
4646
})
4747
next(err)

test/express-integration.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,50 @@ describe('Express Integration', function () {
150150
req.write(body)
151151
req.end()
152152
})
153+
154+
it('should not crash on malformed request that causes two errors to be emitted by busboy', function (done) {
155+
var upload = multer()
156+
157+
app.post('/upload2', upload.single('file'), function (req, res) {
158+
res.status(500).end('Request should not be processed')
159+
})
160+
161+
app.use(function (err, req, res, next) {
162+
assert.strictEqual(err.message, 'Malformed part header')
163+
res.status(200).end('Correct error')
164+
})
165+
166+
var boundary = 'AaB03x'
167+
// this payload causes two errors to be emitted by busboy: `Malformed part header` and `Unexpected end of form`
168+
var body = [
169+
'--' + boundary,
170+
'Content-Disposition: form-data; name="file"; filename="test.txt"',
171+
'Content-Type: text/plain',
172+
'',
173+
'--' + boundary + '--',
174+
''
175+
].join('\r\n')
176+
var options = {
177+
hostname: 'localhost',
178+
port,
179+
path: '/upload2',
180+
method: 'POST',
181+
headers: {
182+
'content-type': 'multipart/form-data; boundary=' + boundary,
183+
'content-length': body.length
184+
}
185+
}
186+
187+
var req = http.request(options, (res) => {
188+
assert.strictEqual(res.statusCode, 200)
189+
done()
190+
})
191+
192+
req.on('error', (err) => {
193+
done(err)
194+
})
195+
196+
req.write(body)
197+
req.end()
198+
})
153199
})

0 commit comments

Comments
 (0)