The HTML Sanitizer API gives browsers a built-in method, Element.setHTML(), that inserts HTML into a page and strips scripts and other unsafe markup first. It works in Chrome, Edge and Firefox as of November 2026. Safari does not support it yet.
That matters for anyone who builds CMS themes, blocks or widgets. Cross-site scripting (XSS) bugs often start with one line: element.innerHTML = someString. When that string holds content from users or editors, a single crafted tag can run JavaScript on your site. This piece covers what the API does, how setHTML() differs from innerHTML and setHTMLUnsafe(), how to write a custom allow list, browser support, and a fallback for Safari.
The short version: use setHTML() anywhere you used to put untrusted HTML into innerHTML. It always removes script-running elements and attributes, even if your configuration allows them. Feature-detect it, keep a library fallback for Safari, and keep sanitizing on the server too.
Why innerHTML keeps causing XSS bugs
innerHTML parses whatever string you give it and adds the result to the page. It does not care where the string came from. If an attacker can get <img src=x onerror=...> into a comment, a profile field or a block attribute, that handler can run when the HTML is inserted.
CMS platforms ship fixes for this class of bug all the time. WordPress’s 7.1.4 release notes list 12 security fixes from October 28, 2026, and eight of those entries are stored XSS issues. They include problems in block markup, the Interactivity API, the Categories block and comment pagination blocks. Our post on WordPress 7.1.4 covers that release.
Server-side filters such as WordPress’s wp_kses_post() still matter. WordPress’s function reference describes it as a function that "Sanitizes content for allowed HTML tags for post content." The gap is front-end JavaScript. Scripts that fetch content from a REST endpoint and drop it into the page with innerHTML skip the server’s rendering path, and that is where setHTML() helps.
What the HTML Sanitizer API does
MDN’s HTML Sanitizer API guide describes two families of methods.
| Method | Type | What it does |
|---|---|---|
Element.setHTML() |
Safe | Sanitizes, then replaces the element’s content |
ShadowRoot.setHTML() |
Safe | Same, for a shadow root |
Document.parseHTML() |
Safe | Sanitizes and parses into a new document |
setHTMLUnsafe(), parseHTMLUnsafe() |
Unsafe | Use only the sanitizer you pass; with none, allow everything the context allows |
According to MDN, the safe methods always remove what it calls XSS-unsafe content. That baseline covers <script>, <iframe>, <frame>, <embed>, <object>, <use> and every event handler attribute such as onclick.
When you call a safe method with no configuration, it uses the default sanitizer. MDN says the default goes further than the baseline. It also removes items that could help clickjacking or spoofing, plus comments and data-* attributes.
The methods are also context-aware. MDN’s setHTML() reference says they drop elements that are not valid where you insert them, such as a <col> outside a table.
How to replace innerHTML with setHTML()
The basic swap is one line:
const box = document.querySelector("#comment-body");
// Before: box.innerHTML = userHtml;
box.setHTML(userHtml);
If userHtml is abc <script>alert(1)</script> def, the script element is removed and the text around it stays. The script never reaches the page.
setHTML() returns nothing. It throws a TypeError if you pass an invalid configuration, such as one that both allows and removes elements, per MDN.
Writing a custom allow list with the HTML Sanitizer API
The default is a good start. For comment boxes and member profiles, you often want a tighter list. You can pass a Sanitizer object or a plain configuration:
const basic = new Sanitizer({
elements: ["p", "br", "a", "strong", "em", "ul", "ol", "li"],
attributes: ["href"],
});
box.setHTML(userHtml, { sanitizer: basic });
MDN describes two kinds of configuration:
- Allow configuration uses
elementsandattributes. Anything not listed is dropped. MDN calls this "generally safer." - Remove configuration uses
removeElementsandremoveAttributes. Anything not listed stays, subject to the safe baseline.
You cannot mix elements with removeElements, or attributes with removeAttributes, in one configuration.
A Sanitizer object also has methods such as allowElement(), removeElement() and replaceElementWithChildren(). MDN notes that a Sanitizer is likely more efficient to share and reuse than a plain configuration object. Build it once per page, not on every insert.
One rule holds no matter what you configure. If you add "script" to the allow list and call setHTML(), the script is still removed. MDN says a custom sanitizer used with a safe method is implicitly updated to remove anything XSS-unsafe.
setHTML() vs setHTMLUnsafe()
setHTMLUnsafe() exists for trusted HTML that needs something the safe methods block, such as an inline handler in markup your own team writes.
With no sanitizer, it inserts everything the context allows. With a sanitizer, it applies only that sanitizer, with no safe baseline on top. MDN’s guidance is to use the unsafe methods only when the HTML must contain some XSS-unsafe elements or attributes.
The unsafe methods also work with Trusted Types, a browser feature that forces risky DOM calls to accept only values that passed through a policy. The safe methods do not need Trusted Types, because they already filter. Firefox added Trusted Types in version 148, according to its release notes.
For editor content in a CMS, the safe method is almost always the right call.
Browser support for the HTML Sanitizer API
Support as of November 2026, from each browser’s own release notes:
| Browser | First version with the API | Release date |
|---|---|---|
| Firefox | 148 | February 24, 2026 |
| Chrome | 146 | March 10, 2026 |
| Edge | 146 | March 12, 2026 |
| Safari | Not supported | No date announced |
Firefox 148’s release notes say the HTML Sanitizer API "is now supported, along with related methods like setHTML()." Chrome 146’s release notes list the Sanitizer API, with the goal "to make it easier to build XSS-free web applications." Edge 146’s web platform notes list it too.
For Safari, WebKit’s tracking bug, "Implement Sanitizer API", was still open with status NEW when we checked on November 2. WebKit’s post on features in Safari 27.1, published October 23, does not mention it.
Because Safari lacks it, MDN marks the API as "Limited availability," which means it is not Baseline. Our guide to browser engines explains why one engine can hold a feature back.
Recent changes in Firefox and Chrome
Both engines kept adjusting the API this fall. If you wrote code against it in spring, check these:
- Firefox 158 (October 13, 2026) added the
javascriptURLsoption andSanitizer.setJavascriptURLs(), according to its release notes. The option controls whether a sanitizer removesjavascript:URLs, like<a href="javascript:...">, with unsafe methods. Safe methods always remove them. As a result,setHTMLUnsafe()with the default sanitizer now removes them too. - Firefox 159 (October 27, 2026) sanitizes while it parses, instead of parsing first and cleaning up after. The release notes say the result is the same, except neighboring text now lands in one text node.
- Chrome 157, scheduled for stable on November 3, 2026, adds the same
javascriptURLsoption. Chrome’s release notes say nothing changes when you do not use the option.
Expect more small changes as both engines refine the API. Our post on Chrome’s two-week release cycle covers how to plan QA around faster browser releases.
Where the HTML Sanitizer API spec lives now
The API started in a Web Platform Incubator Community Group draft. That WICG draft now says "The Sanitizer API has been moved to WHATWG HTML" and that it should no longer be used for implementations.
The HTML Standard’s Sanitizer section is the current reference. It also defines newer insertion methods, such as appendHTML() and beforeHTML(), with safe and unsafe versions. Check MDN for support before you use those.
A fallback for Safari
Until Safari ships the API, use feature detection with a library fallback. DOMPurify is the common choice. Its GitHub README describes it as an XSS sanitizer for HTML, MathML and SVG, and says it inspired the HTML Sanitizer API.
function insertSafe(el, html) {
if ("setHTML" in Element.prototype) {
el.setHTML(html);
} else if (window.DOMPurify) {
el.innerHTML = DOMPurify.sanitize(html);
} else {
el.textContent = html;
}
}
The last branch shows the HTML as plain text, which is safe but less readable. Load the library only for browsers that need it if page weight matters.
Two caveats. The browser default and DOMPurify’s default are not identical, so the same input can produce slightly different output in Safari. Test the elements you care about in both. And keep server-side sanitizing, because the browser can only protect what passes through these methods.
el.innerHTML after setHTML() and writing it into another element with innerHTML skips sanitizing. That can open the door to mutated XSS. Pass the string through setHTML() again instead.Rolling out the HTML Sanitizer API in CMS front ends
- Search your theme and block code for
innerHTML,insertAdjacentHTMLandouterHTML. List each place that inserts content you do not fully control. - Swap untrusted inserts to
insertSafe()orsetHTML()directly. - Start with the default sanitizer. Add a custom allow list only where the default removes something you need.
- Test in Safari to confirm the fallback path.
- Keep server-side filters like
wp_kses_post()in place. - Track security releases for your CMS and plugins. Our posts on what a CVE is and blocking PHP execution in upload folders cover other layers of the same defense.
Frequently Asked Questions
What is the HTML Sanitizer API?
It is a browser feature that cleans a string of HTML before inserting it into the page. Its main method, setHTML(), removes scripts, event handlers and other unsafe markup.
Is setHTML() a drop-in replacement for innerHTML?
For untrusted HTML, yes. MDN says it should replace innerHTML when the string comes from users or other untrusted sources. The output can differ, because unsafe parts are removed.
Which browsers support setHTML()?
Firefox 148 and later, Chrome 146 and later, and Edge 146 and later. Safari does not support it as of November 2026.
Can I allow script tags with setHTML()?
No. The safe methods always remove script elements and event handler attributes, even if your configuration allows them.
When should I use setHTMLUnsafe()?
Only for trusted HTML that needs elements or attributes the safe methods block. With no sanitizer passed, it inserts everything the context allows.
Do I still need DOMPurify?
MDN says third-party libraries should not be needed with the safe methods. You still need one as a fallback for Safari until it supports the API.
Does the HTML Sanitizer API replace server-side sanitizing?
No. It protects HTML inserted by browser JavaScript. Keep server-side filters such as wp_kses_post() in WordPress.
What does the default sanitizer remove?
All XSS-unsafe elements and attributes, plus items that could help clickjacking or spoofing, comments and data-* attributes, according to MDN.
Where is the HTML Sanitizer API defined?
In the WHATWG HTML Standard. The older WICG draft says the API has moved there.