首页 > 解决方案 > 存储 API 授予后如何访问第一方 cookie?

问题描述

我研究了使用存储访问 API https://developer.mozilla.org/en-US/docs/Web/API/Storage_Access_API/从第 3 个 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>

并且 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 没有任何影响。

任何线索可能是问题所在?还是该功能过于实验性,以至于现在可能有问题?

标签: javascriptcookieslocal-storagestorage-access-api

解决方案


看起来您的点击事件被document.hasStorageAccess()承诺所掩盖。您是否尝试过document.requestStorageAccess()直接在点击处理程序内部而不是在hasStorageAccess回调内部执行?


推荐阅读