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

Support wildcard in API Gateway cors domains #6043

Merged
merged 2 commits into from
Apr 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/providers/aws/events/apigateway.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -257,6 +257,15 @@ functions:
allowCredentials: false allowCredentials: false
``` ```


Wildcards are accepted. The following example will match all sub-domains of example.com over http:

```yml
cors:
origins:
- http://*.example.com
- http://example2.com
```

Please note that since you can't send multiple values for [Access-Control-Allow-Origin](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin), this configuration uses a response template to check if the request origin matches one of your provided `origins` and overrides the header with the following code: Please note that since you can't send multiple values for [Access-Control-Allow-Origin](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin), this configuration uses a response template to check if the request origin matches one of your provided `origins` and overrides the header with the following code:


``` ```
Expand Down
13 changes: 11 additions & 2 deletions lib/plugins/aws/package/compile/events/apiGateway/lib/cors.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -117,12 +117,21 @@ module.exports = {
]; ];
}, },


regexifyWildcards(orig) {
return orig.map((str) => str.replace(/\./g, '[.]')
.replace('*', '.*'));
},

generateCorsResponseTemplate(origins) { generateCorsResponseTemplate(origins) {
// glob pattern needs to be parsed into a Java regex
// escape literal dots, replace wildcard * for .*
const regexOrigins = this.regexifyWildcards(origins);

return ( return (
'#set($origin = $input.params("Origin"))\n' + '#set($origin = $input.params("Origin"))\n' +
'#if($origin == "") #set($origin = $input.params("origin")) #end\n' + '#if($origin == "") #set($origin = $input.params("origin")) #end\n' +
`#if(${origins `#if(${regexOrigins
.map((o, i, a) => `$origin == "${o}"${i < a.length - 1 ? ' || ' : ''}`) .map((o, i, a) => `$origin.matches("${o}")${i < a.length - 1 ? ' || ' : ''}`)
.join('')}` + .join('')}` +
') #set($context.responseOverride.header.Access-Control-Allow-Origin = $origin) #end' ') #set($context.responseOverride.header.Access-Control-Allow-Origin = $origin) #end'
); );
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('#compileCors()', () => {
cacheControl: 'max-age=600, s-maxage=600', cacheControl: 'max-age=600, s-maxage=600',
}, },
'users/create': { 'users/create': {
origins: ['http://localhost:3000', 'http://example.com'], origins: ['http://localhost:3000', 'https://*.example.com'],
headers: ['*'], headers: ['*'],
methods: ['OPTIONS', 'POST'], methods: ['OPTIONS', 'POST'],
allowCredentials: true, allowCredentials: true,
Expand Down Expand Up @@ -108,7 +108,7 @@ describe('#compileCors()', () => {
.Resources.ApiGatewayMethodUsersCreateOptions .Resources.ApiGatewayMethodUsersCreateOptions
.Properties.Integration.IntegrationResponses[0] .Properties.Integration.IntegrationResponses[0]
.ResponseTemplates['application/json'] .ResponseTemplates['application/json']
).to.equal('#set($origin = $input.params("Origin"))\n#if($origin == "") #set($origin = $input.params("origin")) #end\n#if($origin == "http://localhost:3000" || $origin == "http://example.com") #set($context.responseOverride.header.Access-Control-Allow-Origin = $origin) #end'); ).to.equal('#set($origin = $input.params("Origin"))\n#if($origin == "") #set($origin = $input.params("origin")) #end\n#if($origin.matches("http://localhost:3000") || $origin.matches("https://.*[.]example[.]com")) #set($context.responseOverride.header.Access-Control-Allow-Origin = $origin) #end');


expect( expect(
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
Expand Down Expand Up @@ -230,7 +230,7 @@ describe('#compileCors()', () => {
.Resources.ApiGatewayMethodUsersAnyOptions .Resources.ApiGatewayMethodUsersAnyOptions
.Properties.Integration.IntegrationResponses[0] .Properties.Integration.IntegrationResponses[0]
.ResponseTemplates['application/json'] .ResponseTemplates['application/json']
).to.equal('#set($origin = $input.params("Origin"))\n#if($origin == "") #set($origin = $input.params("origin")) #end\n#if($origin == "http://localhost:3000" || $origin == "http://example.com") #set($context.responseOverride.header.Access-Control-Allow-Origin = $origin) #end'); ).to.equal('#set($origin = $input.params("Origin"))\n#if($origin == "") #set($origin = $input.params("origin")) #end\n#if($origin.matches("http://localhost:3000") || $origin.matches("http://example[.]com")) #set($context.responseOverride.header.Access-Control-Allow-Origin = $origin) #end');


expect( expect(
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
Expand Down