Search code examples
javascriptcookiessubdomain

Can I see cookie domain specificity in JS?


When I am on a domain some.example.com, the browser uses both cookies set for some.example.com and .example.com. I can easily see this cookie domain specificity with various browser extensions, e. g. EditThisCookie or Cookie Inspector.

My question is: can I read (and maybe even write) the cookie domain in JavaScript?

(I found a related answer about cookie path, but it was neither accepted nor upvoted. And maybe there is some hack that can allow me to see just the domain, not path. A quick and fragile hack is enough for me - this is for development and testing purposes.)


Solution

  • Short answer: no.

    The reason is for security and sandboxing. Browsers purposely limit the information that JavaScript is able to know about browsers, to prevent cross-site scripting as well as snooping.

    The only way you'd be able to tell is if you somehow modified the data in the cookies to give you some indicator. An example would be if the value was normally 5, you could change it to something like .example.com|5 or some.example.com|5 (at the time you set the value) and then handle it when you went to read the value.

    Short of that, you're out of luck.