Skip to content

Commit 1242a8c

Browse files
committed
Merge pull request #681 from LordMajestros/master
Fix for #680 (#680)
2 parents 8490213 + ecc5b17 commit 1242a8c

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

lib/Receiver.hixie.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ module.exports = Receiver;
4747
*/
4848

4949
Receiver.prototype.add = function(data) {
50+
if (this.dead) return;
5051
var self = this;
5152
function doAdd() {
5253
if (self.state === EMPTY) {
@@ -153,8 +154,17 @@ Receiver.prototype.parse = function() {
153154
*/
154155

155156
Receiver.prototype.error = function (reason, terminate) {
157+
if (this.dead) return;
156158
this.reset();
157-
this.onerror(reason, terminate);
159+
if(typeof reason == 'string'){
160+
this.onerror(new Error(reason), terminate);
161+
}
162+
else if(reason.constructor == Error){
163+
this.onerror(reason, terminate);
164+
}
165+
else{
166+
this.onerror(new Error("An error occured"),terminate);
167+
}
158168
return this;
159169
};
160170

lib/Receiver.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ module.exports = Receiver;
8383
*/
8484

8585
Receiver.prototype.add = function(data) {
86+
if (this.dead) return;
8687
var dataLength = data.length;
8788
if (dataLength == 0) return;
8889
if (this.expectBuffer == null) {
@@ -327,8 +328,17 @@ Receiver.prototype.concatBuffers = function(buffers) {
327328
*/
328329

329330
Receiver.prototype.error = function (reason, protocolErrorCode) {
331+
if (this.dead) return;
330332
this.reset();
331-
this.onerror(reason, protocolErrorCode);
333+
if(typeof reason == 'string'){
334+
this.onerror(new Error(reason), protocolErrorCode);
335+
}
336+
else if(reason.constructor == Error){
337+
this.onerror(reason, protocolErrorCode);
338+
}
339+
else{
340+
this.onerror(new Error("An error occured"),protocolErrorCode);
341+
}
332342
return this;
333343
};
334344

test/Receiver.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,32 @@ describe('Receiver', function() {
382382
});
383383
});
384384
});
385+
it('will not crash if another message is received after receiving a message that exceeds maxpayload', function(done) {
386+
var perMessageDeflate = new PerMessageDeflate({},false,2);
387+
perMessageDeflate.accept([{}]);
388+
389+
var p = new Receiver({ 'permessage-deflate': perMessageDeflate },2);
390+
var buf1 = new Buffer('fooooooooooooooooooooooooooooooooooooooooooooooooooooooo');
391+
var buf2 = new Buffer('baaaarrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr');
392+
393+
p.onerror = function(reason,code) {
394+
assert.equal(code, 1009);
395+
};
396+
397+
perMessageDeflate.compress(buf1, false, function(err, compressed1) {
398+
if (err) return done(err);
399+
p.add(new Buffer([0x41, compressed1.length]));
400+
p.add(compressed1);
401+
402+
assert.equal(p.onerror,null);
403+
404+
perMessageDeflate.compress(buf2, true, function(err, compressed2) {
405+
p.add(new Buffer([0x80, compressed2.length]));
406+
p.add(compressed2);
407+
done();
408+
});
409+
});
410+
});
385411
it('can cleanup during consuming data', function(done) {
386412
var perMessageDeflate = new PerMessageDeflate();
387413
perMessageDeflate.accept([{}]);

test/WebSocketServer.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,21 @@ describe('WebSocketServer', function() {
185185
}
186186
});
187187
});
188+
it('will not crash when it receives an unhandled opcode', function(done) {
189+
var wss = new WebSocketServer({ port: 8080 });
190+
wss.on('connection', function connection(ws) {
191+
ws.onerror = function(error) {
192+
done();
193+
};
194+
});
195+
196+
var socket = new WebSocket('ws://127.0.0.1:8080/');
197+
198+
socket.onopen = function() {
199+
socket._socket.write(new Buffer([5]));
200+
socket.send('');
201+
};
202+
});
188203
});
189204

190205
describe('#close', function() {

0 commit comments

Comments
 (0)