Mic*_*cil 5 javascript cookies local-storage storage-access-api
我调查了使用 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 => {
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>
Run Code Online (Sandbox Code Playgroud)
所以意图是明确的 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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
请注意,DOMException这不会影响我访问第一方 cookie 的能力。我最初使用了https://webkit.org/blog/8124/introducing-storage-access-api/ 中描述的表单,但是.then使用两个函数回调调用对访问 Cookie Jar 没有任何影响。
任何线索可能是问题所在?还是该功能过于实验性,现在可能有问题?
小智 1
看起来您的点击事件被document.hasStorageAccess()承诺所掩盖。您是否尝试过document.requestStorageAccess()直接在点击处理程序内执行而不是在hasStorageAccess回调内执行?
| 归档时间: |
|
| 查看次数: |
1939 次 |
| 最近记录: |