Skip to content

Commit 0976211

Browse files
authored
ci: run bandit on test code (fixes #1528) (#1579)
* ci: skip assert on tests * fix: issue B108 in test_scanner.py * fix: issue B310 in utils.py and test_json.py * fix: issue B307 in test_input_engine.py and test_merge.py * doc: update bandit in contributing.md
1 parent b436343 commit 0976211

File tree

5 files changed

+12
-13
lines changed

5 files changed

+12
-13
lines changed

CONTRIBUTING.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,12 @@ specify a whole folder using ```./```
211211

212212
### Running bandit by itself
213213

214-
We have a configuration file for bandit called `bandit.conf` that you should use. This disables a few of the checkers and disables scanning of the test directory.
214+
We have a configuration file for bandit called `bandit.conf` that you should use. This disables a few of the checkers.
215215

216216
To run it on all the code we scan, use the following:
217217

218218
```bash
219-
bandit -c bandit.conf -r cve_bin_tool/
219+
bandit -c bandit.conf -r cve_bin_tool/ test/
220220
```
221221

222222
You can also run it on individual files:
@@ -225,7 +225,7 @@ You can also run it on individual files:
225225
bandit -c bandit.conf filename.py
226226
```
227227

228-
If you run it without the config file, it will run a few extra checkers and will run on test code, so you'll get additional warnings.
228+
If you run it without the config file, it will run a few extra checkers, so you'll get additional warnings.
229229

230230
Bandit helps you target manual code review, but bandit issues aren't always things that need to be fixed, just reviewed. If you have a bandit finding that doesn't actually need a fix, you can mark it as reviewed using a `# nosec` comment. If possible, include details as to why the bandit results are ok for future reviewers. For example, we have comments like `#nosec uses static https url above` in cases where bandit prompted us to review the variable being passed to urlopen().
231231

bandit.conf

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,9 @@ skips: ['B603', 'B607', 'B404', "B608"]
9292
# Explantion: cve-bin-tool is at heart a shell script that calls other processes.
9393
# Switching to pure python has significant performance impacts.
9494

95-
exclude_dirs:
96-
- "test/"
97-
- "/test/"
98-
- "./test/"
99-
- "./build/lib/test/"
95+
# skips assert rule on tests
96+
assert_used:
97+
skips: ['*/test_*.py']
10098

10199
### (optional) plugin settings - some test plugins require configuration data
102100
### that may be given here, per-plugin. All bandit test plugins have a built in

test/test_input_engine.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import os
55
import re
6+
from ast import literal_eval
67

78
import pytest
89

@@ -109,7 +110,7 @@ def test_missing_fields(self, filepath, missing_fields):
109110
match = self.MISSING_FIELD_REGEX.search(exc.value.args[0])
110111
raised_fields = match.group(1)
111112

112-
assert missing_fields - eval(raised_fields) == set()
113+
assert missing_fields - literal_eval(raised_fields) == set()
113114

114115
@pytest.mark.parametrize(
115116
"filepath, parsed_data",

test/test_merge.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import os
55
import re
6+
from ast import literal_eval
67

78
import pytest
89

@@ -80,7 +81,7 @@ def test_missing_fields(self, filepaths, missing_fields):
8081
match = self.MISSING_FIELD_REGEX.search(exc.value.args[0])
8182
raised_fields = match.group(1)
8283

83-
assert missing_fields - eval(raised_fields) == set()
84+
assert missing_fields - literal_eval(raised_fields) == set()
8485

8586
@pytest.mark.parametrize(
8687
"filepaths, merged_data",

test/test_scanner.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,8 @@ def test_cannot_open_file(self, caplog):
284284
assert str.find("Invalid file", caplog.text)
285285

286286
def test_clean_file_path(self):
287-
filepath = (
288-
"/tmp/cve-bin-tool/dhtei34fd/file_name.extracted/usr/bin/vulnerable_file"
289-
)
287+
filepath = "/tmp/cve-bin-tool/dhtei34fd/file_name.extracted/usr/bin/vulnerable_file" # nosec
288+
# temp path is hardcoded for testing, not for usage
290289
expected_path = "/usr/bin/vulnerable_file"
291290

292291
cleaned_path = self.scanner.clean_file_path(filepath)

0 commit comments

Comments
 (0)