https://developer.mozilla.org/en-US/docs/Web/Security/HTTP_strict_transport_security
【阅读理解式翻译,非严格遵循原始文档,以更生动表现出文章本义】
HTTP Strict Transport Security (often abbreviated as HSTS) is a security feature that lets a web site tell browsers that it should only be communicated with using HTTPS, instead of using HTTP.
HSTS 是这样一种功能, 其让网站可以告知浏览器, 此网站的访问只能通过HTTPS, 以彻底替换HTTP协议。
理解: 因为HTTP是明文的协议,容易泄露内容, 在网络上容易被黑客截获报文, 或者访问被欺骗到钓鱼网站, 所以使用HTTPS访问目标网站可以解决上面两个风险。
If a web site accepts a connection through HTTP and redirects to HTTPS, the user in this case may initially talk to the non-encrypted version of the site before being redirected, if, for example, the user types http://www.foo.com/ or even just foo.com.
This opens up the potential for a man-in-the-middle attack, where the redirect could be exploited to direct a user to a malicious site instead of the secure version of the original page.
The HTTP Strict Transport Security feature lets a web site inform the browser that it should never load the site using HTTP, and should automatically convert all attempts to access the site using HTTP to HTTPS requests instead.
众所周知, https功能可以认证目标网站的真实性, 并且可以保证传输数据的不被偷窥。
所以很多网站(包括现在的baidu.com)都是使用http跳转到https, 此不跳转使用 http协议的 302 等状态。
这种跳转方式, 开启了中间人攻击的可能(中间人攻击, 可以理解为 本次访问到目标网站上的链路上, 所有涉及到HTTP应用节点都可能作为中间人角色出现, 例如公司搭建的上网代理服务器, 请求访问经过的路由器网关)。
因为对于第一次使用 http 访问, 有可能被中间人利用跳转到 一个恶意的网站上, 而不是目标网站的https访问方式。
HSTS功能给了网站主动告知浏览器, 浏览器应该从不使用http方式访问我这个网站, 应该自动切换所有使用http访问本网站的请求, 为https方案。
You log into a free WiFi access point at an airport and start surfing the web, visiting your online banking service to check your balance and pay a couple of bills. Unfortunately, the access point you're using is actually a hacker's laptop, and they're intercepting your original HTTP request and redirecting you to a clone of your bank's site instead of the real thing. Now your private data is exposed to the hacker.
Strict Transport Security resolves this problem; as long as you've accessed your bank's web site once using HTTPS, and the bank's web site uses Strict Transport Security, your browser will know to automatically use only HTTPS, which prevents hackers from performing this sort of man-in-the-middle attack.
你在机场使用无线上网, 接入了一个黑客设置的无线SSID, 你开始浏览web, 去访问在线银行服务, 检查余额并支付一些账单。
非常不幸,你正在使用的接入点, 实际上一个黑客的笔记本, 他们接活了你的原始 http请求, 并将此请求跳转到了 他们精心制作的 你的银行网站的 一个副本, 但这绝不是你的银行, 很危险吧 , 在副本网站上, 你提交的一切信息都将被暴漏给 黑客。
【 现在国内银行信用卡消费, 对于网站的消费支付, 都会要求要求手机即时验证码, 这样可以避免钓鱼网站盗取到你的信用卡密码后, 进行网络消费。 同时黑客也无法在实体店的POS机器上消费, 他没有卡, 如果他有你的卡的副本, 那就危险了。 】
HSTS解决了此问题, 一旦你使用曾经使用https访问过你的银行网站, 并且此银行网站启用了 Strict Transport Security(严格传输安全), 你的浏览器将自动只使用https协议访问网站, 这样阻止了黑客执行这种类型的中间人攻击。
ps: 浏览器启动此功能后, 无论用户输入http地址, 还是html和脚本使用使用的http地址, 浏览器都作为https地址访问。
这里说明, hsts有缺点, 必须让用户https访问过, 是否可以这个第一次也可以省去? 且看后来描述。
Enabling this feature for your site is as simple as returning the
Strict-Transport-Security
HTTP header when your site is accessed over HTTPS:Strict-Transport-Security: max-age=expireTime [; includeSubDomains] [; preload]
max-age
- The time, in seconds, that the browser should remember that this site is only to be accessed using HTTPS.
includeSubDomains
Optional- If this optional parameter is specified, this rule applies to all of the site's subdomains as well.
preload
Optional- See Preloading Strict Transport Security for details. Not part of the specification.
Note: TheStrict-Transport-Security
header is ignored by the browser when your site is accessed using HTTP; this is because an attacker may intercept HTTP connections and inject the header or remove it. When your site is accessed over HTTPS with no certificate errors, the browser knows your site is HTTPS capable and will honor theStrict-Transport-Security
header.
启用网站的此功能是简单的, 仅仅在响应报文头中 添加 Strict-Transport-Security
头域, 当你的网站使用https协议访问的时候。
此头域值, 可以包括 超期时间, 是否作用于子域名, 是否支持 preload STS功能。
请注意, 为什么不在http请求的的响应报文中? 有两个原因:
1、添加此报文, 因为作为中间人攻击, 黑客可以获取此响应的全部内容, 将此头域删除, 所以没有根本没有作用。
2、 同时当网站被https访问, 并且没有证书错误, 浏览器知道你的网站是支持 https协议的, 将会遵守此头域, 实施hsts策略。
The first time your site is accessed using HTTPS and it returns the
Strict-Transport-Security
header, the browser records this information, so that future attempts to load the site using HTTP will automatically use HTTPS instead.When the expiration time specified by the Strict-Transport-Security header elapses, the next attempt to load the site via HTTP will proceed as normal instead of automatically using HTTPS.
Whenever the Strict-Transport-Security header is delivered to the browser, it will update the expiration time for that site, so sites can refresh this information and prevent the timeout from expiring. Should it be necessary to disable Strict Transport Security, setting the max-age to 0 (over a https connection) will immediately expire the
Strict-Transport-Security
header, allowing access via http.
正常流程: 浏览器第一次访问你的网站使用https协议, 返回了 此头域, 浏览器会记录此信息, 一遍将来的加载此网站的请求,都自动使用https替代。
关于超期: 如果此头域指定了超期时间, 并且此超期时间已经到期, 则下次的http访问将被允许, 而不是自动使用https替代。
超期延迟: 如果网站想延长浏览器的超期时限, 则网站在后续过访问中, 可以继续设置此头域, 并设置新的超期时间, 则浏览器使用此策略的超期时间, 从这次网站设置时刻起计算。 如果网站想关闭此策略, 设置 max-age为 0.
Google maintains an HSTS preload service. By following the guidelines and successfully submitting your domain browsers will never connect to your domain using an insecure connection. While the service is hosted by Google, all browsers have stated to use (or start using) the submitted domains.
- Informations and consultation of the HSTS preload list in Chrome : https://www.chromium.org/hsts
- Consultation of the Firefox HSTS preload list : nsSTSPreloadList.inc
谷歌维护一个 hsts 预加载服务。 按照指南, 成功提交你的域名, 浏览器将从不连接你的 域名, 使用http连接。 尽管此服务是谷歌管理, 所有的浏览器都可以使用。
Specification | Status | Comment |
---|---|---|
HTTP Strict Transport Security (HSTS) | IETF RFC | Initial definition |
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 4.0 | 4.0 (2.0) | 11[1] | 12 | 7 |
[1] Added with KB 3058515 in June 2015
进阶:
https://www.eff.org/deeplinks/2014/02/websites-hsts
https://blog.mozilla.org/security/2012/11/01/preloading-hsts/
https://www.owasp.org/index.php/HTTP_Strict_Transport_Security