How Bandit Performs Static Analysis on Python Code

Bandit is a dedicated static application security testing (SAST) tool designed to identify common security flaws in Python code. It operates by converting raw Python files into an Abstract Syntax Tree (AST), scanning the structural nodes against an extensible collection of security-focused test plugins, and generating reports that rate identified vulnerabilities by severity and confidence. By analyzing syntax and context rather than raw text, Bandit enables developers to automate code reviews and catch potential security regressions directly within development and continuous integration environments.

Abstract Syntax Tree (AST) Generation

Instead of relying on regex pattern matching, Bandit leverages Python’s built-in ast module. When provided with a target directory or file, Bandit parses the source text into an Abstract Syntax Tree. This hierarchical tree representation breaks the code down into structural elements, such as module imports, function definitions, variable assignments, and function calls. Analyzing the AST allows Bandit to understand the programmatic structure and relationships within the code, significantly reducing false positives compared to simple text-search tools.

Node Traversal and Context Evaluation

Once the AST is built, Bandit traverses the tree using a Node Visitor pattern. As Bandit walks through each node of the AST, it evaluates the node's context. For instance, if Bandit encounters a Call node, it inspects:

This structural context ensures that security rules trigger only when specific, potentially dangerous programmatic conditions are met.

Plugin-Based Security Testing

Bandit organizes its analysis logic into modular test plugins. Each plugin is registered to watch for specific AST node types. When a matching node is encountered during the AST walk, the relevant plugin executes its evaluation logic.

Common security tests performed by Bandit plugins include:

Severity and Confidence Metrics

When a plugin discovers a vulnerability, Bandit assigns two distinct ratings to the issue:

  1. Severity (Low, Medium, High): Measures the potential impact of the security flaw on the application if exploited.
  2. Confidence (Low, Medium, High): Measures the likelihood that the reported issue represents an actual vulnerability rather than a false positive.

These ratings allow development teams to filter and prioritize findings, focusing immediately on high-severity, high-confidence flaws while ignoring lower-risk patterns according to their risk tolerance.

Configuration, Baseline, and Pipeline Execution

Bandit supports custom configuration through YAML files or pyproject.toml, where teams can disable specific plugins, customize test parameters, or specify file paths to exclude. To prevent redundant alerts on existing codebases, Bandit supports baseline reporting, which allows users to suppress previously known issues and only fail builds when new security flaws are introduced. Because it executes via the command line and outputs formats such as JSON, CSV, HTML, and SARIF, Bandit fits seamlessly into modern CI/CD pipelines to enforce static security standards before code is merged into production.