PyCQA / PyCQA/bandit

False Negatives in B105/B106/B107 Hardcoded Password Detection

Open
#1,383 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
8.3k
Forks
836
Avg merge
5d 3h
Merged PRs (30d)
1

Description

Describe the bug

The current implementations of Bandit's B105 (Hardcoded Password String), B106 (Hardcoded Password Function Arguments), and B107 (Hardcoded Password Default Values) have some false negatives. These rules rely on rigid regular expressions for identifier matching and incomplete AST traversal for comparison operations. Consequently, hardcoded credentials are missed when used with common naming conventions (e.g., camelCase, api_key) or accessed via dictionary subscripts (e.g., data['password']).

For example

import pymysql
from genai import configure

class ConfigLoader:
    def __init__(self, data):
        self.data = data

    def authenticate_user(self):
        # FAILS
        # The rule expects ast.Name (e.g., 'password') or ast.Attribute (e.g., 'obj.password').
        # It misses dictionary access like data['password'].
        if self.data['password'] == 'hardcoded_secret_123':
            return True
        return False

    def load_credentials(self):
        # FAILS
        # 'DATABASEPASSWORD' does not match ^password$ or _password_.
        DATABASEPASSWORD = "password123"
        
        # FAILS: 'api_key' is not in the RE_WORDS list (password, secret, token, etc.).
        # Common credential identifiers like 'api_key', 'API_KEY)' are ignored.
        configure(api_key="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
Reproduction steps
bandit -r  the_code_above.py
Expected behavior

3 alarms on these hardcoded cases

Bandit version

1.9.1 (Default)

Python version

3.14 (Default)

Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the B105, B106, and B107 implementations and inspect how identifier matching and comparison-operation AST traversal handle names, attributes, and subscripts. Run the provided bandit reproduction against the example code, then verify that the three hardcoded credential cases produce alarms without regressing existing detections.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
security, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
62/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.