再见 innerHTML,你好 setHTML:Firefox 148 中的更强 XSS 保护
Goodbye InnerHTML, Hello SetHTML: Stronger XSS Protection in Firefox 148

原始链接: https://hacks.mozilla.org/2026/02/goodbye-innerhtml-hello-sethtml-stronger-xss-protection-in-firefox-148/

## 新的消毒器 API 增强网络安全 跨站脚本攻击 (XSS) 是一种长期存在且普遍的网络漏洞,允许攻击者将恶意代码注入网站。虽然内容安全策略 (CSP) 提供了一种防御手段,但其复杂的实施阻碍了广泛采用。现在,标准化的 **消毒器 API** 提供了一个更简单的解决方案,它在 Firefox 148 中首次亮相。 该 API 提供了一种直接的方法来 **消毒** 不受信任的 HTML *在*将其添加到网页之前,有效地删除有害代码,同时保留安全元素。开发者可以轻松地将潜在危险的 `innerHTML` 赋值替换为更安全的 `setHTML()` 方法,所需的代码更改最少。 该 API 可配置,允许开发者自定义允许的 HTML 元素和属性。它还与 **Trusted Types** 很好地集成,以提供更强大的保护。通过采用消毒器 API,网站可以显著降低 XSS 风险,并为用户创造更安全的使用体验,即使没有专门的安全专业知识。预计将获得更广泛的浏览器支持,有望为所有人提供更安全的网络。

Firefox 148 引入了 `setHTML`,这是一种新方法,旨在提供比旧的 `innerHTML` 更强的跨站脚本 (XSS) 漏洞保护。此更改旨在提供一种更安全的方式将任意 HTML 插入到网页中。 然而,此更新引起了开发人员的担忧。一位评论员指出,在处理安全和不安全方法混合时,可能会产生混淆,并强调了识别哪些函数能正确清理用户输入方面的困难。另一位评论员指出,根据 CanIUse.com 的数据,目前浏览器支持有限,这意味着目前尚不具备广泛依赖 `setHTML` 的可行性。 这场讨论强调了在 Web 开发中平衡安全性和实用性的持续挑战,以及清晰的 API 设计对于防止意外漏洞的重要性。
相关文章

原文

Cross-site scripting (XSS) remains one of the most prevalent vulnerabilities on the web. The new standardized Sanitizer API provides a straightforward way for web developers to sanitize untrusted HTML before inserting it into the DOM. Firefox 148 is the first browser to ship this standardized security enhancing API, advancing a safer web for everyone. We expect other browsers to follow soon.

An XSS vulnerability arises when a website inadvertently lets attackers inject arbitrary HTML or JavaScript through user-generated content. With this attack, an attacker could monitor and manipulate user interactions and continually steal user data for as long as the vulnerability remains exploitable. XSS has a long history of being notoriously difficult to prevent and has ranked among the top three web vulnerabilities (CWE-79) for nearly a decade.

Firefox has been deeply involved in solutions for XSS from the beginning, starting with spearheading the Content-Security-Policy (CSP) standard in 2009. CSP allows websites to restrict which resources (scripts, styles, images, etc.) the browser can load and execute, providing a strong line of defense against XSS. Despite a steady stream of improvements and ongoing maintenance, CSP did not gain sufficient adoption to protect the long tail of the web as it requires significant architectural changes for existing web sites and continuous review by security experts.

The Sanitizer API is designed to help fill that gap by providing a standardized way to turn malicious HTML into harmless HTML — in other words, to sanitize it. The setHTML( ) method integrates sanitization directly into HTML insertion, providing safety by default. Here is an example of sanitizing a simple unsafe HTML:

document.body.setHTML(`<h1>Hello my name is <img src="x" 
onclick="alert('XSS')">`);

This sanitization will allow the HTML <h1> element while removing the embedded <img> element and its onclick attribute, thereby eliminating the XSS attack resulting in the following safe HTML:

<h1>Hello my name is</h1>

Developers can opt into stronger XSS protections with minimal code changes by replacing error-prone innerHTML assignments with setHTML(). If the default configuration of setHTML( ) is too strict (or not strict enough) for a given use case, developers can provide a custom configuration that defines which HTML elements and attributes should be kept or removed. To experiment with the Sanitizer API before introducing it on a web page, we recommend exploring the Sanitizer API playground.

For even stronger protections, the Sanitizer API can be combined with Trusted Types, which centralize control over HTML parsing and injection. Once setHTML( ) is adopted, sites can enable Trusted Types enforcement more easily, often without requiring complex custom policies. A strict policy can allow setHTML( ) while blocking other unsafe HTML insertion methods, helping prevent future XSS regressions.

The Sanitizer API enables an easy replacement of innerHTML assignments with setHTML( ) in existing code, introducing a new safer default to protect users from XSS attacks on the web. Firefox 148 supports the Sanitizer API as well as Trusted Types, which creates a safer web experience. Adopting these standards will allow all developers to prevent XSS without the need for a dedicated security team or significant implementation changes.

 


Image credits for the illustration above: Website, by Desi Ratna; Person, by Made by Made; Hacker by Andy Horvath.

 

More articles by Tom Schuster…

Frederik Braun builds security for the web and for Mozilla Firefox from Berlin. As a contributor to standards, Frederik is also improving the web platform by bringing security into the defaults with specifications like the Sanitizer API and Subresource Integrity. When not at work, Frederik likes reading a good novel or going on long bike treks across Europe.

More articles by Frederik Braun…

Content Security Tech Lead

More articles by Christoph Kerschbaumer…

联系我们 contact @ memedata.com