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(s3): reduce false positive in s3 public check #4281

Merged

Conversation

puchy22
Copy link
Contributor

@puchy22 puchy22 commented Jun 20, 2024

Context

Fixes #4257

Not checking conditions in policy statements

Description

Add new policy function to check policy conditions and improve checks using this functions. Everything added have been tested properly.

License

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@puchy22 puchy22 requested review from a team as code owners June 20, 2024 10:36
@github-actions github-actions bot added the provider/aws Issues/PRs related with the AWS provider label Jun 20, 2024
Copy link

codecov bot commented Jun 20, 2024

Codecov Report

Attention: Patch coverage is 93.75000% with 2 lines in your changes missing coverage. Please review.

Project coverage is 87.03%. Comparing base (5353d51) to head (a114b07).
Report is 20 commits behind head on master.

Files Patch % Lines
prowler/providers/aws/services/iam/lib/policy.py 91.66% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4281      +/-   ##
==========================================
+ Coverage   86.66%   87.03%   +0.36%     
==========================================
  Files         818      843      +25     
  Lines       25709    26334     +625     
==========================================
+ Hits        22281    22919     +638     
+ Misses       3428     3415      -13     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

sergargar
sergargar previously approved these changes Jun 20, 2024
Copy link
Member

@sergargar sergargar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, love this fix!

@sergargar sergargar changed the title feat(s3): reduce false positive in s3 public check chore(s3): reduce false positive in s3 public check Jun 20, 2024
@jmanduca-psfy
Copy link

Just tried it out, it works for some bucket policies but not others - this looks like it is due to the case of your no_public_conditions, as the value in policy is not case-sensitive, causing some that were manually created to fail.
Fail:
"Condition": { "StringEquals": { "aws:sourcevpc": "vpc-12334"
Pass:
"Condition": { "StringEquals": { "aws:SourceVpc": "vpc-12334"

@jfagoagas
Copy link
Member

Just tried it out, it works for some bucket policies but not others - this looks like it is due to the case of your no_public_conditions, as the value in policy is not case-sensitive, causing some that were manually created to fail. Fail: "Condition": { "StringEquals": { "aws:sourcevpc": "vpc-12334" Pass: "Condition": { "StringEquals": { "aws:SourceVpc": "vpc-12334"

Great catch, we need to do the match that way, please @puchy22 change it and add some tests to cover that.

Copy link
Member

@jfagoagas jfagoagas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above comment and check the function is_condition_block_restrictive at https://github.com/prowler-cloud/prowler/blob/9fbd627f9aec3edee4f7ddc25849faa11db6187e/prowler/providers/aws/lib/policy_condition_parser/policy_condition_parser.py

We should unify both functions since they are similar.

@sergargar sergargar added the backport-v3 Pending to port to Prowler v3 branch label Jun 20, 2024
@sergargar
Copy link
Member

@puchy22 where did you get the no_public_conditions from?

@sergargar sergargar self-requested a review June 20, 2024 17:10
@@ -68,3 +73,96 @@ def is_policy_public(policy: dict) -> bool:
) and "Condition" not in statement:
return True
return False


def check_full_service_access(service: str, policy: dict) -> bool:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this function being used?

@jfagoagas jfagoagas self-requested a review June 25, 2024 07:04
Comment on lines 147 to 150
if condition_statement["IpAddress"].get("aws:sourceip", ""):
if not isinstance(condition_statement["IpAddress"]["aws:sourceip"], list):
condition_statement["IpAddress"]["aws:sourceip"] = [
condition_statement["IpAddress"]["aws:sourceip"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you define the following as constants?

  • aws:sourceip
  • IpAddress

}
"""

is_from_private_ip = False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a try/except block for the whole function?

Comment on lines 163 to 164
except ValueError:
logger.error(f"Invalid IP: {ip}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cover this line with a test please.


is_from_private_ip = False

if condition_statement.get("IpAddress", {}):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will move this line into the function caller, makes little sense to call the function if the condition is not IpAddress.

Comment on lines 133 to 136
assert check_full_service_access("s3", policy1)
assert not check_full_service_access("s3", policy2)
assert not check_full_service_access("s3", policy3)
assert not check_full_service_access("s3", policy4)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please create one test case for each policy? It's simpler to work with the tests are failing.

@jfagoagas jfagoagas self-requested a review June 25, 2024 08:23
Copy link
Member

@jfagoagas jfagoagas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is pure gold 🏅 Thanks for the effort!!

Copy link
Member

@sergargar sergargar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤩

@sergargar sergargar merged commit 093738c into prowler-cloud:master Jun 25, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-v3 Pending to port to Prowler v3 branch provider/aws Issues/PRs related with the AWS provider
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: False Positive on check s3_bucket_public_access when Conditions in Policy
4 participants