diff --git a/lib/client.js b/lib/client.js index 1ce3fc4986..0ac95dffae 100644 --- a/lib/client.js +++ b/lib/client.js @@ -91,7 +91,9 @@ Client.prototype.connect = function(name){ Client.prototype.disconnect = function(){ for (var id in this.sockets) { - this.sockets[id].disconnect(); + if (this.sockets.hasOwnProperty(id)) { + this.sockets[id].disconnect(); + } } this.sockets = {}; this.close(); @@ -104,7 +106,7 @@ Client.prototype.disconnect = function(){ */ Client.prototype.remove = function(socket){ - if (this.sockets[socket.id]) { + if (this.sockets.hasOwnProperty(socket.id)) { var nsp = this.sockets[socket.id].nsp.name; delete this.sockets[socket.id]; delete this.nsps[nsp]; @@ -204,7 +206,9 @@ Client.prototype.ondecoded = function(packet) { Client.prototype.onerror = function(err){ for (var id in this.sockets) { - this.sockets[id].onerror(err); + if (this.sockets.hasOwnProperty(id)) { + this.sockets[id].onerror(err); + } } this.onclose('client error'); }; @@ -224,7 +228,9 @@ Client.prototype.onclose = function(reason){ // `nsps` and `sockets` are cleaned up seamlessly for (var id in this.sockets) { - this.sockets[id].onclose(reason); + if (this.sockets.hasOwnProperty(id)) { + this.sockets[id].onclose(reason); + } } this.sockets = {}; diff --git a/lib/index.js b/lib/index.js index 1612c0cfa3..4300b99328 100644 --- a/lib/index.js +++ b/lib/index.js @@ -348,7 +348,9 @@ Server.prototype.of = function(name, fn){ Server.prototype.close = function(){ for (var id in this.nsps['/'].sockets) { - this.nsps['/'].sockets[id].onclose(); + if (this.nsps['/'].sockets.hasOwnProperty(id)) { + this.nsps['/'].sockets[id].onclose(); + } } this.engine.close(); diff --git a/lib/namespace.js b/lib/namespace.js index 1a5b7efcfd..8bf460a3e2 100644 --- a/lib/namespace.js +++ b/lib/namespace.js @@ -187,7 +187,7 @@ Namespace.prototype.add = function(client, fn){ */ Namespace.prototype.remove = function(socket){ - if (this.sockets[socket.id]) { + if (this.sockets.hasOwnProperty(socket.id)) { delete this.sockets[socket.id]; } else { debug('ignoring remove for %s', socket.id); diff --git a/lib/socket.js b/lib/socket.js index 1e50a0232b..d792038a45 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -59,7 +59,6 @@ function Socket(nsp, client){ this.nsp = nsp; this.server = nsp.server; this.adapter = this.nsp.adapter; - this.id = client.id; this.id = nsp.name + '#' + client.id; this.client = client; this.conn = client.conn; @@ -223,7 +222,7 @@ Socket.prototype.packet = function(packet, opts){ Socket.prototype.join = function(room, fn){ debug('joining room %s', room); var self = this; - if (this.rooms[room]) return this; + if (this.rooms.hasOwnProperty(room)) return this; this.adapter.add(this.id, room, function(err){ if (err) return fn && fn(err); debug('joined room %s', room); diff --git a/test/socket.io.js b/test/socket.io.js index 026cb5f143..c27dff16d0 100644 --- a/test/socket.io.js +++ b/test/socket.io.js @@ -1668,6 +1668,23 @@ describe('socket.io', function(){ }); }); }); + + it('should not crash when messing with Object prototype', function(done){ + Object.prototype.foo = 'bar'; + var srv = http(); + var sio = io(srv); + srv.listen(function(){ + var socket = client(srv); + + sio.on('connection', function(s){ + s.disconnect(true); + sio.close(); + setTimeout(function(){ + done(); + }, 100); + }); + }); + }); }); describe('messaging many', function(){