Skip to main content
CalcChef

HTML Sanitizer

Sanitize pasted HTML at Strict, Moderate or Permissive level: scripts, event handlers and javascript: links are removed, and the tool lists what it took out. How it works

Use the HTML Sanitizer

0 / 50,000 characters
The sanitized HTML appears here after you press Sanitize

Strict and Moderate remove tags outside their list but keep the text inside them. Script, style, iframe, object, embed and form are removed with everything inside. Links to other sites always stay. Keep link targets keeps target attributes such as target="_blank" and adds rel="noopener noreferrer"; Preserve Comments keeps HTML comments.

Advanced: custom tag and attribute rules

Separate names with commas or spaces. The rules start from the chosen level. Script, style, iframe, object, embed and form can never be allowed, and event handler attributes (onclick and the like) are always removed. Links and image addresses with a script scheme stay blocked.

About the HTML Sanitizer

At every level this HTML sanitizer removes attributes that start with on (onclick, onerror), the script, style, iframe, object, embed and form elements with everything inside them, and turns javascript: links into #. The level only decides which harmless tags stay: Strict keeps text formatting, lists, tables and links, Moderate adds images, video and audio, and Permissive keeps what DOMPurify allows by default, including SVG and inline style attributes.

How to use this tool

  1. Paste the HTML into Raw HTML Input. Up to 50,000 characters are read; anything longer is cut, with a warning.
  2. Pick Strict, Moderate or Permissive, and tick Keep link targets or Preserve Comments if the output needs them. Open Advanced to allow or remove extra tags and attributes.
  3. Press Sanitize, read the Removed list and the character count, then copy the output.

Frequently asked questions

What is the difference between Strict, Moderate and Permissive?
<p style="color:red">Red</p> comes back as <p>Red</p> at Strict and Moderate and unchanged at Permissive. <video src="v.mp4" controls></video> is removed at Strict and kept at Moderate and Permissive. <section><p>Kept</p></section> becomes <p>Kept</p> at Strict and Moderate, because section is not on their list but its content is kept.
Does sanitizing HTML stop XSS?
It removes the usual ways markup runs code, but it is one layer. The OWASP XSS cheat sheet says to sanitize only where people must write HTML, to use output encoding when text should be shown exactly as typed, and to treat a Content Security Policy as an extra layer, not the main defense. Changing the HTML after sanitizing it can undo the cleaning, so sanitize last.
Why did my links lose target="_blank"?
Targets are removed unless Keep link targets is ticked. With it ticked, <a href="https://example.com" target="_blank">Site</a> keeps its target and gets rel="noopener noreferrer". A link to another site keeps its href either way.
Why were my data- attributes removed?
DOMPurify allows data- attributes by default, and this tool turns that off at every level: <p data-id="7">x</p> becomes <p>x</p>. They are not listed under Removed.
Can I allow or remove my own tags and attributes?
Yes, under Advanced: custom tag and attribute rules, with names separated by commas or spaces. They start from the chosen level: Also allow tags details, summary keeps <details><summary>More</summary>…</details> at Strict, which otherwise keeps only the text. Also remove tags takes a tag out and keeps its text, and Also remove attributes drops a name such as class everywhere. Adding style to the kept attributes turns off the style removal. script, style, iframe, object, embed and form, event handlers such as onclick, and javascript: links stay blocked, and the page lists any name it ignored.
Is the HTML uploaded anywhere?
No request is made with it. DOMParser and DOMPurify clean it in this browser tab, and the input is not saved, so it is gone when you close or reload the page.

Related Tools

View all tools