【问题标题】:Unable to access httponly flagged cookie on own domain loaded in iframe无法访问 iframe 中加载的自己域上的 httponly 标记 cookie
【发布时间】:2017-01-11 05:15:02
【问题描述】:

我正在制作一个 chrome 扩展程序,它在网页上注入 iframe 并显示一些内容。

iframe 中加载的内容来自 https://example.com,我对它有完全控制。我正在尝试通过document.cookie 从 iframe(我认为应该可用)访问https://example.com 的 cookie。这不会让我访问带有 httponly 标记的 cookie,我不知道这是为什么。毕竟这不是跨域的。是吗?

这是我用来获取 cookie 的代码

jQuery("#performAction").click(function(e) {
    e.preventDefault();
    console.log(document.domain); // https://example.com
    var cookies = document.cookie;
    console.log('cookies', cookies);
    var httpFlaggedCookie1 = getCookie("login_sess");
    var httpFlaggedCookie2 = getCookie("login_pass");
    console.log('httpFlaggedCookie1 ', httpFlaggedCookie1 ); // shows blank
    console.log('httpFlaggedCookie2 ', httpFlaggedCookie2 ); // shows blank
    if(httpFlaggedCookie2 != "" && httpFlaggedCookie2 != ""){
        doSomething();
    } else{
        somethingElse();
    }
});

有什么建议可以为此做些什么吗?

【问题讨论】:

    标签: javascript jquery iframe cookies google-chrome-extension


    【解决方案1】:

    在 Chrome 中默认为HttpOnly cookies are prevented to be read and written in JavaScript

    但是,由于您正在编写 chrome 扩展,您可以使用 chrome.cookies.getchrome.cookies.set 进行读/写,并在 manifest.json 中声明 cookies 权限。请注意chrome.cookies 只能在后台页面中访问,因此您可能需要使用Message Passing 做一些事情

    【讨论】:

    • 感谢您的回复,能给我举个例子吗?
    • 我所有的代码都在我正在 iframe 中加载的页面中,chrome.cookies.get 会在那里工作吗?如果没有,那么在这种情况下我该如何访问 cookie?
    • @TusharShukla,当然,据我所知,cookie 绑定到 url 而不是标签(框架)
    • 我问这个是因为我收到了Uncaught TypeError: Cannot read property 'get' of undefined 错误。
    猜你喜欢
    • 1970-01-01
    • 2021-06-14
    • 2013-08-03
    • 2010-09-11
    • 2018-11-22
    • 2013-04-08
    • 2018-08-29
    • 2014-02-04
    • 2013-08-02
    相关资源
    最近更新 更多