Confused by nosec: encountered B104, but no failed test

Open
#1,479 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
67/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
devtools, security

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

bug
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
  1. No false negatives about use of # nosec markers
  2. No warnings/errors about nosec on lines that do not have nosec
  3. No more errors about nosec than actual nosec use
  4. 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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from PyCQA/bandit

All issues in PyCQA/bandit

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.