Skip to content

Commit 09f446e

Browse files
Add hasOwnProperty checks
1 parent b73d9be commit 09f446e

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

lib/client.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ Client.prototype.connect = function(name){
9191

9292
Client.prototype.disconnect = function(){
9393
for (var id in this.sockets) {
94-
this.sockets[id].disconnect();
94+
if (this.sockets.hasOwnProperty(id)) {
95+
this.sockets[id].disconnect();
96+
}
9597
}
9698
this.sockets = {};
9799
this.close();
@@ -104,7 +106,7 @@ Client.prototype.disconnect = function(){
104106
*/
105107

106108
Client.prototype.remove = function(socket){
107-
if (this.sockets[socket.id]) {
109+
if (this.sockets.hasOwnProperty(socket.id)) {
108110
var nsp = this.sockets[socket.id].nsp.name;
109111
delete this.sockets[socket.id];
110112
delete this.nsps[nsp];
@@ -204,7 +206,9 @@ Client.prototype.ondecoded = function(packet) {
204206

205207
Client.prototype.onerror = function(err){
206208
for (var id in this.sockets) {
207-
this.sockets[id].onerror(err);
209+
if (this.sockets.hasOwnProperty(id)) {
210+
this.sockets[id].onerror(err);
211+
}
208212
}
209213
this.onclose('client error');
210214
};
@@ -224,7 +228,9 @@ Client.prototype.onclose = function(reason){
224228

225229
// `nsps` and `sockets` are cleaned up seamlessly
226230
for (var id in this.sockets) {
227-
this.sockets[id].onclose(reason);
231+
if (this.sockets.hasOwnProperty(id)) {
232+
this.sockets[id].onclose(reason);
233+
}
228234
}
229235
this.sockets = {};
230236

lib/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,9 @@ Server.prototype.of = function(name, fn){
348348

349349
Server.prototype.close = function(){
350350
for (var id in this.nsps['/'].sockets) {
351-
this.nsps['/'].sockets[id].onclose();
351+
if (this.nsps['/'].sockets.hasOwnProperty(id)) {
352+
this.nsps['/'].sockets[id].onclose();
353+
}
352354
}
353355

354356
this.engine.close();

lib/namespace.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ Namespace.prototype.add = function(client, fn){
187187
*/
188188

189189
Namespace.prototype.remove = function(socket){
190-
if (this.sockets[socket.id]) {
190+
if (this.sockets.hasOwnProperty(socket.id)) {
191191
delete this.sockets[socket.id];
192192
} else {
193193
debug('ignoring remove for %s', socket.id);

lib/socket.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ function Socket(nsp, client){
5959
this.nsp = nsp;
6060
this.server = nsp.server;
6161
this.adapter = this.nsp.adapter;
62-
this.id = client.id;
6362
this.id = nsp.name + '#' + client.id;
6463
this.client = client;
6564
this.conn = client.conn;
@@ -223,7 +222,7 @@ Socket.prototype.packet = function(packet, opts){
223222
Socket.prototype.join = function(room, fn){
224223
debug('joining room %s', room);
225224
var self = this;
226-
if (this.rooms[room]) return this;
225+
if (this.rooms.hasOwnProperty(room)) return this;
227226
this.adapter.add(this.id, room, function(err){
228227
if (err) return fn && fn(err);
229228
debug('joined room %s', room);

test/socket.io.js

+17
Original file line numberDiff line numberDiff line change
@@ -1668,6 +1668,23 @@ describe('socket.io', function(){
16681668
});
16691669
});
16701670
});
1671+
1672+
it('should not crash when messing with Object prototype', function(done){
1673+
Object.prototype.foo = 'bar';
1674+
var srv = http();
1675+
var sio = io(srv);
1676+
srv.listen(function(){
1677+
var socket = client(srv);
1678+
1679+
sio.on('connection', function(s){
1680+
s.disconnect(true);
1681+
sio.close();
1682+
setTimeout(function(){
1683+
done();
1684+
}, 100);
1685+
});
1686+
});
1687+
});
16711688
});
16721689

16731690
describe('messaging many', function(){

0 commit comments

Comments
 (0)