Skip to content

Commit 59ef06a

Browse files
authored
fix(apigateway): fix strange vtl template for cors preflight request (#19104)
CDK will create this VTL template for OPTIONS method. ``` #set($origin = $input.params("Origin")) #if($origin == "") #set($origin = $input.params("origin")) #end #if($origin.matches("https://www.test-cors.org")) #set($context.responseOverride.header.Access-Control-Allow-Origin = $origin) #end ``` This VTL template use `$input.params` for get origin information. But it's references request parameter from these values - path - query string - header [`$input` Variables](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#input-variable-reference) So, this template cause strange behavier like this. ``` $ curl -XOPTIONS https://xxx.execute-api.ap-northeast-1.amazonaws.com/prod/twitch?origin=https://www.test-cors.org -i HTTP/2 204 date: Wed, 23 Feb 2022 06:32:39 GMT x-amzn-requestid: df42e9de-80a4-4db5-985d-5ed8adc40b99 access-control-allow-origin: https://www.test-cors.org ``` [RFC6454](https://datatracker.ietf.org/doc/html/rfc6454#section-7.2) says >the Origin header field indicates > the origin(s) that "caused" the user agent to issue the request its not mention path and querystrings. So VTL template should use only request header for check origin information. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 77f1e0b commit 59ef06a

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

packages/@aws-cdk/aws-apigateway/lib/resource.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,8 @@ export abstract class ResourceBase extends ResourceConstruct implements IResourc
311311

312312
const template = new Array<string>();
313313

314-
template.push('#set($origin = $input.params("Origin"))');
315-
template.push('#if($origin == "") #set($origin = $input.params("origin")) #end');
314+
template.push('#set($origin = $input.params().header.get("Origin"))');
315+
template.push('#if($origin == "") #set($origin = $input.params().header.get("origin")) #end');
316316

317317
const condition = origins.map(o => `$origin.matches("${o}")`).join(' || ');
318318

packages/@aws-cdk/aws-apigateway/test/cors.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ describe('cors', () => {
290290
'method.response.header.Access-Control-Allow-Methods': "'OPTIONS,GET,PUT,POST,DELETE,PATCH,HEAD'",
291291
},
292292
ResponseTemplates: {
293-
'application/json': '#set($origin = $input.params("Origin"))\n#if($origin == "") #set($origin = $input.params("origin")) #end\n#if($origin.matches("https://amazon.com") || $origin.matches("https://aws.amazon.com"))\n #set($context.responseOverride.header.Access-Control-Allow-Origin = $origin)\n#end',
293+
'application/json': '#set($origin = $input.params().header.get("Origin"))\n#if($origin == "") #set($origin = $input.params().header.get("origin")) #end\n#if($origin.matches("https://amazon.com") || $origin.matches("https://aws.amazon.com"))\n #set($context.responseOverride.header.Access-Control-Allow-Origin = $origin)\n#end',
294294
},
295295
StatusCode: '204',
296296
},

packages/@aws-cdk/aws-apigateway/test/integ.cors.expected.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"corsapitest8682546E"
5252
]
5353
},
54-
"corsapitestDeployment2BF1633A228079ea05e5799220dd4ca13512b92d": {
54+
"corsapitestDeployment2BF1633A51392cbce1ac2785bd0e53063423e203": {
5555
"Type": "AWS::ApiGateway::Deployment",
5656
"Properties": {
5757
"RestApiId": {
@@ -74,7 +74,7 @@
7474
"Ref": "corsapitest8682546E"
7575
},
7676
"DeploymentId": {
77-
"Ref": "corsapitestDeployment2BF1633A228079ea05e5799220dd4ca13512b92d"
77+
"Ref": "corsapitestDeployment2BF1633A51392cbce1ac2785bd0e53063423e203"
7878
},
7979
"StageName": "prod"
8080
},
@@ -472,7 +472,7 @@
472472
"method.response.header.Access-Control-Allow-Methods": "'OPTIONS,GET,PUT,POST,DELETE,PATCH,HEAD'"
473473
},
474474
"ResponseTemplates": {
475-
"application/json": "#set($origin = $input.params(\"Origin\"))\n#if($origin == \"\") #set($origin = $input.params(\"origin\")) #end\n#if($origin.matches(\"https://www.test-cors.org\"))\n #set($context.responseOverride.header.Access-Control-Allow-Origin = $origin)\n#end"
475+
"application/json": "#set($origin = $input.params().header.get(\"Origin\"))\n#if($origin == \"\") #set($origin = $input.params().header.get(\"origin\")) #end\n#if($origin.matches(\"https://www.test-cors.org\"))\n #set($context.responseOverride.header.Access-Control-Allow-Origin = $origin)\n#end"
476476
},
477477
"StatusCode": "204"
478478
}

0 commit comments

Comments
 (0)