Skip to content

Commit

Permalink
Merge pull request #5785 from pchynoweth/master
Browse files Browse the repository at this point in the history
Fixes for AWS cors config issues
  • Loading branch information
eahefnawy committed Feb 4, 2019
2 parents 398a92a + 4c935d1 commit 61d3af3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/plugins/aws/package/compile/events/apiGateway/lib/cors.js
Expand Up @@ -18,11 +18,11 @@ module.exports = {
origins = origin.split(',').map(a => a.trim());
}

if (origins) {
if (!_.isEmpty(origins)) {
origin = origins[0];
}

if (!origin && !origins) {
if (!origin) {
const errorMessage = 'must specify either origin or origins';
throw new this.serverless.classes.Error(errorMessage);
}
Expand Down Expand Up @@ -110,7 +110,8 @@ module.exports = {
ResponseParameters: responseParameters,
ResponseTemplates: {
'application/json':
Array.isArray(origins) ? this.generateCorsResponseTemplate(origins) : '',
Array.isArray(origins) && origins.length ?
this.generateCorsResponseTemplate(origins) : '',
},
},
];
Expand Down
Expand Up @@ -64,6 +64,7 @@ describe('#compileCors()', () => {
awsCompileApigEvents.validated.corsPreflight = {
'users/update': {
origin: 'http://example.com',
origins: [],
headers: ['*'],
methods: ['OPTIONS', 'PUT'],
allowCredentials: false,
Expand Down
Expand Up @@ -67,6 +67,10 @@ module.exports = {
cors.maxAge = http.cors.maxAge;
}

if (_.has(http.cors, 'cacheControl')) {
cors.cacheControl = http.cors.cacheControl;
}

corsPreflight[http.path] = cors;
}

Expand Down
Expand Up @@ -644,6 +644,7 @@ describe('#validate()', () => {
origins: ['acme.com'],
methods: ['POST', 'OPTIONS'],
maxAge: 86400,
cacheControl: 'max-age=600, s-maxage=600, proxy-revalidate',
},
},
},
Expand All @@ -659,6 +660,7 @@ describe('#validate()', () => {
origins: ['acme.com'],
allowCredentials: false,
maxAge: 86400,
cacheControl: 'max-age=600, s-maxage=600, proxy-revalidate',
});
});

Expand All @@ -676,6 +678,7 @@ describe('#validate()', () => {
],
allowCredentials: true,
maxAge: 10000,
cacheControl: 'max-age=600, s-maxage=600, proxy-revalidate',
},
},
}, {
Expand Down Expand Up @@ -723,6 +726,8 @@ describe('#validate()', () => {
.to.deep.equal(['TestHeader2', 'TestHeader']);
expect(validated.corsPreflight.users.maxAge)
.to.equal(86400);
expect(validated.corsPreflight.users.cacheControl)
.to.equal('max-age=600, s-maxage=600, proxy-revalidate');
expect(validated.corsPreflight.users.allowCredentials)
.to.equal(true);
expect(validated.corsPreflight['users/{id}'].allowCredentials)
Expand Down

0 comments on commit 61d3af3

Please sign in to comment.