704: Sanitizer API with Frederik Braun
Episode
62 min
Read time
2 min
Topics
Software Development
AI-Generated Summary
Key Takeaways
- ✓setHTML as innerHTML replacement: Replace `element.innerHTML` with `element.setHTML(input)` to get browser-guaranteed XSS-safe HTML insertion. The method uses the browser's own HTML parser, so sanitization stays current as HTML evolves. No configuration required for basic usage — the default safe list handles common elements automatically, making it a near-direct search-and-replace upgrade with zero security expertise needed.
- ✓setHTML vs setHTMLUnsafe: Two methods exist by design. `setHTML` cannot be configured to allow scripts under any circumstance — XSS safety is guaranteed. `setHTMLUnsafe` mirrors React's "dangerously set inner HTML" naming pattern, signaling developer intent. Frederik Braun recommends only using the unsafe variant if a security engineer is actively involved in reviewing the configuration and its implications.
- ✓Allowlist configuration over blocklist: Rather than removing unwanted elements, configure a `new Sanitizer()` with an explicit `elements` array listing only what your use case requires — paragraph, anchor, bold, italic, blockquote, lists. This approach stays evergreen: future HTML elements are automatically excluded. Custom web components with dash-separated names like `dave-card` can be explicitly added to the allowlist by name.
- ✓Defense-in-depth with server-side sanitization: Server-side sanitization and `setHTML` serve complementary roles. Backend libraries sanitize on intake, but browser-side sanitization handles mismatches between server assumptions and the specific browser rendering the page. Academic research confirms that sanitizers outside the browser frequently fail on edge cases — malformed HTML, browser-specific parsing quirks, and newly introduced elements that backend libraries haven't yet catalogued.
- ✓CSP + Trusted Types integration: Content Security Policy's Trusted Types feature can be set to reject all `innerHTML`, `document.write`, `outerHTML`, and `insertAdjacentHTML` calls at runtime via a single HTTP header configuration. Combined with `setHTML`, this creates a hard enforcement layer where unsafe DOM manipulation throws errors rather than silently executing. Despite CSP existing since roughly 2010, over 80% of pages using it still lack sufficient XSS protection.
What It Covers
Frederik Braun, manager of Firefox application security at Mozilla, explains the newly shipped Sanitizer API — a browser-native solution to cross-site scripting (XSS). The episode covers `setHTML` vs `innerHTML`, sanitizer configuration with allowlists, integration with Content Security Policy, DOMPurify's role, and how the API handles custom elements and data attributes.
Key Questions Answered
- •setHTML as innerHTML replacement: Replace `element.innerHTML` with `element.setHTML(input)` to get browser-guaranteed XSS-safe HTML insertion. The method uses the browser's own HTML parser, so sanitization stays current as HTML evolves. No configuration required for basic usage — the default safe list handles common elements automatically, making it a near-direct search-and-replace upgrade with zero security expertise needed.
- •setHTML vs setHTMLUnsafe: Two methods exist by design. `setHTML` cannot be configured to allow scripts under any circumstance — XSS safety is guaranteed. `setHTMLUnsafe` mirrors React's "dangerously set inner HTML" naming pattern, signaling developer intent. Frederik Braun recommends only using the unsafe variant if a security engineer is actively involved in reviewing the configuration and its implications.
- •Allowlist configuration over blocklist: Rather than removing unwanted elements, configure a `new Sanitizer()` with an explicit `elements` array listing only what your use case requires — paragraph, anchor, bold, italic, blockquote, lists. This approach stays evergreen: future HTML elements are automatically excluded. Custom web components with dash-separated names like `dave-card` can be explicitly added to the allowlist by name.
- •Defense-in-depth with server-side sanitization: Server-side sanitization and `setHTML` serve complementary roles. Backend libraries sanitize on intake, but browser-side sanitization handles mismatches between server assumptions and the specific browser rendering the page. Academic research confirms that sanitizers outside the browser frequently fail on edge cases — malformed HTML, browser-specific parsing quirks, and newly introduced elements that backend libraries haven't yet catalogued.
- •CSP + Trusted Types integration: Content Security Policy's Trusted Types feature can be set to reject all `innerHTML`, `document.write`, `outerHTML`, and `insertAdjacentHTML` calls at runtime via a single HTTP header configuration. Combined with `setHTML`, this creates a hard enforcement layer where unsafe DOM manipulation throws errors rather than silently executing. Despite CSP existing since roughly 2010, over 80% of pages using it still lack sufficient XSS protection.
- •DOMPurify as the current fallback: For browsers not yet supporting the Sanitizer API, DOMPurify remains the recommended library — its author Mario Heiderich co-authored the Sanitizer API specification. DOMPurify weighs approximately 8.3KB gzipped. The progressive enhancement pattern is: check `if (window.Sanitizer)`, use `setHTML` if available, otherwise fall back to DOMPurify plus `innerHTML`. Firefox shipped the API first; Chrome support was imminent at recording time.
Notable Moment
Frederik Braun notes that cross-site scripting remains one of the top three most reported vulnerabilities in the US National Vulnerability Database — despite Content Security Policy existing for roughly fifteen years. The implication is that technically sound solutions fail without adoption simplicity, which is precisely the problem `setHTML` is designed to solve.
You just read a 3-minute summary of a 59-minute episode.
Get Shop Talk Show summarized like this every Monday — plus up to 2 more podcasts, free.
Pick Your Podcasts — FreeKeep Reading
More from Shop Talk Show
711: Where did Oh My Zsh Come From? And Using Rails in 2026
Apr 20 · 63 min
a16z Podcast
Ben Horowitz on Venture Capital and AI
Apr 27
More from Shop Talk Show
710: Simen Svale from Sanity
Apr 13 · 56 min
Up First (NPR)
White House Response To Shooting, Shooter Investigation, King Charles State Visit
Apr 27
More from Shop Talk Show
We summarize every new episode. Want them in your inbox?
711: Where did Oh My Zsh Come From? And Using Rails in 2026
710: Simen Svale from Sanity
709: Slopforking a CMS, Apple Browser Feedback, and Custom Theme CSS
708: People Are Not Friction, Getting Rid of the CMS, and Social RSS Follow Up
707: RSS with Social, AI Agent Traffic, and What to Blog About
Similar Episodes
Related episodes from other podcasts
a16z Podcast
Apr 27
Ben Horowitz on Venture Capital and AI
Up First (NPR)
Apr 27
White House Response To Shooting, Shooter Investigation, King Charles State Visit
The Prof G Pod
Apr 27
Why International Stocks Are Beating the S&P + How Scott Invests his Money
Snacks Daily
Apr 27
🏈 “Endorse My Ball” — Fernando Mendoza’s LinkedIn-ing. Intel’s chip-rip-dip. The Vatican’s AI savior. +Uber Spy Pricing
The Indicator
Apr 27
Premium and affordable products are having a moment
Explore Related Topics
This podcast is featured in Best Cybersecurity Podcasts (2026) — ranked and reviewed with AI summaries.
Read this week's Software Engineering Podcast Insights — cross-podcast analysis updated weekly.
You're clearly into Shop Talk Show.
Every Monday, we deliver AI summaries of the latest episodes from Shop Talk Show and 192+ other podcasts. Free for up to 3 shows.
Start My Monday DigestNo credit card · Unsubscribe anytime