Skip to content

Commit 07cbdb7

Browse files
Fridayclaude
andcommitted
Fix spurious nosec warning on f-strings with specific test IDs
When a `# nosec B608` comment successfully suppressed an issue in an f-string, bandit emitted a spurious warning "nosec encountered (B608), but no failed test on line N" for the other Constant sub-nodes of the JoinedStr that intentionally returned no result. Track which (test_id, line) pairs have been suppressed and skip the warning when the same test was already suppressed on the same line range. Fixes #1204 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e418b79 commit 07cbdb7

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

bandit/core/tester.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def __init__(self, testset, debug, nosec_lines, metrics):
2222
self.debug = debug
2323
self.nosec_lines = nosec_lines
2424
self.metrics = metrics
25+
self.skipped_pairs = set()
2526

2627
def run_tests(self, raw_context, checktype):
2728
"""Runs all tests for a certain type of check, for example
@@ -69,9 +70,7 @@ def run_tests(self, raw_context, checktype):
6970
result.linerange = temp_context["linerange"]
7071
if result.col_offset == -1:
7172
result.col_offset = temp_context["col_offset"]
72-
result.end_col_offset = temp_context.get(
73-
"end_col_offset", 0
74-
)
73+
result.end_col_offset = temp_context.get("end_col_offset", 0)
7574
result.test = name
7675
if result.test_id == "":
7776
result.test_id = test._test_id
@@ -87,10 +86,11 @@ def run_tests(self, raw_context, checktype):
8786
self.metrics.note_nosec()
8887
continue
8988
if result.test_id in nosec_tests_to_skip:
90-
LOG.debug(
91-
f"skipped, nosec for test {result.test_id}"
92-
)
89+
LOG.debug(f"skipped, nosec for test {result.test_id}")
9390
self.metrics.note_skipped_test()
91+
if result.linerange:
92+
for ln in result.linerange:
93+
self.skipped_pairs.add((result.test_id, ln))
9494
continue
9595

9696
self.results.append(result)
@@ -103,12 +103,12 @@ def run_tests(self, raw_context, checktype):
103103
val = constants.RANKING_VALUES[result.confidence]
104104
scores["CONFIDENCE"][con] += val
105105
else:
106-
nosec_tests_to_skip = self._get_nosecs_from_contexts(
107-
temp_context
108-
)
106+
nosec_tests_to_skip = self._get_nosecs_from_contexts(temp_context)
109107
if (
110108
nosec_tests_to_skip
111109
and test._test_id in nosec_tests_to_skip
110+
and (test._test_id, temp_context["lineno"])
111+
not in self.skipped_pairs
112112
):
113113
LOG.warning(
114114
f"nosec encountered ({test._test_id}), but no "
@@ -130,9 +130,7 @@ def _get_nosecs_from_contexts(self, context, test_result=None):
130130
"""
131131
nosec_tests_to_skip = set()
132132
base_tests = (
133-
self.nosec_lines.get(test_result.lineno, None)
134-
if test_result
135-
else None
133+
self.nosec_lines.get(test_result.lineno, None) if test_result else None
136134
)
137135
context_tests = utils.get_nosec(self.nosec_lines, context)
138136

0 commit comments

Comments
 (0)