serverless-application-model icon indicating copy to clipboard operation
serverless-application-model copied to clipboard

CORS Allow-Credentials and Expose-Headers support

Open cast opened this issue 8 years ago • 10 comments

I plan to create a PR to add support for Access-Control-Allow-Credentials and Access-Control-Expose-Headers in the CORS configuration block.

Right now you have to add swagger aws integration configuration for options requests to facilitate this. Having these setting in the CORS configuration block will make CORS configuration easier and especially less error prone .

We are using (the same) custom headers for our endpoints so moving this configuration from swagger into the CORS configuration block would improve our configuration workflow.

Adding support for these headers would complete CORS header support according to https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers

cast avatar Jun 08 '18 23:06 cast

It appears to me that "Access-Control-Expose-Headers" is not meant to be set on preflight responses, but only on "actual" responses. SAM can't do anything to modify the application's response headers (IIRC); it's up to the application to set those. I think that adding it to a preflight response carries no meaning.

chrisoverzero avatar Jun 11 '18 16:06 chrisoverzero

Hi @chrisoverzero , the Access-Control-Expose-Headers header is required to be set on the Options preflight call as well (at least in current Chrome version).

cast avatar Jun 11 '18 21:06 cast

I can't seem to recreate that. I've created an API that responds to preflight requests without "Access-Control-Expose-Headers" and to non-preflight requests with "Access-Control-Expose-Headers". In Chrome 67.0.3396.79, I'm running this:

fetch(
  'http://localhost:5000/cors/exposed',
  {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    },
    body: JSON.stringify({})
  }).then(r => console.log(r.headers.get('something-custom')))

…and I am able to read the value of the "Something-Custom" header.

Additionally, the spec calls out "Access-Control-Expose-Headers" as a header that appears in responses to non-preflight requests.

chrisoverzero avatar Jun 11 '18 22:06 chrisoverzero

what about Access-Control-Allow-Credentials ?

manishgkasera avatar Nov 09 '18 16:11 manishgkasera

Access-Control-Allow-Credentials is valid for the responses to both preflight and non-preflight CORS requests.

chrisoverzero avatar Nov 09 '18 17:11 chrisoverzero

exactly and it should be part of preflight requests as well so CORS option in SAM should support this.

manishgkasera avatar Nov 10 '18 06:11 manishgkasera

What should SAM do if a template sets Access-Control-Allow-Credentials and sets Access-Control-Allow-Origin to "'*'"?

chrisoverzero avatar Nov 10 '18 19:11 chrisoverzero

Hi Chris, IMO SAM can leave it as it is and pass it on to API gateway and browser will give error related to this as part of client request. This is the same behaviour as API gateway.

manishgkasera avatar Nov 11 '18 03:11 manishgkasera

Merged into develop. Thank you for this contribution!

keetonian avatar Nov 27 '18 18:11 keetonian

Released!

keetonian avatar Mar 06 '19 23:03 keetonian