Skip to content

Commit

Permalink
fix - catch file stream error
Browse files Browse the repository at this point in the history
  • Loading branch information
haoxin authored and haoxin committed Nov 18, 2016
1 parent 750e8d2 commit 624ad34
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = function (request, options) {
.on('field', onField)
.on('file', onFile)
.on('close', cleanup)
.on('error', onEnd)
.on('error', onError)
.on('finish', onEnd)

busboy.on('partsLimit', function(){
Expand Down Expand Up @@ -103,11 +103,12 @@ module.exports = function (request, options) {
file.filename = filename
file.transferEncoding = file.encoding = encoding
file.mimeType = file.mime = mimetype
file.on('error', onError)
ch(file)
}

function onError(err) {
lastError = err;
lastError = err
}

function onEnd() {
Expand All @@ -120,7 +121,7 @@ module.exports = function (request, options) {
busboy.removeListener('field', onField)
busboy.removeListener('file', onFile)
busboy.removeListener('close', cleanup)
busboy.removeListener('error', onEnd)
busboy.removeListener('error', onError)
busboy.removeListener('partsLimit', onEnd)
busboy.removeListener('filesLimit', onEnd)
busboy.removeListener('fieldsLimit', onEnd)
Expand Down
46 changes: 46 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,26 @@ describe('Co Busboy', function () {

})

describe('invalid multipart', function() {
it('should throw error', function() {
return co(function*(){
var parts = busboy(invalidRequest());
var part;
try {
while (part = yield parts) {
if (!part.length) {
part.resume()
}
}

throw new Error('should not run this')
} catch (err) {
assert.equal(err.message, 'Part terminated early due to unexpected end of multipart data')
}
})
})
})

})

function wait(ms) {
Expand Down Expand Up @@ -391,3 +411,29 @@ function request() {

return stream
}

function invalidRequest() {
// https://github.com/mscdex/busboy/blob/master/test/test-types-multipart.js

var stream = new Stream.PassThrough()

stream.headers = {
'content-type': 'multipart/form-data; boundary=---------------------------paZqsnEHRufoShdX6fh0lUhXBP4k'
}

stream.end([
'-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
'Content-Disposition: form-data; name="upload_file_0"; filename="1k_a.dat"',
'Content-Type: application/octet-stream',
'',
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
'-----------------------------invalid',
'Content-Disposition: form-data; name="upload_file_2"; filename="hack.exe"',
'Content-Type: application/octet-stream',
'',
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
'-----------------------------invalid--'
].join('\r\n'))

return stream
}

0 comments on commit 624ad34

Please sign in to comment.