diff --git a/litellm/proxy/auth/master_key_policy.py b/litellm/proxy/auth/master_key_policy.py index 123ff9c972d..0d4bb0bde4b 100644 --- a/litellm/proxy/auth/master_key_policy.py +++ b/litellm/proxy/auth/master_key_policy.py @@ -1,6 +1,7 @@ from collections.abc import Mapping, Sequence from typing import Final, Literal +from fastapi import Request from pydantic import TypeAdapter from typing_extensions import ReadOnly, TypedDict, assert_never @@ -115,6 +116,14 @@ def stored_credentials_present() -> bool: return any((d.get("model_info") or {}).get("db_model") is True for d in deployments) +def request_http_method(request: Request) -> str: + try: + method: Final = request.method + except (KeyError, AttributeError): + return "" + return method if isinstance(method, str) else "" + + def master_key_lockout_action( route: str, method: str, diff --git a/litellm/proxy/auth/user_api_key_auth.py b/litellm/proxy/auth/user_api_key_auth.py index 1d561af4fa9..c191a474718 100644 --- a/litellm/proxy/auth/user_api_key_auth.py +++ b/litellm/proxy/auth/user_api_key_auth.py @@ -84,6 +84,7 @@ from litellm.proxy.auth.master_key_policy import ( insecure_master_key_reason, master_key_lockout_action, master_key_lockout_exception, + request_http_method, stored_credentials_present, ) from litellm.proxy.auth.network import TrustedProxyConfig, resolve_network_context @@ -1362,15 +1363,11 @@ async def _user_api_key_auth_builder( _lockout_reason: Final = insecure_master_key_reason( master_key, alternative_auth_enabled=alternative_auth_enabled(general_settings) ) - try: - _request_method: Final = request.method - except (KeyError, AttributeError): - _request_method = "" if ( _lockout_reason is not None and master_key_lockout_action( route=route, - method=_request_method if isinstance(_request_method, str) else "", + method=request_http_method(request), reason=_lockout_reason, stored_credentials_present=stored_credentials_present(), )