由于奇怪的域/子域cookie问题,我得到了,我想知道浏览器如何处理cookie。如果他们用不同的方式做这件事,知道其中的区别也会很好。
换句话说,当浏览器接收到一个cookie时,该cookie可能有一个域和一个路径附加到它。或者不是,在这种情况下,浏览器可能会用一些默认值代替它们。问题1:它们是什么?
稍后,当浏览器准备发出请求时,它会检查它的cookie并过滤掉应该为该请求发送的cookie。它通过将它们与请求路径和域进行匹配来做到这一点。问题2:匹配规则是什么?
补充道:
我问这个问题是因为我对一些边缘情况感兴趣。如:
用于。example.com的cookie可以用于www.example.com吗?
example.com的cookie是否可用于example.com?
example.com的cookie可以用于www.example.com吗?
example.com的cookie是否可用于anotherexample.com?
www.example.com可以为example.com设置cookie吗?
www.example.com是否可以为www2.example.com设置cookie ?
www.example.com是否可以为。com设置cookie ?
等。
补充2:
还有,谁能建议一下我应该如何设置cookie,以便:
可以通过www.example.com或example.com设置;
它可以通过www.example.com和example.com访问。
我很惊讶地读到3.3.2节关于拒绝cookie的内容:
https://www.rfc-editor.org/rfc/rfc2965
这意味着浏览器应该拒绝来自x.y.z.com的域名为。z.com的cookie,因为“x.y”包含一个点。所以,除非我误解了RFC和/或上面的问题,否则可能会添加一些问题:
用于。example.com的cookie可以用于www.yyy.example.com吗?不。
一个由源服务器www.yyy.example.com设置的cookie,域名为。example.com,它的值会被用户代理发送到xxx.example.com吗?不。
2019年,我在最新的Chrome、Firefox和Safari中测试了所有的保护套。
回复新增:
Will a cookie for .example.com be available for www.example.com? YES
Will a cookie for .example.com be available for example.com? YES
Will a cookie for example.com be available for www.example.com? NO, Domain without wildcard only matches itself.
Will a cookie for example.com be available for anotherexample.com? NO
Will www.example.com be able to set cookie for example.com? NO, it will be able to set cookie for '.example.com', but not 'example.com'.
Will www.example.com be able to set cookie for www2.example.com? NO. But it can set cookie for .example.com, which www2.example.com can access.
Will www.example.com be able to set cookie for .com? NO