Bandit

1.9.4 · active · verified Sun Apr 05

Bandit is an open-source security-oriented static analyser for Python code, designed to find common security issues early in the development lifecycle. It processes each file, builds an Abstract Syntax Tree (AST) from it, and runs a set of security-focused plugins against the AST nodes, generating reports with severity and confidence levels. Maintained by the PyCQA community, Bandit is currently at version 1.9.4 and requires Python >=3.10. Its release cadence focuses on compatibility updates and rule maintenance, indicating a stable and actively supported utility.

Warnings

Install

Quickstart

Bandit is primarily a command-line tool. To quickly scan your code for security issues, you first create a Python file, and then run Bandit against it. This example creates a dummy file with common vulnerabilities and instructs on how to run Bandit.

# Save this as vulnerable_app.py
import os
import subprocess

def execute_command(command_str):
    # B602: subprocess_popen_with_shell_equals_true - High severity, high confidence
    subprocess.call(command_str, shell=True) 

def process_user_input(user_input):
    # B307: eval - High severity, high confidence
    eval(user_input)

if __name__ == "__main__":
    print("Creating a dummy vulnerable file for Bandit scan.")
    with open("dummy_code.py", "w") as f:
        f.write("import subprocess\n")
        f.write("command = os.environ.get('UNSAFE_COMMAND', 'ls -l')\n")
        f.write("subprocess.call(command, shell=True)\n")

    print("Now run Bandit from your terminal:")
    print("bandit -r .\n")
    print("Or specifically on the dummy file:")
    print("bandit dummy_code.py\n")
    print("Example output will show security issues like B602.")

# To clean up after running:
# os.remove("dummy_code.py")

view raw JSON →