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

Fixes for AWS cors config issues #5785

Merged
merged 5 commits into from Feb 4, 2019
Merged
Diff settings

Always

Just for now

@@ -18,11 +18,11 @@ module.exports = {
origins = origin.split(',').map(a => a.trim()); origins = origin.split(',').map(a => a.trim());
} }


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


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


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

corsPreflight[http.path] = cors; corsPreflight[http.path] = cors;
} }


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


@@ -676,6 +678,7 @@ describe('#validate()', () => {
], ],
allowCredentials: true, allowCredentials: true,
maxAge: 10000, maxAge: 10000,
cacheControl: 'max-age=600, s-maxage=600, proxy-revalidate',
}, },
}, },
}, { }, {
@@ -723,6 +726,8 @@ describe('#validate()', () => {
.to.deep.equal(['TestHeader2', 'TestHeader']); .to.deep.equal(['TestHeader2', 'TestHeader']);
expect(validated.corsPreflight.users.maxAge) expect(validated.corsPreflight.users.maxAge)
.to.equal(86400); .to.equal(86400);
expect(validated.corsPreflight.users.cacheControl)
.to.equal('max-age=600, s-maxage=600, proxy-revalidate');
expect(validated.corsPreflight.users.allowCredentials) expect(validated.corsPreflight.users.allowCredentials)
.to.equal(true); .to.equal(true);
expect(validated.corsPreflight['users/{id}'].allowCredentials) expect(validated.corsPreflight['users/{id}'].allowCredentials)
ProTip! Use n and p to navigate between commits in a pull request.