Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2060,6 +2060,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 key
if existing_key_row is None:
existing_key_row = await _get_and_validate_existing_key(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11264,6 +11264,67 @@ async def test_process_single_key_update_cache_invalidation_with_token_hash():
assert call_kwargs["hashed_token"] == token_hash


@pytest.mark.asyncio
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)


@pytest.mark.asyncio
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.asyncio
async def test_execute_virtual_key_regeneration_cache_invalidation_with_token_hash():
"""
Expand Down
Loading