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

时间:2019-09-02 12:12:46

标签: javascript cookies local-storage

我研究了使用Storage Access API https://developer.mozilla.org/en-US/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,但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。

任何线索可能出在哪里?还是该功能太具有实验性,以至于现在可能有问题?

1 个答案:

答案 0 :(得分:1)

似乎document.hasStorageAccess()承诺掩盖了您的点击事件。您是否尝试过直接在点击处理程序中而不是在document.requestStorageAccess()回调内部执行hasStorageAccess