Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Erroneous "no test failure on line" warning from plugin alert nosec? #942

Open
fiendish opened this issue Aug 17, 2022 · 3 comments
Open
Labels
bug Something isn't working

Comments

@fiendish
Copy link

fiendish commented Aug 17, 2022

Describe the bug

It looks like bandit warns about "no test failure on line" for a nosec where there definitely would be a test failure without the nosec.

Reproduction steps

  1. I start with a line that does foo = hashlib.md5(buffer.read()).digest().
  2. bandit -ll rightly complains about the use of MD5 with a B324 error from the hashlib plugin.
  3. I add #nosec B324 like this: foo = hashlib.md5(buffer.read()).digest() # nosec B324
  4. Bandit now says [tester] WARNING nosec encountered (B324), but no failed test on line 85

Seems a bit weird.

Expected behavior

Don't warn at me for this.

Bandit version

1.7.4 (Default)

Python version

3.9

Additional context

No response

@fiendish fiendish added the bug Something isn't working label Aug 17, 2022
@hrantzsch
Copy link

I'm facing the same issue using Bandit 1.7.4 with Python 3.10.4. Here's another minimal example for repro:

import os
cmd = "df -h"
os.popen(cmd).read()  # nosec B605

bandit -v test.py says WARNING nosec encountered (B605), but no failed test on line 3 while it will report B605 if I remove the # nosec.

full log

[main]	INFO	profile include tests: None
[main]	INFO	profile exclude tests: None
[main]	INFO	cli include tests: None
[main]	INFO	cli exclude tests: None
[main]	INFO	running on Python 3.10.4
[node_visitor]	WARNING	Unable to find qualified name for module: test.py
[tester]	WARNING	nosec encountered (B605), but no failed test on line 3
Run started:2022-09-06 09:31:19.271042
Files in scope (1):
	test.py (score: {SEVERITY: 0, CONFIDENCE: 0})
Files excluded (0):

Test results:
	No issues identified.

Code scanned:
	Total lines of code: 3
	Total lines skipped (#nosec): 0

Run metrics:
	Total issues (by severity):
		Undefined: 0
		Low: 0
		Medium: 0
		High: 0
	Total issues (by confidence):
		Undefined: 0
		Low: 0
		Medium: 0
		High: 0
Files skipped (0):

However, the problem does not occur if I remove .read(). (It continues to claim Total lines skipped (#nosec): 0 though, as long as the B605 is specified.)

@creideiki
Copy link

creideiki commented Jul 4, 2023

I'm still seeing the same thing with Bandit 1.7.5 on Python 3.11.4. Yet another minimal example:

import os
import subprocess  # nosec B404

subprocess.run([os.getenv("SHELL")], check=False)  # nosec B603

Bandit says:

[tester]        WARNING nosec encountered (B603), but no failed test on line 4

but if I remove the # nosec B603, I get a test failure:

>> Issue: [B603:subprocess_without_shell_equals_true] subprocess call - check for execution of untrusted input.
   Severity: Low   Confidence: High
   CWE: CWE-78 (https://cwe.mitre.org/data/definitions/78.html)
   More Info: https://bandit.readthedocs.io/en/1.7.5/plugins/b603_subprocess_without_shell_equals_true.html
   Location: bandit.py:4:0
3
4       subprocess.run([os.getenv("SHELL")], check=False)

@fiendish
Copy link
Author

fiendish commented May 20, 2024

However, the problem does not occur if I remove .read(). (It continues to claim Total lines skipped (#nosec): 0 though, as long as the B605 is specified.)

The problem appears to be related to how bandit observes "lines" where multiple function calls occur on the same line?

I get the warning if I do

return requests.get(url).json()  # nosec B113

or

requests.get(CARRIER_URL) and print("hello") # nosec B113

But I do not get the warning if I do

foo = requests.get(url)  # nosec B113
return foo.json()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants