HTTP安全标头是设计用于增强站点安全性的HTTP响应标头。它们指导浏览器的行为,并防止浏览器执行可能危及用户的漏洞。
扫描网站地址: https://securityheaders.com
我的博客扫描结果如下:
可以看到我有6个http安全头没有设置。
Strict-Transport-Security
参考文档:HSTS
HTTP-Strict-Transport-Security
(通常简称为 HSTS)响应标头用来通知浏览器应该只通过 HTTPS 访问该站点,并且以后使用 HTTP 访问该站点的所有尝试都应自动转换为 HTTPS。
语法
Strict-Transport-Security: max-age=<expire-time>
Strict-Transport-Security: max-age=<expire-time>; includeSubDomains
Strict-Transport-Security: max-age=<expire-time>; preload
参数
max-age=<expire-time>
设置在浏览器收到这个请求后的<expire-time>
秒的时间内凡是访问这个域名下的请求都使用 HTTPS 请求。
includeSubDomains
可选
如果这个可选的参数被指定,那么说明此规则也适用于该网站的所有子域名。
preload
可选
查看 预加载 HSTS 获得详情。不是标准的一部分。
Content-Security-Policy
参考文档:内容安全策略 ( CSP )
CSP 的主要目标是减少和报告 XSS 攻击 ,XSS 攻击利用了浏览器对于从服务器所获取的内容的信任。恶意脚本在受害者的浏览器中得以运行,因为浏览器信任其内容来源,即使有的时候这些脚本并非来自于它本该来的地方。
语法
Content-Security-Policy: policy
示例
1、所有内容均来自站点的同一个源 (不包括其子域名)
Content-Security-Policy: default-src 'self'
2、允许内容来自信任的域名及其子域名 (域名不必须与 CSP 设置所在的域名相同)
Content-Security-Policy: default-src 'self' *.trusted.com
3、允许内容来自新人的域名及允许使用内联资源
Content-Security-Policy: default-src 'self' 'unsafe-inline' *.trusted.com
关于default-src的更多说明,可以参考 CSP: default-src
X-Frame-Options
参考文档:X-Frame-Options
X-Frame-Options
HTTP 响应头是用来给浏览器指示允许一个页面可否在 frame
、iframe
、embed
或者 object
中展现的标记。站点可以通过确保网站没有被嵌入到别人的站点里面,从而避免点击劫持 (en-US)攻击。
语法
X-Frame-Options: DENY
X-Frame-Options: SAMEORIGIN
参数
DENY
表示该页面不允许在 frame 中展示,即便是在相同域名的页面中嵌套也不允许。
SAMEORIGIN
表示该页面可以在相同域名页面的 frame 中展示。
X-Content-Type-Options
X-Content-Type-Options
HTTP 消息头相当于一个提示标志,被服务器用来提示客户端一定要遵循在 Content-Type
首部中对 MIME 类型 的设定,而不能对其进行修改。这就禁用了客户端的 MIME 类型嗅探行为,换句话说,也就是意味着网站管理员确定自己的设置没有问题。
语法
X-Content-Type-Options: nosniff
参数
nosniff
下面两种情况的请求将被阻止:
- 请求类型是"
style
" 但是 MIME 类型不是 “text/css
", - 请求类型是”
script
" 但是 MIME 类型不是 JavaScript MIME 类型。
Referrer-Policy
参考文档:Referrer-Policy
Referrer-Policy
首部用来监管哪些访问来源信息——会在 Referer
中发送——应该被包含在生成的请求当中。
语法
Referrer-Policy: no-referrer
Referrer-Policy: no-referrer-when-downgrade
Referrer-Policy: origin
Referrer-Policy: origin-when-cross-origin
Referrer-Policy: same-origin
Referrer-Policy: strict-origin
Referrer-Policy: strict-origin-when-cross-origin
Referrer-Policy: unsafe-url
no-referrer
整个 Referer
首部会被移除。访问来源信息不随着请求一起发送。
no-referrer-when-downgrade
(默认值)
在没有指定任何策略的情况下用户代理的默认行为。在同等安全级别的情况下,引用页面的地址会被发送 (HTTPS->HTTPS),但是在降级的情况下不会被发送 (HTTPS->HTTP)。
origin
在任何情况下,仅发送文件的源作为引用地址。例如 https://example.com/page.html
会将 https://example.com/ 作为引用地址。
origin-when-cross-origin
对于同源的请求,会发送完整的 URL 作为引用地址,但是对于非同源请求仅发送文件的源。
same-origin
对于同源的请求会发送引用地址,但是对于非同源请求则不发送引用地址信息。
strict-origin
在同等安全级别的情况下,发送文件的源作为引用地址 (HTTPS->HTTPS),但是在降级的情况下不会发送 (HTTPS->HTTP)。
strict-origin-when-cross-origin
对于同源的请求,会发送完整的 URL 作为引用地址;在同等安全级别的情况下,发送文件的源作为引用地址 (HTTPS->HTTPS);在降级的情况下不发送此首部 (HTTPS->HTTP)。
unsafe-url
无论是同源请求还是非同源请求,都发送完整的 URL(移除参数信息之后)作为引用地址。
Permissions-Policy
参考文档:Feature-Policy
语法
Feature-Policy: <directive> <allowlist>
<directive>
The Feature Policy directive to apply the allowlist
to. See Directives below for a list of the permitted directive names.
<allowlist>
An allowlist
is a list of origins that takes one or more of the following values, separated by spaces:
*
: The feature will be allowed in this document, and all nested browsing contexts (iframes) regardless of their origin.'self'
: The feature will be allowed in this document, and in all nested browsing contexts (iframes) in the same origin. The feature is not allowed in cross-origin documents in nested browsing contexts.'src'
: (In an iframeallow
attribute only) The feature will be allowed in this iframe, as long as the document loaded into it comes from the same origin as the URL in the iframe’s src attribute.'none'
: The feature is disabled in top-level and nested browsing contexts.<origin(s)>
: The feature is allowed for specific origins (for example,https://example.com
). Origins should be separated by a space.
The values *
(enable for all origins) or 'none'
(disable for all origins) may only be used alone, while 'self'
and 'src'
may be used with one or more origins.
Features have a default allowlist, which is one of: *
, 'self'
, or 'none'
.
最后修改于 2022-09-13
此篇文章的评论功能已经停用。