Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions bandit/plugins/general_hardcoded_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ def hardcoded_password_string(context):
):
return _report(node.value)

elif (
isinstance(node._bandit_parent, ast.Dict)
and node in node._bandit_parent.keys
and RE_CANDIDATES.search(node.value)
):
# looks for "{'candidate': 'some_string'}"
dict_node = node._bandit_parent
pos = dict_node.keys.index(node)
value_node = dict_node.values[pos]
if isinstance(value_node, ast.Constant):
return _report(value_node.value)

elif isinstance(
node._bandit_parent, ast.Subscript
) and RE_CANDIDATES.search(node.value):
Expand Down
16 changes: 16 additions & 0 deletions examples/hardcoded-passwords.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,19 @@ def __init__(self, auth_scheme, auth_token=None, auth_username=None, auth_passwo
default='',
secret=True,
)

# Possible hardcoded password: 'pass'
# Severity: Low Confidence: Medium
# https://github.com/PyCQA/bandit/issues/313
log({"server": server, "password": 'pass', "user": user})

# ... but not:
log({"server": server, "password": password, "user": user})

# Possible hardcoded password: '12345'
# Severity: Low Confidence: Medium
# https://github.com/PyCQA/bandit/issues/1267
info = {"password": "12345"}

# ... but not:
info = {"password": password}
4 changes: 2 additions & 2 deletions tests/functional/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ def test_exec(self):
def test_hardcoded_passwords(self):
"""Test for hard-coded passwords."""
expect = {
"SEVERITY": {"UNDEFINED": 0, "LOW": 14, "MEDIUM": 0, "HIGH": 0},
"CONFIDENCE": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 14, "HIGH": 0},
"SEVERITY": {"UNDEFINED": 0, "LOW": 16, "MEDIUM": 0, "HIGH": 0},
"CONFIDENCE": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 16, "HIGH": 0},
}
self.check_example("hardcoded-passwords.py", expect)

Expand Down