安全相关HTTP响应头解读、配置、示例与检测
字数 2391 2025-08-25 22:59:03
HTTP安全响应头配置指南
概述
HTTP安全响应头是在浏览器层面提高Web安全性的有效方法,相比代码层改动更简单方便。本文全面总结各类安全响应头的配置方法、示例及检测工具。
主要安全响应头详解
1. HSTS (HTTP Strict Transport Security)
作用:强制所有HTTP请求使用HTTPS连接,防止非HTTPS连接的安全隐患。
属性:
max-age(必选):作用时间(秒)includeSubDomains(可选):作用于所有子域名preload(可选):纳入预加载列表(谨慎使用)
示例:
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
注意:preload选项会使域名被部分浏览器默认启用HSTS,阻止所有HTTP请求。
2. HPKP (HTTP公钥固定)
作用:防止替换证书的中间人攻击(MITM)。
注意:Chrome自69版本已移除支持,了解即可。
属性:
pin-sha256(必选):base64编码的证书公钥指纹max-age(必选):作用时间(秒)includeSubdomains(可选):是否覆盖子域report-uri(可选):违规报告URL
示例:
Public-Key-Pins: pin-sha256="d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM="; pin-sha256="E9CZ9INDbd+2eRQozYqqbQ2yXLVKB9+xcprMF+44U1g="; report-uri="http://0sec.com.cn/report"; max-age=10000; includeSubDomains
3. Expect-CT
作用:验证签名证书的时间戳,HPKP的替代品。
属性:
max-age=seconds(必选):作用时间(秒)enforce(可选):强制模式,拒绝违反策略的连接report-uri(可选):CT策略违规报告URL
示例:
Expect-CT: max-age=86400, enforce, report-uri="http://0sec.com.cn/report"
4. Referrer-Policy
作用:控制Referer头的发送策略(注意拼写差异)。
可选值:
no-referrer:不带referer头no-referrer-when-downgrade(默认):HTTPS→HTTP时不发送same-origin:同源请求才发送strict-origin:安全传输(HTTPS→HTTPS)才发送origin:只发送originorigin-when-cross-origin:同源发送完整URL,跨域发送originstrict-origin-when-cross-origin:安全传输同源发送完整URL,跨域发送originunsafe-url:所有情况发送完整URL(危险)
示例:
Referrer-Policy: no-referrer
5. Cache-Control
作用:控制浏览器/代理的缓存机制。
常用指令:
max-age:有效时间(秒)public:所有内容缓存private:仅客户端缓存no-cache:响应无变化才使用缓存no-store:所有内容不缓存must-revalidation:缓存失效必须重新验证
示例:
Cache-Control: max-age=0, no-cache, no-store, must-revalidate
6. Expires
作用:设置请求缓存的过期时间。
注意:Cache-Control的max-age优先级高于Expires。
安全配置:
Expires: 0
时间格式示例:
Expires: Wed, 21 Oct 2015 07:28:00 GMT
7. X-Frame-Options
作用:防止点击劫持(Clickjacking),控制页面嵌入。
可选值:
DENY:不允许任何页面嵌入SAMEORIGIN:不允许本域外页面嵌入ALLOW-FROM uri:不允许指定域名外页面嵌入
示例:
X-Frame-Options: SAMEORIGIN
注意:CSP中的frame-ancestors选项有同样作用。
8. X-XSS-Protection
作用:启用浏览器XSS过滤器。
可选值:
0:禁用1:启用1; mode=block:启用并在检测到XSS时停止渲染1; report=URL:启用并报告
推荐配置:
X-XSS-Protection: 1, mode=block
注意:XSS过滤器较易绕过,不应作为唯一防护。
9. X-Content-Type-Options
作用:阻止浏览器解析与Content-Type声明不一致的内容。
配置:
X-Content-Type-Options: nosniff
10. Content-Security-Policy (CSP)
作用:资源加载白名单策略。
常用指令:
default-src:默认资源加载策略script-src:JS加载策略img-src:图片加载策略style-src:样式表加载策略font-src:字体加载策略object-src:<object>,<embed>,<applet>等加载策略media-src:<audio>,<video>加载策略connect-src:Ajax、WebSocket等加载策略frame-src/child-src:frame加载策略
示例:
Content-Security-Policy: script-src 'self'; object-src 'none'; style-src 1.com 2.cn; child-src https
11. X-Permitted-Cross-Domain-Policies
作用:指定客户端能访问的跨域策略文件类型。
可选值:
none:不允许使用策略文件master-only:仅允许主策略文件by-content-type:仅限HTTP(S)的Content-Type:text/x-cross-domain-policyby-ftp-filename:仅限FTP的crossdomain.xmlall:目标域上所有策略文件
配置方法示例
以X-Frame-Options为例:
Apache:
Header always set X-Frame-Options "sameorigin"
Nginx:
add_header X-Frame-Options sameorigin always;
IIS:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="X-Frame-Options" value="sameorigin" />
</customHeaders>
</httpProtocol>
</system.webServer>
lighthttpd:
setenv.add-response-header = ("X-Frame-Options" => "sameorigin",)
知名网站配置范例
GitHub
Content-Security-Policy: default-src 'none'; base-uri 'self'; block-all-mixed-content; connect-src 'self' uploads.github.com www.githubstatus.com collector.githubapp.com api.github.com www.google-analytics.com github-cloud.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com wss://live.github.com; font-src github.githubassets.com; form-action 'self' github.com gist.github.com; frame-ancestors 'none'; frame-src render.githubusercontent.com; img-src 'self' data: github.githubassets.com identicons.github.com collector.githubapp.com github-cloud.s3.amazonaws.com *.githubusercontent.com customer-stories-feed.github.com spotlights-feed.github.com; manifest-src 'self'; media-src 'none'; script-src github.githubassets.com; style-src 'unsafe-inline' github.githubassets.com
Expect-CT: max-age=2592000, report-uri="https://api.github.com/_private/browser/errors"
Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
X-Content-Type-Options: nosniff
X-Frame-Options: deny
X-XSS-Protection: 1; mode=block
cache-control: private, max-age=0
expires: -1
strict-transport-security: max-age=31536000
x-frame-options: SAMEORIGIN
x-xss-protection: 0
cache-control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0
content-security-policy: connect-src 'self' blob: https://(略); object-src 'none'; script-src 'self' 'unsafe-inline' https://*.twimg.com https://www.google-analytics.com https://twitter.com 'nonce-MDRlMWY3ZTMtM2E5Yi00YjQxLTk5N2UtZTVmYzI5ZjA3ZTY2'; style-src 'self' 'unsafe-inline' https://*.twimg.com; worker-src 'self' blob:; report-uri https://twitter.com/i/csp_report?a=O5RXE%3D%3D%3D&ro=false
pragma: no-cache
strict-transport-security: max-age=631138519
x-content-type-options: nosniff
x-frame-options: DENY
x-xss-protection: 0
检测工具
-
在线分析工具:
-
浏览器插件:
- Recx Security Analyser (Chrome插件)
参考资源
- 《Web漏洞防护》- 李建熠
- MDN Web文档
- 各响应头的RFC规范