Skip to content

Commit

Permalink
use bufferStartsWith
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Aug 24, 2023
1 parent 3ac7d31 commit f2b3f6c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/types/multipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ function Multipart (boy, cfg) {
Multipart.prototype.write = function (chunk, cb) {
let r
if ((r = this.parser.write(chunk)) && !this._pause) {
if (this._boundaryNotFound && (this._nparts !== 0 || chunk.includes(`--${this._boundary}--`))) {
if (this._boundaryNotFound && (this._nparts !== 0 || bufferStartsWith(chunk, Buffer.from(`--${this._boundary}--`)))) {
this._boundaryNotFound = false
}
cb()
Expand All @@ -295,6 +295,18 @@ Multipart.prototype.end = function () {
} else if (self.parser.writable) { self.parser.end() }
}

function bufferStartsWith (buf, val) {
const valLen = val.length

if (buf.length < valLen) { return false }

for (let i = 0; i < valLen; ++i) {
if (buf[i] !== val[i]) { return false }
}

return true
}

function skipPart (part) {
part.resume()
}
Expand Down
9 changes: 9 additions & 0 deletions test/types-multipart.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,15 @@ 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: [
['-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
Expand Down

0 comments on commit f2b3f6c

Please sign in to comment.