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:

ToolEngineStrengthBlind spot
Bandit (PyCQA, Apache 2.0)Single-pass AST plugins, 47 checksFast enough for pre-commit; low noise; Python-idiomatic rulesNo taint or cross-file dataflow
SemgrepPattern + taint, YAML rulesCustom rules; covers 12+ Python deserialization libraries with dataflowPro rules and interfile analysis are paid
CodeQL (GitHub)Database + queryDeep interprocedural taint; free for public reposHeavier setup (builds a DB per scan)
Pyre/Pysa (Meta)Taint analysisSource-to-sink flows across modules—
Bearer CLIDataflow, OWASP/CWE rules plus privacyFlags sensitive-data flows (logging, cookies, JWT)Cross-file Python analysis is Pro-only
Snyk Code / Checkmarx / VeracodeCommercial semantic and taintInterfile taint; IDE integrationLicensing

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:

RangeCategoryExamples
B1xxMiscassert_used (B101), exec_used (B102), hardcoded_password_string (B105), request_without_timeout
B2xxFramework misconfigflask_debug_true, tarfile_unsafe_members
B3xxBlacklisted callspickle (B301), MD5/SHA1 (B303/B304), eval (B307), unsafe XML
B4xxBlacklisted importsknown-dangerous modules
B5xxCrypto/TLSssl_with_bad_version, weak_cryptographic_key, yaml_load
B6xxInjectionsubprocess_popen_with_shell_equals_true (B602), hardcoded_sql_expressions, django_extra_used, trojansource
B7xxXSSjinja2_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 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:

# 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.

Sources