I'm working with a web service that will give me values like:
var text = "<<<&&&";
And i need to print this to look like "<<<&&&" with javascript.
But here's the catch: i can't use inner HTML(I'm actually sending this values to a prototype library that creates Text Nodes so it doesn't unescape my raw html string. If editing the library would not be an option, how would you unescape this html?
I need to undertand the real deal here, what's the risk of unescaping this type of strings? how does innerHTML does it? and what other options exist?
EDIT- The problem is not about using javascript normal escape/unescape or even jQuery/prototype implementations of them, but about the security issues that could come from using any of this... aka "They told me it was pretty insecure to use them"
(For those trying to undertand what the heck im talking about with innerHTML unescaping this weird string, check out this simple example:
<html>
<head>
<title>createTextNode example</title>
<script type="text/javascript">
var text = "<<<&&&";
function addTextNode(){
var newtext = document.createTextNode(text);
var para = document.getElementById("p1");
para.appendChild(newtext);
}
function innerHTMLTest(){
var para = document.getElementById("p1");
para.innerHTML = text;
}
</script>
</head>
<body>
<div style="border: 1px solid red">
<p id="p1">First line of paragraph.<br /></p>
</div><br />
<button onclick="addTextNode();">add another textNode.</button>
<button onclick="innerHTMLTest();">test innerHTML.</button>
</body>
</html>
JavaScript unescape() Function The unescape() function in JavaScript takes a string as a parameter and uses to decode that string encoded by the escape() function. The hexadecimal sequence in the string is replaced by the characters they represent when decoded via unescape().
Unescape HTML Entities with a Text Area One way to unescape HTML entities is to put our escaped text in a text area. This will unescape the text, so we can return the unescaped text afterward by getting the text from the text area. We have an htmlDecode function that takes an input string as a parameter.
The unescape() function is deprecated. Use decodeURI() or decodeURIComponent() instead.
The escape() function is used to encode a string, making it safe for use in a URL. The unescape() function is used to decode an encoded string.
Change your test string to <b><<&&&</b>
to get a better handle on what the risk is... (or better, <img src='http://www.spam.com/ASSETS/0EE75B480E5B450F807117E06219CDA6/spamReg.png' onload='alert(document.cookie);'>
for cookie-stealing spam)
See the example at http://jsbin.com/uveme/139/ (based on your example, using prototype for the unescaping.) Try clicking the four different buttons to see the different effects. Only the last one is a security risk. (You can view/edit the source at http://jsbin.com/uveme/139/edit) The example doesn't actually steal your cookies...
createTextNode
to create a text node and appendChild
to insert that unaltered node object directly into your document, you are safe.Note: As pointed out by Ben Vinegar Using createTextNode
is not a magic bullet: using it to escape the string, then using textContent
or innerHTML
to get the escaped text out and doing other stuff with it does not protect you in your subsequent uses. In particluar, the escapeHtml method in Peter Brown's answer below is insecure if used to populate attributes.
A very good read is http://benv.ca/2012/10/4/you-are-probably-misusing-DOM-text-methods/ which explains why the convention wisdom of using createTextNode is actually not secure at all.
A representative example take from the article above of the risk:
function escapeHtml(str) {
var div = document.createElement('div');
div.appendChild(document.createTextNode(str));
return div.innerHTML;
};
var userWebsite = '" onmouseover="alert(\'derp\')" "';
var profileLink = '<a href="' + escapeHtml(userWebsite) + '">Bob</a>';
var div = document.getElementById('target');
div.innerHtml = profileLink;
// <a href="" onmouseover="alert('derp')" "">Bob</a>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With