DEV Community

Nyrolo
Nyrolo

Posted on Originally published at texttoolsai.org

DOMPurify Alternatives for Node.js and Markdown: Tested Defaults

DOMPurify needs a real DOM. In Node that means adding jsdom, which is awkward on serverless, edge and small containers. So what are the options? I ran six risky inputs through sanitize-html, xss (js-xss) and DOMPurify, and tested rehype-sanitize in a remark pipeline. All of them neutralised the script tag and the javascript: link, but in different ways (removed, escaped, or emptied). They differed on remote images and forms, so decide on images on purpose (allow-list or proxy). My short rule: plain Node or edge with strings, use sanitize-html; want blocked markup visible as text, use xss; already on remark/rehype, use rehype-sanitize; browser-only or already running jsdom, use DOMPurify. Whatever you pick, write an allow-list and test it with XSS payloads. Full table of results and the FAQ are in the original: https://texttoolsai.org/blog/dompurify-alternatives

Top comments (0)