Skip to content

Commit d6ade7c

Browse files
terrikoBreadGenie
andauthored
CI: Add bandit to pre-commit (fixes #1110) (#1523)
* fixes #1110 Co-authored-by: Bread Genie <[email protected]>
1 parent 19534e7 commit d6ade7c

File tree

7 files changed

+45
-5
lines changed

7 files changed

+45
-5
lines changed

.github/actions/spelling/allow.txt

+2
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ nitishsaini
234234
nlk
235235
noopener
236236
noreferrer
237+
nosec
237238
nowdailynever
238239
nplurals
239240
ntp
@@ -341,6 +342,7 @@ unicode
341342
uniq
342343
unittest
343344
url
345+
urlopen
344346
usecase
345347
username
346348
usr

.github/workflows/pythonapp.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
strategy:
1818
fail-fast: false
1919
matrix:
20-
tool: ['isort', 'black', 'pyupgrade', 'flake8', 'format_checkers']
20+
tool: ['isort', 'black', 'pyupgrade', 'flake8', 'format_checkers', 'bandit']
2121
steps:
2222
- uses: actions/checkout@v2
2323
- uses: actions/setup-python@v2

.pre-commit-config.yaml

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@ repos:
1414
hooks:
1515
- id: pyupgrade
1616
args: ["--py37-plus"]
17-
17+
1818
- repo: https://github.com/pycqa/flake8
1919
rev: 4.0.1
2020
hooks:
2121
- id: flake8
2222

23+
- repo: https://github.com/PyCQA/bandit
24+
rev: 1.7.1
25+
hooks:
26+
- id: bandit
27+
args: ["-c", "bandit.conf"]
28+
2329
- repo: local
2430
hooks:
2531
- id: format_checkers

CONTRIBUTING.md

+22
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ If you've already contributed to other open source projects, contributing to the
1717
- [Using pre-commit to run linters automatically](#using-pre-commit-to-run-linters-automatically)
1818
- [Running isort by itself](#running-isort-by-itself)
1919
- [Running black by itself](#running-black-by-itself)
20+
- [Running bandit by itself](#running-bandit-by-itself)
2021
- [Other linting tools](#other-linting-tools)
2122
- [Making a new branch & pull request](#making-a-new-branch--pull-request)
2223
- [Commit message tips](#commit-message-tips)
@@ -160,6 +161,7 @@ CVE Binary Tool uses a few tools to improve code quality and readability:
160161
- `black` provides automatic style formatting. This will give you basic [PEP8](https://www.python.org/dev/peps/pep-0008/) compliance. (PEP8 is where the default python style guide is defined.)
161162
- `flake8` provides additional code "linting" for more complex errors like unused imports.
162163
- `pyupgrade` helps us be forward compatible with new versions of python.
164+
- `bandit` is more of a static analysis tool than a linter and helps us find potential security flaws in the code.
163165

164166
We provide a `dev-requirements.txt` file which includes all the precise versions of tools as they'll be used in GitHub Actions. You an install them all using pip:
165167

@@ -206,6 +208,26 @@ files you've changed because you won't have to scroll through a pile of
206208
auto-formatting changes to find your own modifications. However, you can also
207209
specify a whole folder using ```./```
208210

211+
### Running bandit by itself
212+
213+
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+
215+
To run it on all the code we scan, use the following:
216+
217+
```bash
218+
bandit -c bandit.conf -r cve_bin_tool/
219+
```
220+
221+
You can also run it on individual files:
222+
223+
```bash
224+
bandit -c bandit.conf filename.py
225+
```
226+
227+
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+
229+
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().
230+
209231
### Other linting tools
210232

211233
As well as `black` for automatically making sure code adheres to the style guide, we use `flake8` to help us find things like unused imports. The [flake8 documentation](https://flake8.pycqa.org/en/latest/user/index.html) covers what you need to know about running it.

bandit.conf

+10-1
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,19 @@
8585
tests:
8686

8787
# (optional) list skipped test IDs here, eg '[B101, B406]':
88-
skips: ['B603', 'B607', 'B404']
88+
skips: ['B603', 'B607', 'B404', "B608"]
89+
# B603, B607 and B404 are all subprocess-related.
90+
# B608 should be re-enabled when multi-line issues can be marked with nosec
91+
8992
# Explantion: cve-bin-tool is at heart a shell script that calls other processes.
9093
# Switching to pure python has significant performance impacts.
9194

95+
exclude_dirs:
96+
- "test/"
97+
- "/test/"
98+
- "./test/"
99+
- "./build/lib/test/"
100+
92101
### (optional) plugin settings - some test plugins require configuration data
93102
### that may be given here, per-plugin. All bandit test plugins have a built in
94103
### set of sensible defaults and these will be used if no configuration is

cve_bin_tool/available_fix/redhat_cve_tracker.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ def cve_info(
7474

7575
def get_data(self, cve_number: str, product: str):
7676
try:
77-
full_query = f"{RH_CVE_API}/{cve_number}.json"
78-
response = request.urlopen(full_query).read().decode("utf-8")
77+
full_query = f"{RH_CVE_API}/{cve_number}.json" # static https url above
78+
response = request.urlopen(full_query).read().decode("utf-8") # nosec
7979
return loads(response)
8080
except error.HTTPError as e:
8181
LOGGER.debug(e)

dev-requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ black==21.12b0
22
isort==5.10.1
33
pre-commit==2.16.0
44
flake8==4.0.1
5+
bandit==1.7.1

0 commit comments

Comments
 (0)