Skip to content

Commit 5ec806d

Browse files
authored
Handle variant in how policy is passed in paramiko (#1078)
Paramiko permits various ways of importing the missing host key policy. It allows paramiko.client.AutoAddPolicy or paramiko.AutoAddPolicy. The later isn't being handled in Bandit. This change adds news tests and modifies the plugin to inspect the AST to determine whether the argument is an Attribute, Name, or Call. Fixes #1077 Signed-off-by: Eric Brown <[email protected]>
1 parent 53fe99b commit 5ec806d

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

bandit/plugins/ssh_no_host_key_verification.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,13 @@ def ssh_no_host_key_verification(context):
5555
policy_argument_value = None
5656
if isinstance(policy_argument, ast.Attribute):
5757
policy_argument_value = policy_argument.attr
58+
elif isinstance(policy_argument, ast.Name):
59+
policy_argument_value = policy_argument.id
5860
elif isinstance(policy_argument, ast.Call):
59-
policy_argument_value = policy_argument.func.attr
61+
if isinstance(policy_argument.func, ast.Attribute):
62+
policy_argument_value = policy_argument.func.attr
63+
elif isinstance(policy_argument.func, ast.Name):
64+
policy_argument_value = policy_argument.func.id
6065

6166
if policy_argument_value in ["AutoAddPolicy", "WarningPolicy"]:
6267
return bandit.Issue(
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
from paramiko import client
2+
from paramiko import AutoAddPolicy
3+
from paramiko import WarningPolicy
24

35
ssh_client = client.SSHClient()
46
ssh_client.set_missing_host_key_policy(client.AutoAddPolicy)
57
ssh_client.set_missing_host_key_policy(client.WarningPolicy)
68
ssh_client.set_missing_host_key_policy(client.AutoAddPolicy())
79
ssh_client.set_missing_host_key_policy(client.WarningPolicy())
10+
11+
ssh_client.set_missing_host_key_policy(AutoAddPolicy)
12+
ssh_client.set_missing_host_key_policy(WarningPolicy)
13+
ssh_client.set_missing_host_key_policy(AutoAddPolicy())
14+
ssh_client.set_missing_host_key_policy(WarningPolicy())

tests/functional/test_functional.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,8 +543,8 @@ def test_yaml(self):
543543
def test_host_key_verification(self):
544544
"""Test for ignoring host key verification."""
545545
expect = {
546-
"SEVERITY": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 0, "HIGH": 4},
547-
"CONFIDENCE": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 4, "HIGH": 0},
546+
"SEVERITY": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 0, "HIGH": 8},
547+
"CONFIDENCE": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 8, "HIGH": 0},
548548
}
549549
self.check_example("no_host_key_verification.py", expect)
550550

0 commit comments

Comments
 (0)