Reading a cookie from a different domain

54,842

Solution 1

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).

Solution 2

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..

Share:
54,842
Reverend Bubbles
Author by

Reverend Bubbles

Updated on January 28, 2021

Comments

  • Reverend Bubbles
    Reverend Bubbles over 3 years

    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!