什么是主机专用 cookie?

Posted

技术标签:

【中文标题】什么是主机专用 cookie?【英文标题】:What is a host only cookie? 【发布时间】:2012-09-05 10:10:32 【问题描述】:

我想知道host only cookie 是什么。

在检索form auth 时,浏览器会在标头中获取一个显示为host only 的JSESSIONID cookie。

【问题讨论】:

【参考方案1】:

首先,foo.com无法设置bar.com可以读取的cookie。 Host-only 仅保护 example.com cookie 不被 bar.example.com 读取。

来自RFC 6265关于设置cookie及其Domain属性:

If the domain-attribute is non-empty:

  If the canonicalized request-host does not domain-match the domain-attribute:

    Ignore the cookie entirely and abort these steps.

  Otherwise:

    Set the cookie's host-only-flag to false.

    Set the cookie's domain to the domain-attribute.

Otherwise:

  Set the cookie's host-only-flag to true.

  Set the cookie's domain to the canonicalized request-host.

这是什么意思

以上可以总结为“Host-only is default”。也就是说,如果未指定Domain,则cookie 只能被设置cookie 的确切域读取。这可以通过在设置 cookie 时设置 Domain 属性来放松。

例如,如果 cookie 由 www.example.com 设置且未指定 Domain 属性,则 cookie 将使用域 www.example.com 设置,并且 cookie 将是主机专用 cookie。

另一个例子:如果cookie是由www.example.com设置的,并且Domain属性被指定为example.com(所以cookie也会被发送到foo.example.com),那么cookie将被设置为域@987654340 @(或者可能是 .example.com,某些浏览器使用之前 RFC 2109 中的点来表示 not host-only)并且 cookie 将 not 仅是 host饼干。

关于 cookie 标头何时由浏览器发送的第 5.4 节介绍了 cookie 的发送:

         The cookie's host-only-flag is true and the canonicalized
         request-host is identical to the cookie's domain.
      Or:
         The cookie's host-only-flag is false and the canonicalized
         request-host domain-matches the cookie's domain.

因此,域 example.comhost-only 为 false 的 cookie 被发送到 foo.example.com 。如果 host-only 为真,则 example.com cookie 仅发送到 example.com

【讨论】:

刚刚在一个客户的网站上遇到了这个并去查找它。您的评论清晰易懂。支持它。 很好的描述,谢谢。值得注意的是,IE 故意不支持此功能(blogs.msdn.microsoft.com/ieinternals/2009/08/20/…),即使设置了 domain 属性,即使是最新的 IE 11 仍将 cookie 发送到子域。 最后一句错了。它应该是:“所以域example.comhost-onlyfalse 的cookie 被发送到foo.example.com。如果host-onlytrue,则example.com 仅发送到example.com。” @SkrewEverything 很好发现。五年来第一个发现我*故意错误的人! *咳嗽 @sparrowrt 最终(在您发表评论后)在 IE 和 Edge 中得到了修复:“通过 Windows 10 RS4(2018 年 4 月),Edge 和 Internet Explorer 都与其他浏览器匹配。” (来自您链接到的常见问题解答)【参考方案2】:

Host Only cookie 意味着 cookie 应该由浏览器处理到服务器only到首先发送到浏览器的同一主机/服务器。

您不想为广告活动发送此主机专用 cookie,因为它可能包含敏感信息。

【讨论】:

您的回答比 SilverLightFox 晚了 3 年,而且在各方面都没有那么好(甚至有点误导)。我不知道您为什么决定编写自己的答案而不是选择其他答案。 @MyUsername112358 你倒退了。 SilverLightFox 的答案在这之后的 2 年多后就出来了。【参考方案3】:

cookie 的 host-only-flag 为 true,并且规范化的 request-host 与 cookie 的域相同。

https://www.rfc-editor.org/rfc/rfc6265#section-5.4

【讨论】:

以上是关于什么是主机专用 cookie?的主要内容,如果未能解决你的问题,请参考以下文章

微软增加了Azure专用主机选项 用于在单租户服务器上运行vm

海外主机是什么意思?与国内主机有什么区别?

flash xmlsocket连接主机是不是需要专用IP?

hd/专用/etc/主机

WHM / CPANEL / 专用主机 / 查看所有已配置的转发器

wordpress用啥主机好?