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.
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 orpyproject.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:
# 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 Pydanticmodel_rebuild()or Hydrainstantiate(). 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
- Bandit Python: Free SAST in 10 Seconds (2026 Review) · original
- Bandit: Python Static Application Security Testing Guide · original
- Python SAST Tools: Free & Paid Solutions · original
- Insecure Deserialization in Python (Semgrep) · original
- Bearer: SAST Tool · original
- 10 common security gotchas in Python · original
- Python Security Vulnerabilities: Top Issues (Aikido) · original
- PyJWT 2.14/2.15: algorithm confusion advisories explained · original
- JWT verification bugs: pin the algorithm, then prove the fix · original
- Benchmarking Secure-and-Functional Remediation (Snyk) · original