Skip to content

Commit 1c9a61a

Browse files
Filter deny from unrestricted actions (#170)
* filter deny statements from "all_allowed_unrestricted_actions" * add pylint disable for python lambda code
1 parent cb822b3 commit 1c9a61a

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

cloudsplaining/scan/policy_document.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,30 @@ def all_allowed_actions(self):
5757
if statement.effect_allow: # if Effect is "Deny" - it is not an allowed action
5858
if statement.expanded_actions:
5959
allowed_actions.extend(statement.expanded_actions)
60+
allowed_actions = self.filter_deny_statements(allowed_actions)
61+
allowed_actions = list(dict.fromkeys(allowed_actions))
62+
return allowed_actions
63+
64+
def filter_deny_statements(self, allowed_actions):
65+
"""
66+
filter all denied statements from actions
67+
"""
6068
for statement in self.statements:
6169
if statement.effect_deny:
6270
if statement.expanded_actions:
71+
# pylint: disable=W0640
6372
allowed_actions = filter(lambda x: x not in statement.expanded_actions, allowed_actions)
64-
allowed_actions = list(dict.fromkeys(allowed_actions))
6573
return allowed_actions
6674

6775
@property
6876
def all_allowed_unrestricted_actions(self):
6977
"""Output all IAM actions that do not practice resource constraints"""
7078
allowed_actions = []
7179
for statement in self.statements:
72-
if not statement.has_resource_constraints and not statement.has_condition:
80+
if not statement.has_resource_constraints and not statement.has_condition and statement.effect_allow:
7381
if statement.expanded_actions:
7482
allowed_actions.extend(statement.expanded_actions)
83+
allowed_actions = self.filter_deny_statements(allowed_actions)
7584
allowed_actions = list(dict.fromkeys(allowed_actions))
7685
return allowed_actions
7786

test/scanning/test_policy_document.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,22 @@ def test_policy_document_all_allowed_actions(self):
108108
]
109109
self.assertListEqual(result, expected_result)
110110

111+
def test_all_allowed_unrestriced_deny(self):
112+
"""scan.policy_document.all_allowed_unrestricted_actions"""
113+
test_policy = {
114+
"Version": "2012-10-17",
115+
"Statement": [
116+
{
117+
"Effect": "Deny",
118+
"Action": "*",
119+
"Resource": "*",
120+
}
121+
]
122+
}
123+
policy_document = PolicyDocument(test_policy)
124+
result = policy_document.all_allowed_unrestricted_actions
125+
self.assertEquals([],result)
126+
111127
def test_policy_document_all_allowed_actions_deny(self):
112128
"""scan.policy_document.all_allowed_actions"""
113129
test_policy = {

0 commit comments

Comments
 (0)