在子域和域之间共享Cookie

我有两个问题。 我知道,如果我指定的域名为.mydomain.com (与领先的点)在cookie中的所有子域可以共享一个cookie。

subdomain.mydomain.com可以访问在mydomain.com (不含www子域)中创build的cookie吗?

如果在subdomain.mydomain.com创build了,可以mydomain.com (不含www子域)访问cookie吗?

如果在Set-Cookie头中明确指定了域,那么2个域mydomain.comsubdomain.mydomain.com只能共享cookie。 否则,cookie的范围仅限于请求主机。 (这被称为“主机专用cookie”。请参阅什么是主机专用cookie? )

例如,如果您从subdomain.mydomain.com发送了以下标题:

 Set-Cookie: name=value 

那么cookie将不会被发送到mydomain.com请求。 但是,如果使用以下内容,则可以在两个域上使用:

 Set-Cookie: name=value; domain=mydomain.com 

在RFC 2109中 ,没有前导点的域意味着它不能在子域上使用,只有一个前导点( .mydomain.com )将允许跨子域使用它。

但是,现代浏览器尊重新规范RFC 6265 ,并且会忽略任何前导点,这意味着您可以在子域以及顶级域上使用cookie。

总而言之,如果您像mydomain.com上面的第二个示例一样设置cookie,则可以通过subdomain.mydomain.com访问cookie,反之亦然。

也可以看看:

  • www vs no-www和cookies
  • cookietesting脚本来尝试一下

我不确定@cmbuckley答案显示完整的图片。 我读的是:

  Unless the cookie's attributes indicate otherwise, the cookie is returned only to the origin server (and not, for example, to any subdomains), and it expires at the end of the current session (as defined by the user agent). User agents ignore unrecognized cookie attributes (but not the entire cookie). 

 8.6. Weak Integrity Cookies do not provide integrity guarantees for sibling domains (and their subdomains). For example, consider foo.example.com and bar.example.com. The foo.example.com server can set a cookie with a Domain attribute of "example.com" (possibly overwriting an existing "example.com" cookie set by bar.example.com), and the user agent will include that cookie in HTTP requests to bar.example.com. In the worst case, bar.example.com will be unable to distinguish this cookie from a cookie it set itself. The foo.example.com server might be able to leverage this ability to mount an attack against bar.example.com. 

对我来说,这意味着你可以保护cookie不被子域/域读取,但不能防止写cookies到其他域。 所以有人可能会通过控制另一个由同一浏览器访问的子域来重写您的站点Cookie。 这可能不是一个大问题。

令人敬畏的cookietesting网站@cmbuckley /为那些错过了他的回答,像我一样; 值得滚动和upvoting /:

这是一个使用DOM cookie API( https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie )的例子,所以我们可以看到自己的行为。

如果我们执行下面的JavaScript:

document.cookie =“key = value”

它看起来和执行一样:

document.cookie =“key = value; domain = mydomain.com”

Cookie 密钥仅在域mydomain.com上可用(仅)。


现在,如果您在mydomain.com上执行以下JavaScript:

document.cookie =“key = value; domain = .mydomain.com”

Cookie 密钥可用于mydomain.com以及subdomain.mydomain.com


最后,如果您尝试在subdomain.mydomain.com上执行以下操作:

document.cookie =“key = value; domain = .mydomain.com”

Cookie 密钥是否可用于subdomain.mydomain.com ? 这让我感到有些惊讶, 我曾经认为这将是一个安全违规的子域可以设置一个父域的cookie。

在这两种情况下都可以,这是IE和Edge的默认行为。

其他答案增加了有价值的见解,但主要描述了Chrome中的行为。 重要的是要注意,IE中的行为完全不同。 CMBuckley的非常有用的testing脚本表明,在Chrome中,没有指定域时,Cookie不会在根和子域之间共享。 然而,IE中的相同testing显示它们是共享的。 这个IE案例更接近于CMBuckley的www-or-www-link链接。 我知道这是因为我们有一个系统在根和子域上使用不同的服务堆栈cookie。 这一切都工作得很好,直到有人在IE浏览器中访问它,两个系统战斗谁的会话cookie会赢,直到我们吹了caching。

解决scheme简单

 setcookie("NAME", "VALUE", time()+3600, '/', EXAMPLE.COM); 

Setcookie的第五个参数决定了cookie可用的(子)域。 将其设置为(EXAMPLE.COM)使其可用于任何子域(例如:SUBDOMAIN.EXAMPLE.COM)

参考: http : //php.net/manual/en/function.setcookie.php