Why AI tools produce this: AI coding tools generate this anti-pattern by default, it appears in a large share of AI-written auth code.
Why this matters
The credential is baked into the source, so anyone who reads the code (or a leaked repo) has a working login, and the secret cannot be rotated without a redeploy (CWE-798). This is a common AI-generated mistake: FastAPI's own HTTP Basic example uses the literals "stanleyjobson" / "swordfish", and assistants copy that shape straight into production auth.
Compare against a secret loaded from the environment or a secret manager instead, e.g. secrets.compare_digest(credentials.password.encode("utf8"), os.environ["ADMIN_PASSWORD"].encode("utf8")), and prefer a real user store with per-user salted password hashes for anything beyond a single service account.
import osimport secretsfrom typing import Annotatedfrom fastapi import Depends, FastAPI, HTTPException, statusfrom fastapi.security import HTTPBasic, HTTPBasicCredentialsapp = FastAPI()security = HTTPBasic()def check_env(credentials: HTTPBasicCredentials = Depends(security)): # Compared against secrets from the environment — not hard-coded. # ok: auth.py.fastapi.hardcoded-http-basic is_user = secrets.compare_digest( credentials.username.encode("utf8"), os.environ["ADMIN_USER"].encode("utf8"), ) # ok: auth.py.fastapi.hardcoded-http-basic is_pass = secrets.compare_digest( credentials.password.encode("utf8"), os.environ["ADMIN_PASSWORD"].encode("utf8"), ) if not (is_user and is_pass): raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED) return credentials.usernamedef check_userstore( credentials: Annotated[HTTPBasicCredentials, Depends(security)],): user = lookup_user(credentials.username) # ok: auth.py.fastapi.hardcoded-http-basic if user and verify_password(credentials.password, user.password_hash): return user raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)def unrelated_login(username: str, password: str): # Not a FastAPI HTTPBasicCredentials dependency — must not fire. # ok: auth.py.fastapi.hardcoded-http-basic if password == "letmein": return True return False
Suppressing this rule
If a finding is a genuine false positive, scope the suppression to the exact line and leave a reason, never disable the rule project-wide. Disable directives are line-scoped by design.