升级到Chrome 80+的SameSite问题,及Asp.net和aspnetcore站点修改

缘起

有用户反映,之前正常使用的站点,出现无法登录情况。

调查

  1. 用户使用场景,使用iframe嵌套了我们的Web,跨在一个跨域
  2. 用户升级了最新的Chrome 80
  3. 根据浏览记录看到,Post请求没有发送Cookie
  4. Chrome console 提示:A cookie associated with a cross-site resource at http://xxxx/ was set without the SameSite attribute. It has been blocked, as Chrome now only delivers cookies with cross-site requests if they are set with SameSite=None and Secure. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.

原因

Chrome 80 版本升级,提升了Cookie SameSite的默认安全等级,强推 SameSite Cookie

  • Chrome <80 默认值:SameSite=None;请求带Cookie
  • Chrome >=80 默认值:SameSite=Lax;请限制带Cookie

什么是SameSite

SameSite 是 Chrome 51 版本为浏览器的 Cookie 新增的了一个属性, SameSite 阻止浏览器将此 Cookie 与跨站点请求一起发送。其主要目标是降低跨源信息泄漏的风险。同时也在一定程度上阻止了 CSRF(Cross-site request forgery 跨站请求伪造)。

Cookie 的SameSite属性用来限制第三方 Cookie,从而减少安全风险。

它可以设置三个值:

  • Strict
  • Lax
  • None

Strict

Strict最为严格,完全禁止第三方 Cookie,跨站点时,任何情况下都不会发送 Cookie。换言之,只有当前网页的 URL 与请求目标一致,才会带上 Cookie。

Set-Cookie: username=cnblogs; SameSite=Strict;

Lax

Lax规则稍稍放宽,大多数情况也是不发送第三方 Cookie,但是导航到目标网址的 Get 请求除外。

Set-Cookie: username=cnblogs; SameSite=Lax;

导航到目标网址的 GET 请求,只包括三种情况:链接,预加载请求,GET 表单。详见下表。

请求类型 示例 正常情况Lax
链接 发送 Cookie 发送 Cookie
预加载 发送 Cookie 发送 Cookie
GET 表单
发送 Cookie 发送 Cookie
POST 表单 发送 Cookie 不发送
iframe <iframe src="..."></iframe> 发送 Cookie 不发送
AJAX $.get("...") 发送 Cookie 不发送
Image <img src="..." > 发送 Cookie 不发送

设置了Strict或Lax以后,基本就杜绝了 CSRF 攻击。当然,前提是用户浏览器支持 SameSite 属性。

None

Chrome 计划将Lax变为默认设置。这时,网站可以选择显式关闭SameSite属性,将其设为None。不过,前提是必须同时设置Secure属性(Cookie 只能通过 HTTPS 协议发送),否则无效。

只设置SameSite=None,非https协议时,None无效,SameSite=Lax;

Set-Cookie: username=cnblogs; SameSite=None

设置SameSite=None,协议Https时,None有效

Set-Cookie: username=cnblogs; SameSite=None; Secure

Chrome策略更新

在旧版浏览器,如果 SameSite 属性没有设置,或者没有得到运行浏览器的支持,那么它的行为等同于 None,Cookies 会被包含在任何请求中——包括跨站请求。

chrome <80, 默认SameSite=None:

Set-Cookie: username=cnblogs;

chrome:
Set-Cookie: username=cnblogs; SameSite=None;

但是,在 Chrome 80+ 版本中,SameSite 的默认属性是 SameSite=Lax。换句话说,当 Cookie 没有设置 SameSite 属性时,将会视作 SameSite 属性被设置为Lax 。如果想要指定 Cookies 在同站、跨站请求都被发送,那么需要明确指定 SameSite 为 None。具有 SameSite=None 的 Cookie 也必须标记为安全并通过 HTTPS 传送。

chrome >80,默认SameSite=Lax

Set-Cookie: username=cnblogs;

chrome:
Set-Cookie: username=cnblogs; SameSite=Lax;

如何解决

个人用户解决

站点不支持,个人可以设置浏览器,禁用SameSite=Lax设置解决:

Chrome 80 解决步骤:

  1. 地址栏:chrome://flags
  2. 搜索:SameSite by default cookies
  3. 选择:disabled
  4. 重启浏览器

Asp.Net站点解决

net 4.7.2+

  1. 需要netframeword 4.7.2 or 4.8
  2. 安装补丁:KB补丁

Web.Config 设置

<configuration>
 <system.web>
  <compilation targetFramework="4.7.2"/>
  <httpRuntime targetFramework="4.7.2"/>
  <httpCookies sameSite="[Strict|Lax|None|Unspecified]" requireSSL="[true|false]" />
  <anonymousIdentification cookieRequireSSL="false" /> <!-- No config attribute for SameSite -->
  <authentication>
      <forms cookieSameSite="Lax" requireSSL="false" />
  </authentication>
  <sessionState cookieSameSite="Lax" /> <!-- No config attribute for Secure -->
  <roleManager cookieRequireSSL="false" /> <!-- No config attribute for SameSite -->
 <system.web>
<configuration>
注意:
  1. sessionState 设置: ASP.NET_SessionId Cookie
  2. httpCookies 设置:普通Cookie
  3. cookieRequireSSL="True",必须是Https协议

aspnetcore 3.1

ConfigureServices:

services.Configure<CookiePolicyOptions>(options =>
    {
        options.MinimumSameSitePolicy = SameSiteMode.None;
    });

Reference

https://docs.microsoft.com/en-us/aspnet/samesite/system-web-samesite
https://docs.microsoft.com/en-us/aspnet/core/security/samesite?view=aspnetcore-3.1
http://www.ruanyifeng.com/blog/2019/09/cookie-samesite.html
https://cloud.tencent.com/developer/article/1590217

posted on 2020-03-20 11:11  b4b4  阅读(3628)  评论(0编辑  收藏  举报

导航