会话 cookie 设置`SameSite=None;安全;` 不起作用

Session cookie set `SameSite=None; Secure;` does not work

我添加了 SameSite=None;安全的;设置cookie。但是 cookie 没有设置,我无法登录我的网站。

response.writeHead(200, {
  'Content-Type': 'application/json',
  'Set-Cookie': 'token=' + token + '; SameSite=None; Secure; Expires=' + time.toUTCString() + '; Path=/' + '; Domain=' + hostname,
  'csrf-token': csrfToken
});

我在应用程序>存储>Cookies 下的开发人员工具中查看了 cookie,并查看了更多详细信息。它显示一条警告消息:

this set-cookie was blocked because it was not sent over a secure connection

chrome 阻止 cookie,因为我在开发环境中工作并且发送 http 请求。但是这个在 Firefox 浏览器上的测试可以正确登录。
我把 secure 这个词放在 cookie 里,它工作正常,但是因为 secure 这个词必须用在 samesite = none为了跨域,否则cookie会被屏蔽
我的问题是为什么当我使用 secure 时,只有 Chrome 浏览器会阻止 cookie,但在其他浏览器中却是这样。 而且,如果我不使用 secure,我将无法测试支付网关,因为如果我不使用 secure,它会阻止 Chrome cross-orign...

My question is why when I use secure, only the Chrome browser blocks the cookie, but it is true in other browsers

我不确定其他浏览器,但 Chrome 根据此 IETF draft.

实施允许具有 secure 属性的 cookie 通过安全连接的策略

虽然此草案是为 Chrome 实施的,但它不在 Firefox 上,这就是为什么在 Firefox 上您转到 about:config > network.cookie.sameSite.noneRequiresSecure,默认值为 false.

如果您只需要为您的本地开发环境执行此操作,您可以通过 禁用

保留 chrome 中 cookie 的旧行为
  1. chrome://flags/#same-site-by-default-cookies
  2. chrome://flags/#cookies-without-same-site-must-be-secure

I have to support legacy http clients, but if I make https:// origin secure , I can't set cookie from http, more over I can't access this cookie from http, my goal is to have SameSite=None, Secure on http and not secure on http:// origin, any ideas, instead of establishing protests near google office ?

鉴于它将在不久的将来成为标准,我怀疑您是否能够为客户端应用程序实现此行为,唯一的途径是安全,HTTPS。

参考:

  1. https://web.dev/samesite-cookies-explained/#changes-to-the-default-behavior-without-samesite
  2. https://redmondmag.com/articles/2020/01/28/samesite-cookie-changes-break-apps.aspx

Sometome cookie 无法按预期工作,因为某些 cookie 滥用了 sameSite 属性。 Cookie SomeCookie 被拒绝,因为它具有 sameSite=none 属性但缺少 secure 属性。因此任何请求 SameSite=None 的 cookie 必须标记为 Secure.

Set-Cookie: product=pen; SameSite=None

要解决此问题,您必须将 Secure 属性添加到 SameSite=None cookie。

Set-Cookie: flavor=choco; SameSite=None; Secure

A 安全 cookie 只会通过 HTTPS 协议通过加密请求发送到服务器。

注意: 不安全站点 (http:) 无法使用 Secure 指令设置 cookie。