Python Passlib Insufficient Bcrypt Cost
Description
This detector identifies Python code using the `passlib` library where bcrypt password hashing is configured with an insufficient cost (work) factor. A low bcrypt cost factor makes password hashes faster to compute, significantly reducing the effort required for brute-force or dictionary attacks against stolen password hashes. The vulnerability arises when passlib's bcrypt handler or context objects are instantiated or configured with a `rounds`/`cost` parameter below the recommended secure threshold.
Detection Strategy
• Scan all import statements in the file to identify whether passlib's bcrypt handler classes (e.g., `bcrypt`, `bcrypt_sha256`) or context/CryptContext objects are imported and in use. If neither category of passlib bcrypt symbol is imported, no further analysis is performed.
• For each function call found in the code, check whether the called function or class matches one of the identified passlib bcrypt handler imports or context imports.
• For calls matching a bcrypt handler (e.g., direct handler instantiation or `using()`), inspect the keyword arguments associated with the call for cost-related parameters (e.g., `rounds`). For calls matching a CryptContext configuration, inspect the relevant cost keyword arguments (e.g., `bcrypt__rounds` or equivalent context-specific parameters).
• A vulnerability is reported when a matched call contains a cost/rounds keyword argument whose value is a numeric literal below the minimum acceptable bcrypt work factor threshold, indicating the hash would be computed too quickly to provide adequate brute-force resistance.
Vulnerable code example
from passlib.context import CryptContext
from passlib.hash import bcrypt, bcrypt_sha256
LEGACY_ROUNDS = 8
# UNSAFE: CryptContext with cost 6 for bcrypt.
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto", bcrypt__rounds=6)
...✅ Secure code example
from passlib.context import CryptContext
from passlib.hash import bcrypt, bcrypt_sha256
SAFE_ROUNDS = 12 # meets OWASP minimum of 10+
# SAFE: cost 12 meets the minimum safe threshold for bcrypt.
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto", bcrypt__rounds=12)
...Search for vulnerabilities in your apps for free with Fluid Attacks' automated security testing! Start your 21-day free trial and discover the benefits of the Essential plan. If you prefer the Advanced plan, which includes the expertise of Fluid Attacks' pentesting team, fill out this contact form.