我发现了一个奇怪的问题,并且不知道需要应用什么解决方法。
最简单的安全改进之一是将所有 cookie 设置为 HttpOnly 和 Secure。我知道,如果您在非安全模式下(即方案是 HTTP)打开带有安全 cookie 的网站,则 cookie 将被忽略。
但我们的情况如下。
假设有一个 URL 可以让您登录:contoso.com/AutoLogin/
如果我在 HTTPS 模式下打开它,则设置 AUTH cookie 并且它是安全的:
GET https://contoso.com/AutoLogin/<user token>
Response: Set-Cookie: .ASPXAUTH=<cookie is here>; expires=Fri, 11-Oct-2019 14:51:40 GMT; path=/; secure; HttpOnly
那绝对没问题。我可以在开发工具中看到 cookie。
现在,相同的浏览器 session ,我试图打开相同的 URL,但在 HTTP 模式下。请求 Cookie 不再有 AUTH cookie - 由于安全 cookie 的性质,这很清楚并且可以预测。
GET http://contoso.com/AutoLogin/<user token>
Response: .ASPXAUTH=<here comes the cookie>; expires=Fri, 11-Oct-2019 14:54:07 GMT; path=/; HttpOnly
但是,cookie 未设置,所有后续请求都没有 AUTH cookie。
至少在 Chrome 和 Firefox 中确认了该行为(未在其他浏览器中检查)。
正如您可能注意到的那样,后端是使用 ASP.NET MVC 实现的。
也许,GET 请求是 AJAX 请求这一事实可能会有所帮助。
感谢您的帮助。
最佳答案
我找到了 perfect article关于这个问题。简而言之:
当网站通过 http(端口 80)和 https(端口 443)提供请求并且具有 https cookie 的安全标志时,就会发生这种情况。
如果用户首先访问 https,则站点会设置安全 cookie。然后,对于任何非安全脚本的请求,服务器不会收到 session cookie,因为请求是通过非 https 连接传输的。如果端点具有 session 机制,它将启动新 session ,该 session 通过 header 发送但未存储在浏览器中(因为浏览器已经拥有此名称的 cookie - 它只是不发送它)
不同浏览器行为的作者状态 - 有些浏览器允许通过不安全的 cookie 覆盖安全的 cookie,有些则不允许。
Both Chrome 71, and Firefox 64 prevent a secure cookie from being overwritten on plain http. I tested Edge 42 and it does not work the same way, it will overwrite the secure cookie with the non-secure cookie. Safari 12 works similar to Edge.
我想唯一的解决方案是仅以一种所需的方式使用网站 - 安全与否。
关于authentication - 如果我们尝试设置之前是 Secure 的 cookie,浏览器将忽略 Set-Cookie 响应 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52763345/