Storage API授权后如何访问第一方Cookie?

5
我正在研究使用存储访问 API(Storage Access API)https://developer.mozilla.org/zh-CN/docs/Web/API/Storage_Access_API/来从第三方 iframe 中获取第一方 cookie 的可能性。问题在于,尽管 Storage Access API 给了我访问权限,但 iframe 中的 document.cookie 与上面的不同。
我使用 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,但cookies和存储看起来不同。我预计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时并没有产生任何区别。
有什么线索可以解决问题吗?或者这个功能实验性太强,在现在可能存在错误?

1
我也遇到了同样的问题,不幸的是,没有任何文档/帖子可以证实第一方 cookie 无法从第三方 iFrame 访问(即使 MDN 表示可以)。 - yashhy
1个回答

1

看起来您的点击事件被document.hasStorageAccess() promise遮挡了。您是否尝试在点击处理程序内直接执行document.requestStorageAccess()而不是在hasStorageAccess回调内执行?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接