From 0f6898ad0a9d8769bb26038f80107b309c8fb91e Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Thu, 11 Sep 2025 19:10:00 -0700 Subject: [PATCH] fix(key_management_endpoints.py): check if key is a hashed token or sk key before lookup Fixes https://github.com/BerriAI/litellm/issues/13887 --- .../key_management_endpoints.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/litellm/proxy/management_endpoints/key_management_endpoints.py b/litellm/proxy/management_endpoints/key_management_endpoints.py index bd8faf34be8..a7f15cc6c19 100644 --- a/litellm/proxy/management_endpoints/key_management_endpoints.py +++ b/litellm/proxy/management_endpoints/key_management_endpoints.py @@ -346,6 +346,7 @@ def handle_key_type(data: GenerateKeyRequest, data_json: dict) -> dict: data_json["allowed_routes"] = ["info_routes"] return data_json + async def validate_team_id_used_in_service_account_request( team_id: Optional[str], prisma_client: Optional[PrismaClient], @@ -358,13 +359,13 @@ async def validate_team_id_used_in_service_account_request( status_code=400, detail="team_id is required for service account keys. Please specify `team_id` in the request body.", ) - + if prisma_client is None: raise HTTPException( status_code=400, detail="prisma_client is required for service account keys. Please specify `prisma_client` in the request body.", ) - + # check if team_id exists in the database team = await prisma_client.db.litellm_teamtable.find_unique( where={"team_id": team_id}, @@ -376,6 +377,7 @@ async def validate_team_id_used_in_service_account_request( ) return True + async def _common_key_generation_helper( # noqa: PLR0915 data: GenerateKeyRequest, user_api_key_dict: UserAPIKeyAuth, @@ -557,7 +559,7 @@ async def _common_key_generation_helper( # noqa: PLR0915 status_code=400, detail={ "error": f"Invalid key format. LiteLLM Virtual Key must start with 'sk-'. Received: {data.key}" - } + }, ) response = await generate_key_helper_fn( @@ -2885,7 +2887,10 @@ async def unblock_key( param="key", code=status.HTTP_400_BAD_REQUEST, ) - hashed_token = hash_token(token=data.key) + if data.key.startswith("sk-"): + hashed_token = hash_token(token=data.key) + else: + hashed_token = data.key if litellm.store_audit_logs is True: # make an audit log for key update