In this post I’ll be talking about XSS. For anyone wondering what is XSS I think you should watch this video from Computerphile about it : YouTube
This list will probably help you understand how easily it can be performed (I’ll provide a solution against XSS at the end in the form of a Javascript function).
For this list I searched “chat” on the Glitch search engine and found some projects where it’s possible to do XSS.
Chat App IO - Chat App Io
It’s possible to do it by sending a message and also with your nickname (you can set it with the button on the top of the page)
Chatter - https://chatter.glitch.me/chat#general
You can do it with messages but nicknames does not work BUT it makes the website not work correctly because there will be an “html tag jam” instead of the normal website.
There isn’t that many chat apps that aren’t remixes of an example by socket.io where HTML was handled correctly. So I’ll probably add more to this list as time passes so people can try that out, because it will allow you, the reader, to learn how to not do chat apps.
To counter XSS, you have to ensure every bit of data generated by a user is not detected as executable. To do that, there’s one simple way : The characters used to start and end tags have equivalents, it means that graphically they are the same, but they’re not able to start/end an html tag. Since you’ll need to replace this characters quite a lot of time (when displaying usernames, user-generated text, etc…) when building chat apps, here’s a function that will handle it for you. It returns text that wont be detected as HTML in any way.
function nohtml(message){ return message.replace(/</ig, '<').replace(/>/ig, '>'); }
This function replaces < to the symbol called “lesser than”, and > to the symbol “greater than” and returns the result string.