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

Fix for #680 (https://github.com/websockets/ws/issues/680) #681

Merged
merged 3 commits into from
Mar 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/Receiver.hixie.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module.exports = Receiver;
*/

Receiver.prototype.add = function(data) {
if (this.dead) return;
var self = this;
function doAdd() {
if (self.state === EMPTY) {
Expand Down Expand Up @@ -153,8 +154,17 @@ Receiver.prototype.parse = function() {
*/

Receiver.prototype.error = function (reason, terminate) {
if (this.dead) return;
this.reset();
this.onerror(reason, terminate);
if(typeof reason == 'string'){
this.onerror(new Error(reason), terminate);
}
else if(reason.constructor == Error){
this.onerror(reason, terminate);
}
else{
this.onerror(new Error("An error occured"),terminate);
}
return this;
};

Expand Down
12 changes: 11 additions & 1 deletion lib/Receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ module.exports = Receiver;
*/

Receiver.prototype.add = function(data) {
if (this.dead) return;
var dataLength = data.length;
if (dataLength == 0) return;
if (this.expectBuffer == null) {
Expand Down Expand Up @@ -327,8 +328,17 @@ Receiver.prototype.concatBuffers = function(buffers) {
*/

Receiver.prototype.error = function (reason, protocolErrorCode) {
if (this.dead) return;
this.reset();
this.onerror(reason, protocolErrorCode);
if(typeof reason == 'string'){
this.onerror(new Error(reason), protocolErrorCode);
}
else if(reason.constructor == Error){
this.onerror(reason, protocolErrorCode);
}
else{
this.onerror(new Error("An error occured"),protocolErrorCode);
}
return this;
};

Expand Down
26 changes: 26 additions & 0 deletions test/Receiver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,32 @@ describe('Receiver', function() {
});
});
});
it('will not crash if another message is received after receiving a message that exceeds maxpayload', function(done) {
var perMessageDeflate = new PerMessageDeflate({},false,2);
perMessageDeflate.accept([{}]);

var p = new Receiver({ 'permessage-deflate': perMessageDeflate },2);
var buf1 = new Buffer('fooooooooooooooooooooooooooooooooooooooooooooooooooooooo');
var buf2 = new Buffer('baaaarrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr');

p.onerror = function(reason,code) {
assert.equal(code, 1009);
};

perMessageDeflate.compress(buf1, false, function(err, compressed1) {
if (err) return done(err);
p.add(new Buffer([0x41, compressed1.length]));
p.add(compressed1);

assert.equal(p.onerror,null);

perMessageDeflate.compress(buf2, true, function(err, compressed2) {
p.add(new Buffer([0x80, compressed2.length]));
p.add(compressed2);
done();
});
});
});
it('can cleanup during consuming data', function(done) {
var perMessageDeflate = new PerMessageDeflate();
perMessageDeflate.accept([{}]);
Expand Down
15 changes: 15 additions & 0 deletions test/WebSocketServer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,21 @@ describe('WebSocketServer', function() {
}
});
});
it('will not crash when it receives an unhandled opcode', function(done) {
var wss = new WebSocketServer({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.onerror = function(error) {
done();
};
});

var socket = new WebSocket('ws://127.0.0.1:8080/');

socket.onopen = function() {
socket._socket.write(new Buffer([5]));
socket.send('');
};
});
});

describe('#close', function() {
Expand Down