首页 > 解决方案 > 我可以在 Google Chrome 的 iframe 中允许使用 CORS 的第一方 cookie 吗?

问题描述

withCredentials=true为什么在 Chrome 中启用“阻止第三方 cookie”时,我的跨域请求 cookie不再发送?

环境:Chrome 80.0.3987.122, nodejs server, 2 域 ( a.com, b.com)

测试用例:

  1. test=cookie; SameSite=None; Secure使用域的新规则设置 cookiea.com
  2. <iframe src="https://b.com" />下来放到a.com/index.html
  3. var xhr = new XMLHttpRequest(); xhr.open('GET', 'https://a.com/', true); xhr.withCredentials = true; xhr.send(null);下来放到b.com/index.html
  4. 将 CORS 添加到a.com喜欢:access-control-allow-origin: https://b.comaccess-control-allow-credentials: true
  5. https://a.com在 Chrome 中打开

结果:发送成功的 xhr 请求并获得 200,但是:启用a.com“阻止第三方 cookie”时:请求中不存在cookie cookie。test=cookie

为什么?这是第一方 cookie,因为域a.com是在 chrome 选项卡中打开的。

事实:

  1. 新的 google cookie 设计允许使用SameSite=None; Secure( source )制作 cookie
  2. CORS 允许在site cookies何时发送access-control-allow-credentials: true来源
  3. XMLHttpRequest 允许使用withCredentials=true( source )发送 cookie
  4. RequestCookiefor 1rd-site(not 3rd) 而选项被称为 ablock third-party cookie

铬问题:https ://bugs.chromium.org/p/chromium/issues/detail?id=918322

标签: javascriptnode.jsgoogle-chromecookies

解决方案


为什么?这是第一方 cookie,因为域 a.com 是在 chrome 选项卡中打开的。

这是一个第三方 cookie,因为请求是由 触发的,b.com即使b.com是由a.com.

我想您可以通过使用postMessagewindow.parent(即a.com)发送消息并通过发出 XHR 请求本身来响应那里的页面来解决此问题。


推荐阅读