# Python SAST Tools
> Static analyzers for Python source. They range from Bandit's single-pass AST pattern matching to cross-file taint engines (Semgrep, CodeQL, Pysa, Snyk Code, Bearer Pro) and targeted grep for library misconfigurations.
## How it works
The tool tiers differ in what they can model:
| Tool | Engine | Strength | Blind spot |
|---|---|---|---|
| **Bandit** (PyCQA, Apache 2.0) | Single-pass AST plugins, 47 checks | Fast enough for pre-commit; low noise; Python-idiomatic rules | No taint or cross-file dataflow |
| **Semgrep** | Pattern + taint, YAML rules | Custom rules; covers 12+ Python deserialization libraries with dataflow | Pro rules and interfile analysis are paid |
| **CodeQL** (GitHub) | Database + query | Deep interprocedural taint; free for public repos | Heavier setup (builds a DB per scan) |
| **Pyre/Pysa** (Meta) | Taint analysis | Source-to-sink flows across modules | — |
| **Bearer CLI** | Dataflow, OWASP/CWE rules plus privacy | Flags sensitive-data flows (logging, cookies, JWT) | Cross-file Python analysis is Pro-only |
| **Snyk Code / Checkmarx / Veracode** | Commercial semantic and taint | Interfile taint; IDE integration | Licensing |
Practical layering from the appsecsanta review: run Bandit in pre-commit and CI for anti-patterns, then CodeQL or Semgrep underneath for taint flows that pass through several helper modules.
## Bandit reference
ID ranges:
| Range | Category | Examples |
|---|---|---|
| B1xx | Misc | `assert_used` (B101), `exec_used` (B102), `hardcoded_password_string` (B105), `request_without_timeout` |
| B2xx | Framework misconfig | `flask_debug_true`, `tarfile_unsafe_members` |
| B3xx | Blacklisted calls | pickle (B301), MD5/SHA1 (B303/B304), `eval` (B307), unsafe XML |
| B4xx | Blacklisted imports | known-dangerous modules |
| B5xx | Crypto/TLS | `ssl_with_bad_version`, `weak_cryptographic_key`, `yaml_load` |
| B6xx | Injection | `subprocess_popen_with_shell_equals_true` (B602), `hardcoded_sql_expressions`, `django_extra_used`, `trojansource` |
| B7xx | XSS | `jinja2_autoescape_false`, `use_of_mako_templates`, `django_mark_safe`, `markupsafe_markup_xss` |
Newer plugins cover the ML supply chain: `torch.load()` checks and **B615** for unsafe Hugging Face model downloads.
```bash
bandit -r src/ --severity-level high # CI gate
bandit -r src/ -f json -o baseline.json # snapshot
bandit -r src/ -b baseline.json # only new findings
bandit -r src/ -f sarif -o results.sarif # GitHub code scanning
```
- Configure via `.bandit`, YAML or `pyproject.toml` (`skips = ["B101", "B601"]`). Extras: `bandit[toml]`, `bandit[sarif]`, `bandit[baseline]`.
- Suppress narrowly with `# nosec B602, B607`, not a bare `# nosec`.
- B101 asserts dominate findings on real codebases. Gate high severity in CI and run the full set locally.
- Signed multi-arch image: `docker run -v $(pwd):/src ghcr.io/pycqa/bandit/bandit -r /src`.
## Targeted grep for library misuse
Some high-impact bugs are configuration, not dataflow, and grep finds them faster than SAST:
```bash
# PyJWT algorithm-confusion precondition: HMAC + asymmetric in one allow-list
grep -rnE "algorithms\s*=\s*\[[^]]*HS[0-9]+[^]]*(RS|ES|PS|EdDSA)" --include=*.py .
grep -rnE 'jwt\.decode\(|verify_signature|get_unverified_header' --include=*.py .
grep -rn "PyJWKClient" --include=*.py .
```
Other high-value greps: `\.objects\.(filter|exclude|get)\(\*\*` (Django CVE-2025-64459 pattern), `shell=True`, `pickle.load`, `yaml.load(`, `allow_pickle=True`, `torch.load(`, `instantiate(`, `request.url.path` used in authz middleware (Starlette BadHost).
## Limits
- Pattern tools miss sinks reached through library internals, such as `eval()` inside Pydantic `model_rebuild()` or Hydra `instantiate()`. See [[Unsafe Python Functions]].
- AI remediation: Snyk's ~150-sample benchmark (JS, Java, Python) found frontier models plateau at 72–75% secure-and-functional fixes alone. Adding vendor security context raised Opus 4.6 from 74.6% to 85.4%. Verify fixes; don't just accept them.
## Related
- [[Python Security]]
- [[Unsafe Python Functions]]
- [[Python Web Framework Security]]
- [[Python JWT Library Vulnerabilities]]
- [[Python Pickle Deserialization]]
- [[Hardcoded Secrets]]
- [[Secret Scanning]]
- [[Software Supply Chain Security]]
## Sources
- [[raw/appsecsanta.com/bandit-python-free-sast-in-10-seconds-2026-review-4809|Bandit Python: Free SAST in 10 Seconds (2026 Review)]]
- [[raw/dev.to/bandit-python-static-application-security-testing-guide-2858|Bandit: Python Static Application Security Testing Guide]]
- [[raw/bito.ai/python-sast-tools-free-paid-solutions-for-secure-code-analysis-2852|Python SAST Tools: Free & Paid Solutions]]
- [[raw/semgrep.dev/insecure-deserialization-in-python-semgrep-2854|Insecure Deserialization in Python (Semgrep)]]
- [[raw/github.com/bearer-sast-tool-to-discover-filter-and-prioritize-security-and-privac-4817|Bearer: SAST Tool]]
- [[raw/link.medium.com/10-common-security-gotchas-in-python-and-how-to-avoid-them-831|10 common security gotchas in Python]]
- [[raw/aikido.dev/python-security-vulnerabilities-top-issues-aikido-2859|Python Security Vulnerabilities: Top Issues (Aikido)]]
- [[raw/safeguard.sh/pyjwt-2-14-2-15-algorithm-confusion-advisories-explained-13229|PyJWT 2.14/2.15: algorithm confusion advisories explained]]
- [[raw/dev.to/jwt-verification-bugs-pin-the-algorithm-then-prove-the-fix-14230|JWT verification bugs: pin the algorithm, then prove the fix]]
- [[raw/snyk.io/benchmarking-secure-and-functional-remediation-and-how-snyk-agent-fix-11528|Benchmarking Secure-and-Functional Remediation (Snyk)]]