Skip to content

Commit def123a

Browse files
B107: Skip None values in hardcoded password detection (#1232)
* B107: Skip None values in hardcoded password detection The B107 check was incorrectly flagging None default values as hardcoded passwords in function definitions. This is a false positive since None is a legitimate and commonly used within __init__ initialization This change modifies the hardcoded_password_default function to: - Skip None values in parameter defaults - Handle both ast.Constant (Python 3.8+) and ast.NameConstant (Python 3.7-) representations of None - Update documentation to clarify None handling behavior Example of code that no longer triggers B107: def __init__(self, auth_scheme, auth_password=None): pass Fixes ##1227 * [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 00b1e95 commit def123a

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

bandit/plugins/general_hardcoded_password.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ def hardcoded_password_default(context):
201201
- "token"
202202
- "secrete"
203203
204-
Note: this can be noisy and may generate false positives.
204+
Note: this can be noisy and may generate false positives. We do not
205+
report on None values which can be legitimately used as a default value,
206+
when initializing a function or class.
205207
206208
**Config Options:**
207209
@@ -242,5 +244,11 @@ def hardcoded_password_default(context):
242244
# go through all (param, value)s and look for candidates
243245
for key, val in zip(context.node.args.args, defs):
244246
if isinstance(key, (ast.Name, ast.arg)):
247+
# Skip if the default value is None
248+
if val is None or (
249+
isinstance(val, (ast.Constant, ast.NameConstant))
250+
and val.value is None
251+
):
252+
continue
245253
if isinstance(val, ast.Str) and RE_CANDIDATES.search(key.arg):
246254
return _report(val.s)

examples/hardcoded-passwords.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def NoMatch3(a, b):
5151

5252
# Possible hardcoded password: 'blerg'
5353
# Severity: Low Confidence: Medium
54-
d["password"] = "blerg"
54+
password["password"] = "blerg"
5555

5656
# Possible hardcoded password: 'secret'
5757
# Severity: Low Confidence: Medium
@@ -68,3 +68,13 @@ def NoMatch3(a, b):
6868
# Possible hardcoded password: '1234'
6969
# Severity: Low Confidence: Medium
7070
passphrase='1234'
71+
72+
# Possible hardcoded password: None
73+
# Severity: High Confidence: High
74+
def __init__(self, auth_scheme, auth_token=None, auth_username=None, auth_password=None, auth_link=None, **kwargs):
75+
self.auth_scheme = auth_scheme
76+
self.auth_token = auth_token
77+
self.auth_username = auth_username
78+
self.auth_password = auth_password
79+
self.auth_link = auth_link
80+
self.kwargs = kwargs

0 commit comments

Comments
 (0)