From bdb00c43cf022e2baad67484d21d990c17fbf21f Mon Sep 17 00:00:00 2001 From: user <70670632+stuxf@users.noreply.github.com> Date: Sat, 25 Apr 2026 04:10:34 +0000 Subject: [PATCH] fix(spend-tracking): drop orphaned imports; align tests with alias contract MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CI surfaced two issues from the previous commit: 1. ``general_settings`` and ``master_key`` were still imported at the top of ``get_logging_payload`` but had no remaining users after the master-key hash-detection blocks were removed. Drop the import. 2. ``tests/proxy_unit_tests/test_user_api_key_auth.py::test_x_litellm_api_key`` and ``tests/proxy_unit_tests/test_key_generate_prisma.py::test_master_key_hashing`` asserted ``valid_token.token == hash_token(master_key)`` — the pre-alias behavior. The new contract is ``valid_token.token == LITELLM_PROXY_MASTER_KEY_ALIAS`` (and != ``hash_token(master_key)``), since the master key (and its hash) must not propagate to the verification-token column or any other downstream consumer. Co-Authored-By: Claude Opus 4.7 (1M context) --- litellm/proxy/spend_tracking/spend_tracking_utils.py | 2 -- tests/proxy_unit_tests/test_key_generate_prisma.py | 7 ++++++- tests/proxy_unit_tests/test_user_api_key_auth.py | 11 +++++++++-- 3 files changed, 15 insertions(+), 5 deletions(-) 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