我的页面上有一个 iframe。由于 Safari 阻止了 3rd 方 cookie,我正在尝试按照“开发人员指南”中的建议使用 Storage Access API:https : //webkit.org/blog/10218/full-third-party-cookie-blocking-and-more / . 我从文档中复制了以下代码:
<script type="text/javascript">
window.addEventListener('load', () => {
document.getElementById('test-button').addEventListener('click', () => {
document.hasStorageAccess().then(hasAccess => {
console.log('hasAccess: ' + hasAccess);
if (!hasAccess) {
return document.requestStorageAccess();
}
}).then(_ => {
console.log('Now we have first-party storage access!');
document.cookie = "foo=bar";
console.log(`document.cookie: ${document.cookie}`);
}).catch(_ => {
console.log('error');
});
});
});
</script>
<button id="test-button">Test</button>
Run Code Online (Sandbox Code Playgroud)
浏览器控制台输出:
[Log] hasAccess: true
[Log] Now we have first-party storage access!
[Log] document.cookie:
Run Code Online (Sandbox Code Playgroud)
如您所见,授权似乎成功,但仍然无法设置 cookie。有谁知道出了什么问题? …
我有一个使用 cookie 身份验证的 iframe。当用户登录时,cookie 在我的域中正常设置。我使用 SameSite=None;Secure。问题是,当第三方网站从我的域中嵌入 iframe 时,我的身份验证 cookie 未通过,因此 iframe 无法对用户进行身份验证。
这在 Chrome 和 Firefox 中工作正常,但在 Safari 中不起作用(它曾经工作到大约一个月前)
我知道SameSite=None的Webkit 错误,据说在 Safari 13 中已修复。我知道 Safari 不再允许 iframe 设置第三方 cookie(多年来一直如此,所以我没有看到它如何与最近的变化相关)。但是,我不是要设置 cookie - 我只是想阅读它。直到最近,这是可能的(请参阅此 SO 问题)。
在 Safari 13 之前,有一种变通方法允许人们通过将首页重定向到 cookie 设置域然后返回原始页面来设置第三方 cookie。在这种情况下,iframe 将能够看到 cookie(因为 iframe 无法写入但可以读取 cookie。
我尝试设置一个没有 SameSite 属性的辅助 cookie,因为这些应该可以工作,但它仍然没有被发送。
无论是否设置了 SameSite,Safari 现在是否会完全丢弃 cookie?如果是这样,他们为什么要费心修复 SameSite 错误?有些东西不加起来。
更新:这似乎与 Apple 的 ITP 2 有关,它对第三方 cookie 设定了严格的标准,甚至根据他们认为可能进行不良跟踪的域来区分域:https : //www.seerinteractive.com/blog/什么是智能跟踪预防/
因此,从我收集的信息来看,这些似乎无法规避这种情况。
更新 2:我想我可能在 Webkit 的存储访问 API 中找到了一个可靠的解决方案:https : …
我调查了使用 Storage Access API https://developer.mozilla.org/en-US/docs/Web/API/Storage_Access_API/从第三个 iframe 访问第一方 cookie的可能性。问题是,尽管存储访问 API 给了我一个访问权限,但document.cookieiframe 内部与上面的不同。
我用的是Firefox 67,dom.storage_access.enabled设置为true。我阅读了 MDN 和 webkit.org 上的所有文档,但仍然无法使其工作。
这是从本地 HTTP 服务器提供的顶级页面 first.test:8002/
<html>
<head>
<script type="application/javascript" src="http://bcjs.test:8003/app.js"></script>
<script type="application/javascript">
{
const key="KEY"
const value="42"
const c = "KEY=42"
document.cookie = c
console.log("document.cookie=", document.cookie)
window.localStorage[key] = c;
console.log("window.localStorage[KEY]=", window.localStorage[key])
}
</script>
</head>
<body>
<!-- injected by app.js above -->
<iframe sandbox="allow-storage-access-by-user-activation allow-scripts allow-same-origin" src="http://bcjs.test:8003/ff-iframe.html"></iframe>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
并且 iframe 包含以下代码(它主要是从上面链接的 MDN 复制的)
<html>
<head>
function makeRequest() {
document.hasStorageAccess().then(hasAccess …Run Code Online (Sandbox Code Playgroud) 我目前正在努力使我的代码与Safari ITP 2.0兼容。在 onClick 上触发的方法中,我的代码类似于以下代码:
if (document.hasStorageAccess && document.requestStorageAccess) {
console.log('On Safari 12');
document.hasStorageAccess().then(
function successful(hasAccess) {
console.log('Testing if hasAccess');
if (hasAccess) {
console.log('Access granted already');
} else {
console.log('Requesting access');
document.requestStorageAccess().then(
function successful() {
console.log('Access request was a success');
window.location.reload();
},
function fail() {
console.log('Storage Access API call failed...');
});
}
},
function rejected(reason) {
console.log('hasStorageAccess failed: ', reason);
});
}
Run Code Online (Sandbox Code Playgroud)
但是,运行它让我得到日志语句“'Storage Access API call failed...'”,而没有来自 Safari 的提示——更令人沮丧的是它以前可以工作,但现在又开始失败了。有什么方法可以调试 requestStorageAccess 调用失败的原因吗?
我试图使ITP调试模式日志按照指示,和我没有得到一些使用了这一点。它给了我一次这个错误:
2018-09-04 15:15:40.930157-0700 0x110c87 Info …