Skip to content

Commit

Permalink
Fix traceback in hashlib_insecure_functions (#834)
Browse files Browse the repository at this point in the history
This check should not raise an exception if there are no keywords
defined for the call. Makes use of dict get() for safety.

Closes #832

Signed-off-by: Eric Brown <browne@vmware.com>
  • Loading branch information
ericwb committed Feb 28, 2022
1 parent 20a0510 commit fbaf2ce
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions bandit/plugins/hashlib_insecure_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ def _hashlib_func(context):

if "hashlib" in qualname_list:
func = qualname_list[-1]
args = context.call_args
keywords = context.call_keywords
name = args[0] if args else keywords["name"]

if func in ("md4", "md5", "sha", "sha1"):
if keywords.get("usedforsecurity", "True") == "True":
Expand All @@ -67,6 +65,8 @@ def _hashlib_func(context):
lineno=context.node.lineno,
)
elif func == "new":
args = context.call_args
name = args[0] if args else keywords.get("name", None)
if isinstance(name, str) and name.lower() in (
"md4",
"md5",
Expand All @@ -92,7 +92,7 @@ def _hashlib_new(context):
if "hashlib" in qualname_list and func == "new":
args = context.call_args
keywords = context.call_keywords
name = args[0] if args else keywords["name"]
name = args[0] if args else keywords.get("name", None)
if isinstance(name, str) and name.lower() in (
"md4",
"md5",
Expand Down

0 comments on commit fbaf2ce

Please sign in to comment.