javascript - 存储 API 授予后如何访问第一方 cookie?
问题描述
我研究了使用存储访问 API https://developer.mozilla.org/en-US/docs/Web/API/Storage_Access_API/从第 3 个 iframe 访问第一方 cookie 的可能性。问题是,尽管存储访问 API 给了我访问权限,但document.cookie
iframe 内部与上面的不同。
我用的是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>
并且 iframe 包含以下代码(主要是从上面链接的 MDN 复制)
<html>
<head>
function makeRequest() {
document.hasStorageAccess().then(hasAccess => {
if (hasAccess) {
console.log("document.hasStorageAccess")
} else {
return document.requestStorageAccess()
}
}).then(_ => {
// example from MDN
console.log("3rd party: document.cookie=", document.cookie)
document.cookie = "foo=bar"; // set a cookie
const key = "KEY";
console.log("window.localStorage[KEY]=", window.localStorage[key])
console.log("document=", document)
console.log("window=", window)
console.log("window.parent=", window.parent)
console.log("window.parent.document=", window.parent.document)
}).catch(reason => {
console.log("Some promise have failed, reason=", reason)
})
}
</script>
</head>
<p>
<button onclick="makeRequest()">Make request</button>
</p>
</html>
所以意图很明确 1. 第一方将 KEY=42 存储到 document.cookie 2. 第三方 iframe 请求并访问和打印 document.cookie 并存储foo=bar
在那里。
问题是,尽管 hasStorageAccess 或 requestStorageAccess 解析为 true,但 cookie 和存储看起来不同。我希望 document.cookie 现在会解析到第一方商店。
document.cookie= KEY=42 #b.html:31:10
window.localStorage[KEY]= KEY=42 #b.html:34:10
3rd party: document.cookie= foo=bar #ff-iframe.html:25:17
window.localStorage[KEY]= undefined #ff-iframe.html:28:17
document= HTMLDocument http://bcjs.test:8003/ff-iframe.html #ff-iframe.html:30:17
window= Window http://bcjs.test:8003/ff-iframe.html #ff-iframe.html:31:17
window.parent= Window http://blesk.test:8002/b.html #ff-iframe.html:32:17
Some promise have failed, reason= DOMException: "Permission denied to access property "document" on cross-origin object" #ff-iframe.html:36:17
请注意,DOMException
这对我访问第一方 cookie 的能力没有任何影响。我最初使用https://webkit.org/blog/8124/introducing-storage-access-api/中描述的表单,但是.then
使用两个函数回调调用对访问 Cookie Jar 没有任何影响。
任何线索可能是问题所在?还是该功能过于实验性,以至于现在可能有问题?
解决方案
看起来您的点击事件被document.hasStorageAccess()
承诺所掩盖。您是否尝试过document.requestStorageAccess()
直接在点击处理程序内部而不是在hasStorageAccess
回调内部执行?
推荐阅读
- go - Golang 用文字 \xnn 解码字符串
- python - 在 Raspberry Pi / Linux 上从 Python 脚本运行和停止 C++ 程序
- excel - 如何在excel中使用命名范围作为IF语句的输出?
- asp.net-core - ASP.Net Core 2.2 不在 Azure webApp 上呈现完整的 HTML
- android - 为什么我的应用程序没有在我的 recyclerview 中显示数据库中的用户?
- php - PHP 在递归函数中追加数组 [0] 以获取未知深度
- arduino - 为什么我的 i2c 传感器在将另一个传感器连接到总线时停止响应?
- ajax - 如何在highchart中动态对齐3d饼图
- reactjs - 为什么当我使用 history.push() 时我的状态没有更新?
- html - Ng Zorro 表 - 表中的项目类型为 `any`(错误的 Intellisense)