緣起
有用戶反映,之前正常使用的站點,出現無法登錄情況。
調查
- 用戶使用場景,使用iframe嵌套了我們的Web,跨在一個跨域
- 用戶升級了最新的Chrome 80
- 根據瀏覽記錄看到,Post請求沒有發送Cookie
- 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 withSameSite=None
andSecure
. 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 表單。詳見下表。
設置了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 解決步驟:
- 地址欄:chrome://flags
- 搜索:SameSite by default cookies
- 選擇:disabled
- 重啟瀏覽器
Asp.Net站點解決
net 4.7.2+
- 需要netframeword 4.7.2 or 4.8
- 安裝補丁: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>
注意:
- sessionState 設置: ASP.NET_SessionId Cookie
- httpCookies 設置:普通Cookie
- 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