You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When working with API Gateway + Lambda, developers need to handle CORS Headers properly, to make sure their API can be called by a web frontend and to avoid cross origin requests to be blocked. This module would simplify adding CORS headers in the response of their Lambda function when used with an API Gateway Proxy.
Motivation
Adding CORS in the response headers is either forgotten (and nothing works as expected), either boring (a lot of boilerplate code, with very specific syntax). With the wish to simplify developer life, Lambda Powertools could bring something.
Proposal
Adding a new annotation for the function handler method: @CrossOrigin
When using this annotation with a handler implementing RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent>, the annotation will automatically add the headers Access-Control-Expose-Headers, Access-Control-Allow-Origin, Access-Control-Allow-Methods, Access-Control-Allow-Credentials, Access-Control-Max-Age to the response object.
publicclassFunctionProxyimplementsRequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
@CrossOriginpublicAPIGatewayProxyResponseEventhandleRequest(finalAPIGatewayProxyRequestEventinput, finalContextcontext) {
// CORS headers will be automatically added in the following object:returnnewAPIGatewayProxyResponseEvent()
.withStatusCode(200)
.withBody(body);
}
}
The annotation can obviously be configured to specify the different values (there are some default values to simplify the job of developpers):
When using multiple origins in the configuration (comma-separated), the annotation will find the one matching the request and use this specific one in the header, as browsers don't support multiple origins in the header.
Drawbacks
This new module can bring a few additional kb to the lambda package size. It does not use any dependency which is not already in the core module.
Rationale and alternatives
What other designs have been considered? Why not them? N/A
What is the impact of not doing this? N/A
Unresolved questions
N/A
The text was updated successfully, but these errors were encountered:
This is one of the most common issues people face. It will greatly improve DX.
Some comments:
From security perspective, we should force most fields to be required rather than provide '*' as a default. Many developers do not know why we have CORS and may use the default values (out of convenient) in production without knowing the consequence.
origin header doesn't support multiple value. I use usually information from request header to determine which value should I return (e.g. dev.site.com, staging.site.com, etc.). But I think we should leave this to clients to keep this feature generic.
There are two additional steps required to enable CORS. People usually miss them. We should be document or link as a prerequisite in the doc. (to avoid "why this doesn't work" issues). They are are:
Add OPTIONS for preflight request (with no authorization)
Should document if it overrides values if the handler has already specified .withHeader() with the same key. I suggest not to override so that the handler can override the origin header value from event/context data.
Key information
Summary
When working with API Gateway + Lambda, developers need to handle CORS Headers properly, to make sure their API can be called by a web frontend and to avoid cross origin requests to be blocked. This module would simplify adding CORS headers in the response of their Lambda function when used with an API Gateway Proxy.
Motivation
Adding CORS in the response headers is either forgotten (and nothing works as expected), either boring (a lot of boilerplate code, with very specific syntax). With the wish to simplify developer life, Lambda Powertools could bring something.
Proposal
@CrossOrigin
RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent>
, the annotation will automatically add the headersAccess-Control-Expose-Headers
,Access-Control-Allow-Origin
,Access-Control-Allow-Methods
,Access-Control-Allow-Credentials
,Access-Control-Max-Age
to the response object.Authorization, *
(*)Access-Control-Allow-Headers
header*
Access-Control-Expose-Headers
header*
Access-Control-Allow-Origin
header*
Access-Control-Allow-Methods
headertrue
Access-Control-Allow-Credentials
header29
Access-Control-Max-Age
headerExample:
Authorization, *
(*)Access-Control-Allow-Headers
header*
Access-Control-Expose-Headers
header*
Access-Control-Allow-Origin
header*
Access-Control-Allow-Methods
headertrue
Access-Control-Allow-Credentials
header29
Access-Control-Max-Age
headerDrawbacks
This new module can bring a few additional kb to the lambda package size. It does not use any dependency which is not already in the core module.
Rationale and alternatives
Unresolved questions
N/A
The text was updated successfully, but these errors were encountered: