-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(terraform): Over permissive Lambda Cors check (Terraform & Cloudformation) #7113
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
Merged
yuvalmich
merged 7 commits into
bridgecrewio:main
from
yuvalmich:yuval/add-lambda-cors-rule
Apr 22, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c559cd5
Add terraform graph check: over permissive lambda cors
d42f197
Add cloudformation graph check: over permissive lambda cors
dd60e98
Merge branch 'main' into yuval/add-lambda-cors-rule
yuvalmich 17977cd
CR changes: apply the filter on the function itself
dbb086e
Pass the check if no connection between aws_lambda_function and aws_l…
68fe0c1
Merge branch 'main' into yuval/add-lambda-cors-rule
235a21a
increment checkov check id counter after pull changes from main
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
checkov/cloudformation/checks/graph_checks/LambdaOpenCorsPolicy.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| metadata: | ||
| name: "Ensure no open CORS policy" | ||
| id: "CKV2_AWS_75" | ||
| category: "NETWORKING" | ||
| scope: | ||
| provider: "aws" | ||
| definition: | ||
| and: | ||
| - cond_type: "filter" | ||
| attribute: "resource_type" | ||
| value: | ||
| - "AWS::Lambda::Function" | ||
| operator: "within" | ||
| - or: | ||
| - cond_type: "connection" | ||
| operator: "not_exists" | ||
| resource_types: | ||
| - "AWS::Lambda::Function" | ||
| connected_resource_types: | ||
| - "AWS::Lambda::Url" | ||
| - and: | ||
| - cond_type: "connection" | ||
| operator: "exists" | ||
| resource_types: | ||
| - "AWS::Lambda::Function" | ||
| connected_resource_types: | ||
| - "AWS::Lambda::Url" | ||
| - or: | ||
| - cond_type: "attribute" | ||
| resource_types: | ||
| - "AWS::Lambda::Url" | ||
| attribute: "Cors.AllowOrigins" | ||
| operator: "not_contains" | ||
| value: "*" | ||
| - cond_type: "attribute" | ||
| resource_types: | ||
| - "AWS::Lambda::Url" | ||
| attribute: "Cors.AllowMethods" | ||
| operator: "not_contains" | ||
| value: "*" |
40 changes: 40 additions & 0 deletions
40
checkov/terraform/checks/graph_checks/aws/LambdaOpenCorsPolicy.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| metadata: | ||
| name: "Ensure no open CORS policy" | ||
| id: "CKV2_AWS_75" | ||
| category: "NETWORKING" | ||
| scope: | ||
| provider: "aws" | ||
| definition: | ||
| and: | ||
| - cond_type: "filter" | ||
| attribute: "resource_type" | ||
| value: | ||
| - "aws_lambda_function" | ||
| operator: "within" | ||
| - or: | ||
| - cond_type: "connection" | ||
| operator: "not_exists" | ||
| resource_types: | ||
| - "aws_lambda_function" | ||
| connected_resource_types: | ||
| - "aws_lambda_function_url" | ||
| - and: | ||
| - cond_type: "connection" | ||
| operator: "exists" | ||
| resource_types: | ||
| - "aws_lambda_function" | ||
| connected_resource_types: | ||
| - "aws_lambda_function_url" | ||
| - or: | ||
| - cond_type: "attribute" | ||
| resource_types: | ||
| - "aws_lambda_function_url" | ||
| attribute: "cors.allow_origins" | ||
| operator: "not_contains" | ||
| value: "*" | ||
| - cond_type: "attribute" | ||
| resource_types: | ||
| - "aws_lambda_function_url" | ||
| attribute: "cors.allow_methods" | ||
| operator: "not_contains" | ||
| value: "*" |
12 changes: 12 additions & 0 deletions
12
tests/cloudformation/graph/checks/resources/LambdaOpenCorsPolicy/expected.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| pass: | ||
| - "AWS::Lambda::Function.ValidLambdaFunction" | ||
| - "AWS::Lambda::Function.ValidLambdaFunctionWithoutUrl" | ||
| - "AWS::Lambda::Function.ValidLambdaFunctionNoCorsDefinition" | ||
| - "AWS::Lambda::Function.ValidLambdaFunctionOnlyOriginsStar" | ||
| - "AWS::Lambda::Function.ValidLambdaFunctionOnlyMethodsStar" | ||
| fail: | ||
| - "AWS::Lambda::Function.InvalidLambdaFunction" | ||
| evaluated_keys: | ||
| - Cors/AllowMethods | ||
| - Cors/AllowOrigins | ||
| - resource_type | ||
129 changes: 129 additions & 0 deletions
129
tests/cloudformation/graph/checks/resources/LambdaOpenCorsPolicy/template.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| Resources: | ||
| ValidLambdaFunctionWithoutUrl: | ||
| Type: AWS::Lambda::Function | ||
| Properties: | ||
| Handler: index.handler | ||
| Runtime: nodejs14.x | ||
| Role: !GetAtt ExampleRole.Arn | ||
| Code: | ||
| S3Bucket: example-bucket | ||
| S3Key: lambda_function_payload.zip | ||
|
|
||
| ValidLambdaFunction: | ||
| Type: AWS::Lambda::Function | ||
| Properties: | ||
| FunctionName: example_lambda_function | ||
| Handler: index.handler | ||
| Runtime: nodejs14.x | ||
| Role: !GetAtt ExampleRole.Arn | ||
| Code: | ||
| S3Bucket: example-bucket | ||
| S3Key: lambda_function_payload.zip | ||
|
|
||
| ValidLambdaUrl: | ||
| Type: AWS::Lambda::Url | ||
| Properties: | ||
| FunctionName: !Ref ValidLambdaFunction | ||
| Cors: | ||
| AllowOrigins: | ||
| - https://example.com | ||
| AllowMethods: | ||
| - GET | ||
| - POST | ||
| ServiceToken: "arn:aws:lambda:us-west-2:123456789012:function:dummy-token" | ||
|
|
||
| ValidLambdaFunctionNoCorsDefinition: | ||
| Type: AWS::Lambda::Function | ||
| Properties: | ||
| FunctionName: example_lambda_function | ||
| Handler: index.handler | ||
| Runtime: nodejs14.x | ||
| Role: !GetAtt ExampleRole.Arn | ||
| Code: | ||
| S3Bucket: example-bucket | ||
| S3Key: lambda_function_payload.zip | ||
|
|
||
| ValidLambdaUrlNoCorsDefinition: | ||
| Type: AWS::Lambda::Url | ||
| Properties: | ||
| FunctionName: !Ref ValidLambdaFunctionNoCorsDefinition | ||
| ServiceToken: "arn:aws:lambda:us-west-2:123456789012:function:dummy-token" | ||
|
|
||
| ValidLambdaFunctionOnlyOriginsStar: | ||
| Type: AWS::Lambda::Function | ||
| Properties: | ||
| FunctionName: example_lambda_function | ||
| Handler: index.handler | ||
| Runtime: nodejs14.x | ||
| Role: !GetAtt ExampleRole.Arn | ||
| Code: | ||
| S3Bucket: example-bucket | ||
| S3Key: lambda_function_payload.zip | ||
|
|
||
| ValidLambdaUrlOnlyOriginsStar: | ||
| Type: AWS::Lambda::Url | ||
| Properties: | ||
| FunctionName: !Ref ValidLambdaFunctionOnlyOriginsStar | ||
| Cors: | ||
| AllowOrigins: | ||
| - "*" | ||
| AllowMethods: | ||
| - GET | ||
| - POST | ||
| ServiceToken: "arn:aws:lambda:us-west-2:123456789012:function:dummy-token" | ||
|
|
||
| ValidLambdaFunctionOnlyMethodsStar: | ||
| Type: AWS::Lambda::Function | ||
| Properties: | ||
| FunctionName: example_lambda_function | ||
| Handler: index.handler | ||
| Runtime: nodejs14.x | ||
| Role: !GetAtt ExampleRole.Arn | ||
| Code: | ||
| S3Bucket: example-bucket | ||
| S3Key: lambda_function_payload.zip | ||
|
|
||
| ValidLambdaUrlOnlyMethodsStar: | ||
| Type: AWS::Lambda::Url | ||
| Properties: | ||
| FunctionName: !Ref ValidLambdaFunctionOnlyMethodsStar | ||
| Cors: | ||
| AllowOrigins: | ||
| - https://example.com | ||
| AllowMethods: | ||
| - "*" | ||
| ServiceToken: "arn:aws:lambda:us-west-2:123456789012:function:dummy-token" | ||
|
|
||
| InvalidLambdaFunction: | ||
| Type: AWS::Lambda::Function | ||
| Properties: | ||
| FunctionName: example_lambda_function | ||
| Handler: index.handler | ||
| Runtime: nodejs14.x | ||
| Role: !GetAtt ExampleRole.Arn | ||
| Code: | ||
| S3Bucket: example-bucket | ||
| S3Key: lambda_function_payload.zip | ||
|
|
||
| InvalidLambdaUrl: | ||
| Type: AWS::Lambda::Url | ||
| Properties: | ||
| FunctionName: !Ref InvalidLambdaFunction | ||
| Cors: | ||
| AllowOrigins: | ||
| - "*" | ||
| AllowMethods: | ||
| - "*" | ||
| ServiceToken: "arn:aws:lambda:us-west-2:123456789012:function:dummy-token" | ||
|
|
||
| ExampleRole: | ||
| Type: AWS::IAM::Role | ||
| Properties: | ||
| RoleName: example_role | ||
| AssumeRolePolicyDocument: | ||
| Version: "2012-10-17" | ||
| Statement: | ||
| - Action: sts:AssumeRole | ||
| Effect: Allow | ||
| Principal: | ||
| Service: lambda.amazonaws.com |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
tests/terraform/graph/checks/resources/LambdaOpenCorsPolicy/expected.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| pass: | ||
| - "aws_lambda_function.valid_lambda_function" | ||
| - "aws_lambda_function.valid_lambda_function_no_cors_definition" | ||
| - "aws_lambda_function.valid_lambda_function_only_allow_origins_star" | ||
| - "aws_lambda_function.valid_lambda_function_only_allow_methods_star" | ||
| - "aws_lambda_function.valid_lambda_function_without_url" | ||
| fail: | ||
| - "aws_lambda_function.invalid_lambda_function" |
107 changes: 107 additions & 0 deletions
107
tests/terraform/graph/checks/resources/LambdaOpenCorsPolicy/main.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| provider "aws" { | ||
| region = "us-west-2" | ||
| } | ||
|
|
||
| resource "aws_lambda_function" "valid_lambda_function" { | ||
| function_name = "example_lambda_function" | ||
| handler = "index.handler" | ||
| runtime = "nodejs14.x" | ||
| role = aws_iam_role.example_role.arn | ||
| filename = "lambda_function_payload.zip" | ||
| } | ||
|
|
||
| resource "aws_lambda_function" "valid_lambda_function_without_url" { | ||
| function_name = "example_lambda_function" | ||
| handler = "index.handler" | ||
| runtime = "nodejs14.x" | ||
| role = aws_iam_role.example_role.arn | ||
| filename = "lambda_function_payload.zip" | ||
| } | ||
|
|
||
| resource "aws_lambda_function_url" "valid_lambda_function_url" { | ||
| function_name = aws_lambda_function.valid_lambda_function.function_name | ||
| cors { | ||
| allow_origins = ["https://example.com"] | ||
| allow_methods = ["GET", "POST"] | ||
| } | ||
| authorization_type = "AWS_IAM" | ||
| } | ||
|
|
||
| resource "aws_lambda_function" "valid_lambda_function_no_cors_definition" { | ||
| function_name = "example_lambda_function" | ||
| handler = "index.handler" | ||
| runtime = "nodejs14.x" | ||
| role = aws_iam_role.example_role.arn | ||
| filename = "lambda_function_payload.zip" | ||
| } | ||
|
|
||
| resource "aws_lambda_function_url" "valid_lambda_function_url_no_cors_definition" { | ||
| function_name = aws_lambda_function.valid_lambda_function_no_cors_definition.function_name | ||
| authorization_type = "AWS_IAM" | ||
| } | ||
|
|
||
| resource "aws_lambda_function" "valid_lambda_function_only_allow_origins_star" { | ||
| function_name = "example_lambda_function" | ||
| handler = "index.handler" | ||
| runtime = "nodejs14.x" | ||
| role = aws_iam_role.example_role.arn | ||
| filename = "lambda_function_payload.zip" | ||
| } | ||
|
|
||
| resource "aws_lambda_function_url" "valid_lambda_function_url_only_allow_origins_star" { | ||
| function_name = aws_lambda_function.valid_lambda_function_only_allow_origins_star.function_name | ||
| cors { | ||
| allow_origins = ["*"] | ||
| } | ||
| authorization_type = "AWS_IAM" | ||
| } | ||
|
|
||
| resource "aws_lambda_function" "valid_lambda_function_only_allow_methods_star" { | ||
| function_name = "example_lambda_function" | ||
| handler = "index.handler" | ||
| runtime = "nodejs14.x" | ||
| role = aws_iam_role.example_role.arn | ||
| filename = "lambda_function_payload.zip" | ||
| } | ||
|
|
||
| resource "aws_lambda_function_url" "valid_lambda_function_url_only_allow_methods_star" { | ||
| function_name = aws_lambda_function.valid_lambda_function_only_allow_methods_star.function_name | ||
| cors { | ||
| allow_methods = ["*"] | ||
| } | ||
| authorization_type = "AWS_IAM" | ||
| } | ||
|
|
||
| resource "aws_lambda_function" "invalid_lambda_function" { | ||
| function_name = "example_lambda_function" | ||
| handler = "index.handler" | ||
| runtime = "nodejs14.x" | ||
| role = aws_iam_role.example_role.arn | ||
| filename = "lambda_function_payload.zip" | ||
| } | ||
|
|
||
| resource "aws_lambda_function_url" "invalid_lambda_function_url" { | ||
| function_name = aws_lambda_function.invalid_lambda_function.function_name | ||
| cors { | ||
| allow_origins = ["*"] | ||
| allow_methods = ["*"] | ||
| } | ||
| authorization_type = "AWS_IAM" | ||
| } | ||
|
|
||
| resource "aws_iam_role" "example_role" { | ||
| name = "example_role" | ||
|
|
||
| assume_role_policy = jsonencode({ | ||
| Version = "2012-10-17", | ||
| Statement = [ | ||
| { | ||
| Action = "sts:AssumeRole", | ||
| Effect = "Allow", | ||
| Principal = { | ||
| Service = "lambda.amazonaws.com" | ||
| } | ||
| } | ||
| ] | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -471,7 +471,7 @@ def test_no_missing_ids(self): | |
| for check_list in [aws_checks, gcp_checks, azure_checks]: | ||
| check_list.sort(reverse=True, key=lambda s: int(s.split('_')[-1])) | ||
|
|
||
| for i in range(1, len(aws_checks) + 3): | ||
| for i in range(1, len(aws_checks) + 2): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this needed?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. He added the same check to 2 frameworks, so they need the same ID |
||
| if f'CKV2_AWS_{i}' == 'CKV2_AWS_17': | ||
| # CKV2_AWS_17 was overly keen and those resources it checks are created by default | ||
| continue | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the purpose of the evaluated keys here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't find any documentation about this field but I assume it asserts that the evaluated keys are the same keys that has an attribute field in the check yaml itself.
So if for example I ensured that the attribute
AllowOriginsdoesn't contain*it should appear in the testsevaluated_keys(otherwise the tests fail)