Share cookie between subdomain and domain

前端 未结 7 1797
伪装坚强ぢ
伪装坚强ぢ 2020-11-21 13:58

I have two questions. I understand that if I specify the domain as .mydomain.com (with the leading dot) in the cookie that all subdomains can share a cookie.

7条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-11-21 14:23

    I'm not sure @cmbuckley answer is showing the full picture. What I read is:

    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.

    RFC 6265

    Also

    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.
    

    To me that means you can protect cookies from being read by subdomain/domain but cannot prevent writing cookies to the other domains. So somebody may rewrite your site cookies by controlling another subdomain visited by the same browser. Which might not be a big concern.

    Awesome cookies test site provided by @cmbuckley /for those that missed it in his answer like me; worth scrolling up and upvoting/:

    • http://scripts.cmbuckley.co.uk/cookies.php

提交回复
热议问题