Skip to content

Instantly share code, notes, and snippets.

@diouldebalde
Forked from jorendorff/saferhtml-example.js
Last active July 19, 2019 17:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diouldebalde/13e57f79771e29e49079f420dd018046 to your computer and use it in GitHub Desktop.
Save diouldebalde/13e57f79771e29e49079f420dd018046 to your computer and use it in GitHub Desktop.
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, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;");
// 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>`);
@diouldebalde
Copy link
Author

Hydrogen_Density_Plots

@diouldebalde
Copy link
Author

[__](

  • #url

)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment