Skip to content

Instantly share code, notes, and snippets.

@mashingan
Created April 19, 2017 02:30
Show Gist options
  • Save mashingan/2d301b684c9d24444b5516025ef36205 to your computer and use it in GitHub Desktop.
Save mashingan/2d301b684c9d24444b5516025ef36205 to your computer and use it in GitHub Desktop.
input {
width: 100%;
display: block;
}
<!-- Note: &amp;nbsp; is used to make sure that the value of the input is &nbsp; instead of a space. -->
<form id="form">
<input type="text" id="input" placeholder="input" value="Entity:&amp;nbsp;
Bad attempt at XSS:<script>alert('new\nline?')</script><br>">
<input type="submit" value="alert(input)">
In the above example, "&amp;nbsp;" should be replaced with a space, and the rest of the text should be displayed as-is. See http://stackoverflow.com/a/7394787/938089.
</form>
function decodeHtml(html) {
var txt = document.createElement("textarea");
txt.innerHTML = html;
return txt.value;
}
document.getElementById('form').onsubmit = function(e) {
e.preventDefault();
var input = document.getElementById('input').value;
var output = decodeHtml(input);
alert(output);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment