Fixes XSS attack through user renaming (#47)

This commit is contained in:
Dan Seripap 2016-11-28 12:18:23 -05:00
parent 5f894a891f
commit 282f43e8f1

View File

@ -12,6 +12,7 @@ class Room {
EventEmitter.call(this); EventEmitter.call(this);
const thisIO = io.of(this._id); const thisIO = io.of(this._id);
thisIO.on('connection', (socket) => { thisIO.on('connection', (socket) => {
let addedUser = false; let addedUser = false;
@ -34,10 +35,13 @@ class Room {
if (addedUser) { return; } if (addedUser) { return; }
data.id = uuid.v4(); data.id = uuid.v4();
this.users.push(data); this.users.push(data);
const username = this.sanitizeUsername(data.username);
// we store the username in the socket session for this client // we store the username in the socket session for this client
socket.username = data.username; socket.username = username;
socket.user = data; socket.user = data;
++this.numUsers; ++this.numUsers;
addedUser = true; addedUser = true;
@ -87,16 +91,18 @@ class Room {
// Update user // Update user
socket.on('update user', (data) => { socket.on('update user', (data) => {
if (data.newUsername.length > 16) { const newUsername = this.sanitizeUsername(data.newUsername);
if (newUsername.length > 16) {
return false; return false;
} }
let user = _.find(this.users, (users) => {
const user = _.find(this.users, (users) => {
return users === socket.user; return users === socket.user;
}); });
if (user) { if (user) {
user.username = data.newUsername; socket.username = user.username = newUsername;
socket.username = user.username;
socket.user = user; socket.user = user;
thisIO.emit('user update', { thisIO.emit('user update', {
@ -110,6 +116,10 @@ class Room {
}); });
} }
sanitizeUsername(str) {
return str.replace(/[^A-Za-z0-9]/g, '-');
}
roomId() { roomId() {
return this.id; return this.id;
} }