如何在没有 HSTS 预加载的情况下让客户端通过 HTTPS 请求?

How to make clients request over HTTPS without HSTS preload?

如果我使用 HTTP http://example.com 请求我们的网站,响应是 301 Moved PermanentlyLocation header 设置为 https://example.com - 当然, 由于 MIM 攻击不安全。

有没有办法直接向浏览器回复 "make the same request again but this time over HTTPS" 而不是明确告诉浏览器 URL?

我期待在 Troy Hunt 的 blog post 上找到这种解决方案,但唯一的建议是使用我们不想要的 HSTS 预加载(即用 Google 注册我们的网站)要做。

HTTP Strict-Transport-Security (HSTS) 允许你发送一个 HTTP Header 来表示“下次你使用这个域时——确保它是通过 HTTPS 即使用户键入 http:// 或使用 link 开头的 http://“.

在 Apache 中,它使用以下配置设置:

Header always set Strict-Transport-Security "max-age=60;"

这会发送一条消息,告诉浏览器记住此 header 60 秒。当您确认没有问题时,您应该增加它。通常建议设置为 63072000(2 年)。

所以这比重定向更安全,因为它是自动发生的,不需要发送不安全的 HTTP 请求,在不安全的网络上可能会被拦截、读取甚至更改。

例如,假设您之前使用家庭 WiFi 登录网上银行,浏览器记住了 HSTS 设置,然后您访问了当地的咖啡店。在这里,您尝试连接到免费 WiFi,但实际上连接到黑客 WiFi。如果您使用 HTTP link、书签或键入 URL 访问您的网上银行,那么 HSTS 将启动,您将直接通过 HTTPS,黑客无法解密您的流量(在合理范围内) ).

所以。一切都很好。您还可以添加 includeSubDomains 属性:

Header always set Strict-Transport-Security "max-age= 63072000; includeSubDomains"

这增加了额外的安全性。

HSTS 的一个缺陷是它需要初始连接才能加载此 HTTP header 并在将来保护您。它也会在 max-age 时间后超时。这就是预加载的用武之地。您可以将您的域提交给浏览器,它们会将此域的 HSTS 设置加载到浏览器代码中并使其永久化,这样即使第一次连接也是安全的。

不过I really don’t like preload to be honest. I just find the fact it’s out of your control dangerous. So if you discover some domain is not using HTTPS (e.g. http://blog.example.com or http://intranet.example.com or http://dev.example.com) then as soon as the preload comes into affect - BANG you’ve forced yourself to upgrade these and quickly as they are inaccessible until then. Reversing from browser takes months at least and few can live with that downtime. Of course you should test this, but that requires going to https://example.com (instead of https://www.example.com) and using includeSubDomains to fully replicate what preload will do and not everyone does that. There are many, many examples of sites getting this wrong.

你还得问问你在防范什么,你让自己面临什么风险?使用 http:// link 黑客拦截可以访问 cookie(站点可以通过使用 cookie 上的 secure 属性来防止这种情况)并可能通过让您保持在 http:/ 来拦截流量/ 而不是升级到 https://(主要通过 HSTS 和 is increasingly flagged by the browser anyway 缓解)。请记住,即使在攻击者 WiFi 网络上,绿色挂锁也意味着连接是安全的(在合理的限制内)。所以只要你在寻找这个(你的用户也在寻找,我承认这更难),风险就相当小。这就是为什么在所有地方都迁移到 HTTPS,然后默认迁移到 HTTPS 如此重要的原因。因此,对于大多数网站,我认为没有预加载的 HSTS 就足够了,并将控制权留给网站所有者。