Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CORS requests with session/cookie

My application has a PHP server and a client (a JS single-page app). They are separate projects and deployed in different domains. The client consumes a RESTful API exposed by the server.

This application is to be integrated with a third party which handles authentication, so users cannot login directly. Our server just receives an SSO token (which comes appropriately signed so that we verify its integrity).

We also enforce security at the transport layer for all requests.

What I'd like to do is, once the SSO token is verified, start a session of my own and then redirect the user to the client. I thought that once the session was created the browser would automatically send the right Cookie header in the asynchronous API calls, but it doesn't seem to be the case.

Is this deliberately disabled due to security reasons?

like image 963
cafonso Avatar asked Jan 23 '16 20:01

cafonso


People also ask

How do I allow receiving&sending cookies by cors request?

To allow receiving & sending cookies by a CORS request successfully, do the following. Back-end (server): Set the HTTP header Access-Control-Allow-Credentials value to true . Also, make sure the HTTP headers Access-Control-Allow-Origin and Access-Control-Allow-Headers are set and not with a wildcard *.

What is a third party cookie in Cors response?

Third-party cookies Note that cookies set in CORS responses are subject to normal third-party cookie policies. In the example above, the page is loaded from foo.example, but the cookie on line 20 is sent by bar.other, and would thus not be saved if the user has configured their browser to reject all third-party cookies. The HTTP response headers

How to read cookies from a cross-origin request?

In order for the client to be able to read cookies from cross-origin requests, you need to have: All responses from the server need to have the following in their header: In my implementation with Angular 7 and Spring Boot, I achieved that with the following:

What is CORS (Cross-Origin Resource Sharing)?

CORS is a header-based security mechanism used by the server to tell the browser to send a cross-origin request from trusted domains. The server enabled with CORS headers used to avoid cross-origin requests blocked by browsers.


1 Answers

You must set withCredentials to true for cross-origin XHR requests to include cookies.

The CORS response must also say Access-Control-Allow-Credentials: true (which is why widthCredentials defaults to false).

like image 135
Quentin Avatar answered Oct 07 '22 19:10

Quentin