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
But it turns out that the JavaScript from the site bar.com can only access cookies set by bar.com and not any other.
That isn't true. What matters is where the HTML document containing the <script>
element is, not the URL of the JS file that said <script>
mentions in the src attribute.
I suspect your problem is that you are accessing document.cookies
when the property is called document.cookie
(Singular!)
They load scripts inside the attacked page.
For instance, when comments in a blog system get compromised, they contain a script
element that is executed when the page is rendered. This script can get the cookies and send it to the attacker's server.
That's why you should never trust user input and disallow at least certain tags in comments (or translate every <
to <
). But don't do this on the client side, as this prevention technique can easily be circumvented; test for (and change) malicious input on the server side.
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.