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

feat: not check cognito User Pool Authorizers for OPTIONS method #1834

Merged
merged 7 commits into from
Nov 14, 2024
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
6 changes: 5 additions & 1 deletion src/rules/cognito/CognitoUserPoolAPIGWAuthorizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ SPDX-License-Identifier: Apache-2.0
import { parse } from 'path';
import { CfnResource } from 'aws-cdk-lib';
import { CfnMethod } from 'aws-cdk-lib/aws-apigateway';
import { NagRuleCompliance } from '../../nag-rules';
import { NagRuleCompliance, NagRules } from '../../nag-rules';

/**
* Rest API methods use Cognito User Pool Authorizers
Expand All @@ -14,6 +14,10 @@ import { NagRuleCompliance } from '../../nag-rules';
export default Object.defineProperty(
(node: CfnResource): NagRuleCompliance => {
if (node instanceof CfnMethod) {
const httpMethod = NagRules.resolveIfPrimitive(node, node.httpMethod);
if (httpMethod === 'OPTIONS') {
return NagRuleCompliance.NOT_APPLICABLE;
}
if (node.authorizationType !== 'COGNITO_USER_POOLS') {
return NagRuleCompliance.NON_COMPLIANT;
}
Expand Down
19 changes: 14 additions & 5 deletions test/rules/Cognito.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
import { RestApi, AuthorizationType } from 'aws-cdk-lib/aws-apigateway';
import { AuthorizationType, Cors, RestApi } from 'aws-cdk-lib/aws-apigateway';
import {
UserPool,
Mfa,
CfnUserPool,
CfnIdentityPool,
CfnUserPool,
Mfa,
UserPool,
} from 'aws-cdk-lib/aws-cognito';
import { Aspects, Stack } from 'aws-cdk-lib/core';
import { validateStack, TestType, TestPack } from './utils';
import { TestPack, TestType, validateStack } from './utils';
import {
CognitoUserPoolAPIGWAuthorizer,
CognitoUserPoolAdvancedSecurityModeEnforced,
Expand Down Expand Up @@ -114,6 +114,15 @@ describe('Amazon Cognito', () => {
});
validateStack(stack, ruleId, TestType.COMPLIANCE);
});

test('Compliance 2', () => {
new RestApi(stack, 'Rest', {
defaultCorsPreflightOptions: {
allowOrigins: Cors.ALL_ORIGINS,
},
});
validateStack(stack, ruleId, TestType.COMPLIANCE);
});
});

describe('CognitoUserPoolNoUnauthenticatedLogins: Cognito identity pools do not allow for unauthenticated logins without a valid reason', () => {
Expand Down
Loading