Skip to content

Commit 0f6898a

Browse files
fix(key_management_endpoints.py): check if key is a hashed token or sk key before lookup
Fixes #13887
1 parent 805069c commit 0f6898a

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

litellm/proxy/management_endpoints/key_management_endpoints.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ def handle_key_type(data: GenerateKeyRequest, data_json: dict) -> dict:
346346
data_json["allowed_routes"] = ["info_routes"]
347347
return data_json
348348

349+
349350
async def validate_team_id_used_in_service_account_request(
350351
team_id: Optional[str],
351352
prisma_client: Optional[PrismaClient],
@@ -358,13 +359,13 @@ async def validate_team_id_used_in_service_account_request(
358359
status_code=400,
359360
detail="team_id is required for service account keys. Please specify `team_id` in the request body.",
360361
)
361-
362+
362363
if prisma_client is None:
363364
raise HTTPException(
364365
status_code=400,
365366
detail="prisma_client is required for service account keys. Please specify `prisma_client` in the request body.",
366367
)
367-
368+
368369
# check if team_id exists in the database
369370
team = await prisma_client.db.litellm_teamtable.find_unique(
370371
where={"team_id": team_id},
@@ -376,6 +377,7 @@ async def validate_team_id_used_in_service_account_request(
376377
)
377378
return True
378379

380+
379381
async def _common_key_generation_helper( # noqa: PLR0915
380382
data: GenerateKeyRequest,
381383
user_api_key_dict: UserAPIKeyAuth,
@@ -557,7 +559,7 @@ async def _common_key_generation_helper( # noqa: PLR0915
557559
status_code=400,
558560
detail={
559561
"error": f"Invalid key format. LiteLLM Virtual Key must start with 'sk-'. Received: {data.key}"
560-
}
562+
},
561563
)
562564

563565
response = await generate_key_helper_fn(
@@ -2885,7 +2887,10 @@ async def unblock_key(
28852887
param="key",
28862888
code=status.HTTP_400_BAD_REQUEST,
28872889
)
2888-
hashed_token = hash_token(token=data.key)
2890+
if data.key.startswith("sk-"):
2891+
hashed_token = hash_token(token=data.key)
2892+
else:
2893+
hashed_token = data.key
28892894

28902895
if litellm.store_audit_logs is True:
28912896
# make an audit log for key update

0 commit comments

Comments
 (0)