Bandit Python security linter
Light · Postcard
102 × 152 mm · PDF
Dark · Postcard
102 × 152 mm · PDF
Ink saver · Postcard
102 × 152 mm · PDF
Light · A4 landscape
297 × 210 mm · PDF
Dark · A4 landscape
297 × 210 mm · PDF
Ink saver · A4 landscape
297 × 210 mm · PDF
Light · A3 landscape
420 × 297 mm · PDF
Dark · A3 landscape
420 × 297 mm · PDF
Ink saver · A3 landscape
420 × 297 mm · PDF
Start here
bandit -r <dir>
bandit <file.py>
bandit -r <dir> -ll
bandit -r <dir> -ll -ii
bandit -r <dir> -x <dirs>
bandit -r <dir> -f json -o <file>
bandit -r <dir> -s B101
# nosec B101Concepts
- Python files parsed to AST
- Tests B1xx-B7xx selected by -t / -s
- Issues severity + confidence
- Report screen, json, html...
Scanning
bandit -r <dir>
bandit <file.py> ...
bandit --version
bandit -r <dir> -x <paths>
bandit -r <dir> -v
bandit -r <dir> -q
bandit -r <dir> -n <n>
bandit -r <dir> --exit-zero
bandit -r <dir> -a file
Filtering
bandit -r <dir> -l / -ll / -lll
bandit -r <dir> -i / -ii / -iii
bandit -r <dir> --severity-level <level>
bandit -r <dir> --confidence-level
<level>
bandit -r <dir> -t <ids>
bandit -r <dir> -s <ids>
bandit -r <dir> -p <profile>
Output
bandit -r <dir> -f json
bandit -r <dir> -f <format>
bandit -r <dir> -o <file>
bandit -r <dir> -f custom --msg-template
"{relpath}:{line}: {test_id} {msg}"
bandit -r <dir> -f html -o <file>
Configuration
bandit -r <dir> -c <file>
bandit -r <dir> --ini <path>
bandit-config-generator -o <file>
bandit-config-generator --show-defaults
[tool.bandit]exclude_dirs = ["tests", ".venv"]skips = ["B101"]
Suppressing
# nosec# nosec B602bandit -r <dir> --ignore-nosec
bandit -r <dir> -b <baseline.json>
Common findings
| ID | Flags |
|---|---|
B101 |
assert (stripped under -O) |
B105-B107 |
hardcoded password |
B108 |
hardcoded /tmp path |
B113 |
request without timeout |
B301 |
pickle load |
B311 |
random for security use |
B324 |
weak hash (MD5, SHA1) |
B506 |
yaml.load unsafe |
B602 |
subprocess with shell=True |
B608 |
SQL built from strings |
| Finding | Use instead |
|---|---|
| shell=True | argument list, no shell |
| yaml.load | yaml.safe_load |
| random | secrets module |
| MD5/SHA1 | SHA-256 or usedforsecurity=False |
Recipes
bandit -r src -ll -ii -q# exit 1 fails the job
bandit -r src -f json -o baseline.json --exit-zerobandit -r src -b baseline.json
# .pre-commit-config.yaml- repo: https://github.com/PyCQA/bandit rev: <tag> hooks: - id: bandit args: ["-c", "pyproject.toml"] additional_dependencies: ["bandit[toml]"]
bandit -r src -t B602,B605,B608 -f custom \ --msg-template "{relpath}:{line} {test_id}"



