iam-policy-validator 1.7.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of iam-policy-validator might be problematic. Click here for more details.
- iam_policy_validator-1.7.0.dist-info/METADATA +1057 -0
- iam_policy_validator-1.7.0.dist-info/RECORD +83 -0
- iam_policy_validator-1.7.0.dist-info/WHEEL +4 -0
- iam_policy_validator-1.7.0.dist-info/entry_points.txt +2 -0
- iam_policy_validator-1.7.0.dist-info/licenses/LICENSE +21 -0
- iam_validator/__init__.py +27 -0
- iam_validator/__main__.py +11 -0
- iam_validator/__version__.py +7 -0
- iam_validator/checks/__init__.py +43 -0
- iam_validator/checks/action_condition_enforcement.py +884 -0
- iam_validator/checks/action_resource_matching.py +441 -0
- iam_validator/checks/action_validation.py +72 -0
- iam_validator/checks/condition_key_validation.py +92 -0
- iam_validator/checks/condition_type_mismatch.py +259 -0
- iam_validator/checks/full_wildcard.py +71 -0
- iam_validator/checks/mfa_condition_check.py +112 -0
- iam_validator/checks/policy_size.py +147 -0
- iam_validator/checks/policy_type_validation.py +305 -0
- iam_validator/checks/principal_validation.py +776 -0
- iam_validator/checks/resource_validation.py +138 -0
- iam_validator/checks/sensitive_action.py +254 -0
- iam_validator/checks/service_wildcard.py +107 -0
- iam_validator/checks/set_operator_validation.py +157 -0
- iam_validator/checks/sid_uniqueness.py +170 -0
- iam_validator/checks/utils/__init__.py +1 -0
- iam_validator/checks/utils/policy_level_checks.py +143 -0
- iam_validator/checks/utils/sensitive_action_matcher.py +294 -0
- iam_validator/checks/utils/wildcard_expansion.py +87 -0
- iam_validator/checks/wildcard_action.py +67 -0
- iam_validator/checks/wildcard_resource.py +135 -0
- iam_validator/commands/__init__.py +25 -0
- iam_validator/commands/analyze.py +531 -0
- iam_validator/commands/base.py +48 -0
- iam_validator/commands/cache.py +392 -0
- iam_validator/commands/download_services.py +255 -0
- iam_validator/commands/post_to_pr.py +86 -0
- iam_validator/commands/validate.py +600 -0
- iam_validator/core/__init__.py +14 -0
- iam_validator/core/access_analyzer.py +671 -0
- iam_validator/core/access_analyzer_report.py +640 -0
- iam_validator/core/aws_fetcher.py +940 -0
- iam_validator/core/check_registry.py +607 -0
- iam_validator/core/cli.py +134 -0
- iam_validator/core/condition_validators.py +626 -0
- iam_validator/core/config/__init__.py +81 -0
- iam_validator/core/config/aws_api.py +35 -0
- iam_validator/core/config/aws_global_conditions.py +160 -0
- iam_validator/core/config/category_suggestions.py +104 -0
- iam_validator/core/config/condition_requirements.py +155 -0
- iam_validator/core/config/config_loader.py +472 -0
- iam_validator/core/config/defaults.py +523 -0
- iam_validator/core/config/principal_requirements.py +421 -0
- iam_validator/core/config/sensitive_actions.py +672 -0
- iam_validator/core/config/service_principals.py +95 -0
- iam_validator/core/config/wildcards.py +124 -0
- iam_validator/core/constants.py +74 -0
- iam_validator/core/formatters/__init__.py +27 -0
- iam_validator/core/formatters/base.py +147 -0
- iam_validator/core/formatters/console.py +59 -0
- iam_validator/core/formatters/csv.py +170 -0
- iam_validator/core/formatters/enhanced.py +440 -0
- iam_validator/core/formatters/html.py +672 -0
- iam_validator/core/formatters/json.py +33 -0
- iam_validator/core/formatters/markdown.py +63 -0
- iam_validator/core/formatters/sarif.py +251 -0
- iam_validator/core/models.py +327 -0
- iam_validator/core/policy_checks.py +656 -0
- iam_validator/core/policy_loader.py +396 -0
- iam_validator/core/pr_commenter.py +424 -0
- iam_validator/core/report.py +872 -0
- iam_validator/integrations/__init__.py +28 -0
- iam_validator/integrations/github_integration.py +815 -0
- iam_validator/integrations/ms_teams.py +442 -0
- iam_validator/sdk/__init__.py +187 -0
- iam_validator/sdk/arn_matching.py +382 -0
- iam_validator/sdk/context.py +222 -0
- iam_validator/sdk/exceptions.py +48 -0
- iam_validator/sdk/helpers.py +177 -0
- iam_validator/sdk/policy_utils.py +425 -0
- iam_validator/sdk/shortcuts.py +283 -0
- iam_validator/utils/__init__.py +31 -0
- iam_validator/utils/cache.py +105 -0
- iam_validator/utils/regex.py +206 -0
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
"""Condition Type Mismatch Check.
|
|
2
|
+
|
|
3
|
+
Validates that condition operators match the expected types for condition keys and values.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from iam_validator.core.aws_fetcher import AWSServiceFetcher
|
|
7
|
+
from iam_validator.core.check_registry import CheckConfig, PolicyCheck
|
|
8
|
+
from iam_validator.core.condition_validators import (
|
|
9
|
+
normalize_operator,
|
|
10
|
+
translate_type,
|
|
11
|
+
validate_value_for_type,
|
|
12
|
+
)
|
|
13
|
+
from iam_validator.core.models import Statement, ValidationIssue
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ConditionTypeMismatchCheck(PolicyCheck):
|
|
17
|
+
"""Check for type mismatches between operators, keys, and values."""
|
|
18
|
+
|
|
19
|
+
@property
|
|
20
|
+
def check_id(self) -> str:
|
|
21
|
+
"""Unique identifier for this check."""
|
|
22
|
+
return "condition_type_mismatch"
|
|
23
|
+
|
|
24
|
+
@property
|
|
25
|
+
def description(self) -> str:
|
|
26
|
+
"""Description of what this check does."""
|
|
27
|
+
return "Validates condition operator types match key types and value formats"
|
|
28
|
+
|
|
29
|
+
@property
|
|
30
|
+
def default_severity(self) -> str:
|
|
31
|
+
"""Default severity level for issues found by this check."""
|
|
32
|
+
return "error"
|
|
33
|
+
|
|
34
|
+
async def execute(
|
|
35
|
+
self,
|
|
36
|
+
statement: Statement,
|
|
37
|
+
statement_idx: int,
|
|
38
|
+
fetcher: AWSServiceFetcher,
|
|
39
|
+
config: CheckConfig,
|
|
40
|
+
) -> list[ValidationIssue]:
|
|
41
|
+
"""
|
|
42
|
+
Execute the condition type mismatch check.
|
|
43
|
+
|
|
44
|
+
Validates:
|
|
45
|
+
1. Operator type matches condition key type
|
|
46
|
+
2. Condition values match the expected type format
|
|
47
|
+
|
|
48
|
+
Args:
|
|
49
|
+
statement: The IAM statement to check
|
|
50
|
+
statement_idx: Index of this statement in the policy
|
|
51
|
+
fetcher: AWS service fetcher for looking up condition key types
|
|
52
|
+
config: Check configuration
|
|
53
|
+
|
|
54
|
+
Returns:
|
|
55
|
+
List of validation issues found
|
|
56
|
+
"""
|
|
57
|
+
issues = []
|
|
58
|
+
|
|
59
|
+
# Only check statements with conditions
|
|
60
|
+
if not statement.condition:
|
|
61
|
+
return issues
|
|
62
|
+
|
|
63
|
+
# Skip Null operator - it's special and doesn't need type validation
|
|
64
|
+
# (Null just checks if a key exists or doesn't exist)
|
|
65
|
+
skip_operators = {"Null"}
|
|
66
|
+
|
|
67
|
+
statement_sid = statement.sid
|
|
68
|
+
line_number = statement.line_number
|
|
69
|
+
actions = statement.get_actions()
|
|
70
|
+
resources = statement.get_resources()
|
|
71
|
+
|
|
72
|
+
# Check each condition operator and its keys/values
|
|
73
|
+
for operator, conditions in statement.condition.items():
|
|
74
|
+
# Normalize the operator and get its expected type
|
|
75
|
+
base_operator, operator_type, set_prefix = normalize_operator(operator)
|
|
76
|
+
|
|
77
|
+
if operator_type is None:
|
|
78
|
+
# Unknown operator - this will be caught by another check
|
|
79
|
+
continue
|
|
80
|
+
|
|
81
|
+
if base_operator in skip_operators:
|
|
82
|
+
continue
|
|
83
|
+
|
|
84
|
+
# Check each condition key
|
|
85
|
+
for condition_key, condition_values in conditions.items():
|
|
86
|
+
# Normalize values to a list
|
|
87
|
+
values = (
|
|
88
|
+
condition_values if isinstance(condition_values, list) else [condition_values]
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
# Get the expected type for this condition key
|
|
92
|
+
key_type = await self._get_condition_key_type(
|
|
93
|
+
fetcher, condition_key, actions, resources
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
if key_type is None:
|
|
97
|
+
# Unknown condition key - will be caught by condition_key_validation check
|
|
98
|
+
continue
|
|
99
|
+
|
|
100
|
+
# Normalize the key type
|
|
101
|
+
key_type = translate_type(key_type)
|
|
102
|
+
operator_type = translate_type(operator_type)
|
|
103
|
+
|
|
104
|
+
# Special case: String operators with ARN types (usable but not recommended)
|
|
105
|
+
if operator_type == "String" and key_type == "ARN":
|
|
106
|
+
issues.append(
|
|
107
|
+
ValidationIssue(
|
|
108
|
+
severity="warning",
|
|
109
|
+
message=(
|
|
110
|
+
f"Type mismatch (usable but not recommended): Operator '{operator}' expects "
|
|
111
|
+
f"{operator_type} values, but condition key '{condition_key}' is type {key_type}. "
|
|
112
|
+
f"Consider using an ARN-specific operator like ArnEquals or ArnLike instead."
|
|
113
|
+
),
|
|
114
|
+
statement_sid=statement_sid,
|
|
115
|
+
statement_index=statement_idx,
|
|
116
|
+
issue_type="type_mismatch_usable",
|
|
117
|
+
line_number=line_number,
|
|
118
|
+
)
|
|
119
|
+
)
|
|
120
|
+
# Check if operator type matches key type
|
|
121
|
+
elif not self._types_compatible(operator_type, key_type):
|
|
122
|
+
issues.append(
|
|
123
|
+
ValidationIssue(
|
|
124
|
+
severity=self.get_severity(config),
|
|
125
|
+
message=(
|
|
126
|
+
f"Type mismatch: Operator '{operator}' expects {operator_type} values, "
|
|
127
|
+
f"but condition key '{condition_key}' is type {key_type}."
|
|
128
|
+
),
|
|
129
|
+
statement_sid=statement_sid,
|
|
130
|
+
statement_index=statement_idx,
|
|
131
|
+
issue_type="type_mismatch",
|
|
132
|
+
condition_key=condition_key,
|
|
133
|
+
line_number=line_number,
|
|
134
|
+
)
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
# Validate that the values match the expected type format
|
|
138
|
+
is_valid, error_msg = validate_value_for_type(key_type, values)
|
|
139
|
+
if not is_valid:
|
|
140
|
+
issues.append(
|
|
141
|
+
ValidationIssue(
|
|
142
|
+
severity=self.get_severity(config),
|
|
143
|
+
message=(
|
|
144
|
+
f"Invalid value format for condition key '{condition_key}': {error_msg}"
|
|
145
|
+
),
|
|
146
|
+
statement_sid=statement_sid,
|
|
147
|
+
statement_index=statement_idx,
|
|
148
|
+
issue_type="invalid_value_format",
|
|
149
|
+
condition_key=condition_key,
|
|
150
|
+
line_number=line_number,
|
|
151
|
+
)
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
return issues
|
|
155
|
+
|
|
156
|
+
async def _get_condition_key_type(
|
|
157
|
+
self,
|
|
158
|
+
fetcher: AWSServiceFetcher,
|
|
159
|
+
condition_key: str,
|
|
160
|
+
actions: list[str],
|
|
161
|
+
resources: list[str],
|
|
162
|
+
) -> str | None:
|
|
163
|
+
"""
|
|
164
|
+
Get the expected type for a condition key by checking global keys and service definitions.
|
|
165
|
+
|
|
166
|
+
Args:
|
|
167
|
+
fetcher: AWS service fetcher
|
|
168
|
+
condition_key: The condition key to look up
|
|
169
|
+
actions: List of actions from the statement
|
|
170
|
+
resources: List of resources from the statement
|
|
171
|
+
|
|
172
|
+
Returns:
|
|
173
|
+
Type string or None if not found
|
|
174
|
+
"""
|
|
175
|
+
from iam_validator.core.config.aws_global_conditions import get_global_conditions
|
|
176
|
+
|
|
177
|
+
# Check if it's a global condition key
|
|
178
|
+
global_conditions = get_global_conditions()
|
|
179
|
+
key_type = global_conditions.get_key_type(condition_key)
|
|
180
|
+
if key_type:
|
|
181
|
+
return key_type
|
|
182
|
+
|
|
183
|
+
# Check service-specific and action-specific condition keys
|
|
184
|
+
for action in actions:
|
|
185
|
+
if action == "*":
|
|
186
|
+
continue
|
|
187
|
+
|
|
188
|
+
try:
|
|
189
|
+
service_prefix, action_name = fetcher.parse_action(action)
|
|
190
|
+
service_detail = await fetcher.fetch_service_by_name(service_prefix)
|
|
191
|
+
|
|
192
|
+
# Check service-level condition keys
|
|
193
|
+
if condition_key in service_detail.condition_keys:
|
|
194
|
+
condition_key_obj = service_detail.condition_keys[condition_key]
|
|
195
|
+
if condition_key_obj.types:
|
|
196
|
+
return condition_key_obj.types[0]
|
|
197
|
+
|
|
198
|
+
# Check action-level condition keys
|
|
199
|
+
if action_name in service_detail.actions:
|
|
200
|
+
action_detail = service_detail.actions[action_name]
|
|
201
|
+
|
|
202
|
+
# For action-specific keys, we need to check the service condition keys list
|
|
203
|
+
if (
|
|
204
|
+
action_detail.action_condition_keys
|
|
205
|
+
and condition_key in action_detail.action_condition_keys
|
|
206
|
+
):
|
|
207
|
+
if condition_key in service_detail.condition_keys:
|
|
208
|
+
condition_key_obj = service_detail.condition_keys[condition_key]
|
|
209
|
+
if condition_key_obj.types:
|
|
210
|
+
return condition_key_obj.types[0]
|
|
211
|
+
|
|
212
|
+
# Check resource-specific condition keys
|
|
213
|
+
if resources and action_detail.resources:
|
|
214
|
+
for res_req in action_detail.resources:
|
|
215
|
+
resource_name = res_req.get("Name", "")
|
|
216
|
+
if not resource_name:
|
|
217
|
+
continue
|
|
218
|
+
|
|
219
|
+
resource_type = service_detail.resources.get(resource_name)
|
|
220
|
+
if resource_type and resource_type.condition_keys:
|
|
221
|
+
if condition_key in resource_type.condition_keys:
|
|
222
|
+
# Resource condition keys reference service condition keys
|
|
223
|
+
if condition_key in service_detail.condition_keys:
|
|
224
|
+
condition_key_obj = service_detail.condition_keys[
|
|
225
|
+
condition_key
|
|
226
|
+
]
|
|
227
|
+
if condition_key_obj.types:
|
|
228
|
+
return condition_key_obj.types[0]
|
|
229
|
+
|
|
230
|
+
except Exception:
|
|
231
|
+
# If we can't look up the action, skip it
|
|
232
|
+
continue
|
|
233
|
+
|
|
234
|
+
return None
|
|
235
|
+
|
|
236
|
+
def _types_compatible(self, operator_type: str, key_type: str) -> bool:
|
|
237
|
+
"""
|
|
238
|
+
Check if an operator type is compatible with a key type.
|
|
239
|
+
|
|
240
|
+
Note: String/ARN compatibility is handled separately with a warning,
|
|
241
|
+
so this method returns False for that combination.
|
|
242
|
+
|
|
243
|
+
Args:
|
|
244
|
+
operator_type: Type expected by the operator
|
|
245
|
+
key_type: Type of the condition key
|
|
246
|
+
|
|
247
|
+
Returns:
|
|
248
|
+
True if compatible
|
|
249
|
+
"""
|
|
250
|
+
# Exact match
|
|
251
|
+
if operator_type == key_type:
|
|
252
|
+
return True
|
|
253
|
+
|
|
254
|
+
# EpochTime can accept both Date and Numeric
|
|
255
|
+
# (this is a special case mentioned in Parliament)
|
|
256
|
+
if key_type == "Date" and operator_type == "Numeric":
|
|
257
|
+
return True
|
|
258
|
+
|
|
259
|
+
return False
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"""Full wildcard check - detects Action: '*' AND Resource: '*' together (critical security risk)."""
|
|
2
|
+
|
|
3
|
+
from iam_validator.core.aws_fetcher import AWSServiceFetcher
|
|
4
|
+
from iam_validator.core.check_registry import CheckConfig, PolicyCheck
|
|
5
|
+
from iam_validator.core.models import Statement, ValidationIssue
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class FullWildcardCheck(PolicyCheck):
|
|
9
|
+
"""Checks for both Action: '*' AND Resource: '*' which grants full administrative access."""
|
|
10
|
+
|
|
11
|
+
@property
|
|
12
|
+
def check_id(self) -> str:
|
|
13
|
+
return "full_wildcard"
|
|
14
|
+
|
|
15
|
+
@property
|
|
16
|
+
def description(self) -> str:
|
|
17
|
+
return "Checks for both action and resource wildcards together (critical risk)"
|
|
18
|
+
|
|
19
|
+
@property
|
|
20
|
+
def default_severity(self) -> str:
|
|
21
|
+
return "critical"
|
|
22
|
+
|
|
23
|
+
async def execute(
|
|
24
|
+
self,
|
|
25
|
+
statement: Statement,
|
|
26
|
+
statement_idx: int,
|
|
27
|
+
fetcher: AWSServiceFetcher,
|
|
28
|
+
config: CheckConfig,
|
|
29
|
+
) -> list[ValidationIssue]:
|
|
30
|
+
"""Execute full wildcard check on a statement."""
|
|
31
|
+
issues = []
|
|
32
|
+
|
|
33
|
+
# Only check Allow statements
|
|
34
|
+
if statement.effect != "Allow":
|
|
35
|
+
return issues
|
|
36
|
+
|
|
37
|
+
actions = statement.get_actions()
|
|
38
|
+
resources = statement.get_resources()
|
|
39
|
+
|
|
40
|
+
# Check for both wildcards together (CRITICAL)
|
|
41
|
+
if "*" in actions and "*" in resources:
|
|
42
|
+
message = config.config.get(
|
|
43
|
+
"message",
|
|
44
|
+
"Statement allows all actions on all resources - CRITICAL SECURITY RISK",
|
|
45
|
+
)
|
|
46
|
+
suggestion_text = config.config.get(
|
|
47
|
+
"suggestion",
|
|
48
|
+
"This grants full administrative access. Replace both wildcards with specific actions and resources to follow least-privilege principle",
|
|
49
|
+
)
|
|
50
|
+
example = config.config.get("example", "")
|
|
51
|
+
|
|
52
|
+
# Combine suggestion + example
|
|
53
|
+
suggestion = (
|
|
54
|
+
f"{suggestion_text}\nExample:\n```json\n{example}\n```"
|
|
55
|
+
if example
|
|
56
|
+
else suggestion_text
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
issues.append(
|
|
60
|
+
ValidationIssue(
|
|
61
|
+
severity=self.get_severity(config),
|
|
62
|
+
statement_sid=statement.sid,
|
|
63
|
+
statement_index=statement_idx,
|
|
64
|
+
issue_type="security_risk",
|
|
65
|
+
message=message,
|
|
66
|
+
suggestion=suggestion,
|
|
67
|
+
line_number=statement.line_number,
|
|
68
|
+
)
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
return issues
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"""MFA Condition Anti-Pattern Check.
|
|
2
|
+
|
|
3
|
+
Detects dangerous MFA-related condition patterns that may not enforce MFA as intended.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from iam_validator.core.check_registry import CheckConfig, PolicyCheck
|
|
7
|
+
from iam_validator.core.models import Statement, ValidationIssue
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class MFAConditionCheck(PolicyCheck):
|
|
11
|
+
"""Check for MFA condition anti-patterns."""
|
|
12
|
+
|
|
13
|
+
@property
|
|
14
|
+
def check_id(self) -> str:
|
|
15
|
+
"""Unique identifier for this check."""
|
|
16
|
+
return "mfa_condition_antipattern"
|
|
17
|
+
|
|
18
|
+
@property
|
|
19
|
+
def description(self) -> str:
|
|
20
|
+
"""Description of what this check does."""
|
|
21
|
+
return "Detects dangerous MFA-related condition patterns"
|
|
22
|
+
|
|
23
|
+
@property
|
|
24
|
+
def default_severity(self) -> str:
|
|
25
|
+
"""Default severity level for issues found by this check."""
|
|
26
|
+
return "warning"
|
|
27
|
+
|
|
28
|
+
async def execute(
|
|
29
|
+
self, statement: Statement, statement_idx: int, fetcher, config: CheckConfig
|
|
30
|
+
) -> list[ValidationIssue]:
|
|
31
|
+
"""
|
|
32
|
+
Execute the MFA condition anti-pattern check.
|
|
33
|
+
|
|
34
|
+
Common anti-patterns:
|
|
35
|
+
1. Using Bool with aws:MultiFactorAuthPresent = false
|
|
36
|
+
Problem: The key may not exist in the request, so condition doesn't enforce anything
|
|
37
|
+
|
|
38
|
+
2. Using Null with aws:MultiFactorAuthPresent = false
|
|
39
|
+
Problem: This only checks if the key exists, not if MFA was used
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
statement: The IAM statement to check
|
|
43
|
+
statement_idx: Index of this statement in the policy
|
|
44
|
+
fetcher: AWS service fetcher (not used in this check)
|
|
45
|
+
config: Check configuration
|
|
46
|
+
|
|
47
|
+
Returns:
|
|
48
|
+
List of validation issues found
|
|
49
|
+
"""
|
|
50
|
+
issues = []
|
|
51
|
+
|
|
52
|
+
# Only check statements with conditions
|
|
53
|
+
if not statement.condition:
|
|
54
|
+
return issues
|
|
55
|
+
|
|
56
|
+
statement_sid = statement.sid
|
|
57
|
+
line_number = statement.line_number
|
|
58
|
+
|
|
59
|
+
# Check for anti-pattern #1: Bool with aws:MultiFactorAuthPresent = false
|
|
60
|
+
bool_conditions = statement.condition.get("Bool", {})
|
|
61
|
+
for key, value in bool_conditions.items():
|
|
62
|
+
if key.lower() == "aws:multifactorauthpresent":
|
|
63
|
+
# Normalize value to list
|
|
64
|
+
values = value if isinstance(value, list) else [value]
|
|
65
|
+
# Convert to lowercase strings for comparison
|
|
66
|
+
values_lower = [str(v).lower() for v in values]
|
|
67
|
+
|
|
68
|
+
if "false" in values_lower or False in values:
|
|
69
|
+
issues.append(
|
|
70
|
+
ValidationIssue(
|
|
71
|
+
severity=self.get_severity(config),
|
|
72
|
+
message=(
|
|
73
|
+
"Dangerous MFA condition pattern detected. "
|
|
74
|
+
'Using {"Bool": {"aws:MultiFactorAuthPresent": "false"}} does not enforce MFA '
|
|
75
|
+
"because aws:MultiFactorAuthPresent may not exist in the request context. "
|
|
76
|
+
'Consider using {"Bool": {"aws:MultiFactorAuthPresent": "true"}} in an Allow statement, '
|
|
77
|
+
"or use BoolIfExists in a Deny statement."
|
|
78
|
+
),
|
|
79
|
+
statement_sid=statement_sid,
|
|
80
|
+
statement_index=statement_idx,
|
|
81
|
+
issue_type="mfa_antipattern_bool_false",
|
|
82
|
+
line_number=line_number,
|
|
83
|
+
)
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
# Check for anti-pattern #2: Null with aws:MultiFactorAuthPresent = false
|
|
87
|
+
null_conditions = statement.condition.get("Null", {})
|
|
88
|
+
for key, value in null_conditions.items():
|
|
89
|
+
if key.lower() == "aws:multifactorauthpresent":
|
|
90
|
+
# Normalize value to list
|
|
91
|
+
values = value if isinstance(value, list) else [value]
|
|
92
|
+
# Convert to lowercase strings for comparison
|
|
93
|
+
values_lower = [str(v).lower() for v in values]
|
|
94
|
+
|
|
95
|
+
if "false" in values_lower or False in values:
|
|
96
|
+
issues.append(
|
|
97
|
+
ValidationIssue(
|
|
98
|
+
severity=self.get_severity(config),
|
|
99
|
+
message=(
|
|
100
|
+
"Dangerous MFA condition pattern detected. "
|
|
101
|
+
'Using {"Null": {"aws:MultiFactorAuthPresent": "false"}} only checks if the key exists, '
|
|
102
|
+
"not whether MFA was actually used. This does not enforce MFA. "
|
|
103
|
+
'Consider using {"Bool": {"aws:MultiFactorAuthPresent": "true"}} in an Allow statement instead.'
|
|
104
|
+
),
|
|
105
|
+
statement_sid=statement_sid,
|
|
106
|
+
statement_index=statement_idx,
|
|
107
|
+
issue_type="mfa_antipattern_null_false",
|
|
108
|
+
line_number=line_number,
|
|
109
|
+
)
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
return issues
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"""Policy size validation check.
|
|
2
|
+
|
|
3
|
+
This check validates that IAM policies don't exceed AWS's maximum size limits.
|
|
4
|
+
AWS enforces different size limits based on policy type:
|
|
5
|
+
- Managed policies: 6,144 characters maximum
|
|
6
|
+
- Inline policies for users: 2,048 characters maximum
|
|
7
|
+
- Inline policies for groups: 5,120 characters maximum
|
|
8
|
+
- Inline policies for roles: 10,240 characters maximum
|
|
9
|
+
|
|
10
|
+
Note: AWS does not count whitespace when calculating policy size.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
import json
|
|
14
|
+
import re
|
|
15
|
+
from typing import TYPE_CHECKING
|
|
16
|
+
|
|
17
|
+
from iam_validator.core.aws_fetcher import AWSServiceFetcher
|
|
18
|
+
from iam_validator.core.check_registry import CheckConfig, PolicyCheck
|
|
19
|
+
from iam_validator.core.constants import AWS_POLICY_SIZE_LIMITS
|
|
20
|
+
from iam_validator.core.models import Statement, ValidationIssue
|
|
21
|
+
|
|
22
|
+
if TYPE_CHECKING:
|
|
23
|
+
from iam_validator.core.models import IAMPolicy
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class PolicySizeCheck(PolicyCheck):
|
|
27
|
+
"""Validates that IAM policies don't exceed AWS size limits."""
|
|
28
|
+
|
|
29
|
+
# AWS IAM policy size limits (loaded from constants module)
|
|
30
|
+
DEFAULT_LIMITS = AWS_POLICY_SIZE_LIMITS
|
|
31
|
+
|
|
32
|
+
@property
|
|
33
|
+
def check_id(self) -> str:
|
|
34
|
+
return "policy_size"
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def description(self) -> str:
|
|
38
|
+
return "Validates that IAM policies don't exceed AWS size limits"
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def default_severity(self) -> str:
|
|
42
|
+
return "error"
|
|
43
|
+
|
|
44
|
+
async def execute(
|
|
45
|
+
self,
|
|
46
|
+
statement: Statement,
|
|
47
|
+
statement_idx: int,
|
|
48
|
+
fetcher: AWSServiceFetcher,
|
|
49
|
+
config: CheckConfig,
|
|
50
|
+
) -> list[ValidationIssue]:
|
|
51
|
+
"""Execute the policy size check at statement level.
|
|
52
|
+
|
|
53
|
+
This is a policy-level check, so statement-level execution returns empty.
|
|
54
|
+
The actual check runs in execute_policy() which has access to the full policy.
|
|
55
|
+
|
|
56
|
+
Args:
|
|
57
|
+
statement: The IAM policy statement (unused)
|
|
58
|
+
statement_idx: Index of the statement in the policy (unused)
|
|
59
|
+
fetcher: AWS service fetcher (unused for this check)
|
|
60
|
+
config: Configuration for this check instance (unused)
|
|
61
|
+
|
|
62
|
+
Returns:
|
|
63
|
+
Empty list (actual check runs in execute_policy())
|
|
64
|
+
"""
|
|
65
|
+
del statement, statement_idx, fetcher, config # Unused
|
|
66
|
+
# This is a policy-level check - execution happens in execute_policy()
|
|
67
|
+
return []
|
|
68
|
+
|
|
69
|
+
async def execute_policy(
|
|
70
|
+
self,
|
|
71
|
+
policy: "IAMPolicy",
|
|
72
|
+
policy_file: str,
|
|
73
|
+
fetcher: AWSServiceFetcher,
|
|
74
|
+
config: CheckConfig,
|
|
75
|
+
**kwargs,
|
|
76
|
+
) -> list[ValidationIssue]:
|
|
77
|
+
"""Execute the policy size check on the entire policy.
|
|
78
|
+
|
|
79
|
+
This method calculates the policy size (excluding whitespace) and validates
|
|
80
|
+
it against AWS limits based on the configured policy type.
|
|
81
|
+
|
|
82
|
+
Args:
|
|
83
|
+
policy: The complete IAM policy to validate
|
|
84
|
+
policy_file: Path to the policy file (for context/reporting)
|
|
85
|
+
fetcher: AWS service fetcher (unused for this check)
|
|
86
|
+
config: Configuration for this check instance
|
|
87
|
+
|
|
88
|
+
Returns:
|
|
89
|
+
List of ValidationIssue objects if policy exceeds size limits
|
|
90
|
+
"""
|
|
91
|
+
del policy_file, fetcher # Unused
|
|
92
|
+
issues = []
|
|
93
|
+
|
|
94
|
+
# Get the policy type from config (default to managed)
|
|
95
|
+
policy_type = config.config.get("policy_type", "managed")
|
|
96
|
+
|
|
97
|
+
# Get custom limits if provided in config, otherwise use defaults
|
|
98
|
+
size_limits = config.config.get("size_limits", self.DEFAULT_LIMITS.copy())
|
|
99
|
+
|
|
100
|
+
# Determine the applicable limit
|
|
101
|
+
limit_key = policy_type
|
|
102
|
+
if limit_key not in size_limits:
|
|
103
|
+
# If custom policy_type not found, default to managed
|
|
104
|
+
limit_key = "managed"
|
|
105
|
+
|
|
106
|
+
max_size = size_limits[limit_key]
|
|
107
|
+
|
|
108
|
+
# Convert policy to JSON and calculate size (excluding whitespace)
|
|
109
|
+
policy_json = policy.model_dump(by_alias=True, exclude_none=True)
|
|
110
|
+
policy_string = json.dumps(policy_json, separators=(",", ":"))
|
|
111
|
+
|
|
112
|
+
# Remove all whitespace as AWS doesn't count it
|
|
113
|
+
policy_size = len(re.sub(r"\s+", "", policy_string))
|
|
114
|
+
|
|
115
|
+
# Check if policy exceeds the limit
|
|
116
|
+
if policy_size > max_size:
|
|
117
|
+
severity = self.get_severity(config)
|
|
118
|
+
|
|
119
|
+
# Calculate percentage over limit
|
|
120
|
+
percentage_over = ((policy_size - max_size) / max_size) * 100
|
|
121
|
+
|
|
122
|
+
# Determine policy type description
|
|
123
|
+
policy_type_desc = {
|
|
124
|
+
"managed": "managed policy",
|
|
125
|
+
"inline_user": "inline policy for users",
|
|
126
|
+
"inline_group": "inline policy for groups",
|
|
127
|
+
"inline_role": "inline policy for roles",
|
|
128
|
+
}.get(policy_type, policy_type)
|
|
129
|
+
|
|
130
|
+
issues.append(
|
|
131
|
+
ValidationIssue(
|
|
132
|
+
severity=severity,
|
|
133
|
+
statement_sid=None, # Policy-level issue
|
|
134
|
+
statement_index=-1, # -1 indicates policy-level issue
|
|
135
|
+
issue_type="policy_size_exceeded",
|
|
136
|
+
message=f"Policy size ({policy_size:,} characters) exceeds AWS limit for {policy_type_desc} ({max_size:,} characters)",
|
|
137
|
+
suggestion=f"The policy is {policy_size - max_size:,} characters over the limit ({percentage_over:.1f}% too large). Consider:\n"
|
|
138
|
+
f" 1. Splitting the policy into multiple smaller policies\n"
|
|
139
|
+
f" 2. Using more concise action/resource patterns with wildcards\n"
|
|
140
|
+
f" 3. Removing unnecessary statements or conditions\n"
|
|
141
|
+
f" 4. For inline policies, consider using managed policies instead\n"
|
|
142
|
+
f"\nNote: AWS does not count whitespace in the size calculation.",
|
|
143
|
+
line_number=None,
|
|
144
|
+
)
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
return issues
|