Chrome不允许儿童iframe读取自己的Cookie。
我有一个带有子iframe的父网页:
- 父节点位于
https://first-site.com
<iframe src="https://second-site.com">
处的子项(父项内部)- Cookie设置
- 路径:'/'
- 安全:true
- httpOnly:false
- 域名:'. second-site.com'
我控制这两个站点,我希望iframe在iframe内执行一个操作,该操作需要.second-site.com
的阅读cookie。外面的父母不需要知道这件事。
适用于除Chrome以外的所有浏览器。
Chrome只是不将子页面自己的Cookie提供给孩子。
在子页面自己的窗口中访问子页面并执行操作在所有浏览器中都有效,包括Chrome。
我已经尝试了这两个选项的所有排列:
- 为cookie设置
secure:false
或secure:true
- 为iframe设置
sandbox="allow-same-origin allow-scripts"
,或删除sandbox
属性
Chrome有什么不同之处,Chrome中的iframe如何访问自己的Cookie?
2条答案
按热度按时间xytpbqjk1#
有一个相对较新的cookie属性,名为
SameSite
,由我的服务器自动设置。禁用此功能(同时保留问题中列出的设置)将允许iframe访问Chrome中自己的Cookie。参见Chrome feature status和IETF draft
更新2020年8月
Chrome现在在没有设置
SameSite
的情况下阻止cookie,因此您需要显式地将其设置为samesite=none
和secure=true
。igsr9ssn2#
这个问题很久以前就被问到了,但它仍然是相关的。
我遇到了同样的问题,并得到了部分解决方案。
您可以尝试在服务器上创建代理,将请求重定向到目标域。这样,您就可以从响应中拦截Cookie,用所需的参数覆盖它们,并将它们发送给客户端。
范例:
您的 index.html
您的代理服务器 * index.php *
您的 sw.js
您的 injection.js
因此,您的iframe的内容将在
document.cookie
中拥有实际的cookie。**PS:**在我的例子中,所有端点都在不同的域上