I store some data in firebase (which can be retrieved in the client), and then, I add it to the HTML using JQuery. This is easy - I just prepend the data to the DOM element. However, the issue is, this raw code will be vulnerable to attacks, so I need to properly escape the data taken from the database before adding it to the HTML. I thought the following code would do the job:
function escapeHTML(text) {
var map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return text.replace(/[&<>"']/g, function(m) { return map[m]; });
}
$('#element').prepend('<h1>Heading</h1><p>' + escapeHTML(data) + '</p>');
When I test this code, by making the value of data something like <script>alert('This should not work');</script>
, I thought that it would just add this text to the <p></p>
tags, without executing the JS, but it actually ran the code.
I understand that using $('#element').text()
will escape the text for me, but I don't want everything to get escaped - I still want to add the <h1></h1>
and <p></p>
tags, I just need the variable "data" to be escaped. I saw a couple of StackOverflow posts on similar topics, but none of them seem to address this issue - post 1, post 2, post 3.