【问题标题】:What cookies can a js script loaded from an external domain access?从外部域加载的 js 脚本可以访问哪些 cookie?
【发布时间】:2014-01-13 14:38:10
【问题描述】:

假设一个 web 应用程序(通过 http 服务)有一个标签,其 src 是另一个域。外部脚本正在处理 cookie 和/或 localStorage。它可以访问哪些域 cookie?

<!-- on example.org --> <script src='http://anotherexample.org/script.js'> <!-- This script messes with cookies -->

我的直觉告诉我,它操作的 cookie 是属于 example.org 的。但这是如何工作的呢?

【问题讨论】:

    标签: javascript html cookies


    【解决方案1】:

    你的直觉是非常正确的。 Cookie 是在浏览器开始实施同源策略限制之前创建的,因此这些策略是 not generally protected。因此,当anotherexample.org 传递的脚本访问document.cookie 时,它将在原始HTTP 响应中看到example.org 传递的那些cookie。

    唯一重要的例外是"HttpOnly" cookies。如果example.org 选择,它可以指定它提供的cookie 为“HttpOnly”;任何 JavaScript 代码都无法访问这些 cookie,无论是来自 anotherexample.org 还是来自 example.org 本身。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 2013-10-06
      • 2017-03-29
      • 2020-10-05
      相关资源
      最近更新 更多