HTTP X-XSS-Protection
响应头是 Internet Explorer,Chrome 和 Safari 的一个特性,当检测到跨站脚本攻击 (XSS)时,浏览器将停止加载页面。若网站设置了良好的 Content-Security-Policy
来禁用内联 JavaScript ('unsafe-inline'
),现代浏览器不太需要这些保护, 但其仍然可以为尚不支持 CSP 的旧版浏览器的用户提供保护。
Header type | Response header |
---|---|
Forbidden header name | no |
语法
X-XSS-Protection: 0 X-XSS-Protection: 1 X-XSS-Protection: 1; mode=block X-XSS-Protection: 1; report=<reporting-uri>
- 0
- 禁止XSS过滤。
- 1
- 启用XSS过滤(通常浏览器是默认的)。 如果检测到跨站脚本攻击,浏览器将清除页面(删除不安全的部分)。
- 1;mode=block
- 启用XSS过滤。 如果检测到攻击,浏览器将不会清除页面,而是阻止页面加载。
- 1; report=<reporting-URI> (Chromium only)
- 启用XSS过滤。 如果检测到跨站脚本攻击,浏览器将清除页面并使用CSP
report-uri
指令的功能发送违规报告。
范例
当检测到XSS攻击时阻止页面加载:
X-XSS-Protection: 1;mode=block
PHP
header("X-XSS-Protection: 1; mode=block");
Apache (.htaccess)
<IfModule mod_headers.c>
Header set X-XSS-Protection "1; mode=block"
</IfModule>
Nginx
add_header "X-XSS-Protection" "1; mode=block";
规范
不属于任何一个规范或草案的一部分。
浏览器兼容性
BCD tables only load in the browser
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.