Skip to content

Commit

Permalink
Fix Malformed header bug (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Nov 29, 2021
1 parent aedb42c commit e376ad7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 24 deletions.
46 changes: 22 additions & 24 deletions deps/dicer/lib/HeaderParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function HeaderParser(cfg) {
this.ss.on('info', function(isMatch, data, start, end) {
if (data && !self.maxed) {
if (self.nread + (end - start) > MAX_HEADER_SIZE) {
end = (MAX_HEADER_SIZE - self.nread);
end = MAX_HEADER_SIZE - self.nread + start;
self.nread = MAX_HEADER_SIZE;
} else
self.nread += (end - start);
Expand Down Expand Up @@ -72,8 +72,9 @@ HeaderParser.prototype._parseHeader = function() {
if (this.npairs === this.maxHeaderPairs)
return;

var lines = this.buffer.split(RE_CRLF), len = lines.length, m, h,
modded = false;
const lines = this.buffer.split(RE_CRLF),
len = lines.length;
let m, h;

for (var i = 0; i < len; ++i) {
if (lines[i].length === 0)
Expand All @@ -82,29 +83,26 @@ HeaderParser.prototype._parseHeader = function() {
// folded header content
// RFC2822 says to just remove the CRLF and not the whitespace following
// it, so we follow the RFC and include the leading whitespace ...
this.header[h][this.header[h].length - 1] += lines[i];
} else {
m = RE_HDR.exec(lines[i]);
if (m) {
h = m[1].toLowerCase();
if (m[2]) {
if (this.header[h] === undefined)
this.header[h] = [m[2]];
else
this.header[h].push(m[2]);
} else
this.header[h] = [''];
if (++this.npairs === this.maxHeaderPairs)
break;
} else {
this.buffer = lines[i];
modded = true;
break;
if (h) {
this.header[h][this.header[h].length - 1] += lines[i];
continue;
}
}
m = RE_HDR.exec(lines[i]);
if (m) {
h = m[1].toLowerCase();
if (m[2]) {
if (this.header[h] === undefined)
this.header[h] = [m[2]];
else
this.header[h].push(m[2]);
} else
this.header[h] = [''];
if (++this.npairs === this.maxHeaderPairs)
break;
} else
return;
}
if (!modded)
this.buffer = '';
};

module.exports = HeaderParser;
module.exports = HeaderParser;
25 changes: 25 additions & 0 deletions test/dicer-malformed-header.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const Dicer = require('../deps/dicer/lib/Dicer');
const { expect } = require('chai');

describe('dicer-malformed-header', () => {

it("should gracefully handle headers with leading whitespace", done => {
var d = new Dicer({ boundary: "----WebKitFormBoundaryoo6vortfDzBsDiro" });

d.on('part', function (p) {
p.on('header', function (header) {
expect(header).has.property(" content-disposition");
expect(header[" content-disposition"]).to.be.eql(['form-data; name="bildbeschreibung"'])
});
p.on('data', function (data) {
});
p.on('end', function () {
});
});
d.on('finish', function () {
done();
});

d.write(Buffer.from('------WebKitFormBoundaryoo6vortfDzBsDiro\r\n Content-Disposition: form-data; name="bildbeschreibung"\r\n\r\n\r\n------WebKitFormBoundaryoo6vortfDzBsDiro--'));
});
});

0 comments on commit e376ad7

Please sign in to comment.