Say there is a site foo.com
which loads JavaScript from site bar.com
. Now, say the JavaScript from site bar.com
tries to read cookies usin
You can only access cookies which have been set for the given domain name. From the Wikipedia article on cookies:
Beside the name/value pair, a cookie may also contain an expiration date, a path, a domain name, and whether the cookie is intended only for encrypted connections. RFC 2965 mandates cookies have a version number, but this is usually omitted. These pieces of data follow the name=newvalue pair and are separated by semicolons. For example, a cookie can be created by the server by sending a line Set-Cookie: name=newvalue; expires=date; path=/; domain=.example.org.
The domain and path tell the browser that the cookie has to be sent back to the server when requesting URLs of a given domain and path. If not specified, they default to the domain and path of the object that was requested. As a result, the domain and path strings may tell the browser to send the cookie when it normally would not. For security reasons, the cookie is accepted only if the server is a member of the domain specified by the domain string.
If foo.com
sent a cookie which had the domain name of bar.com
, or even .com
, then JavaSCript code on bar.com
could read that cookie. However most browsers are configured to only accept cookies when the domain name matches, and would reject such a cookie.