I have a chatting website and I wanna know if it is XSS secure. Let me explain what is my method.
I use this JQuery code to add new messages to screen that are incoming from ajax request:
$("#message").prepend(req.msg);
I know this is vulnerable but messages on DB are encoded. So when I send that payload message:
<text>TEST</test>
It stored on database as encoded:
<text>TEST</test>
I know a scenario that is bypassing .htmlentities() function in this answer. Shortly:
<input value=<%= HtmlEncode(somevar) %> id=textbox>
If we send a onclick=alert(document.cookie)
input to this html code, it will become:
<input value=a onclick=alert(document.cookie) id=textbox>
So in this scenario, encoding < > ; &
characters can't save us. But I didn't write my code like this so I think I'm secure from that vulnerabilty. My server responds a JSON like this:
..."msg" : "<tr><td><b>user:<\b> <text>TEST</test><\td><\tr>",...
And I directly .prepend() the "msg" data into page. I think this is fully secure. Hacker must use < > ; &
characters to XSS. I am also encoding \
as \\
. But maybe I am missing something. Is that fully secure ?