TexToolsAI

DOMPurify Alternatives for Node.js and Markdown: Tested Defaults

By VeloForge Labs · · 4 min read

Quick answer: The usual DOMPurify alternatives are sanitize-html, xss (js-xss) and rehype-sanitize. sanitize-html runs in plain Node without a browser DOM, xss escapes what it blocks, and rehype-sanitize fits remark pipelines. Defaults differ, so test with your own inputs before you ship.

TL;DR: People look for a DOMPurify alternative mostly because DOMPurify needs a DOM, which means adding jsdom on a server. We ran six risky inputs through DOMPurify 3.5.0, sanitize-html 2.18.2 and xss 1.0.17, and one remark pipeline with rehype-sanitize 6.0.0. The results below are for default settings only.

DOMPurify is a good library and a common choice for browser code. This page is for the other cases: servers, edge-style runtimes, and Markdown pipelines. For a head-to-head with the most popular option, see sanitize-html vs DOMPurify.

Why look for an alternative to DOMPurify?

DOMPurify parses HTML with a real DOM. In a browser that is built in. In Node you must supply one, for example jsdom, as in this setup:

import createDOMPurify from "dompurify";
import { JSDOM } from "jsdom";

const DOMPurify = createDOMPurify(new JSDOM("").window);
const clean = DOMPurify.sanitize(dirtyHtml);

That extra dependency is fine in many apps. In serverless functions, edge runtimes or small containers it can be awkward. Some teams also want a sanitizer that works on strings, or one that plugs into a Markdown toolchain. These are the reasons to compare.

Which alternatives are there?

  • sanitize-html: string in, string out, no browser DOM. You configure an allow-list of tags, attributes and URL schemes.
  • xss (js-xss): also string-based. It filters with a whitelist and, by default, escapes the markup it blocks, so it stays visible as text.
  • rehype-sanitize: a plugin for the unified, remark and rehype toolchain. It works on the document tree instead of an HTML string, with a GitHub-like default schema.
  • A hosted endpoint: send Markdown to an API and receive sanitized HTML. TexToolsAI does this with marked and sanitize-html.

We did not test the browser's built-in Sanitizer API here. Support differs between browsers, so check current support before you depend on it.

What do they do with dangerous input?

We converted each Markdown input with marked 18.3.0, then ran the HTML through each sanitizer with default settings. For rehype-sanitize we used a remark pipeline with remark-gfm instead, because it sanitizes a tree.

Input DOMPurify sanitize-html xss
<script>alert(1)</script> removed removed escaped to visible text
<img src=x onerror=alert(1)> <img src="x"> removed (img not allowed) <img src>
[x](javascript:alert(1)) <a>x</a> <a>x</a> <a href>x</a>
<iframe src=...> removed removed escaped to visible text
<form action=...><input></form> form and input kept removed escaped to visible text
![t](https://evil.example/p.png) image kept image removed image kept

Three things stand out:

  1. All of them neutralized the script and the javascript: link. The differences are in how: removal, escaping or an emptied attribute.
  2. Only sanitize-html dropped the remote image, and DOMPurify's defaults kept the live form too. A form on your page can post to another site, and a remote image can leak data in its address. xss escaped the form but kept the image. See LLM output safety.
  3. Images are the gap. If you need images, allow them on purpose, and proxy or restrict where they load from.

What about rehype-sanitize?

In a remark pipeline, inline raw HTML is not rendered unless you opt in. In our run, hi <script>alert(1)</script> produced <p>hi alert(1)</p>: the tag is gone but its text stays. A javascript: link was passed through by the plain pipeline and became <a>c</a> after rehype-sanitize was added. A remote image survived in both cases.

Two lessons follow. First, a toolchain that drops raw HTML is not a sanitizer, so keep the sanitize step. Second, if you later enable raw HTML in the pipeline, rehype-sanitize becomes your only safety layer.

How should you choose?

  • Plain Node, serverless or edge, string input: sanitize-html.
  • You like seeing blocked markup as text, for debugging: xss.
  • You already use remark and rehype: rehype-sanitize.
  • Browser only, or you already run jsdom: DOMPurify is still a good fit.

Whatever you pick, write an allow-list, then test it with the Markdown XSS test payloads and your own content. Passing six inputs is not proof of safety.

Frequently asked questions

Is sanitize-html a drop-in replacement for DOMPurify?

No. The functions look similar, but the options and defaults differ, as the table shows. Re-run your tests after a switch.

Do I need a sanitizer if my Markdown parser escapes HTML?

Yes. Parser settings can be changed, links and images are still output, and a later code change can turn raw HTML back on. Keep a sanitizer as a second layer.

Can I avoid running a sanitizer myself?

You can call an endpoint that returns sanitized HTML. TexToolsAI's POST /api/convert does that, and the docs show examples in cURL, Node.js, Python and Go.