Skip to content

Commit

Permalink
throw when there's no boundary (#122)
Browse files Browse the repository at this point in the history
* throw when there's no boundary

* check the boundary at the end of the stream

* optimize boundary detection

* fix check

* _boundaryNotFound

* check for boundary

* use bufferStartsWith

* use _checkBoundary instead of bufferStartsWith

* add test

* fix test

* fix test

* optional

* forward to dicer for validation
  • Loading branch information
gurgunday authored Aug 30, 2023
1 parent 1cdb224 commit 0ff7d3d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 17 deletions.
3 changes: 0 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
module.exports = {
parserOptions: {
ecmaVersion: 2018
},
ignorePatterns: [
'bench',
'deps/encoding'
Expand Down
2 changes: 1 addition & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ inherits(Busboy, WritableStream)
Busboy.prototype.emit = function (ev) {
if (ev === 'finish') {
if (!this._done) {
this._parser && this._parser.end()
this._parser?.end()
return
} else if (this._finished) {
return
Expand Down
29 changes: 16 additions & 13 deletions lib/types/multipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
// * support limits.fieldNameSize
// -- this will require modifications to utils.parseParams

const ReadableStream = require('stream').Readable
const inherits = require('util').inherits
const { Readable } = require('stream')
const { inherits } = require('util')

const Dicer = require('../../deps/dicer/lib/Dicer')

Expand Down Expand Up @@ -47,10 +47,7 @@ function Multipart (boy, cfg) {
function checkFinished () {
if (nends === 0 && finished && !boy._done) {
finished = false
process.nextTick(function () {
boy._done = true
boy.emit('finish')
})
self.end()
}
}

Expand Down Expand Up @@ -268,36 +265,42 @@ function Multipart (boy, cfg) {
}

Multipart.prototype.write = function (chunk, cb) {
let r
if ((r = this.parser.write(chunk)) && !this._pause) { cb() } else {
const r = this.parser.write(chunk)
if (r && !this._pause) {
cb()
} else {
this._needDrain = !r
this._cb = cb
}
}

Multipart.prototype.end = function () {
const self = this
if (this._nparts === 0 && !self._boy._done) {

if (self.parser.writable) {
self.parser.end()
} else if (!self._boy._done) {
process.nextTick(function () {
self._boy._done = true
self._boy.emit('finish')
})
} else if (this.parser.writable) { this.parser.end() }
}
}

function skipPart (part) {
part.resume()
}

function FileStream (opts) {
ReadableStream.call(this, opts)
Readable.call(this, opts)

this.bytesRead = 0

this.truncated = false
}
inherits(FileStream, ReadableStream)

FileStream.prototype._read = function (n) { }
inherits(FileStream, Readable)

FileStream.prototype._read = function (n) {}

module.exports = Multipart
19 changes: 19 additions & 0 deletions test/types-multipart.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ describe('types-multipart', () => {
],
boundary: '----WebKitFormBoundaryTB2MiQ36fnSJlrhY',
expected: [],
shouldError: 'Unexpected end of multipart data',
what: 'No fields and no files'
},
{
Expand Down Expand Up @@ -476,6 +477,24 @@ describe('types-multipart', () => {
expected: [],
what: 'empty form'
},
{
source: [
' ------WebKitFormBoundaryTB2MiQ36fnSJlrhY--\r\n'
],
boundary: '----WebKitFormBoundaryTB2MiQ36fnSJlrhY',
expected: [],
shouldError: 'Unexpected end of multipart data',
what: 'empty form with preceding whitespace'
},
{
source: [
'------WebKitFormBoundaryTB2MiQ36fnSJlrhY--\r\n'
],
boundary: '----WebKitFormBoundaryTB2MiQ36fnSJlrhYY',
expected: [],
shouldError: 'Unexpected end of multipart data',
what: 'empty form with wrong boundary (extra Y)'
},
{
source: [
['-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
Expand Down

0 comments on commit 0ff7d3d

Please sign in to comment.