Skip to content

Commit

Permalink
Added support for blacklisting events that are emitted from the clien…
Browse files Browse the repository at this point in the history
…t side.

Currently it's possible for a client do .emit('disconnect') and this will trigger
the disconnect event on the server.. Which can lead to major issues.

We should black list that by default. You can override or add more events by adding
them to the `blacklist` setting
  • Loading branch information
3rd-Eden committed Oct 11, 2011
1 parent b8f6dc7 commit ecd20b0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function Manager (server, options) {
, resource: '/socket.io'
, transports: defaultTransports
, authorization: false
, blacklist: ['connect', 'disconnect']
, blacklist: ['disconnect']
, 'log level': 3
, 'close timeout': 25
, 'heartbeat timeout': 15
Expand Down
6 changes: 3 additions & 3 deletions lib/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ SocketNamespace.prototype.handlePacket = function (sessid, packet) {
if (packet.endpoint == '') {
connect();
} else {
var manager = handshakeData = manager.handshaken[sessid];
var handshakeData = manager.handshaken[sessid];

this.authorize(handshakeData, function (err, authorized, newData) {
if (err) return error(err);
Expand All @@ -324,15 +324,15 @@ SocketNamespace.prototype.handlePacket = function (sessid, packet) {
case 'event':
// check if the emitted event is not blacklisted
if (-~manager.get('blacklist').indexOf(packet.name)) {
this.log.debug('ignoring blacklisted event `' + packet.name + '`');
} else {
var params = [packet.name].concat(packet.args);

if (dataAck) {
params.push(ack);
}

socket.$emit.apply(socket, params);
} else {
this.log.info('ignoring blacklisted event ' + packet.name);
}
break;

Expand Down
39 changes: 39 additions & 0 deletions test/namespace.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,5 +243,44 @@ module.exports = {
}
});
})
},

'ignoring blacklisted events': function (done) {
var cl = client(++ports)
, io = create(cl)
, calls = 0
, ws;

io.set('heartbeat interval', 1);
io.set('blacklist', ['foobar']);

io.sockets.on('connection', function (socket) {
socket.on('foobar', function () {
calls++;
});
});

cl.handshake(function (sid) {
ws = websocket(cl, sid);

ws.on('open', function (){
ws.packet({
type: 'event'
, name: 'foobar'
, endpoint: ''
});
});

ws.on('message', function (data) {
if (data.type === 'heartbeat') {
cl.end();
ws.finishClose();
io.server.close();

calls.should.equal(0);
done();
}
});
});
}
};

0 comments on commit ecd20b0

Please sign in to comment.