as the title says I made a simple chat room with sockets.io, the only problem is I have no xss protection and my buddies keep putting infinite loops as usernames, so you can imagine how trolly this is getting :P. This is my app.js
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
var server = http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
var io = require('socket.io').listen(server);
var usernames = {};
io.sockets.on('connection', function (socket) {
// When the client emits 'sendchat' this listens and executes
socket.on('sendchat', function(data) {
io.sockets.emit('updatechat', socket.username, data);
});
// When the client emites 'adduser' this listens and executes
socket.on('adduser', function(username) {
// Store the username in the socket session for this client
socket.username = username;
// add the client's username to the global list
usernames[username] = username;
// echo to the client they've connected
socket.emit('updatechat', 'SERVER', 'you have connected');
// echo globally (all clients) that a person has connected
socket.broadcast.emit('updatechat', 'SERVER', username + ' has connected');
// update the list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
});
socket.on('disconnect', function() {
// remove the username from global usernames list
delete usernames[socket.username];
// update list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
// echo globally that the client has left
socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has disconnected');
});
});
How can I sanitize their input to prevent against such things, I tried googling XSS protection prevention, sanitize html input, and other things, but I can't find nothing!
Client Code:
socket.on('updatechat', function(username, data) {
$('#conversation').append('<b>'+username+ ':</b>' + data.replace() + '<br>');
});
As I mentioned in my comment, don't send that whole message from your server. You are wasting bandwidth and mixing your presentation layer into your server, which will make things a real hassle later on.
Instead of this updatechat emit, try sending something a bit more useful such as userDisconnected with the username. Let the client code display the message that the client has disconnected.
Now for your client, do something like this:
socket.on('userDisconnected', function(username, data) {
$('#conversation').append(
$('<span>').addClass('serverMessage').text(username + ' has disconnected'),
);
});
The key here is that you use $.text() to set the innerText. HTML becomes irrelevant.