Created
May 14, 2015 22:38
-
-
Save jorendorff/1a17f69dbfaafa2304f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function SaferHTML(templateData) { | |
var s = templateData[0]; | |
for (var i = 1; i < arguments.length; i++) { | |
var arg = String(arguments[i]); | |
// Escape special characters in the substitution. | |
s += arg.replace(/&/g, "&") | |
.replace(/</g, "<") | |
.replace(/>/g, ">"); | |
// Don't escape special characters in the template. | |
s += templateData[i]; | |
} | |
return s; | |
} | |
var bonk = { | |
sender: "Hacker Steve <script>alert('xss');</script>" | |
}; | |
console.log(SaferHTML`<p>${bonk.sender} sent you a bonk.</p>`); |
object.freeze([<p>
,
has sent you a bonk. <|p>
]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Var message=
saferHTML(template data,bonk.sender);