# X-XSS-Protection

**X-XSS-Protection** 是一个已废弃的非标准 HTTP 响应头，用于控制 [Internet Explorer](https://www.edgepedia.org/zh/internet-explorer)、Chrome 和 Safari 中针对反射型跨站脚本（XSS）攻击的内置浏览器过滤器；当此类过滤器检测到攻击时，浏览器会对页面进行净化处理或阻止其加载。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/X-XSS-Protection)</sup> 微软在 Internet Explorer 8 中随 XSS 过滤器一同引入了该响应头，Chrome 和 Safari 则通过 XSS Auditor 采纳了这一思路。<sup>[2](https://noc.org/learn/x-xss-protection)</sup> 该响应头从未成为任何规范或草案的一部分，其控制的浏览器功能也已被移除，因此现代的建议是发送 `X-XSS-Protection: 0`（或不发送任何内容），转而依赖强内容安全策略（CSP）。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/X-XSS-Protection)</sup><sup> • </sup><sup>[3](https://wstg.owasp.org/latest/4-Web_Application_Security_Testing/02-Configuration_and_Deployment_Management/14-Other_HTTP_Security_Header_Misconfigurations/)</sup>

| 关键事实 | 详情 |
|---|---|
| 用途 | 控制浏览器端对反射型 XSS 的过滤；检测到攻击时，浏览器净化页面或阻止其渲染<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/X-XSS-Protection)</sup> |
| 取值 | `0` 禁用过滤；`1` 启用过滤并进行净化；`1; mode=block` 阻止页面；`1; report=<URI>`（仅 Chromium）净化并上报<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/X-XSS-Protection)</sup> |
| 标准化 | 非标准；不属于任何规范或草案<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/X-XSS-Protection)</sup> |
| 起源 | 由微软在 Internet Explorer 8 中引入；Chrome 和 Safari 以 XSS Auditor 的形式采纳<sup>[2](https://noc.org/learn/x-xss-protection)</sup> |
| 移除 | Edge 于 2018 年 7 月弃用其 XSS 过滤器；Chrome 于 2019 年 8 月 5 日禁用 XSS Auditor，并在 Chrome 78 中将其移除；Firefox 从未实现该响应头<sup>[4](https://www.cde.ual.es/wp-content/uploads/2023/10/web-communication-standards-KJNA31552ENN.pdf)</sup><sup> • </sup><sup>[5](https://www.chromium.org/developers/design-documents/xss-auditor/)</sup> |
| 替代方案 | 内容安全策略，它阻止内联脚本执行，而非启发式地检测反射型载荷<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/X-XSS-Protection)</sup> |
| 当前建议 | 发送 `X-XSS-Protection: 0` 以显式禁用过滤器，因为启用它可能在本就安全的站点中引入 XSS 漏洞<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/X-XSS-Protection)</sup><sup> • </sup><sup>[6](https://centralcsp.com/en/docs/web-security/policies/legacy-headers)</sup> |

## 语法与指令

该响应头有四种形式。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/X-XSS-Protection)</sup>

- `X-XSS-Protection: 0` 禁用 XSS 过滤。
- `X-XSS-Protection: 1` 启用过滤，这通常是浏览器的默认值；检测到攻击时，浏览器通过移除不安全的部分来净化页面。
- `X-XSS-Protection: 1; mode=block` 启用过滤，但在检测到攻击时不进行净化，而是阻止页面渲染。
- `X-XSS-Protection: 1; report=<reporting-URI>`，仅在 Chromium 中支持，净化页面并使用 CSP 的 `report-uri` 机制发送违规报告。

在 Chrome 中，报告 URI 在 Chrome 64 和 65 中必须同源；该限制在 Chrome 66 中被取消。<sup>[5](https://www.chromium.org/developers/design-documents/xss-auditor/)</sup>

## 浏览器过滤器的工作方式

Chrome 的 XSS Auditor 在 HTML 解析阶段运行，查找响应体中请求数据的反射。它并不试图缓解存储型 XSS（保存在站点数据库中的恶意脚本）或基于 DOM 的 XSS（对 `document.location.href` 等数据的不安全客户端处理）。除非 URL 或 POST 正文包含 `" > < '` 中的任何字符，处理开销基本为零；一旦包含，就会对可能危险的属性运行更重的分析；这与 Internet Explorer 的 XSS 过滤器形成对比，后者运行开销高昂的正则表达式。<sup>[5](https://www.chromium.org/developers/design-documents/xss-auditor/)</sup>

当 Auditor 发现可能的反射时，Chrome 可以使特定脚本失效，或用 `ERR_BLOCKED_BY_XSS_AUDITOR` 错误页面阻止页面。早期的一种设计仅移除可疑区块，这允许攻击者通过在请求正文中发送要杀死的脚本来从受害者页面“狙击”掉不需要的脚本，因此从 Chrome 57 起，Auditor 会阻止整个响应。<sup>[5](https://www.chromium.org/developers/design-documents/xss-auditor/)</sup>

该设计默认是安全的：如果响应不携带 X-XSS-Protection 响应头，浏览器的审计器保持启用状态。<sup>[7](https://link.springer.com/article/10.1007/s11280-026-01410-1)</sup>

## 该响应头为何可能造成危害

核心问题在于过滤器是启发式的，可能误判服务器意图中页面的哪些部分是合法的。MDN 记录了一个案例：一个页面在一个脚本中定义 `var productionMode = true;`，并在由 `if (!window.productionMode)` 守卫的第二个脚本中运行有漏洞的调试代码。如果 URL 包含第一个脚本的编码副本，执行净化的过滤器可能丢弃合法的定义，认为服务器只是从 URI 中回显了它；于是 `window.productionMode` 求值为 `undefined`，不安全的调试代码被执行。而在不执行任何 XSS 过滤的浏览器中，该页面是完全安全的。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/X-XSS-Protection)</sup>

将响应头设置为 `0` 或 `1; mode=block` 可防止此类缺陷：`0` 让浏览器按原样运行所有脚本，而 `mode=block` 则完全停止处理页面，尽管可嵌入 `<iframe>` 的页面仍可能通过侧信道被探测。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/X-XSS-Protection)</sup> Auditor 的反射匹配还造成了跨源信息泄露：攻击者可以嵌入受害者页面，并猜测其中的字符串，利用是否被阻止作为预言机，这违反了同源策略。<sup>[5](https://www.chromium.org/developers/design-documents/xss-auditor/)</sup>

## 弃用与移除

该响应头背后的浏览器功能在大约十年间被逐步拆除。Edge 于 2018 年 7 月弃用了其 XSS 过滤器。Chrome 于 2019 年 7 月 15 日发布弃用并移除 XSS Auditor 的意向，于 2019 年 8 月 5 日永久禁用该功能，并在 Chrome 78 中将其完全移除。<sup>[4](https://www.cde.ual.es/wp-content/uploads/2023/10/web-communication-standards-KJNA31552ENN.pdf)</sup><sup> • </sup><sup>[5](https://www.chromium.org/developers/design-documents/xss-auditor/)</sup> Firefox 从未实现该响应头，其他基于 Chromium 的浏览器在采用 Chromium 引擎后也放弃了对 Auditor 的支持。<sup>[4](https://www.cde.ual.es/wp-content/uploads/2023/10/web-communication-standards-KJNA31552ENN.pdf)</sup><sup> • </sup><sup>[2](https://noc.org/learn/x-xss-protection)</sup> OWASP 的 Web 安全测试指南现在将该响应头列为已废弃，并在现代浏览器中已被禁用或移除，同时指出它本身可能在旧版浏览器中引入 XSS。<sup>[3](https://wstg.owasp.org/latest/4-Web_Application_Security_Testing/02-Configuration_and_Deployment_Management/14-Other_HTTP_Security_Header_Misconfigurations/)</sup>

[内容安全策略](https://www.edgepedia.org/zh/content-security-policy)取代了它。CSP 表达力更强：它可以彻底阻止内联脚本，通过哈希验证强制执行完整性检查，并且能主动防护，而非充当被动响应的启发式检测器。它还修复了在 X-XSS-Protection 中观察到的一个配置弱点：携带多个不同响应头值的响应会让浏览器仅执行最后一个值；而 CSP 明确允许多个 `Content-Security-Policy` 响应头，并要求浏览器执行所有响应头，因此最严格的策略生效。<sup>[7](https://link.springer.com/article/10.1007/s11280-026-01410-1)</sup>

## 残留使用

该响应头在现实世界中存留，很大程度上是旧模板的惯性所致。在一项针对钓鱼网站和工具包的纵向研究中，在总使用量站点上观察到的 X-XSS-Protection 值中 97.2% 为 `1; mode=block`，2.1% 为 `0`，0.4% 为 `1`；在使用内容发布服务构建的站点上，`1; mode=block` 的占比达到 99.2%。<sup>[8](https://kyungchanlim.github.io/papers/2025/lim_phishing_www25_1.pdf)</sup> 一项针对 2026 年 9 月 4 日抓取的 27 个首页的小型调查发现，仍有 8 个发送该响应头，其中 5 个发送 `0` 以关闭该功能——鉴于该响应头制造漏洞的能力，这是有意的加固选择而非失误。<sup>[9](https://www.querywin.com/blog/x-xss-protection-survey)</sup> 安全响应头参考资料的当前建议是一致的：弃用该响应头或发送 `0`，并依赖不含 `'unsafe-inline'` 的 CSP 来缓解 XSS。<sup>[6](https://centralcsp.com/en/docs/web-security/policies/legacy-headers)</sup><sup> • </sup><sup>[10](https://centralcsp.com/en/blog/legacy-security-headers-to-retire)</sup>

## References

1. [X-XSS-Protection header, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/X-XSS-Protection)
2. [X-XSS-Protection Security Header, NOC.org](https://noc.org/learn/x-xss-protection)
3. [OWASP Web Security Testing Guide: Other HTTP Security Header Misconfigurations](https://wstg.owasp.org/latest/4-Web_Application_Security_Testing/02-Configuration_and_Deployment_Management/14-Other_HTTP_Security_Header_Misconfigurations/)
4. [Web Communication Standards, European Union Institutions (EUIS), Publications Office of the EU](https://www.cde.ual.es/wp-content/uploads/2023/10/web-communication-standards-KJNA31552ENN.pdf)
5. [XSS Auditor design document, Chromium](https://www.chromium.org/developers/design-documents/xss-auditor/)
6. [Legacy security headers and their modern replacements, CentralCSP](https://centralcsp.com/en/docs/web-security/policies/legacy-headers)
7. [The Death of the X-XSS-Protection Header: A Web Security Post-Mortem, World Wide Web (Springer)](https://link.springer.com/article/10.1007/s11280-026-01410-1)
8. [What's in Phishers: A Longitudinal Study of Security Configurations in Phishing Websites and Kits, WWW 2025](https://kyungchanlim.github.io/papers/2025/lim_phishing_www25_1.pdf)
9. [X-XSS-Protection on 27 homepages, querywin blog](https://www.querywin.com/blog/x-xss-protection-survey)
10. [Legacy security headers you can retire, CentralCSP](https://centralcsp.com/en/blog/legacy-security-headers-to-retire)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 网络与安全 › HTTP and web communication protocols › HTTP header fields

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/x-xss-protection

English original: https://www.edgepedia.org/x-xss-protection
