I use Markdown.js and i use sanitize-html for xss attacks.
I read most articles about xss attack (if want to use markdown.js) like this markdown securely and one more markdown-xss-testing.
Please look at my codes,this is enough or not(for xss attack).
This is my server.js
:
var comment=req.body.comment;
var z=markdown.toHTML(comment);
var clean = sanitizeHtml(z, {
allowedTags: ['b', 'i', 'em', 'strong', 'a','h1','img','blockquote'],
allowedAttributes: {'a': [ 'href' ],'img':['src']},
allowedIframeHostnames: []
})
console.log(clean)
And i tried some codes(values are req.body.comment) for xss attacks, like here:
[Example](javascript://\nalert%28%22xss%22%29)//req.body.comment
{@onclick=alert('hi')}some paragraph //req.body.comment
[test](javascript://%0d%0aprompt(1)) //req.body.comment
Outputs here console.log(clean):
<a>Example</a>
{@onclick=alert('hi')}some paragraph //not clickable
<a>test</a>
I have tried many other code and always avoided attacks.