Skip to content

Commit

Permalink
Handle variant in how policy is passed in paramiko
Browse files Browse the repository at this point in the history
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 <eric_wade_brown@yahoo.com>
  • Loading branch information
ericwb committed Dec 12, 2023
1 parent 923f269 commit f22fe3c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
7 changes: 6 additions & 1 deletion bandit/plugins/ssh_no_host_key_verification.py
Expand Up @@ -55,8 +55,13 @@ def ssh_no_host_key_verification(context):
policy_argument_value = None
if isinstance(policy_argument, ast.Attribute):
policy_argument_value = policy_argument.attr
elif isinstance(policy_argument, ast.Name):
policy_argument_value = policy_argument.id
elif isinstance(policy_argument, ast.Call):
policy_argument_value = policy_argument.func.attr
if isinstance(policy_argument.func, ast.Attribute):
policy_argument_value = policy_argument.func.attr
elif isinstance(policy_argument.func, ast.Name):
policy_argument_value = policy_argument.func.id

if policy_argument_value in ["AutoAddPolicy", "WarningPolicy"]:
return bandit.Issue(
Expand Down
7 changes: 7 additions & 0 deletions examples/no_host_key_verification.py
@@ -1,7 +1,14 @@
from paramiko import client
from paramiko import AutoAddPolicy
from paramiko import WarningPolicy

ssh_client = client.SSHClient()
ssh_client.set_missing_host_key_policy(client.AutoAddPolicy)
ssh_client.set_missing_host_key_policy(client.WarningPolicy)
ssh_client.set_missing_host_key_policy(client.AutoAddPolicy())
ssh_client.set_missing_host_key_policy(client.WarningPolicy())

ssh_client.set_missing_host_key_policy(AutoAddPolicy)
ssh_client.set_missing_host_key_policy(WarningPolicy)
ssh_client.set_missing_host_key_policy(AutoAddPolicy())
ssh_client.set_missing_host_key_policy(WarningPolicy())

0 comments on commit f22fe3c

Please sign in to comment.