Skip to content

Commit

Permalink
Merge pull request serverless#5256 from serverless/fix-cors-race-cond…
Browse files Browse the repository at this point in the history
…ition

Fix CORS race condition
  • Loading branch information
pmuens authored Aug 28, 2018
2 parents b2a0b23 + afaeb65 commit 0179410
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
3 changes: 3 additions & 0 deletions lib/plugins/aws/package/compile/events/apiGateway/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class AwsCompileApigEvents {
this.options = options;
this.provider = this.serverless.getProvider('aws');

// used for the generated method logical ids (GET, PATCH, PUT, DELETE, OPTIONS, ...)
this.apiGatewayMethodLogicalIds = [];

Object.assign(
this,
validate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ describe('AwsCompileApigEvents', () => {
it('should set the provider variable to be an instanceof AwsProvider', () =>
expect(awsCompileApigEvents.provider).to.be.instanceof(AwsProvider));

it('should setup an empty array to gather the method logical ids', () =>
expect(awsCompileApigEvents.apiGatewayMethodLogicalIds).to.deep.equal([]));

it('should run "package:compileEvents" promise chain in order', () => {
const validateStub = sinon
.stub(awsCompileApigEvents, 'validate').returns({
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/aws/package/compile/events/apiGateway/lib/cors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const _ = require('lodash');
const BbPromise = require('bluebird');

module.exports = {

compileCors() {
_.forEach(this.validated.corsPreflight, (config, path) => {
const resourceName = this.getResourceName(path);
Expand Down Expand Up @@ -41,6 +40,8 @@ module.exports = {
.replace('ANY', 'DELETE,GET,HEAD,PATCH,POST,PUT');
}

this.apiGatewayMethodLogicalIds.push(corsMethodLogicalId);

_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, {
[corsMethodLogicalId]: {
Type: 'AWS::ApiGateway::Method',
Expand Down Expand Up @@ -97,5 +98,4 @@ module.exports = {
},
];
},

};
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ describe('#compileCors()', () => {
},
};
awsCompileApigEvents = new AwsCompileApigEvents(serverless, options);
awsCompileApigEvents.apiGatewayMethodLogicalIds = [];
awsCompileApigEvents.apiGatewayRestApiLogicalId = 'ApiGatewayRestApi';
awsCompileApigEvents.apiGatewayResources = {
'users/create': {
name: 'UsersCreate',
resourceLogicalId: 'ApiGatewayResourceUsersCreate',
},

'users/list': {
name: 'UsersList',
resourceLogicalId: 'ApiGatewayResourceUsersList',
Expand Down Expand Up @@ -252,4 +252,29 @@ describe('#compileCors()', () => {
expect(() => awsCompileApigEvents.compileCors())
.to.throw(Error, 'maxAge should be an integer over 0');
});

it('should add the methods resource logical id to the array of method logical ids', () => {
awsCompileApigEvents.validated.corsPreflight = {
'users/create': {
origins: ['*', 'http://example.com'],
headers: ['*'],
methods: ['OPTIONS', 'POST'],
allowCredentials: true,
maxAge: 86400,
},
'users/any': {
origins: ['http://example.com'],
headers: ['*'],
methods: ['OPTIONS', 'ANY'],
allowCredentials: false,
},
};
return awsCompileApigEvents.compileCors().then(() => {
expect(awsCompileApigEvents.apiGatewayMethodLogicalIds)
.to.deep.equal([
'ApiGatewayMethodUsersCreateOptions',
'ApiGatewayMethodUsersAnyOptions',
]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ const BbPromise = require('bluebird');
const _ = require('lodash');

module.exports = {

compileMethods() {
this.apiGatewayMethodLogicalIds = [];
this.permissionMapping = [];

this.validated.events.forEach((event) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe('#compileMethods()', () => {
};
awsCompileApigEvents = new AwsCompileApigEvents(serverless, options);
awsCompileApigEvents.validated = {};
awsCompileApigEvents.apiGatewayMethodLogicalIds = [];
awsCompileApigEvents.apiGatewayRestApiLogicalId = 'ApiGatewayRestApi';
awsCompileApigEvents.apiGatewayResources = {
'users/create': {
Expand Down Expand Up @@ -609,7 +610,7 @@ describe('#compileMethods()', () => {
});
});

it('should create methodLogicalIds array', () => {
it('should update the method logical ids array', () => {
awsCompileApigEvents.validated.events = [
{
functionName: 'First',
Expand All @@ -628,6 +629,10 @@ describe('#compileMethods()', () => {
];
return awsCompileApigEvents.compileMethods().then(() => {
expect(awsCompileApigEvents.apiGatewayMethodLogicalIds.length).to.equal(2);
expect(awsCompileApigEvents.apiGatewayMethodLogicalIds).to.deep.equal([
'ApiGatewayMethodUsersCreatePost',
'ApiGatewayMethodUsersListGet',
]);
});
});

Expand Down

0 comments on commit 0179410

Please sign in to comment.