|
5 | 5 | from policyuniverse.policy import Policy
|
6 | 6 |
|
7 | 7 |
|
8 |
| -# Check that the IAM policy allows resource accessibility via the Internet |
9 |
| -def policy_is_internet_accessible(json_policy): |
10 |
| - if json_policy is None: |
| 8 | +# Check if a policy (string or JSON) allows resource accessibility via the Internet |
| 9 | +# pylint: disable=too-complex |
| 10 | +def policy_is_internet_accessible(policy): |
| 11 | + """ |
| 12 | + Check if a policy (string or JSON) allows resource accessibility via the Internet. |
| 13 | +
|
| 14 | + Args: |
| 15 | + policy: A policy object that can be either a string or a JSON object |
| 16 | +
|
| 17 | + Returns: |
| 18 | + bool: True if the policy allows internet access, False otherwise |
| 19 | + """ |
| 20 | + # Handle empty policies (None, empty strings, empty dicts, etc.) |
| 21 | + if not policy: |
11 | 22 | return False
|
12 |
| - return Policy(json_policy).is_internet_accessible() |
13 | 23 |
|
| 24 | + # Handle string policies by converting to JSON |
| 25 | + if isinstance(policy, str): |
| 26 | + try: |
| 27 | + policy = json.loads(policy) |
| 28 | + except json.JSONDecodeError: |
| 29 | + return False |
14 | 30 |
|
15 |
| -# Normally this check helps avoid overly complex functions that are doing too many things, |
16 |
| -# but in this case we explicitly want to handle 10 different cases in 10 different ways. |
17 |
| -# Any solution that avoids too many return statements only increases the complexity of this rule. |
18 |
| -# pylint: disable=too-many-return-statements, too-complex |
19 |
| -def rule(event): |
20 |
| - if not aws_cloudtrail_success(event): |
21 |
| - return False |
| 31 | + # Check if the policy has a wildcard principal but also has organization ID restrictions |
| 32 | + # which should not be considered internet accessible |
| 33 | + policy_obj = Policy(policy) |
22 | 34 |
|
23 |
| - parameters = event.get("requestParameters", {}) |
24 |
| - # Ignore events that are missing request params |
25 |
| - if not parameters: |
| 35 | + # If policyuniverse thinks it's not internet accessible, trust that |
| 36 | + if not policy_obj.is_internet_accessible(): |
26 | 37 | return False
|
27 | 38 |
|
28 |
| - policy = "" |
29 |
| - |
30 |
| - # S3 |
31 |
| - if event["eventName"] == "PutBucketPolicy": |
32 |
| - return policy_is_internet_accessible(parameters.get("bucketPolicy")) |
| 39 | + # For policies with multiple statements, we need to check each statement individually |
| 40 | + # If ANY statement is truly internet accessible, the policy is internet accessible |
| 41 | + has_internet_accessible_statement = False |
33 | 42 |
|
34 |
| - # ECR |
35 |
| - if event["eventName"] == "SetRepositoryPolicy": |
36 |
| - policy = parameters.get("policyText", {}) |
| 43 | + for statement in policy_obj.statements: |
| 44 | + if statement.effect != "Allow" or "*" not in statement.principals: |
| 45 | + continue |
37 | 46 |
|
38 |
| - # Elasticsearch |
39 |
| - if event["eventName"] in ["CreateElasticsearchDomain", "UpdateElasticsearchDomainConfig"]: |
40 |
| - policy = parameters.get("accessPolicies", {}) |
| 47 | + # Check if there are organization ID conditions which restrict access |
| 48 | + has_org_condition = False |
| 49 | + for condition in statement.condition_entries: |
| 50 | + if condition.category == "organization": |
| 51 | + has_org_condition = True |
| 52 | + break |
41 | 53 |
|
42 |
| - # KMS |
43 |
| - if event["eventName"] in ["CreateKey", "PutKeyPolicy"]: |
44 |
| - policy = parameters.get("policy", {}) |
| 54 | + # If this statement has a wildcard principal but no organization ID restrictions, |
| 55 | + # it's truly internet accessible |
| 56 | + if not has_org_condition: |
| 57 | + has_internet_accessible_statement = True |
| 58 | + break |
45 | 59 |
|
46 |
| - # S3 Glacier |
47 |
| - if event["eventName"] == "SetVaultAccessPolicy": |
48 |
| - policy = deep_get(parameters, "policy", "policy", default={}) |
| 60 | + return has_internet_accessible_statement |
49 | 61 |
|
50 |
| - # SNS & SQS |
51 |
| - if event["eventName"] in ["SetQueueAttributes", "CreateTopic"]: |
52 |
| - policy = deep_get(parameters, "attributes", "Policy", default={}) |
53 | 62 |
|
54 |
| - # SNS |
55 |
| - if ( |
56 |
| - event["eventName"] == "SetTopicAttributes" |
57 |
| - and parameters.get("attributeName", "") == "Policy" |
58 |
| - ): |
59 |
| - policy = parameters.get("attributeValue", {}) |
| 63 | +def rule(event): |
| 64 | + if not aws_cloudtrail_success(event): |
| 65 | + return False |
60 | 66 |
|
61 |
| - # SecretsManager |
62 |
| - if event["eventName"] == "PutResourcePolicy": |
63 |
| - policy = parameters.get("resourcePolicy", {}) |
| 67 | + parameters = event.get("requestParameters", {}) |
| 68 | + # Ignore events that are missing request params |
| 69 | + if not parameters: |
| 70 | + return False |
64 | 71 |
|
65 |
| - if not policy: |
| 72 | + event_name = event.get("eventName", "") |
| 73 | + |
| 74 | + # Special case for SNS topic attributes that need additional attribute name check |
| 75 | + if event_name == "SetTopicAttributes" and parameters.get("attributeName", "") == "Policy": |
| 76 | + policy_value = parameters.get("attributeValue", {}) |
| 77 | + return policy_is_internet_accessible(policy_value) |
| 78 | + |
| 79 | + # Map of event names to policy locations in parameters |
| 80 | + policy_location_map = { |
| 81 | + # S3 |
| 82 | + "PutBucketPolicy": lambda p: p.get("bucketPolicy", {}), |
| 83 | + # ECR |
| 84 | + "SetRepositoryPolicy": lambda p: p.get("policyText", {}), |
| 85 | + # Elasticsearch |
| 86 | + "CreateElasticsearchDomain": lambda p: p.get("accessPolicies", {}), |
| 87 | + "UpdateElasticsearchDomainConfig": lambda p: p.get("accessPolicies", {}), |
| 88 | + # KMS |
| 89 | + "CreateKey": lambda p: p.get("policy", {}), |
| 90 | + "PutKeyPolicy": lambda p: p.get("policy", {}), |
| 91 | + # S3 Glacier |
| 92 | + "SetVaultAccessPolicy": lambda p: deep_get(p, "policy", "policy", default={}), |
| 93 | + # SNS & SQS |
| 94 | + "SetQueueAttributes": lambda p: deep_get(p, "attributes", "Policy", default={}), |
| 95 | + "CreateTopic": lambda p: deep_get(p, "attributes", "Policy", default={}), |
| 96 | + # SecretsManager |
| 97 | + "PutResourcePolicy": lambda p: p.get("resourcePolicy", {}), |
| 98 | + } |
| 99 | + |
| 100 | + # Get the policy extraction function for this event name |
| 101 | + policy_extractor = policy_location_map.get(event_name) |
| 102 | + if not policy_extractor: |
66 | 103 | return False
|
67 | 104 |
|
68 |
| - return policy_is_internet_accessible(json.loads(policy)) |
| 105 | + # Extract the policy using the appropriate function |
| 106 | + policy = policy_extractor(parameters) |
| 107 | + return policy_is_internet_accessible(policy) |
69 | 108 |
|
70 | 109 |
|
71 | 110 | def title(event):
|
|
0 commit comments