Skip to content

Instantly share code, notes, and snippets.

@camelcaseblog
Last active February 14, 2022 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camelcaseblog/3634cc9c4d50825295a52e56cd7c906c to your computer and use it in GitHub Desktop.
Save camelcaseblog/3634cc9c4d50825295a52e56cd7c906c to your computer and use it in GitHub Desktop.
const sanitizeHtml = require("sanitize-html")
const evilHtml = `
<img
id="xss-image"
src="/"
onerror="d = document;
c = ('cooki' + 'e').trim();
qs = 'queryS' + 'elector';
console.log(d[c]);
d[qs + 'All']('.top_nlsitem').forEach(n => n.style.backgroundColor = 'green');
d[qs]('#xss-image').src = 'ht' + 'tps://upload.wikimedia.org/wikipedia/commons/c/ca/1x1.png';"
/>
`
const legitHtml = sanitizeHtml(evilHtml, { allowedTags: "img" })
console.log(legitHtml)
/* Output:
"\n<img src=\"/\" />\n"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment