Python Vulnerability Checks

Tools for checking vulnerabilities in Python code.


Safety - Dependency Scanner

 1# Install
 2pip install safety
 3
 4# Check dependencies
 5safety check
 6
 7# Check requirements file
 8safety check -r requirements.txt
 9
10# JSON output
11safety check --json

Bandit - Static Security Analysis

 1# Install
 2pip install bandit
 3
 4# Scan directory
 5bandit -r ./myproject
 6
 7# Output formats
 8bandit -r ./myproject -f json -o report.json
 9bandit -r ./myproject -f html -o report.html
10
11# Exclude directories
12bandit -r ./myproject -x ./tests,./venv

Pip-audit

 1# Install
 2pip install pip-audit
 3
 4# Audit installed packages
 5pip-audit
 6
 7# Audit requirements file
 8pip-audit -r requirements.txt
 9
10# Fix vulnerabilities
11pip-audit --fix

CI/CD Integration

 1# GitHub Actions
 2name: Security Scan
 3
 4on: [push, pull_request]
 5
 6jobs:
 7  security:
 8    runs-on: ubuntu-latest
 9    steps:
10      - uses: actions/checkout@v3
11      
12      - name: Set up Python
13        uses: actions/setup-python@v4
14        with:
15          python-version: '3.11'
16      
17      - name: Install dependencies
18        run: pip install safety bandit pip-audit
19      
20      - name: Run Safety
21        run: safety check
22      
23      - name: Run Bandit
24        run: bandit -r .
25      
26      - name: Run pip-audit
27        run: pip-audit

Related Snippets