Skip to content

Commit 649b9bd

Browse files
Add check for hardcoded passwords in dicts. (#1338)
* Add check for hardcoded passwords in dicts. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 3c56109 commit 649b9bd

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

bandit/plugins/general_hardcoded_password.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ def hardcoded_password_string(context):
8989
):
9090
return _report(node.value)
9191

92+
elif (
93+
isinstance(node._bandit_parent, ast.Dict)
94+
and node in node._bandit_parent.keys
95+
and RE_CANDIDATES.search(node.value)
96+
):
97+
# looks for "{'candidate': 'some_string'}"
98+
dict_node = node._bandit_parent
99+
pos = dict_node.keys.index(node)
100+
value_node = dict_node.values[pos]
101+
if isinstance(value_node, ast.Constant):
102+
return _report(value_node.value)
103+
92104
elif isinstance(
93105
node._bandit_parent, ast.Subscript
94106
) and RE_CANDIDATES.search(node.value):

examples/hardcoded-passwords.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,19 @@ def __init__(self, auth_scheme, auth_token=None, auth_username=None, auth_passwo
8787
default='',
8888
secret=True,
8989
)
90+
91+
# Possible hardcoded password: 'pass'
92+
# Severity: Low Confidence: Medium
93+
# https://github.com/PyCQA/bandit/issues/313
94+
log({"server": server, "password": 'pass', "user": user})
95+
96+
# ... but not:
97+
log({"server": server, "password": password, "user": user})
98+
99+
# Possible hardcoded password: '12345'
100+
# Severity: Low Confidence: Medium
101+
# https://github.com/PyCQA/bandit/issues/1267
102+
info = {"password": "12345"}
103+
104+
# ... but not:
105+
info = {"password": password}

tests/functional/test_functional.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ def test_exec(self):
168168
def test_hardcoded_passwords(self):
169169
"""Test for hard-coded passwords."""
170170
expect = {
171-
"SEVERITY": {"UNDEFINED": 0, "LOW": 14, "MEDIUM": 0, "HIGH": 0},
172-
"CONFIDENCE": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 14, "HIGH": 0},
171+
"SEVERITY": {"UNDEFINED": 0, "LOW": 16, "MEDIUM": 0, "HIGH": 0},
172+
"CONFIDENCE": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 16, "HIGH": 0},
173173
}
174174
self.check_example("hardcoded-passwords.py", expect)
175175

0 commit comments

Comments
 (0)