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

ci: run bandit on test code #1579

Merged
merged 6 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,12 @@ specify a whole folder using ```./```

### Running bandit by itself

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.
We have a configuration file for bandit called `bandit.conf` that you should use. This disables a few of the checkers.

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

```bash
bandit -c bandit.conf -r cve_bin_tool/
bandit -c bandit.conf -r cve_bin_tool/ test/
```

You can also run it on individual files:
Expand All @@ -224,7 +224,7 @@ You can also run it on individual files:
bandit -c bandit.conf filename.py
```

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.
If you run it without the config file, it will run a few extra checkers, so you'll get additional warnings.

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().

Expand Down
8 changes: 3 additions & 5 deletions bandit.conf
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,9 @@ skips: ['B603', 'B607', 'B404', "B608"]
# Explantion: cve-bin-tool is at heart a shell script that calls other processes.
# Switching to pure python has significant performance impacts.

exclude_dirs:
- "test/"
- "/test/"
- "./test/"
- "./build/lib/test/"
# skips assert rule on tests
assert_used:
skips: ['*/test_*.py']

### (optional) plugin settings - some test plugins require configuration data
### that may be given here, per-plugin. All bandit test plugins have a built in
Expand Down
3 changes: 2 additions & 1 deletion test/test_input_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import os
import re
from ast import literal_eval

import pytest

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

assert missing_fields - eval(raised_fields) == set()
assert missing_fields - literal_eval(raised_fields) == set()

@pytest.mark.parametrize(
"filepath, parsed_data",
Expand Down
3 changes: 2 additions & 1 deletion test/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import os
import re
from ast import literal_eval

import pytest

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

assert missing_fields - eval(raised_fields) == set()
assert missing_fields - literal_eval(raised_fields) == set()

@pytest.mark.parametrize(
"filepaths, merged_data",
Expand Down
5 changes: 2 additions & 3 deletions test/test_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,8 @@ def test_cannot_open_file(self, caplog):
assert str.find("Invalid file", caplog.text)

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

cleaned_path = self.scanner.clean_file_path(filepath)
Expand Down