From e585e3321c476ea113db31382dbec7d1b7c6c408 Mon Sep 17 00:00:00 2001 From: Yujong Lee Date: Mon, 26 Oct 2026 16:04:37 -0700 Subject: [PATCH] fix(auth): update is_master_key callers and type pass-through key lookup Key regeneration still called is_master_key with the old api_key/_master_key keywords and raised TypeError. Also restores the str contract of get_api_key and drops the duplicate hashlib imports left in proxy/utils.py. Co-Authored-By: Claude Opus 5.5 --- litellm/proxy/auth/user_api_key_auth.py | 22 ++++++++++++------- .../key_management_endpoints.py | 6 ++--- litellm/proxy/utils.py | 2 -- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/litellm/proxy/auth/user_api_key_auth.py b/litellm/proxy/auth/user_api_key_auth.py index 625d99d55ff..21bf71de853 100644 --- a/litellm/proxy/auth/user_api_key_auth.py +++ b/litellm/proxy/auth/user_api_key_auth.py @@ -279,6 +279,9 @@ _CLAUDE_MODEL_ROUTES: Final = frozenset( ) _CLAUDE_MODEL_NORMALIZED: Final = "litellm.claude_model_normalized" _GENERAL_SETTINGS_ADAPTER: Final = TypeAdapter(dict[str, object]) +_PASS_THROUGH_HEADERS_ADAPTER: Final[TypeAdapter[Mapping[str, object] | None]] = TypeAdapter( + Mapping[str, object] | None +) async def _normalize_claude_model( @@ -1350,14 +1353,17 @@ def _query_api_key(route: str, request: Request) -> str | None: return safe_get_request_query_params(request).get("key") -def _pass_through_header_api_key(endpoint: dict, route: str, request: Request) -> str | None: - headers: Final[dict | None] = endpoint.get("headers") if endpoint.get("path", "") == route else None - if headers is None: +def _pass_through_header_api_key(endpoint: Mapping[str, object], route: str, request: Request) -> str | None: + if endpoint.get("path", "") != route: return None - return request.headers.get(headers.get("litellm_user_api_key", "")) + headers: Final = _PASS_THROUGH_HEADERS_ADAPTER.validate_python(endpoint.get("headers")) + header_name: Final = headers.get("litellm_user_api_key", "") if headers is not None else None + return request.headers.get(header_name) if isinstance(header_name, str) else None -def _pass_through_api_key(pass_through_endpoints: list[dict] | None, route: str, request: Request) -> str | None: +def _pass_through_api_key( + pass_through_endpoints: Sequence[Mapping[str, object]] | None, route: str, request: Request +) -> str | None: header_values: Final = tuple( value for value in ( @@ -1375,17 +1381,17 @@ def _header_or_none(value: object) -> str | None: def get_api_key( custom_litellm_key_header: str | None, - api_key: str | None, + api_key: str, azure_api_key_header: str | None, anthropic_api_key_header: str | None, google_ai_studio_api_key_header: str | None, azure_apim_header: str | None, - pass_through_endpoints: list[dict] | None, + pass_through_endpoints: Sequence[Mapping[str, object]] | None, route: str, request: Request, *, custom_header: str | None = None, -) -> tuple[str | None, str | None]: +) -> tuple[str, str | None]: selected: Final = select_api_key( PresentedKeys( custom_header=custom_header, diff --git a/litellm/proxy/management_endpoints/key_management_endpoints.py b/litellm/proxy/management_endpoints/key_management_endpoints.py index 0ad4258fb04..33b938726c5 100644 --- a/litellm/proxy/management_endpoints/key_management_endpoints.py +++ b/litellm/proxy/management_endpoints/key_management_endpoints.py @@ -6378,9 +6378,7 @@ async def regenerate_key_fn( # value in that field. regenerate_target_key: Final = data.key if data and data.key else key is_master_key_regeneration: Final = ( - data is not None - and data.new_master_key is not None - and is_master_key(api_key=regenerate_target_key, _master_key=master_key) + data is not None and data.new_master_key is not None and is_master_key(regenerate_target_key, master_key) ) if ( @@ -6408,7 +6406,7 @@ async def regenerate_key_fn( detail={"error": "DB not connected. prisma_client is None"}, ) - _is_master_key_valid: Final = is_master_key(api_key=key, _master_key=master_key) + _is_master_key_valid: Final = is_master_key(key, master_key) if master_key is not None and data and _is_master_key_valid: if data.new_master_key is None: diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py index 9c4ee12ae55..2b897c3ae94 100644 --- a/litellm/proxy/utils.py +++ b/litellm/proxy/utils.py @@ -7882,7 +7882,6 @@ async def send_email( def hash_password(password: str) -> str: """Hash a password using scrypt with a random salt.""" import base64 - import hashlib import os salt: Final = os.urandom(16) @@ -7893,7 +7892,6 @@ def hash_password(password: str) -> str: def verify_password(password: str, stored: str) -> bool: """Verify a password against a stored hash. Supports scrypt and SHA256.""" import base64 - import hashlib import secrets if stored.startswith("scrypt:"):