The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(proxy): admin-gate `permissions` on _process_single_key_update (LIT-4137) (#32002)
yucheng-berrib61fae397b75
diff --git a/litellm/proxy/management_endpoints/key_management_endpoints.py b/litellm/proxy/management_endpoints/key_management_endpoints.pyindex d56ebc5ac99..ade865a5378 100644--- a/litellm/proxy/management_endpoints/key_management_endpoints.py+++ b/litellm/proxy/management_endpoints/key_management_endpoints.py@@ -2069,6 +2069,11 @@ async def _process_single_key_update(# Validate max_budget_validate_max_budget(update_key_request.max_budget)+ _check_permissions_caller_permission(+ data=update_key_request,+ user_api_key_dict=user_api_key_dict,+ )+# Get and validate existing keyif existing_key_row is None:existing_key_row = await _get_and_validate_existing_key(diff --git a/tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.py b/tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.pyindex 7c92757224c..ee9202f522e 100644--- a/tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.py+++ b/tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.py@@ -11507,6 +11507,67 @@ async def test_process_single_key_update_cache_invalidation_with_token_hash():assert call_kwargs["hashed_token"] == token_hash+async def test_process_single_key_update_non_admin_permissions_rejected():+ """`_process_single_key_update` rejects a non-admin when `permissions`+ is present in the constructed `UpdateKeyRequest`. Guards the bulk path+ against a future widening of the `BulkUpdateKeyRequestItem` or+ `KeyUpdateFields` allowlists reopening the class."""+ update_key_request = UpdateKeyRequest(+ key="abc123",+ permissions={"get_spend_routes": True},+ )+ assert "permissions" in update_key_request.model_fields_set++ non_admin = UserAPIKeyAuth(+ user_role=LitellmUserRoles.INTERNAL_USER,+ api_key="sk-non-admin",+ user_id="user-1",+ )++ with pytest.raises(HTTPException) as exc_info:+ await _process_single_key_update(+ update_key_request=update_key_request,+ user_api_key_dict=non_admin,+ litellm_changed_by=None,+ prisma_client=AsyncMock(),+ user_api_key_cache=MagicMock(),+ proxy_logging_obj=MagicMock(),+ llm_router=MagicMock(),+ )+ assert exc_info.value.status_code == 403+ assert "permissions" in str(exc_info.value.detail)+++async def test_process_single_key_update_non_admin_permissions_explicit_empty_rejected():+ """`_process_single_key_update` rejects a non-admin when `permissions`+ is present as `{}` in the constructed `UpdateKeyRequest`. Presence+ check on `model_fields_set` catches the explicit-empty case the same+ as any other value."""+ update_key_request = UpdateKeyRequest(key="abc123", permissions={})+ assert "permissions" in update_key_request.model_fields_set++ non_admin = UserAPIKeyAuth(+ user_role=LitellmUserRoles.INTERNAL_USER,+ api_key="sk-non-admin",+ user_id="user-1",+ )++ with pytest.raises(HTTPException) as exc_info:+ await _process_single_key_update(+ update_key_request=update_key_request,+ user_api_key_dict=non_admin,+ litellm_changed_by=None,+ prisma_client=AsyncMock(),+ user_api_key_cache=MagicMock(),+ proxy_logging_obj=MagicMock(),+ llm_router=MagicMock(),+ )+ assert exc_info.value.status_code == 403+ assert "permissions" in str(exc_info.value.detail)++@pytest.mark.asyncioasync def test_execute_virtual_key_regeneration_cache_invalidation_with_token_hash():"""