Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(apigateway): set passthrough behavior to avoid errors on CORS preflight requests #30985

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/aws-cdk-lib/aws-apigateway/lib/resource.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Construct } from 'constructs';
import { CfnResource, CfnResourceProps } from './apigateway.generated';
import { Cors, CorsOptions } from './cors';
import { Integration } from './integration';
import { Integration, PassthroughBehavior } from './integration';
import { MockIntegration } from './integrations';
import { Method, MethodOptions, AuthorizationType } from './method';
import { IRestApi, RestApi } from './restapi';
Expand Down Expand Up @@ -295,6 +295,7 @@ export abstract class ResourceBase extends ResourceConstruct implements IResourc
integrationResponses: [
{ statusCode: `${statusCode}`, responseParameters: integrationResponseParams, responseTemplates: renderResponseTemplate() },
],
passthroughBehavior: PassthroughBehavior.NEVER,
}), {
authorizer: {
authorizerId: '',
Expand Down
47 changes: 47 additions & 0 deletions packages/aws-cdk-lib/aws-apigateway/test/cors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,4 +733,51 @@ describe('cors', () => {
HttpMethod: 'OPTIONS',
});
});

test('CORS resource with passthroughBehavior set to NEVER', () => {
// GIVEN
const stack = new Stack();
const api = new apigw.RestApi(stack, 'api');
const resource = api.root.addResource('MyResource');

// WHEN
resource.addCorsPreflight({
allowOrigins: ['https://amazon.com'],
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::Method', {
HttpMethod: 'OPTIONS',
ResourceId: { Ref: 'apiMyResourceD5CDB490' },
Integration: {
IntegrationResponses: [
{
ResponseParameters: {
'method.response.header.Access-Control-Allow-Headers': "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,X-Amz-User-Agent'",
'method.response.header.Access-Control-Allow-Origin': "'https://amazon.com'",
'method.response.header.Vary': "'Origin'",
'method.response.header.Access-Control-Allow-Methods': "'OPTIONS,GET,PUT,POST,DELETE,PATCH,HEAD'",
},
StatusCode: '204',
},
],
PassthroughBehavior: 'NEVER',
RequestTemplates: {
'application/json': '{ statusCode: 200 }',
},
Type: 'MOCK',
},
MethodResponses: [
{
ResponseParameters: {
'method.response.header.Access-Control-Allow-Headers': true,
'method.response.header.Access-Control-Allow-Origin': true,
'method.response.header.Vary': true,
'method.response.header.Access-Control-Allow-Methods': true,
},
StatusCode: '204',
},
],
});
});
});
Loading