From cb552341f329aaf4d2bd4ae13d7614b1a93abf52 Mon Sep 17 00:00:00 2001 From: Alejandro Romero Herrera Date: Fri, 4 Sep 2020 10:50:29 +0300 Subject: [PATCH 1/5] Fix stored XSS --- server.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/server.js b/server.js index 2e72800..f30dde1 100644 --- a/server.js +++ b/server.js @@ -11,9 +11,11 @@ server.listen(process.env.PORT || 3000);//publish to heroku //server.listen(process.env.OPENSHIFT_NODEJS_PORT || 3000);//publish to openshift //console.log('server started on port'+process.env.PORT || 3000); //handle the socket + io.sockets.on('connection', function(socket) { //new user login socket.on('login', function(nickname) { + nickname = nickname.replace(/[^A-Za-z0-9]/g, ''); if (users.indexOf(nickname) > -1) { socket.emit('nickExisted'); } else { @@ -34,6 +36,7 @@ io.sockets.on('connection', function(socket) { }); //new message get socket.on('postMsg', function(msg, color) { + msg = msg.replace(/on[^\s]+[\s]*(=|=|=)[\s]*("|').+("|')/gi, ''); socket.broadcast.emit('newMsg', socket.nickname, msg, color); }); //new image get From 574183b7da8b9cc7d9d378002e8127d0aace3bde Mon Sep 17 00:00:00 2001 From: Alejandro Romero Herrera Date: Fri, 4 Sep 2020 11:31:33 +0300 Subject: [PATCH 2/5] Avoid showing XSS in self browser window With option to show message --- www/scripts/hichat.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/www/scripts/hichat.js b/www/scripts/hichat.js index 7be8ed7..a920c9f 100644 --- a/www/scripts/hichat.js +++ b/www/scripts/hichat.js @@ -50,6 +50,10 @@ HiChat.prototype = { document.getElementById('loginBtn').addEventListener('click', function() { var nickName = document.getElementById('nicknameInput').value; if (nickName.trim().length != 0) { + if(testXSSattemtp(nickName)){ + document.getElementById('nicknameInput').value = ''; + return false; + } that.socket.emit('login', nickName); } else { document.getElementById('nicknameInput').focus(); @@ -59,6 +63,10 @@ HiChat.prototype = { if (e.keyCode == 13) { var nickName = document.getElementById('nicknameInput').value; if (nickName.trim().length != 0) { + if(testXSSattemtp(nickName)){ + document.getElementById('nicknameInput').value = ''; + return false; + } that.socket.emit('login', nickName); }; }; @@ -70,6 +78,7 @@ HiChat.prototype = { messageInput.value = ''; messageInput.focus(); if (msg.trim().length != 0) { + if(testXSSattemtp(msg))return false; that.socket.emit('postMsg', msg, color); that._displayNewMsg('me', msg, color); return; @@ -81,6 +90,7 @@ HiChat.prototype = { color = document.getElementById('colorStyle').value; if (e.keyCode == 13 && msg.trim().length != 0) { messageInput.value = ''; + if(testXSSattemtp(msg))return false; that.socket.emit('postMsg', msg, color); that._displayNewMsg('me', msg, color); }; @@ -174,3 +184,12 @@ HiChat.prototype = { return result; } }; + +const showXSSMsg = false; +function testXSSattemtp(txt){ + if(txt.match(/<.*on[^\s]+[\s]*(=|=|=)[\s]*("|').+("|').*>/gi)){ + if(showXSSMsg)alert('Please avoid attempting code execution'); + return true; + } + return false; +} From c085762871d858ceb18df75b22ca48c2c117552f Mon Sep 17 00:00:00 2001 From: Alejandro Romero Herrera Date: Fri, 4 Sep 2020 14:50:05 +0300 Subject: [PATCH 3/5] Updated to use DOMPurify Library --- package.json | 3 ++- server.js | 1 + www/index.html | 1 + www/scripts/hichat.js | 16 ++++++---------- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 66d3a3b..9c33700 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "version": "0.4.0", "main": "server.js", "dependencies": { + "dompurify": "^2.0.15", "express": "3.4.x", "socket.io": "0.9.x" }, @@ -11,4 +12,4 @@ "node": "0.10.x", "npm": "1.2.x" } -} \ No newline at end of file +} diff --git a/server.js b/server.js index f30dde1..0915bbe 100644 --- a/server.js +++ b/server.js @@ -5,6 +5,7 @@ var express = require('express'), users = []; //specify the html we will use app.use('/', express.static(__dirname + '/www')); +app.use('/dompurify', express.static(__dirname + '/node_modules/dompurify/dist')); //bind the server to the 80 port //server.listen(3000);//for local test server.listen(process.env.PORT || 3000);//publish to heroku diff --git a/www/index.html b/www/index.html index a63719b..de1e1d8 100644 --- a/www/index.html +++ b/www/index.html @@ -46,6 +46,7 @@

HiChat :)

view on GitHub | contact me +