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

chore(apigatewayv2): disallow allowCredentials when allowOrigin has '*' #9415

Merged
merged 1 commit into from
Aug 3, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions packages/@aws-cdk/aws-apigatewayv2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ The `corsPreflight` option lets you specify a CORS configuration for an API.
```ts
new HttpApi(stack, 'HttpProxyApi', {
corsPreflight: {
allowCredentials: true,
allowHeaders: ['Authorization'],
allowMethods: [HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS, HttpMethod.POST],
allowOrigins: ['*'],
Expand Down Expand Up @@ -136,8 +135,6 @@ the API's URL - `https://{api_id}.execute-api.{region}.amazonaws.com/`.

Note that, `HttpApi` will always creates a `$default` stage, unless the `createDefaultStage` property is unset.



### Custom Domain

Custom domain names are simpler and more intuitive URLs that you can provide to your API users. Custom domain name are associated to API stages.
Expand Down
4 changes: 4 additions & 0 deletions packages/@aws-cdk/aws-apigatewayv2/lib/http/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ export class HttpApi extends Resource implements IHttpApi {

let corsConfiguration: CfnApi.CorsProperty | undefined;
if (props?.corsPreflight) {
const cors = props.corsPreflight;
if (cors.allowOrigins && cors.allowOrigins.includes('*') && cors.allowCredentials) {
throw new Error("CORS preflight - allowCredentials is not supported when allowOrigin is '*'");
}
const {
allowCredentials,
allowHeaders,
Expand Down
45 changes: 44 additions & 1 deletion packages/@aws-cdk/aws-apigatewayv2/test/http/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import '@aws-cdk/assert/jest';
import { ABSENT } from '@aws-cdk/assert';
import { Code, Function, Runtime } from '@aws-cdk/aws-lambda';
import { Stack } from '@aws-cdk/core';
import { Duration, Stack } from '@aws-cdk/core';
import { HttpApi, HttpMethod, LambdaProxyIntegration } from '../../lib';

describe('HttpApi', () => {
Expand Down Expand Up @@ -113,4 +114,46 @@ describe('HttpApi', () => {
RouteKey: 'ANY /pets',
});
});

describe('cors', () => {
test('cors is correctly configured.', () => {
const stack = new Stack();
new HttpApi(stack, 'HttpApi', {
corsPreflight: {
allowHeaders: ['Authorization'],
allowMethods: [HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS, HttpMethod.POST],
allowOrigins: ['*'],
maxAge: Duration.seconds(36400),
},
});

expect(stack).toHaveResource('AWS::ApiGatewayV2::Api', {
CorsConfiguration: {
AllowHeaders: ['Authorization'],
AllowMethods: ['GET', 'HEAD', 'OPTIONS', 'POST'],
AllowOrigins: ['*'],
MaxAge: 36400,
},
});
});

test('cors is absent when not specified.', () => {
const stack = new Stack();
new HttpApi(stack, 'HttpApi');

expect(stack).toHaveResource('AWS::ApiGatewayV2::Api', {
CorsConfiguration: ABSENT,
});
});

test('errors when allowConfiguration is specified along with origin "*"', () => {
const stack = new Stack();
expect(() => new HttpApi(stack, 'HttpApi', {
corsPreflight: {
allowCredentials: true,
allowOrigins: ['*'],
},
})).toThrowError(/allowCredentials is not supported/);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ABSENT } from '@aws-cdk/assert';
import '@aws-cdk/assert/jest';
import { Duration, Stack } from '@aws-cdk/core';
import { Stack } from '@aws-cdk/core';
import { HttpApi, HttpIntegration, HttpIntegrationType, HttpMethod, HttpProxyIntegration, HttpRoute, HttpRouteKey, PayloadFormatVersion } from '../../../lib';

describe('HttpProxyIntegration', () => {
Expand Down Expand Up @@ -72,37 +71,3 @@ describe('HttpProxyIntegration', () => {
});
});
});

describe('CORS', () => {
test('CORS Configuration is correctly configured.', () => {
const stack = new Stack();
new HttpApi(stack, 'HttpApi', {
corsPreflight: {
allowCredentials: true,
allowHeaders: ['Authorization'],
allowMethods: [HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS, HttpMethod.POST],
allowOrigins: ['*'],
maxAge: Duration.seconds(36400),
},
});

expect(stack).toHaveResource('AWS::ApiGatewayV2::Api', {
CorsConfiguration: {
AllowCredentials: true,
AllowHeaders: ['Authorization'],
AllowMethods: ['GET', 'HEAD', 'OPTIONS', 'POST'],
AllowOrigins: ['*'],
MaxAge: 36400,
},
});
});

test('CorsConfiguration is ABSENT when not specified.', () => {
const stack = new Stack();
new HttpApi(stack, 'HttpApi');

expect(stack).toHaveResource('AWS::ApiGatewayV2::Api', {
CorsConfiguration: ABSENT,
});
});
});