diff --git a/litellm/proxy/spend_tracking/spend_tracking_utils.py b/litellm/proxy/spend_tracking/spend_tracking_utils.py index 36ed16e0262..ec6245f47e9 100644 --- a/litellm/proxy/spend_tracking/spend_tracking_utils.py +++ b/litellm/proxy/spend_tracking/spend_tracking_utils.py @@ -228,8 +228,6 @@ def _extract_usage_for_ocr_call(response_obj: Any, response_obj_dict: dict) -> d def get_logging_payload( # noqa: PLR0915 kwargs, response_obj, start_time, end_time ) -> SpendLogsPayload: - from litellm.proxy.proxy_server import general_settings, master_key - if kwargs is None: kwargs = {} diff --git a/tests/proxy_unit_tests/test_key_generate_prisma.py b/tests/proxy_unit_tests/test_key_generate_prisma.py index 19ee9f75d87..6a568d94f8c 100644 --- a/tests/proxy_unit_tests/test_key_generate_prisma.py +++ b/tests/proxy_unit_tests/test_key_generate_prisma.py @@ -2715,7 +2715,12 @@ async def test_master_key_hashing(prisma_client): request=request, api_key=bearer_token ) - assert result.api_key == hash_token(master_key) + # Master-key auth substitutes a stable alias so the master key (or + # its hash) never propagates into spend logs / metrics / audit trails. + from litellm.constants import LITELLM_PROXY_MASTER_KEY_ALIAS + + assert result.api_key == LITELLM_PROXY_MASTER_KEY_ALIAS + assert result.api_key != hash_token(master_key) except Exception as e: print("Got Exception", e) diff --git a/tests/proxy_unit_tests/test_user_api_key_auth.py b/tests/proxy_unit_tests/test_user_api_key_auth.py index 3239b95d50c..e51f81561aa 100644 --- a/tests/proxy_unit_tests/test_user_api_key_auth.py +++ b/tests/proxy_unit_tests/test_user_api_key_auth.py @@ -1118,11 +1118,17 @@ async def test_jwt_non_admin_team_route_access(monkeypatch): @pytest.mark.asyncio async def test_x_litellm_api_key(): """ - Check if auth can pick up x-litellm-api-key header, even if Bearer token is provided + Check if auth can pick up x-litellm-api-key header, even if Bearer token is provided. + + On a master-key match, ``UserAPIKeyAuth.api_key`` (and the derived + ``token``) are now the stable alias ``LITELLM_PROXY_MASTER_KEY_ALIAS`` + rather than ``hash_token(master_key)`` — the master key (or its hash) + must not propagate into spend logs / metrics / audit trails. """ from fastapi import Request from starlette.datastructures import URL + from litellm.constants import LITELLM_PROXY_MASTER_KEY_ALIAS from litellm.proxy._types import ( LiteLLM_TeamTable, LiteLLM_TeamTableCachedObj, @@ -1148,7 +1154,8 @@ async def test_x_litellm_api_key(): api_key="Bearer " + ignored_key, custom_litellm_key_header=master_key, ) - assert valid_token.token == hash_token(master_key) + assert valid_token.token == LITELLM_PROXY_MASTER_KEY_ALIAS + assert valid_token.token != hash_token(master_key) @pytest.mark.asyncio