Skip to content

Commit d7ca03f

Browse files
Add bandit security scanning to CI
Co-authored-by: Johanan Oppong Amoateng <johananoppongamoateng2001@gmail.com> Co-authored-by: Brian Kohan <bckohan@gmail.com>
1 parent 13521c6 commit d7ca03f

File tree

6 files changed

+220
-6
lines changed

6 files changed

+220
-6
lines changed

.github/workflows/bandit.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: Bandit
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
paths:
8+
- "src/**"
9+
- "pyproject.toml"
10+
workflow_dispatch:
11+
12+
permissions:
13+
contents: read
14+
15+
jobs:
16+
bandit-analysis:
17+
name: Run Bandit
18+
runs-on: ubuntu-latest
19+
permissions:
20+
contents: read
21+
security-events: write
22+
23+
steps:
24+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
25+
with:
26+
persist-credentials: false
27+
- name: Set up Python
28+
uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405
29+
id: sp
30+
with:
31+
python-version: '3.x'
32+
allow-prereleases: true
33+
- name: Install uv
34+
uses: astral-sh/setup-uv@5a095e7a2014a4212f075830d4f7277575a9d098
35+
with:
36+
enable-cache: false
37+
- name: Install Just
38+
uses: extractions/setup-just@f8a3cce218d9f83db3a2ecd90e41ac3de6cdfd9b
39+
40+
- name: Run Bandit
41+
run: |
42+
just bandit
43+
44+
- name: Upload analysis results
45+
if: ${{ always() }}
46+
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f
47+
with:
48+
name: bandit-results
49+
path: bandit.sarif
50+
retention-days: 7
51+
52+
- name: Upload to code-scanning
53+
uses: github/codeql-action/upload-sarif@0d579ffd059c29b07949a3cce3983f0780820c98
54+
with:
55+
sarif_file: bandit.sarif

.github/workflows/zizmor.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,25 @@ jobs:
4040
4141
- name: Run Zizmor analysis
4242
run: |
43-
zizmor --format sarif .github/workflows/ > results.sarif
43+
zizmor --format sarif .github/workflows/ > zizmor.sarif
4444
4545
- name: Upload analysis results
4646
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f
4747
with:
4848
name: zizmor-results
49-
path: results.sarif
49+
path: zizmor.sarif
5050
retention-days: 7
5151

5252
- name: Upload to code-scanning
5353
uses: github/codeql-action/upload-sarif@0d579ffd059c29b07949a3cce3983f0780820c98
5454
with:
55-
sarif_file: results.sarif
55+
sarif_file: zizmor.sarif
5656

5757
- name: Fail on Findings
5858
run: |
5959
count="$(
6060
jq '([.runs[]? | (.results // [])[] | select(.level != "note")] | length) // 0' \
61-
results.sarif
61+
zizmor.sarif
6262
)"
6363
echo "Zizmor findings: $count"
6464
test "$count" -eq 0

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,4 @@ test2.db
227227
example/example.db
228228
CLAUDE.md
229229
zizmor.sarif
230+
bandit.sarif

justfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ lint: sort-imports
195195
# fix formatting, linting issues and import sorting
196196
fix: lint format
197197

198+
# run bandit static security analysis
199+
bandit:
200+
@just run --no-default-groups --group lint bandit -c pyproject.toml -r ./src -f sarif -o bandit.sarif
201+
198202
# run all static checks
199203
check *ENV:
200204
@just check-lint {{ ENV }}
@@ -203,6 +207,7 @@ check *ENV:
203207
@just check-docs {{ ENV }}
204208
@just check-readme {{ ENV }}
205209
@just check-package
210+
@just bandit
206211

207212
# run all checks including documentation link checking (slow)
208213
check-all *ENV:

pyproject.toml

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,30 @@ reportGeneralTypeIssues = false
207207
reportOptionalSubscript = false
208208
reportPrivateImportUsage = false
209209

210+
211+
[tool.bandit]
212+
targets = ["src"]
213+
exclude_dirs = [
214+
"./src/polymorphic/tests",
215+
"./src/polymorphic/migrations",
216+
"./example",
217+
"./.venv",
218+
"./docs",
219+
]
220+
221+
skips = [
222+
"B101", # assert_used - assertions are fine in tests and Django code
223+
]
224+
225+
# Severity level threshold
226+
# Options: LOW, MEDIUM, HIGH
227+
severity = "MEDIUM"
228+
229+
# Confidence level threshold
230+
# Options: LOW, MEDIUM, HIGH
231+
confidence = "MEDIUM"
232+
233+
210234
[dependency-groups]
211235
typing = [
212236
"django-stubs>=5.1.1",
@@ -216,9 +240,10 @@ typing = [
216240
"mypy>=1.19.1",
217241
]
218242
lint = [
219-
"ruff>=0.15.0",
243+
"ruff>=0.15.0",
220244
"readme-renderer[md]>=43.0",
221-
"doc8>=1.1.2"
245+
"doc8>=1.1.2",
246+
"bandit[toml,sarif]>=1.7.10"
222247
]
223248
coverage = ["coverage>=7.6.1"]
224249
test = [

uv.lock

Lines changed: 128 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)