Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/types/multipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ function Multipart (boy, cfg) {
}

Multipart.prototype.write = function (chunk, cb) {
if (this.parser._finished) {
cb()
return
}

const r = this.parser.write(chunk)
if (r && !this._pause) {
cb()
Expand Down
46 changes: 46 additions & 0 deletions test/types-multipart-coverage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,52 @@ test('multipart parser accepts prototype property names', async (t) => {
}
})

test('multipart parser ignores an epilogue written after the closing boundary', { timeout: 1000 }, async (t) => {
const boundary = 'epilogueboundary'
const body = buildBody([[
'Content-Disposition: form-data; name="field"',
'',
'value'
]], boundary)
const busboy = new Busboy({
headers: { 'content-type': 'multipart/form-data; boundary=' + boundary }
})
const events = []
const fields = []
let finishes = 0
busboy.on('field', (key, value) => {
events.push('field')
fields.push([key, value])
})
const finished = new Promise((resolve, reject) => {
busboy.once('error', reject)
busboy.on('finish', () => {
events.push('finish')
finishes++
resolve()
})
})

busboy.write(Buffer.from(body, 'utf8'))
await new Promise(resolve => setImmediate(resolve))
await new Promise((resolve, reject) => {
busboy.write(Buffer.from('epilogue', 'utf8'), error => {
if (error) reject(error)
else {
events.push('epilogue write')
resolve()
}
})
})
busboy.end()

await finished
await new Promise(resolve => setImmediate(resolve))
t.assert.deepStrictEqual(fields, [['field', 'value']])
t.assert.deepStrictEqual(events, ['field', 'epilogue write', 'finish'])
t.assert.strictEqual(finishes, 1)
})

test('multipart parser rejects bare CR or LF in disposition parameters', async (t) => {
const boundary = 'barecrlf'
const dispositionValues = [
Expand Down