Firefox 158 Release: Safe Markup Insertion with the HTML Sanitizer API and Typed Arithmetic in CSSArticle · digest for November 16
Infographic blueprint of an HTML markup sanitization pipeline using the browser's Sanitizer API filter.
Article4 sections≈ 6 min

Firefox 158 Release: Safe Markup Insertion with the HTML Sanitizer API and Typed Arithmetic in CSS

On October 13, 2026, Mozilla shipped Firefox 158, a release highlighted by the Frontend Programmer's Library channel. There's no flashy UI redesign in the changelog, so it's easy to scroll right past it—yet two major upgrades landed deep inside the engine. The browser expanded filtering for dangerous javascript: links in the native HTML Sanitizer API, and CSS gained typed arithmetic inside calc() when dividing matching units.

When I opened the MDN release notes and bug 2068934, it immediately reminded me of that evergreen headache in any app with comments or rich-text editors: how do you render user-submitted HTML with profile links without handing attackers someone else's session tokens—and without bloating your bundle with third-party parsers? And while we're at it, how do you compute CSS ratios without ugly workarounds?

The Dynamic Markup Trap: Why innerHTML and Regexes Leak XSS

Let's trace step by step what happens when you render someone else's formatted text. If your frontend drops a comment string straight into Element.innerHTML, the parser unquestioningly turns all of it into live DOM nodes. To the engine, there's no difference between a harmless <p> tag and an attacker's payload—it's like letting a delivery driver walk into your server room and plug whatever cables they want into the outlets.

If an attacker submits a comment with a broken image like <img src="avatar.png" onerror="alert('XSS')">, the browser executes the onerror script the moment the image fails to load. Hide a call inside <a href="javascript:stealTokens()">Profile</a>, and clicking the link walks off with user tokens. Filtering with regular expressions always springs a leak: encodings like java&#x0A;script: or malformed tag nesting fool string checks, and the HTML parser helpfully "fixes" the broken markup into a working exploit (mutation XSS).

Since regexes always lose to the parser, sanitization belongs inside the browser engine itself. With the HTML Sanitizer API, the browser parses the string into an isolated tree, strips dangerous nodes, and mounts only the clean result. It gives you two modes with clear boundaries:

  • The safe method Element.setHTML() is built for any untrusted input: comments, reviews, and notes. Regardless of configuration, setHTML() always strips <script>, event handlers like onerror, and javascript: protocol links.
  • The unsafe method Element.setHTMLUnsafe() is strictly for cases where default sanitization would break trusted markup—such as inserting declarative Shadow DOM (<template shadowrootmode="open">). Never pass raw user input to it without a sanitizer.

In Firefox 158 (bug 2068934), the Sanitizer configuration gained the javascriptURLs option and the Sanitizer.setJavascriptURLs() method. Now, with a default new Sanitizer() or after calling Sanitizer.removeUnsafe(), even setHTMLUnsafe() automatically strips javascript: from href and src attributes. Let's run our booby-trapped comment through both when inserting into #comment-box:

const userComment = `
  <p>Привет! Загляни в <a href="javascript:stealTokens()">мой профиль</a>
     и оцени аватарку: <img src="avatar.png" onerror="alert('XSS')">
  </p>
`;

const container = document.getElementById('comment-box');

// 1. Safe insertion: the engine strips javascript: and onerror automatically
container.setHTML(userComment);

// 2. Explicit link control in the sanitizer (Firefox 158+)
const sanitizer = new Sanitizer({ javascriptURLs: false });
if (typeof sanitizer.setJavascriptURLs === 'function') {
  sanitizer.setJavascriptURLs(false);
}

container.setHTMLUnsafe(userComment, { sanitizer });

When container.setHTML(userComment) runs, Firefox parses the markup in the context of the target element, wipes out onerror on the image, and neutralizes javascript:stealTokens() inside the link. Only a clean paragraph remains on the page. In the second block, we pass javascriptURLs: false and call setJavascriptURLs(false)—so even setHTMLUnsafe() won't let that malicious URL into the DOM.

In production, keep in mind that the HTML Sanitizer API is still a W3C draft, and older browsers may not support setHTML(). Check for the method (typeof container.setHTML === 'function') and keep a fallback library ready, as covered in our guide on safe markup rendering and Trusted Types policies.

Typed Arithmetic in CSS: Dividing Units in calc() Without Hacks

Firefox 158 cleared out a similar pile of workarounds in stylesheets by enabling CSS typed arithmetic (bug 2067411). Previously, calc() only let you divide a length by a bare number (calc(1200px / 4)), while dividing two lengths like calc(1200px / 300px) threw an error—as if a calculator refused to tell you how many 30-centimeter tiles fit along a three-meter wall.

Now, when you divide values of the same type, the units cancel out and return a unitless scalar. Divide container width --container-width: 1200px by card width --base-column-width: 300px via calc(var(--container-width) / var(--base-column-width)), and the browser gets 4. You can drop that straight into grid-template-columns: repeat(var(--columns-count), 1fr) or line-height, or multiply it by a duration calc(var(--columns-count) * 75ms) to get a 300ms animation (to work with numeric style objects from JS, check out our article on manipulating styles through CSS Typed OM).

Graphics, Networking, and Notifications: JPEG XL, Click Navigation, and WebTransport

Alongside DOM protection and CSS math, the release brought several other web platform upgrades:

Key Platform Additions in Firefox 158
  • JPEG XL enabled by default (bug 2065096): image/jxl is turned on out of the box and included in the Accept header (image/avif,image/jxl,image/webp,image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5). It supports lossless compression, HDR, transparency, and JPEG transcoding. Back in our Firefox 153 developer overview, it was still tucked behind about:config, whereas now CDNs can serve .jxl natively.
  • Notification navigation (bug 2069920): the navigate option in Notification() and ServiceWorkerRegistration.showNotification() (plus the Notification.navigate property) sets a target URL to open on click without manual event handlers.
  • Network telemetry and accessibility (bug 2007202, bug 1914120, bug 908042): WebTransport.getStats() exposes connection and datagram stats (WebTransport.supportsReliableOnly in bug 2007783 returns false), PerformanceResourceTiming.deliveryType reports cache hits ("cache"), and Firefox for Android now supports aria-busy.

Tasks that used to require external libraries and clever workarounds are steadily moving into the browser standard. Before you reflexively type element.innerHTML = data or maintain duplicate unitless CSS variables in your next commit, ask yourself one quick question: why build manual scaffolding when the engine already knows how to sanitize markup with setHTML() and divide units directly?

Want a deep diveVotes help us choose what to break down in detail. This material has 0 votes.
Sources · 3