tl;dr, an XHR client in domain A is sending a request to a server in domain B, server responds with a Set-Cookie
with Domain=A
(the client's domain, the XHR's Origin
), all CORS headers set correctly, should it work?
It's well known that one can't set a cookie to another domain. ( How to set a cookie for another domain
However given the following scenario:
Actors:
Client in domain A, a web based client
Server in domain B, setup with CORS headers permitting A as origin, including Access-Control-Allow-Credentials
set to true
Communication flow 1 (baseline):
- Client is issuing a simple GET request to the Server
- Server responds with a cookie, and sets the Domain property to be of the server (Domain=B)
- Client is sending another HXR request and has
withCredentials=true
- The cookie is sent back to the server without any issues
Note: the cookie sent in step #1 is not showing in document.cookies, even if it was not set as httpOnly (since it doesn't belong to the client's domain). Also attempts to get it from the
xhr
via looking at the "Set-Cookie" header, you'll be blocked, by design: https://fetch.spec.whatwg.org/#forbidden-response-header-name it will even won't show in Chrome dev tools under the network tab! but it will still be sent)
Communication flow 2 (my question):
- Client is issuing a simple GET request to the Server
- Server responds with a cookie, but sets the Domain property to be of the client (Domain=A)
- Client is sending an HXR request and has
withCredentials=true
- The cookie is not sent back and doesn't seem to be stored anywhere
Why am I a bit surprised? Since the XHR origin is A and it requests something that sets the cookie to domain A (if I look in Postman I clearly see the Set-Cookie
header being sent with Domain
being the same as the request's Origin
), and I have the most permissive CORS setting for that, what's the reasoning behind not letting me do it? (I was expecting it to fail, but still made me wonder)
Questions
Where is the best place in the spec/RFC that it clarifies that this won't work also for XHR where the cookie
Domain
equals theOrigin
What is the attack vector in scenario 2 if theoretically the browser did allow the server to store the cookie if and only if the
Origin
is the same as the cookieDomain
and the CORS origin allows that Origin.Is there another way to make it work? Maybe it works but my POC was setup incorrectly?
Appendix: Reasoning
I'm looking for a way to have a cross origin CSRF using something like the Cookie to header token method, but due to the cross origin issue, it seems that it's impossible. The only workaround I thought of is sending the CSRF token as a header from the server, then the client can just save it as a cookie it can access later, is there any other way to do it? Is this considered secure?