Confused by nosec: encountered B104, but no failed test
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 67/100
Research direction
Start by reproducing the warning with the supplied function-call example, then compare how Bandit handles nosec markers in that case and in the two examples that work. Trace the nosec handling and its tests; done means warnings refer only to lines containing markers and suppressions apply to the intended line without false negatives.
Written by the indexing model from the issue text.
Description
Describe the bug
% bandit -q -c pyproject.toml -r .
[tester] WARNING nosec encountered (B104), but no failed test on file ./tests/tpsrv/test_cli.py:405
[tester] WARNING nosec encountered (B104), but no failed test on file ./tests/tpsrv/test_cli.py:407
% sed -ne '404,413p' ./tests/tpsrv/test_cli.py
@pytest.mark.parametrize(
"args, host, port",
[
(["--host", "0.0.0.0"], "0.0.0.0", 8000), # nosec: B104
(["-h", "::1"], "::1", 8000),
(["--port", "9999"], "127.0.0.1", 9999),
(["-p", "1024"], "127.0.0.1", 1024),
(["-p", "65535"], "127.0.0.1", 65535),
(["-h", "localhost", "-p", "8080"], "localhost", 8080),
],
There is no nosec on line 405, but reported as found.
And if I remove the nosec on line 407, then B104 is reported on that line:
% sed -e 's,# nosec:.*,,' ./tests/tpsrv/test_cli.py | bandit -c pyproject.toml - 2>&1 | grep -A8 B104
>> Issue: [B104:hardcoded_bind_all_interfaces] Possible binding to all interfaces.
Severity: Medium Confidence: Medium
CWE: CWE-605 (https://cwe.mitre.org/data/definitions/605.html)
More Info: https://bandit.readthedocs.io/en/1.9.4/plugins/b104_hardcoded_bind_all_interfaces.html
Location: <stdin>:407:20
406 [
407 (["--host", "0.0.0.0"], "0.0.0.0", 8000),
408 (["-h", "::1"], "::1", 8000),
--
>> Issue: [B104:hardcoded_bind_all_interfaces] Possible binding to all interfaces.
Severity: Medium Confidence: Medium
CWE: CWE-605 (https://cwe.mitre.org/data/definitions/605.html)
More Info: https://bandit.readthedocs.io/en/1.9.4/plugins/b104_hardcoded_bind_all_interfaces.html
Location: <stdin>:407:32
406 [
407 (["--host", "0.0.0.0"], "0.0.0.0", 8000),
408 (["-h", "::1"], "::1", 8000),
Reproduction steps
Interestingly,
% echo -e "IP = '0.0.0.0' # nosec B104\nprint(IP)" | bandit -
does not have this problem, and neither does
% echo -e "IP = [\n '0.0.0.0' # nosec: B104,\n '127.0.0.1'\n]\nprint(IP)" | bandit -
It seems to do with being inside a function call:
% echo -e "print(\n [\n (['--host', '0.0.0.0'], '0.0.0.0'), # nosec: B104\n ]\n)\n" | bandit -
[…]
[tester] WARNING nosec encountered (B104), but no failed test on file <stdin>:3
[…]
Expected behavior
- No false negatives about use of
# nosecmarkers - No warnings/errors about
nosecon lines that do not havenosec - No more errors about
nosecthan actualnosecuse - The ability to silence the warning just for the very line that it's expected to warn about.
Bandit version
1.9.1 (Default)
Python version
3.14 (Default)
Additional context
Bandit 1.9.4, which I cannot select in the above.
- Dominant language
- Python
- Stars
- 8.4k
- Forks
- 893
- Avg merge
- 6h 52m
- Merged PRs (30d)
- 3
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from PyCQA/bandit
-
Difficulty 1/5 1-3 hours Newbie friendliness 85/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Turn off progress bar in CIPossibly taken @silvatechf claimed this 28 days ago. Openenhancement
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
-
`bandit -ii -ll -ii -ll` raises an `IndexError`Possibly taken @botbikamordehai2-sketch claimed this 181 days ago. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Similar issues
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 85/100
NatLabRockies/openstudio-mcp#286 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
intersystems/ipm#1338 ·
Maintainers usually reply within 1 day
-
separate_packages splits imports from the same explicitly configured packagePossibly taken @rahul05ranjan claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 2 days
-
The v0-through-v1 path derives ShutdownDuration its own way, silently overriding a user's definitionOpenv1
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day