Registry / devops / bandit

bandit

JSON →
library1.9.4pypypiunverified

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.

devopstesting
pip install bandit
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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")
bandit --version
Debug
Known issues
breakingUsing `subprocess` calls with `shell=True` (e.g., `subprocess.call(command, shell=True)`) is a major security vulnerability (B602) if the `command` string is derived from untrusted input, as it enables shell injection attacks.
fix
Avoid `shell=True`. Instead, pass commands and arguments as a list (e.g., `subprocess.call(['ls', '-l'])`). If `shell=True` is unavoidable, ensure all user-supplied input is rigorously sanitized.
affects: All versions
gotchaThe Python `assert` statement (B101) should not be used for security-critical checks or enforcing interface constraints in production code. Asserts are removed when Python is run with optimizations (`python -O`), which can bypass security controls.
fix
Replace `assert` statements used for critical logic with proper exception handling (e.g., `raise ValueError(...)` or `raise AssertionError(...)`).
affects: All versions
gotchaBandit can produce false positives, requiring manual review of reported issues. The tool's output provides severity and confidence levels to help prioritize findings, but human judgment is still necessary.
fix
Review each reported issue carefully. Use inline comments like `# nosec` to suppress specific findings that are confirmed false positives or acceptable risks, documenting the reason for suppression.
affects: All versions
gotchaRunning Bandit recursively on large codebases can be time-consuming and impact development workflow if integrated as a blocking pre-commit hook for every change.
fix
Integrate Bandit into your CI/CD pipeline for comprehensive scans on pull requests or merges. For local development, consider running it less frequently, targeting specific files, or configuring it to only fail on high-severity issues.
affects: All versions
gotchaWhen using configuration files (`.bandit`, `bandit.yaml`, `pyproject.toml`), only `.bandit` (INI format) is automatically discovered when running `bandit -r`. For YAML or TOML files, you must explicitly specify them using the `-c` flag.
fix
For YAML or TOML configurations, always run Bandit with `bandit -c your_config.yaml -r .` or `bandit -c pyproject.toml -r .`. Ensure the configuration file path is correct.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'bandit'
The Bandit package has not been installed in the current Python environment, or the Python environment where it's installed is not active.
fix
Install Bandit using pip: `pip install bandit`
command not found: bandit
The `bandit` executable is not found in the system's PATH, usually because it wasn't installed or its installation directory is not included in the PATH environment variable.
fix
Ensure Bandit is installed (`pip install bandit`) and verify that the directory containing the `bandit` executable (e.g., `~/.local/bin` or a virtual environment's `bin` directory) is in your system's PATH. You might need to reactivate your virtual environment or restart your terminal.
ModuleNotFoundError: No module named 'pbr'
This error typically occurs with older versions of Bandit (e.g., 1.7.0) due to a missing `pbr` dependency, which was an indirect dependency via `stevedore` and might not have been explicitly installed or declared in certain setups.
fix
Install the `pbr` package: `pip install pbr`. Alternatively, update Bandit to a more recent version where this dependency issue might be resolved or managed differently: `pip install --upgrade bandit`.
bandit: error: unrecognized arguments: LOW
Users often provide severity or confidence levels (like 'LOW', 'MEDIUM', 'HIGH') incorrectly as direct arguments instead of using the expected syntax for `--severity-level` or `--confidence-level` flags.
fix
Specify the severity or confidence level using the correct flag and format, typically in lowercase and without the level name directly as an argument, e.g., `bandit examples/*.py --severity-level high` or `bandit examples/*.py --confidence-level medium`.
Error: Process completed with exit code 1.
This generic error can appear when Bandit encounters syntax errors in the scanned Python code that are incompatible with the Python interpreter version Bandit is running on, or when the Bandit version itself is not compatible with the Python version it's analyzing. Bandit 1.9.4 requires Python >=3.10.
fix
Ensure that the Python version used to run Bandit is compatible with the Python code being analyzed. If your project uses Python 3.10+, run Bandit with a Python 3.10+ interpreter. For example, if you are scanning a Python 3.7 project, you should run Bandit with a Python 3.7 interpreter. Consider using a virtual environment to manage Python versions. Update Bandit to the latest version if there are compatibility concerns: `pip install --upgrade bandit`.
Upgrade
Version history
1.9.4latest on PyPI
Audit
Dependencies

No dependency data recorded yet.

Agent activity
19 hits · last 30 days
ahrefsbot
3
seranking-bot
3
node
2
Amazon
2
mj12bot
2
amazonbot
1
bytedance
1
Resources