Abstract
While many developers use security scanners, few understand how they actually "read" code. This article explains the inner workings of Bandit, focusing on its use of the Abstract Syntax Tree (AST) to identify security patterns without ever executing a single line of code.
1. The Core Engine: AST (Abstract Syntax Tree)
Unlike a simple text search (which might give many false positives), Bandit doesn't just look for words like "password". It converts your Python code into an AST.
What is AST? It is a tree representation of the abstract syntactic structure of your source code.
Why it matters: By building a tree, Bandit understands the context. It knows if a string is just a comment or if it's actually being assigned to a sensitive variable or passed to a dangerous function like eval().
2. How the "Scanning" Happens
Bandit works through a set of plugins. Each plugin is designed to look for a specific type of vulnerability:
Blacklist Plugins: These look for the use of insecure modules (like pickle or telnetlib).
Function Call Plugins: These trigger when they see dangerous calls (like subprocess.shell=True).
Hardcoded Secret Plugins: These use heuristics to identify strings that look like passwords or API keys.
3. Severity and Confidence levels
One of Bandit's best features is its scoring system:
Severity: How bad is the bug? (Low, Medium, High).
Confidence: How sure is Bandit that this is actually a bug and not a mistake?
Why use it in the CI/CD?
The main advantage is speed. Because it doesn't need to compile or run the code, it can scan thousands of lines in seconds. Integrating it into GitHub Actions (as shown in my previous post) ensures that no "illegal" AST patterns make it into the main branch.
Conclusion
Bandit isn't just a linter; it's a security-focused parser. By understanding the structure of Python, it provides a robust first line of defense for any backend developer.

Top comments (1)
Great technical deep dive! Understanding that Bandit uses AST instead of just 'grep' or text searching is crucial. It explains why the tool is so efficient at reducing false positives by understanding the actual structure and context of the Python code. This is definitely a must-read for anyone trying to understand how SAST really works.