返回综合榜
安全与数据Mozilla Hacks

告别 innerHTML,拥抱 setHTML:Firefox 148 带来更强的 XSS 防护

Firefox 148 是首个正式提供标准化 Sanitizer API 的浏览器,该 API 新增了 setHTML() 方法,可在将不受信任的 HTML 插入 DOM 之前对其进行净化,默认即提供 XSS 防护。Mozilla 预计其他浏览器很快会跟进。

XSS(CWE-79)近十年来一直位列前三大 Web 漏洞。Firefox 曾在 2009 年牵头推动内容安全策略(CSP),但 CSP 需要对现有网站进行较大的架构改动并持续由安全专家审查,因此未能覆盖到广大长尾网站。Sanitizer API 旨在填补这一空白,将恶意 HTML 转为无害 HTML,例如移除 script 元素和 onclick 属性。

开发者只需将 innerHTML 赋值替换为 setHTML(),即可用极少的代码改动获得更强的防护。当默认配置过严或过松时,可以提供自定义配置来决定保留或移除哪些元素和属性。Firefox 148 同时支持 Trusted Types,二者结合后可以允许 setHTML() 并阻止其他不安全的 HTML 插入方式,有助于防止未来出现 XSS 回归。

Mozilla 建议先在 Sanitizer API playground 中试验,再将其用于实际页面;并指出采用这些标准后,开发者无需专门的安全团队或大规模实现改动即可防范 XSS。

Goodbye innerHTML, Hello setHTML: Stronger XSS Protection in Firefox 148

Firefox 148 is the first browser to ship the standardized Sanitizer API, which adds a setHTML() method that sanitizes untrusted HTML before inserting it into the DOM, providing XSS protection by default. Mozilla expects other browsers to follow soon.

XSS (CWE-79) has ranked among the top three web vulnerabilities for nearly a decade. Firefox spearheaded Content Security Policy (CSP) in 2009, but CSP requires significant architectural changes and continuous security review, which limited adoption across the long tail of the web. The Sanitizer API aims to fill that gap by turning malicious HTML into harmless HTML, for example removing script elements and onclick attributes.

Developers can adopt stronger protection with minimal code changes by replacing innerHTML assignments with setHTML(). A custom configuration can allow or remove specific elements and attributes when the default is too strict or too loose. Firefox 148 also supports Trusted Types, which can be combined with the Sanitizer API to allow setHTML() while blocking other unsafe insertion methods, helping prevent future XSS regressions.

Mozilla suggests experimenting with the Sanitizer API playground before using it in production, and notes that adopting these standards lets developers prevent XSS without a dedicated security team or major implementation changes.

FirefoxSanitizer APIsetHTMLXSSTrusted Typesweb security
综合8
质量90
猎物热度0
时效0
信息来源1 个来源,主来源:Mozilla Hacks

猎鹰不保存原文正文。标题与摘要用于快速发现信息,事实细节请以来源原文为准。

查看来源