From c09d9e5e8e71806ebbfdbcb94e6b19970df2495d Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Wed, 10 Sep 2025 18:58:48 -0700 Subject: [PATCH] feat: Validate LiteLLM Virtual Key format (#14428) Co-authored-by: Cursor Agent Co-authored-by: ishaan --- .../management_endpoints/key_management_endpoints.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/litellm/proxy/management_endpoints/key_management_endpoints.py b/litellm/proxy/management_endpoints/key_management_endpoints.py index 0bf957f265..bd8faf34be 100644 --- a/litellm/proxy/management_endpoints/key_management_endpoints.py +++ b/litellm/proxy/management_endpoints/key_management_endpoints.py @@ -551,6 +551,15 @@ async def _common_key_generation_helper( # noqa: PLR0915 prisma_client=prisma_client, ) + # Validate user-provided key format + if data.key is not None and not data.key.startswith("sk-"): + raise HTTPException( + 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( request_type="key", **data_json, table_name="key" )