litellm/tests/code_coverage_tests/test_no_hardcoded_secrets.py
yuneng-jiang 3357ec8d34
test: run the 30 test files stranded in the second mirror (#37595)
* test: run the 30 test files stranded in the second mirror

tests/litellm sat beside tests/test_litellm, which is the mirror the repo
convention names, and no job collected it. The allowlist called the directory
unresolved and assumed it was a duplicate. It is not: 30 of its 34 files have no
counterpart in the real mirror, so they are tests nobody has run since they were
written, not copies of tests that run elsewhere.

Moving them in is byte-identical, and it is what makes them run. Every one is
now claimed by a shard's test-path rather than by an allowlist entry, and the
216 tests they hold pass. Directories that needed to become packages did, since
several files are named test_transformation.py and pytest cannot import two of
those from non-package directories in one session.

Never running is why three assertions had drifted away from the code:

  * nvidia.nemotron-super-3-120b max_output_tokens, 32000 -> 32768
  * sambanova/MiniMax-M2.7 max_input_tokens, 204800 -> 196608
  * the Vertex text-to-speech handler moved from data= to json=, so the test
    reads the decoded body off the json kwarg instead of parsing the data one

The first two follow model_prices_and_context_window.json, which the catalog
sync keeps current; the third follows the handler. In all three the test was the
stale side.

The lint workflow ran test_no_hardcoded_secrets.py by path and now points at the
new one.

Four files stay behind. Each shares a filename with a live test whose contents
are disjoint from it, so landing those means merging test bodies, which is a
content review rather than a move. The allowlist entry now names those four and
records how many tests each would bring, in place of calling the whole
directory unresolved.

* fix(ci): keep the secret scan out of the mirror's conftest

The secret-scan job runs pytest under uv run --no-project, so its environment
holds pytest and nothing else. That worked while the file sat in tests/litellm,
which has no conftest, and broke the moment it moved into tests/test_litellm,
whose conftest imports litellm on collection: ModuleNotFoundError: No module
named 'dotenv', before a single test ran.

The file is a repo-wide static scan that imports only base64, os, re and pytest,
so it belongs with the other repo-wide checks in tests/code_coverage_tests,
which has no conftest, rather than in the package mirror. Installing the full
dependency set into a 15-second job to satisfy a conftest it does not use would
be the wrong trade.

Verified with the job's exact command:
  uv run --no-project --with 'pytest==9.0.2' pytest \
    tests/code_coverage_tests/test_no_hardcoded_secrets.py -q
  1 passed in 0.47s
2026-08-20 10:59:43 -07:00

70 lines
2.6 KiB
Python

"""
Test to ensure no hardcoded secrets exist in the codebase.
This catches Base64 Basic Authentication strings and other secret patterns
that would be flagged by secret scanners like GitGuardian/ggshield.
"""
import base64
import os
import re
import pytest
# Root of the litellm package
LITELLM_ROOT = os.path.join(os.path.dirname(__file__), "..", "..", "litellm")
# Regex for Base64 Basic Auth patterns: 'Basic <base64string>'
# Matches strings like: Basic YW55dGhpbmc6YW55dGhpbmc=
BASIC_AUTH_PATTERN = re.compile(r"""['"]Basic\s+([A-Za-z0-9+/]{16,}={0,2})['"]""")
# Directories/files to skip
SKIP_DIRS = {"__pycache__", ".git", "node_modules", ".mypy_cache", ".ruff_cache"}
def _is_real_base64_credentials(match_str: str) -> bool:
"""Check if a Base64 string decodes to something that looks like credentials (user:pass)."""
try:
# Add padding if needed - Base64 strings may omit trailing '='
padded = match_str + "=" * (-len(match_str) % 4)
decoded = base64.b64decode(padded).decode("utf-8", errors="ignore")
return ":" in decoded
except Exception:
return False
def _collect_python_files():
"""Collect all Python files under the litellm package."""
python_files = []
for root, dirs, files in os.walk(LITELLM_ROOT):
dirs[:] = [d for d in dirs if d not in SKIP_DIRS]
for f in files:
if f.endswith(".py"):
python_files.append(os.path.join(root, f))
return python_files
def test_no_hardcoded_basic_auth_secrets():
"""Ensure no hardcoded Base64 Basic Authentication credentials exist in source code.
This test prevents regressions like the one caught by T-Mobile's GitGuardian
container scan, where a docstring contained a literal Base64-encoded
'Basic YW55dGhpbmc6YW55dGhpbmc' string (anything:anything).
"""
violations = []
for filepath in _collect_python_files():
with open(filepath, "r", errors="ignore") as f:
for line_num, line in enumerate(f, start=1):
for match in BASIC_AUTH_PATTERN.finditer(line):
b64_value = match.group(1)
if _is_real_base64_credentials(b64_value):
rel_path = os.path.relpath(filepath, LITELLM_ROOT)
violations.append(f" {rel_path}:{line_num}: {match.group(0)}")
assert not violations, (
"Found hardcoded Base64 Basic Auth credentials that will be flagged by "
"secret scanners (e.g. GitGuardian/ggshield):\n"
+ "\n".join(violations)
+ "\n\nUse placeholders like '<base64(username:password)>' in comments/docs instead."
)