Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed a bug which could cause a crash in HeaderParser, and as consequence could potentially crash a web server based on it #22

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 6 additions & 15 deletions lib/Dicer.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ function Dicer(cfg) {
this._dashes = 0;
this._parts = 0;
this._finished = false;
this._realFinish = false;
this._isPreamble = true;
this._justMatched = false;
this._firstWrite = true;
Expand All @@ -54,7 +53,7 @@ function Dicer(cfg) {
inherits(Dicer, WritableStream);

Dicer.prototype.emit = function(ev) {
if (ev === 'finish' && !this._realFinish) {
if (ev === 'finish') {
if (!this._finished) {
var self = this;
process.nextTick(function() {
Expand All @@ -64,15 +63,11 @@ Dicer.prototype.emit = function(ev) {
self._part.emit('error', new Error(type + ' terminated early due to unexpected end of multipart data'));
self._part.push(null);
process.nextTick(function() {
self._realFinish = true;
self.emit('finish');
self._realFinish = false;
WritableStream.prototype.emit.call(self, 'finish');
});
return;
}
self._realFinish = true;
self.emit('finish');
self._realFinish = false;
WritableStream.prototype.emit.call(self, 'finish');
});
}
} else
Expand Down Expand Up @@ -160,9 +155,7 @@ Dicer.prototype._oninfo = function(isMatch, data, start, end) {
this._finished = true;
// no more parts will be added
if (self._parts === 0) {
self._realFinish = true;
self.emit('finish');
self._realFinish = false;
WritableStream.prototype.emit.call(self, 'finish');
}
}
if (this._dashes)
Expand Down Expand Up @@ -190,7 +183,7 @@ Dicer.prototype._oninfo = function(isMatch, data, start, end) {
shouldWriteMore = this._part.push(data.slice(start, end));
if (!shouldWriteMore)
this._pause = true;
} else if (!this._isPreamble && this._inHeader) {
} else {
if (buf)
this._hparser.push(buf);
r = this._hparser.push(data.slice(start, end));
Expand All @@ -207,9 +200,7 @@ Dicer.prototype._oninfo = function(isMatch, data, start, end) {
this._part.on('end', function() {
if (--self._parts === 0) {
if (self._finished) {
self._realFinish = true;
self.emit('finish');
self._realFinish = false;
WritableStream.prototype.emit.call(self, 'finish');
} else {
self._unpause();
}
Expand Down
42 changes: 19 additions & 23 deletions 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,7 @@ HeaderParser.prototype._parseHeader = function() {
if (this.npairs === this.maxHeaderPairs)
return;

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

for (var i = 0; i < len; ++i) {
if (lines[i].length === 0)
Expand All @@ -82,29 +81,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;