@@ -445,12 +445,62 @@ export class BackendStack extends cdk.Stack {
445445
446446 // Add CORS to all resources
447447 api . root . addCorsPreflight ( corsOptions ) ;
448- apiResource . addCorsPreflight ( corsOptions ) ;
449- reportsResource . addCorsPreflight ( corsOptions ) ;
450- latestResource . addCorsPreflight ( corsOptions ) ;
451- reportIdResource . addCorsPreflight ( corsOptions ) ;
452- reportStatusResource . addCorsPreflight ( corsOptions ) ;
453- docsResource . addCorsPreflight ( corsOptions ) ;
448+ apiResource . addCorsPreflight ( {
449+ ...corsOptions ,
450+ allowCredentials : false , // This is crucial - make sure OPTIONS requests don't require credentials
451+ } ) ;
452+ reportsResource . addCorsPreflight ( {
453+ ...corsOptions ,
454+ allowCredentials : false ,
455+ } ) ;
456+ latestResource . addCorsPreflight ( {
457+ ...corsOptions ,
458+ allowCredentials : false ,
459+ } ) ;
460+ reportIdResource . addCorsPreflight ( {
461+ ...corsOptions ,
462+ allowCredentials : false ,
463+ } ) ;
464+ reportStatusResource . addCorsPreflight ( {
465+ ...corsOptions ,
466+ allowCredentials : false ,
467+ } ) ;
468+ docsResource . addCorsPreflight ( {
469+ ...corsOptions ,
470+ allowCredentials : false ,
471+ } ) ;
472+
473+ // Configure Gateway Responses to add CORS headers to error responses
474+ const gatewayResponseTypes = [
475+ apigateway . ResponseType . UNAUTHORIZED ,
476+ apigateway . ResponseType . ACCESS_DENIED ,
477+ apigateway . ResponseType . DEFAULT_4XX ,
478+ apigateway . ResponseType . DEFAULT_5XX ,
479+ apigateway . ResponseType . RESOURCE_NOT_FOUND ,
480+ apigateway . ResponseType . MISSING_AUTHENTICATION_TOKEN ,
481+ apigateway . ResponseType . INVALID_API_KEY ,
482+ apigateway . ResponseType . THROTTLED ,
483+ apigateway . ResponseType . INTEGRATION_FAILURE ,
484+ apigateway . ResponseType . INTEGRATION_TIMEOUT ,
485+ ] ;
486+
487+ gatewayResponseTypes . forEach ( responseType => {
488+ new apigateway . CfnGatewayResponse (
489+ this ,
490+ `${ appName } GatewayResponse-${ responseType . responseType . toString ( ) } -${ props . environment } ` ,
491+ {
492+ restApiId : api . restApiId ,
493+ responseType : responseType . responseType . toString ( ) ,
494+ responseParameters : {
495+ 'gatewayresponse.header.Access-Control-Allow-Origin' : "'*'" ,
496+ 'gatewayresponse.header.Access-Control-Allow-Headers' :
497+ "'Content-Type,Authorization,X-Amz-Date,X-Api-Key'" ,
498+ 'gatewayresponse.header.Access-Control-Allow-Methods' :
499+ "'GET,POST,PUT,PATCH,DELETE,OPTIONS'" ,
500+ } ,
501+ } ,
502+ ) ;
503+ } ) ;
454504
455505 // Create API Gateway execution role with required permissions
456506 new iam . Role ( this , `${ appName } APIGatewayRole-${ props . environment } ` , {
0 commit comments