Confused by nosec: encountered B104, but no failed test

Aperta
#1,479 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
67/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
devtools, security

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
8.4k
Fork
893
Merge medio
6h 52m
PR unite (30g)
3

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di PyCQA/bandit

Tutte le issue di PyCQA/bandit

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.