diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.py index b8d9530d2eb..c5e77ccce6a 100644 --- a/litellm/proxy/auth/auth_checks.py +++ b/litellm/proxy/auth/auth_checks.py @@ -5919,6 +5919,25 @@ def can_customer_access_model( return True +def customer_can_call_model( + model: str, + end_user_object: LiteLLM_EndUserTable, + llm_router: Router | None, + valid_token: UserAPIKeyAuth | None, +) -> bool: + """Non-raising form of the per-customer model check that request-time authorization enforces.""" + try: + can_customer_access_model( + model=model, + end_user_object=end_user_object, + llm_router=llm_router, + valid_token=valid_token, + ) + except ProxyException: + return False + return True + + async def can_user_call_model( model: str | list[str], llm_router: Router | None, diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py index b1055bb9124..3224b0ba346 100644 --- a/litellm/proxy/utils.py +++ b/litellm/proxy/utils.py @@ -305,6 +305,7 @@ if TYPE_CHECKING: from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj from litellm.llms.base_llm.guardrail_translation.base_translation import BaseTranslation + from litellm.models.end_user import LiteLLM_EndUserTable from litellm.models.team import LiteLLM_TeamTableCachedObj from litellm.proxy.db.autorouter_session_rollup import AutoRouterTurnTransaction from litellm.proxy.db.baseline_accounting import BaselineAccountingRecord @@ -9542,6 +9543,27 @@ async def _agent_access_group_visible_models( ) +async def _get_customer_allowed_models( + user_api_key_dict: "UserAPIKeyAuth", + prisma_client: Optional["PrismaClient"], + user_api_key_cache: Optional["UserApiKeyCache"], + proxy_logging_obj: Optional["ProxyLogging"], +) -> Optional["LiteLLM_EndUserTable"]: + """Load the customer/end-user object for listing-time scope narrowing, or ``None`` when unrestricted/unknown.""" + from litellm.proxy.auth.auth_checks import get_end_user_object + + if user_api_key_dict.end_user_id is None or prisma_client is None or user_api_key_cache is None: + return None + return await get_end_user_object( + end_user_id=user_api_key_dict.end_user_id, + prisma_client=prisma_client, + user_api_key_cache=user_api_key_cache, + proxy_logging_obj=proxy_logging_obj, + token_end_user_max_budget=user_api_key_dict.end_user_max_budget, + key_end_user_budget_id=None, + ) + + async def get_available_models_for_user( user_api_key_dict: "UserAPIKeyAuth", llm_router: Optional["Router"], @@ -9698,10 +9720,29 @@ async def get_available_models_for_user( team_id=effective_team_id, resolve_agent_ceiling=resolve_agent_ceiling, ) - if agent_visible is None: - return all_models - capped: Final = [m for m in all_models if m in agent_visible] - return capped + visible_models: Final = all_models if agent_visible is None else [m for m in all_models if m in agent_visible] + + end_user_object: Final = await _get_customer_allowed_models( + user_api_key_dict=user_api_key_dict, + prisma_client=prisma_client, + user_api_key_cache=user_api_key_cache, + proxy_logging_obj=proxy_logging_obj, + ) + if end_user_object is None or not end_user_object.models: + return visible_models + + from litellm.proxy.auth.auth_checks import customer_can_call_model + + return [ + model + for model in visible_models + if customer_can_call_model( + model=model, + end_user_object=end_user_object, + llm_router=llm_router, + valid_token=user_api_key_dict, + ) + ] def _safe_get_model_info(model: str, get_model_info: Callable[[str], ModelInfo]) -> ModelInfo | None: diff --git a/tests/e2e/access_control/access_control_client.py b/tests/e2e/access_control/access_control_client.py index 3f1ddcd1a13..5bcd5cb4359 100644 --- a/tests/e2e/access_control/access_control_client.py +++ b/tests/e2e/access_control/access_control_client.py @@ -9,15 +9,23 @@ from dataclasses import dataclass from pydantic import BaseModel, RootModel, ValidationError from proxy_client import ProxyClient +from e2e_metadata import step +from e2e_http import EndUserHeaders, NoBody, StreamingResponse, is_ok, unwrap from e2e_metadata import STEP_FRAMES, step from e2e_http import NoBody, StreamingResponse, is_ok, unwrap from models import ( ChatBody, ChatMessage, + CustomerInfoParams, + CustomerNewBody, + CustomerResponse, KeyGenerateBody, LiteLLMParamsBody, ModelInfoBody, + ModelListEntry, ModelNewBody, + ModelsListParams, + ModelsListResponse, ProjectCreateBody, ProjectDeleteBody, ProjectIdentity, @@ -69,9 +77,7 @@ class AccessControlClient: @step("Generate a virtual key that can only call LLM API routes") def llm_only_key(self) -> str: - return self.proxy.generate_key( - KeyGenerateBody(models=[], allowed_routes=["llm_api_routes"]) - ) + return self.proxy.generate_key(KeyGenerateBody(models=[], allowed_routes=["llm_api_routes"])) @step("Delete the virtual key") def delete_key(self, key: str) -> None: @@ -181,6 +187,44 @@ class AccessControlClient: time.sleep(self.proxy.poll_interval) raise AssertionError(f"/team/info never resolved team {team_id!r} created by /team/new") + @step("Create the customer {user_id} with models: {models}") + def create_customer(self, user_id: str, models: list[str] | None) -> str: + _ = unwrap( + self.proxy.transport.post( + "/customer/new", + headers=self.proxy.transport.master, + json=CustomerNewBody(user_id=user_id, models=models), + response_type=CustomerResponse, + ) + ) + return user_id + + @step("Read the customer {user_id}'s recorded models from /customer/info") + def customer_models(self, user_id: str) -> list[str] | None: + return unwrap( + self.proxy.transport.get( + "/customer/info", + headers=self.proxy.transport.master, + params=CustomerInfoParams(end_user_id=user_id), + response_type=CustomerResponse, + ) + ).models + + @step("List the model ids GET /v1/models returns for the end user {end_user_id}") + def models_for_customer(self, end_user_id: str) -> list[str]: + entries: tuple[ModelListEntry, ...] = unwrap( + self.proxy.transport.get( + "/v1/models", + headers=EndUserHeaders( + authorization=self.proxy.transport.master.authorization, + x_litellm_end_user_id=end_user_id, + ), + params=ModelsListParams(), + response_type=ModelsListResponse, + ) + ).data + return sorted(entry.id for entry in entries) + @step("Add a deployment named {model_name} that calls openai/gpt-4o-mini with the given key") def create_model_status(self, key: str, model_name: str) -> StreamingResponse: return self.proxy.transport.send( diff --git a/tests/e2e/access_control/test_customer_model_scope_e2e.py b/tests/e2e/access_control/test_customer_model_scope_e2e.py new file mode 100644 index 00000000000..dec712b84f0 --- /dev/null +++ b/tests/e2e/access_control/test_customer_model_scope_e2e.py @@ -0,0 +1,128 @@ +"""Live e2e: a customer/end-user model allow-list narrows the discovery endpoints. + +The customer analogue of PR #42159: when a request carries an end-user id whose +customer row has a models allow-list, GET /v1/models returns only the models that +customer may call, the same gate inference enforces. An empty allow-list is +unrestricted, not deny-all. This walks the recorded state (the customer's stored +models) and the enforced behavior (the narrowed discovery listing) against a real +booted proxy. + +The proxy serves no fixed `kimi`/`gpt-4o`, so each test registers the deployments +it names via /model/new (unique marker per run so a leftover row cannot satisfy a +stale assertion) and tears them down, matching the access_control suite's +create-and-clean pattern. +""" + +from __future__ import annotations + +from dataclasses import dataclass +from typing import Final + +import pytest + +from access_control_client import AccessControlClient +from e2e_config import unique_marker +from lifecycle import ResourceManager +from models import LiteLLMParamsBody, ModelInfoBody, ModelNewBody +from e2e_metadata import Domain, Subject, meta + +pytestmark = pytest.mark.e2e + +BACKEND: Final = "openai/gpt-4o-mini" + + +@dataclass(frozen=True, slots=True) +class ScopedDeployments: + """Two live deployments: one a restricted customer is granted, one it is not.""" + + allowed_model: str + other_model: str + + +def _deployment(model_name: str) -> ModelNewBody: + return ModelNewBody( + model_name=model_name, + litellm_params=LiteLLMParamsBody(model=BACKEND), + model_info=ModelInfoBody(), + ) + + +def _register(client: AccessControlClient, resources: ResourceManager, name: str) -> str: + model_id: Final = client.proxy.register_model(_deployment(name)) + resources.defer(lambda: client.proxy.delete_model(model_id)) + return name + + +@pytest.fixture +def deployments(client: AccessControlClient, resources: ResourceManager) -> ScopedDeployments: + marker: Final = unique_marker() + return ScopedDeployments( + allowed_model=_register(client, resources, f"e2e-custmodel-allowed-{marker}"), + other_model=_register(client, resources, f"e2e-custmodel-other-{marker}"), + ) + + +class TestCustomerModelScope: + @meta(Subject(domain=Domain.PROXY_AUTH)) + def test_restricted_customer_sees_only_allowed_models( + self, client: AccessControlClient, resources: ResourceManager, deployments: ScopedDeployments + ) -> None: + """A customer with models=[allowed_model] lists only that model, and its + recorded allow-list matches what discovery enforces.""" + customer_id: Final = f"e2e-cust-scope-{unique_marker()}" + client.create_customer(customer_id, models=[deployments.allowed_model]) + resources.defer(lambda: client.proxy.delete_customers([customer_id])) + + recorded: Final = client.customer_models(customer_id) + assert recorded == [deployments.allowed_model], ( + f"customer must record its allow-list {[deployments.allowed_model]!r}, got {recorded!r}" + ) + + listed: Final = client.models_for_customer(customer_id) + assert deployments.allowed_model in listed, ( + f"restricted customer must see its allowed model {deployments.allowed_model!r} on " + f"/v1/models, got {listed!r}" + ) + assert deployments.other_model not in listed, ( + f"restricted customer must not see {deployments.other_model!r} it cannot call, got {listed!r}" + ) + + @meta(Subject(domain=Domain.PROXY_AUTH)) + def test_restricted_customer_list_is_narrower_than_unrestricted( + self, client: AccessControlClient, resources: ResourceManager, deployments: ScopedDeployments + ) -> None: + """The narrowing is observable: the restricted customer's list is strictly + shorter than an unrestricted customer's list against the same proxy.""" + marker: Final = unique_marker() + restricted: Final = f"e2e-cust-restricted-{marker}" + unrestricted: Final = f"e2e-cust-unrestricted-{marker}" + client.create_customer(restricted, models=[deployments.allowed_model]) + resources.defer(lambda: client.proxy.delete_customers([restricted])) + client.create_customer(unrestricted, models=None) + resources.defer(lambda: client.proxy.delete_customers([unrestricted])) + + restricted_models: Final = client.models_for_customer(restricted) + unrestricted_models: Final = client.models_for_customer(unrestricted) + assert len(restricted_models) < len(unrestricted_models), ( + f"restricted list {restricted_models!r} must be shorter than unrestricted {unrestricted_models!r}" + ) + assert deployments.other_model in unrestricted_models, ( + f"unrestricted customer must see {deployments.other_model!r} the restricted one cannot, " + f"got {unrestricted_models!r}" + ) + + @meta(Subject(domain=Domain.PROXY_AUTH)) + def test_empty_allowlist_customer_is_not_deny_all( + self, client: AccessControlClient, resources: ResourceManager, deployments: ScopedDeployments + ) -> None: + """An empty allow-list means unrestricted: discovery returns the full set, + a regression guard that an empty list is not read as deny-all.""" + customer_id: Final = f"e2e-cust-empty-{unique_marker()}" + client.create_customer(customer_id, models=None) + resources.defer(lambda: client.proxy.delete_customers([customer_id])) + + listed: Final = client.models_for_customer(customer_id) + assert deployments.allowed_model in listed and deployments.other_model in listed, ( + f"empty-allowlist customer must see the full set including {deployments.allowed_model!r} " + f"and {deployments.other_model!r}, got {listed!r}" + ) diff --git a/tests/e2e/e2e_http.py b/tests/e2e/e2e_http.py index 2aea32909ce..e175d01ec16 100644 --- a/tests/e2e/e2e_http.py +++ b/tests/e2e/e2e_http.py @@ -53,6 +53,13 @@ class AnthropicHeaders(AuthHeaders): x_litellm_session_id: str | None = Field(default=None, serialization_alias="x-litellm-session-id") +class EndUserHeaders(AuthHeaders): + """Auth plus the ``x-litellm-end-user-id`` header the discovery routes read to + resolve the customer/end-user whose model allow-list narrows the listing.""" + + x_litellm_end_user_id: str | None = Field(default=None, serialization_alias="x-litellm-end-user-id") + + class PartialBody(BaseModel): """A body for a partial-update route (absent = keep, null = clear): a field left unset is omitted from the wire, and a field set to None is sent as JSON null.""" diff --git a/tests/e2e/models.py b/tests/e2e/models.py index 504adec5401..e6c1234f616 100644 --- a/tests/e2e/models.py +++ b/tests/e2e/models.py @@ -192,10 +192,12 @@ class KeyInfoResponse(BaseModel): class CustomerNewBody(BaseModel): user_id: str + models: list[str] | None = None class CustomerResponse(BaseModel): user_id: str | None = None + models: list[str] | None = None class CustomerInfoParams(BaseModel): diff --git a/tests/unit/proxy/auth/test_auth_checks_object_access_and_lookup.py b/tests/unit/proxy/auth/test_auth_checks_object_access_and_lookup.py index 0bdc5f1910a..bd34fa3d047 100644 --- a/tests/unit/proxy/auth/test_auth_checks_object_access_and_lookup.py +++ b/tests/unit/proxy/auth/test_auth_checks_object_access_and_lookup.py @@ -11869,82 +11869,111 @@ async def test_authoritative_group_grants_propagate_policy_outages( assert await get_agent_ids_from_access_groups(["group"]) == [] -def _client_tag_request(client_tags, baggage): - return Request( - { - "type": "http", - "headers": [ - *([(b"baggage", b"entrypoint=a")] if baggage else []), - *([(b"x-litellm-tags", b"usecase:a")] if client_tags == "header" else []), - ], - } +def _end_user_with_models(models: list[str]) -> LiteLLM_EndUserTable: + return LiteLLM_EndUserTable(user_id="end-user-1", blocked=False, models=models) + + +def test_customer_can_call_model_true_for_allowed_false_for_disallowed() -> None: + from litellm.proxy.auth.auth_checks import customer_can_call_model + + end_user_object: Final = _end_user_with_models(["somemodel"]) + + assert ( + customer_can_call_model( + model="somemodel", + end_user_object=end_user_object, + llm_router=None, + valid_token=None, + ) + is True + ) + assert ( + customer_can_call_model( + model="othermodel", + end_user_object=end_user_object, + llm_router=None, + valid_token=None, + ) + is False ) -def _client_tag_body(client_tags, client_slot): - if client_tags is None or client_tags == "header": - return {} - return {client_slot: client_tags if isinstance(client_tags, str) else {"tags": client_tags}} +@pytest.mark.parametrize("models", [[], None]) +def test_customer_can_call_model_unrestricted_when_allowlist_empty(models: list[str] | None) -> None: + from litellm.proxy.auth.auth_checks import customer_can_call_model + end_user_object: Final = LiteLLM_EndUserTable(user_id="end-user-1", blocked=False, models=models or []) -def _client_tag_rejection(route, data, request): - from litellm.proxy.auth.auth_checks import _reject_clientside_metadata_tags_check - - try: - _reject_clientside_metadata_tags_check({"reject_clientside_metadata_tags": True}, data, route, request) - except ProxyException as error: - return error.code - return None - - -@pytest.mark.asyncio -@pytest.mark.parametrize("route", ["/v1/chat/completions", "/v1/messages", "/v1/responses"]) -@pytest.mark.parametrize("client_tags", [None, [], ["usecase:a"], '{"tags":["usecase:a"]}', "header"]) -@pytest.mark.parametrize("client_slot", ["metadata", "litellm_metadata"]) -@pytest.mark.parametrize("baggage", [True, False]) -async def test_baggage_mapping_rejects_exactly_the_client_tags_rejected_without_it( - route, client_tags, client_slot, baggage -): - from litellm.proxy.litellm_pre_call_utils import LiteLLMProxyRequestSetup - - unmapped_request = _client_tag_request(client_tags, baggage=False) - unmapped = _client_tag_body(client_tags, client_slot) - LiteLLMProxyRequestSetup.pre_seed_litellm_metadata_for_route(unmapped, route) - LiteLLMProxyRequestSetup.apply_client_tag_policy_pre_auth(unmapped_request, unmapped, UserAPIKeyAuth(api_key="qa-key")) - - mapped_request = _client_tag_request(client_tags, baggage=baggage) - mapped = _client_tag_body(client_tags, client_slot) - LiteLLMProxyRequestSetup.pre_seed_litellm_metadata_for_route(mapped, route) - await LiteLLMProxyRequestSetup.apply_baggage_tag_mappings_pre_auth( - mapped_request, - mapped, - {"request_tag_mappings": [{"key": "entrypoint", "tag_prefix": "usecase:"}]}, - None, - MagicMock(), + assert ( + customer_can_call_model( + model="any-model", + end_user_object=end_user_object, + llm_router=None, + valid_token=None, + ) + is True ) - LiteLLMProxyRequestSetup.apply_client_tag_policy_pre_auth(mapped_request, mapped, UserAPIKeyAuth(api_key="qa-key")) - assert _client_tag_rejection(route, mapped, mapped_request) == _client_tag_rejection( - route, unmapped, unmapped_request + +def test_customer_can_call_model_honors_wildcard_allowlist() -> None: + from litellm.proxy.auth.auth_checks import customer_can_call_model + + end_user_object: Final = _end_user_with_models(["openai/*"]) + + assert ( + customer_can_call_model( + model="openai/gpt-4o", + end_user_object=end_user_object, + llm_router=None, + valid_token=None, + ) + is True ) - if client_tags is None and baggage: - assert "usecase:a" in mapped["metadata" if "chat/completions" in route else "litellm_metadata"]["tags"] - - -@pytest.mark.asyncio -async def test_baggage_mapping_still_rejects_client_metadata_tags(): - from litellm.proxy.litellm_pre_call_utils import LiteLLMProxyRequestSetup - - request = _client_tag_request(["usecase:b"], baggage=True) - data = {"metadata": {"tags": ["usecase:b"]}} - await LiteLLMProxyRequestSetup.apply_baggage_tag_mappings_pre_auth( - request, - data, - {"request_tag_mappings": [{"key": "entrypoint", "tag_prefix": "usecase:"}]}, - None, - MagicMock(), + assert ( + customer_can_call_model( + model="anthropic/claude-sonnet-4-5", + end_user_object=end_user_object, + llm_router=None, + valid_token=None, + ) + is False ) - assert _client_tag_rejection("/v1/chat/completions", data, request) == "400" - from litellm.utils import get_request_guardrails - assert get_request_guardrails(data) == [] + +def test_customer_can_call_model_resolves_router_access_group() -> None: + from litellm import Router + from litellm.proxy.auth.auth_checks import customer_can_call_model + + router: Final = Router( + model_list=[ + { + "model_name": "fast-model", + "litellm_params": {"model": "openai/gpt-4o", "api_key": "sk-test"}, + "model_info": {"access_groups": ["fast-models"]}, + }, + { + "model_name": "slow-model", + "litellm_params": {"model": "openai/gpt-4.1", "api_key": "sk-test"}, + }, + ] + ) + end_user_object: Final = _end_user_with_models(["fast-models"]) + + assert ( + customer_can_call_model( + model="fast-model", + end_user_object=end_user_object, + llm_router=router, + valid_token=None, + ) + is True + ) + assert ( + customer_can_call_model( + model="slow-model", + end_user_object=end_user_object, + llm_router=router, + valid_token=None, + ) + is False + ) diff --git a/tests/unit/proxy/utils/helpers/test_model_access.py b/tests/unit/proxy/utils/helpers/test_model_access.py index 324360cc2ee..1b22c2fc5bd 100644 --- a/tests/unit/proxy/utils/helpers/test_model_access.py +++ b/tests/unit/proxy/utils/helpers/test_model_access.py @@ -1,3 +1,4 @@ +from typing import Final from unittest.mock import MagicMock import pytest @@ -577,6 +578,62 @@ async def test_get_available_models_for_user_resolves_key_access_group_models( assert result == ["model-b"] +@pytest.mark.asyncio +async def test_get_available_models_for_user_narrows_to_customer_allowlist( + monkeypatch: pytest.MonkeyPatch, +) -> None: + from litellm.proxy._types import LiteLLM_EndUserTable + + async def _get_end_user_object(**_kwargs: object) -> LiteLLM_EndUserTable: + return LiteLLM_EndUserTable(user_id="end-user-1", blocked=False, models=["model-a"]) + + monkeypatch.setattr("litellm.proxy.auth.auth_checks.get_end_user_object", _get_end_user_object) + + result: Final = await get_available_models_for_user( + user_api_key_dict=UserAPIKeyAuth( + api_key="sk-test-key", + user_id="user-1", + end_user_id="end-user-1", + models=["model-a", "model-b"], + ), + llm_router=_router_with_models(["model-a", "model-b"]), + general_settings={}, + user_model=None, + prisma_client=MagicMock(), + proxy_logging_obj=MagicMock(), + user_api_key_cache=MagicMock(), + ) + assert result == ["model-a"] + + +@pytest.mark.asyncio +async def test_get_available_models_for_user_unrestricted_customer_keeps_full_list( + monkeypatch: pytest.MonkeyPatch, +) -> None: + from litellm.proxy._types import LiteLLM_EndUserTable + + async def _get_end_user_object(**_kwargs: object) -> LiteLLM_EndUserTable: + return LiteLLM_EndUserTable(user_id="end-user-1", blocked=False, models=[]) + + monkeypatch.setattr("litellm.proxy.auth.auth_checks.get_end_user_object", _get_end_user_object) + + result: Final = await get_available_models_for_user( + user_api_key_dict=UserAPIKeyAuth( + api_key="sk-test-key", + user_id="user-1", + end_user_id="end-user-1", + models=["model-a", "model-b"], + ), + llm_router=_router_with_models(["model-a", "model-b"]), + general_settings={}, + user_model=None, + prisma_client=MagicMock(), + proxy_logging_obj=MagicMock(), + user_api_key_cache=MagicMock(), + ) + assert sorted(result) == ["model-a", "model-b"] + + def _agent_ceiling(models: frozenset[str] | None): from litellm.proxy.agent_endpoints.auth.agent_access_groups import AgentAccessGroupCeiling