Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a cookie from a different domain

I'm developing a page/form for a campaign inside my company. However, the first step is to check if the person is logged in. This is easily checked against a cookie - CUSTOMER - that is set once they're logged in.

However: 1) I'm developing locally, not on the same domain, and, as a result can't see that cookie 2) The final campaign may or may not end up residing on the actual domain. They may end up using a vanity URL or something.

For purposes of this, let's assume I do NOT have access to the main domain where the cookie was set.

How can I read that cookie from off the domain? Oh, and since IT folks don't let us touch the back-end grumble, it has to be a JS solution.

Thanks!

like image 434
Reverend Bubbles Avatar asked Mar 30 '16 20:03

Reverend Bubbles


People also ask

Can I access cookies from different domains?

You cannot share cookies across domains. You can however allow all subdomains to have access.

Can JavaScript read cookies from other domains?

You can't. The only cookies you can read with client side JavaScript are those belonging to the host of the HTML document in which the <script> is embedded.

Can cookies be shared across sub domains?

Browser cookies can be shared across subdomains if their domain flag is set to a common parent domain.


2 Answers

You can if you can install server side components.

You can use a dedicated domain to host your cookie and then share it using XSS technics

When dom1.foo.com logs in then you register a cookie on cookie.foo.com using an Ajax XSS call then when you go on dom2.foo.com you have to query cookie.foo.com with your XSS api

I' ve played with it some time ago https://github.com/quazardous/mudoco/blob/master/mudoco/README.txt It's just some sort of POC..

like image 26
quazardous Avatar answered Oct 19 '22 17:10

quazardous


You can't.

The only cookies you can read with client side JavaScript are those belonging to the host of the HTML document in which the <script> is embedded.

By setting withCredentials you can support cookies in cross-origin requests, but they are handled transparently by the browser and JS has no direct access to them (the XHR spec goes to far as to explicitly ban getAllResponseHeaders from reading cookie related headers). The only way for a cross-origin request to get access to cookies is for the server (which you say you don't have access to) to copy the data into the body or a different response header).

like image 67
Quentin Avatar answered Oct 19 '22 16:10

Quentin