Skip to content

Commit 6969489

Browse files
authored
fix(plugins/B507): also detect class instances (#1064)
`paramiko` supports passing both a class and a class instance for the policy in `set_missing_host_key_policy` (https://github.com/paramiko/paramiko/blob/8e389c77660c5cdae3069b478665427d23012853/paramiko/client.py#L171-L191). This updates B507 to account for both styles.
1 parent 02faada commit 6969489

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

bandit/plugins/ssh_no_host_key_verification.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
CWE information added
3636
3737
"""
38+
import ast
39+
3840
import bandit
3941
from bandit.core import issue
4042
from bandit.core import test_properties as test
@@ -46,11 +48,17 @@ def ssh_no_host_key_verification(context):
4648
if (
4749
context.is_module_imported_like("paramiko")
4850
and context.call_function_name == "set_missing_host_key_policy"
51+
and context.node.args
4952
):
50-
if context.call_args and context.call_args[0] in [
51-
"AutoAddPolicy",
52-
"WarningPolicy",
53-
]:
53+
policy_argument = context.node.args[0]
54+
55+
policy_argument_value = None
56+
if isinstance(policy_argument, ast.Attribute):
57+
policy_argument_value = policy_argument.attr
58+
elif isinstance(policy_argument, ast.Call):
59+
policy_argument_value = policy_argument.func.attr
60+
61+
if policy_argument_value in ["AutoAddPolicy", "WarningPolicy"]:
5462
return bandit.Issue(
5563
severity=bandit.HIGH,
5664
confidence=bandit.MEDIUM,

examples/no_host_key_verification.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
ssh_client = client.SSHClient()
44
ssh_client.set_missing_host_key_policy(client.AutoAddPolicy)
55
ssh_client.set_missing_host_key_policy(client.WarningPolicy)
6+
ssh_client.set_missing_host_key_policy(client.AutoAddPolicy())
7+
ssh_client.set_missing_host_key_policy(client.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": 2},
547-
"CONFIDENCE": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 2, "HIGH": 0},
546+
"SEVERITY": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 0, "HIGH": 4},
547+
"CONFIDENCE": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 4, "HIGH": 0},
548548
}
549549
self.check_example("no_host_key_verification.py", expect)
550550

0 commit comments

Comments
 (0)