rehype-sanitize

Removes everything that is not on an allowlist, so untrusted HTML cannot run scripts or inject dangerous attributes.

Category
Security
Maintained by
rehype team
License
MIT
Module format
ESM, default export
Install
npm install rehype-sanitize

What it does

rehype-sanitize walks the hast tree and keeps only the elements, attributes and URL protocols that a schema allows. Everything else is dropped. With no options it uses defaultSchema, which follows the rules GitHub applies to user content. The schema format is documented in hast-util-sanitize, which does the actual work.

Use it whenever HTML comes from a source you do not control: comments, Markdown written by users, CMS content, external feeds.

Basic use

sanitize-basic.mjs
import {rehype} from 'rehype'
import rehypeSanitize from 'rehype-sanitize'

const untrusted = `<p onclick="steal()">Hello <a href="javascript:alert(1)">click</a></p>
<img src="photo.jpg" alt="Photo" onerror="alert(2)">
<script>alert(3)</script>`

const file = await rehype()
  .data('settings', {fragment: true})
  .use(rehypeSanitize)
  .process(untrusted)

console.log(String(file))
Output node sanitize-basic.mjs
<p>Hello <a>click</a></p>
<img src="photo.jpg" alt="Photo">

The event handlers, the javascript: URL and the <script> element are gone; the text, the link text and the image remain.

Allow a little more

Copy defaultSchema and extend the parts you need. Here one extra class is allowed on <div>; the unknown class danger is still removed:

sanitize-schema.mjs
import {rehype} from 'rehype'
import rehypeSanitize, {defaultSchema} from 'rehype-sanitize'

// Start from the default (GitHub-style) schema and allow one extra class.
const schema = {
  ...defaultSchema,
  attributes: {
    ...defaultSchema.attributes,
    div: [...(defaultSchema.attributes.div || []), ['className', 'callout']]
  }
}

const file = await rehype()
  .data('settings', {fragment: true})
  .use(rehypeSanitize, schema)
  .process('<div class="callout danger">Careful!</div>')

console.log(String(file))
Output node sanitize-schema.mjs
<div class="callout">Careful!</div>

Order matters

Sanitize as late as possible after plugins that might introduce unsafe markup, and before plugins whose output you trust but the schema would reject. The default schema prefixes id and name values with user-content- to prevent DOM clobbering, which is why the order relative to rehype-slug changes the result. The sanitizing tutorial shows both orders side by side.

Good to know

  • Sanitizing removes style attributes and <style> elements by default; CSS can be abused, so allow it only with care.
  • <script> content is stripped completely, not kept as text.
  • It works on trees, so raw HTML strings inside Markdown must first become nodes with rehype-raw.

Try it on your own input with the HTML Sanitizer.

Official references

Facts on this page were checked against the package readme on November 11, 2026. Options not covered here are documented upstream.

Use ↑ ↓ to move, Enter to open.