【发布时间】:2018-10-07 13:17:20
【问题描述】:
我正在尝试从父域 (example.org) 访问子域 (small.example.org) 上设置的 cookie,我想通过页面中的一些 Javascript 来执行此操作。
首先我为small.example.org域设置一个cookie
document.cookie = "name=Mike; domain=small.example.org"
当我加载small.example.org 时,我可以成功看到我刚刚设置的cookie。加载example.org 时,我看不到来自small.example.org 的任何cookie。也许并不那么令人惊讶。
所以我想我需要向子域发出请求,以在主域中包含一些内容,即脚本标记。
<script src="small.example.org/script.js"></script>
现在,当我将example.org 与脚本标签的请求一起加载并在浏览器中查看时,我可以看到来自small.example.org 的cookie。
但是当我尝试使用 document.cookie 从 Javascript 访问它时,我什么也得不到。
这是预期的行为吗?我认为只有设置了 HTTPOnly 标志时,您才能从 Javascript 访问 cookie。
有没有办法解决这个问题?上面的例子非常接近我的实际用例场景,不幸的是我不能过多地使用架构。
【问题讨论】:
标签: javascript node.js http security cookies