aws-apigateway: introduce convenience method for adding CORS headers to authorizer responses
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 74
Description
### Describe the feature
When using custom authorizers with API Gateway, CORS headers are not automatically added to 401 responses since these responses come directly from the authorizer before reaching any Lambda integration. This requires developers to manually add Gateway Responses with CORS headers for each API Gateway instance, leading to repetitive boilerplate code.
This proposal suggests adding a convenience method `addAuthorizerCORSHeaders()` to the `RestApi` class to streamline this common use case.
### Use Case
When building web applications that use API Gateway with custom authorizers, browsers require proper CORS headers even for unauthorized (401) responses. Currently, developers need to manually add Gateway Responses like this for each API:
```typescript
new apigateway.GatewayResponse(this, "UnauthorizedResponse", {
restApi: api,
type: apigateway.ResponseType.UNAUTHORIZED,
responseHeaders: {
"Access-Control-Allow-Origin": "'*'",
}
});
```
This is repetitive and easy to forget, leading to CORS issues that are hard to debug.
### Proposed Solution
Add a simple convenience method to `RestApi`:
```typescript
class RestApi {
/**
* Adds CORS headers to 401 responses from authorizers.
* This adds Access-Control-Allow-Origin: '*' to enable
* proper error handling in browser applications.
*/
public addAuthorizerCORSHeaders(): void {
// Internally creates the necessary GatewayResponse resource
// with Access-Control-Allow-Origin: '*' for 401 responses
}
}
```
Usage would be as simple as:
```typescript
const api = new apigateway.RestApi(this, "api", {
// ... other options
});
api.addAuthorizerCORSHeaders();
```
For cases requiring custom CORS settings, developers can still use the `GatewayResponse` construct directly.
### Other Information
This is a common issue that many developers face when working with API Gateway custom authorizers, as evidenced by:
- Stack Overflow questions like [AWS API Gateway and Authorizer CORS error](https://stackoverflow.com/questions/71343329/aws-api-gateway-and-authorizer-cors-error)
- Medium articles like [AWS Serverless how to fix CORS for custom lambda authorizer](https://sergeygultyayev.medium.com/aws-serverless-how-to-fix-cors-for-custom-lambda-authorizer-ef6680e4f03)
### Acknowledgements
- [ ] I may be able to implement this feature request
- [ ] This feature might incur a breaking change
### CDK version used
2.178.2
### Environment details (OS name and version, etc.)
Ubuntu 24.04.1
Contributor guide
Research direction
Read the RestApi class and GatewayResponse construct first, then locate tests covering gateway responses and RestApi methods. Confirm the proposed method creates an UNAUTHORIZED response with the specified Access-Control-Allow-Origin header; done means the behavior is tested and direct GatewayResponse use remains available for custom settings.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, typescript
- Domain
- api, cloud
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100