From 402ace7705b2f23f6a6122e27cb87328172ae590 Mon Sep 17 00:00:00 2001 From: Dhrumil Shah Date: Fri, 9 Oct 2026 12:38:19 +0000 Subject: [PATCH 1/6] fix(proxy): filter model discovery by customer/end-user allowed models The customer/end-user `models` restriction was enforced at inference time (403 customer_model_access_denied) but not applied to the model-listing endpoints, so an OpenAI-compatible client (e.g. LibreChat) building its picker from GET /v1/models showed models the end-user cannot call. Narrow the listing results by the customer/end-user `models` restriction, reusing the exact inference-time gate (access groups, wildcards like `anthropic/*`, sentinels all-proxy-models/all-team-models/no-default-models, alias expansion). Strict no-op when the customer is unrestricted (empty or absent `models`). This is the customer analogue of upstream PR #42159 (team-member case); scope is kept strictly to the customer/end-user case so it does not collide. /v2/model/info customer narrowing is deferred. - auth_checks: add non-raising customer_can_call_model(...) wrapping can_customer_access_model in try/except ProxyException. - utils: add cache-backed loader _get_customer_allowed_models(...) and a single narrowing pass at the end of get_available_models_for_user, covering /v1/models, /models, /v1/models/{id}, /models/{id}. The admin scope=expand branch short-circuits earlier and stays exempt. - proxy_server: /model/info + /v1/model/info parity via _get_v1_model_info_allowed_model_names, intersecting the key/team allowlist with the customer-allowed names (None = unrestricted -> clean intersection). Tests (mock-only, no network/LLM/DB): - tests/unit/proxy/utils/helpers/test_model_access.py: customer models=['kimi'] -> only kimi; empty/absent restriction -> full list (regression); wildcard anthropic/* expands; sentinel all-proxy-models passthrough. - tests/unit/proxy/proxy_server/test_routes_model_info.py: /v1/model/info and /model/info narrow to kimi for a restricted customer; empty allowlist -> full list. Verification (see .agents/tasks/verification.md for details and environment limits): - pytest tests/unit/proxy/utils/helpers/test_model_access.py tests/unit/proxy/proxy_server/test_routes_model_info.py tests/unit/proxy/proxy_server/test_routes_models.py -> 121 passed. - ruff format --check and ruff check on all 5 changed files -> clean. - basedpyright on the added lines -> 0 new findings (gate's isolated typecheck venv could not be provisioned: Rust/cc toolchain absent). --- litellm/proxy/auth/auth_checks.py | 19 +++ litellm/proxy/proxy_server.py | 60 +++++++++- litellm/proxy/utils.py | 49 +++++++- .../proxy_server/test_routes_model_info.py | 75 ++++++++++++ .../proxy/utils/helpers/test_model_access.py | 113 ++++++++++++++++++ 5 files changed, 309 insertions(+), 7 deletions(-) diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.py index 1435033e1be..ade5d044e99 100644 --- a/litellm/proxy/auth/auth_checks.py +++ b/litellm/proxy/auth/auth_checks.py @@ -5506,6 +5506,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/proxy_server.py b/litellm/proxy/proxy_server.py index 67fec38e39e..7a21b8949ec 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -16295,11 +16295,37 @@ def _deployment_matches_allowed_model_names(model: dict[str, JsonValue], allowed return isinstance(team_public_model_name, str) and team_public_model_name in allowed_model_names +def _get_customer_allowed_v1_model_names( + customer_object: LiteLLM_EndUserTable, + user_api_key_dict: UserAPIKeyAuth, + llm_router: Router, +) -> set[str]: + """Public model names the customer/end-user is allowed to call, via the same inference-time gate.""" + from litellm.proxy.auth.auth_checks import customer_can_call_model + + return { + model + for model in llm_router.get_model_names() + if customer_can_call_model( + model=model, + end_user_object=customer_object, + llm_router=llm_router, + valid_token=user_api_key_dict, + ) + } + + def _get_v1_model_info_allowed_model_names( user_api_key_dict: UserAPIKeyAuth, llm_router: Router, + customer_object: LiteLLM_EndUserTable | None = None, ) -> set[str] | None: - """Return key/team allowlisted public model names, or None if unrestricted.""" + """Return key/team allowlisted public model names, or None if unrestricted. + + When the caller is a restricted customer/end-user, intersect the key/team result with the + customer-allowed names (same inference-time gate). Unrestricted customer (None/empty models) + leaves the result unchanged. + """ model_access_groups: Final = llm_router.get_model_access_groups() proxy_model_list: Final = llm_router.get_model_names() key_models: Final = get_key_models( @@ -16312,9 +16338,18 @@ def _get_v1_model_info_allowed_model_names( proxy_model_list=proxy_model_list, model_access_groups=model_access_groups, ) + customer_allowed: Final = ( + _get_customer_allowed_v1_model_names( + customer_object=customer_object, + user_api_key_dict=user_api_key_dict, + llm_router=llm_router, + ) + if customer_object is not None and customer_object.models + else None + ) if not key_models and not team_models: - return None - return set( + return customer_allowed + key_team_allowed: Final = set( get_complete_model_list( key_models=key_models, team_models=team_models, @@ -16325,6 +16360,9 @@ def _get_v1_model_info_allowed_model_names( return_wildcard_routes=False, ) ) + if customer_allowed is None: + return key_team_allowed + return key_team_allowed & customer_allowed def _filter_v1_model_info_deployments( @@ -16532,9 +16570,25 @@ async def model_info_v1( all_models = expand_wildcard_deployments_for_model_info(all_models) + # Narrow the listing by the customer/end-user allowlist (same gate inference enforces). + # Skip entirely when there is no end-user on the token or no DB to load from. + customer_object: LiteLLM_EndUserTable | None = None + if user_api_key_dict.end_user_id is not None and prisma_client is not None: + from litellm.proxy.auth.auth_checks import get_end_user_object + + customer_object = 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, + ) + allowed_model_names: Final = _get_v1_model_info_allowed_model_names( user_api_key_dict=user_api_key_dict, llm_router=llm_router, + customer_object=customer_object, ) all_models = _filter_v1_model_info_deployments( diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py index 4743970a526..3d220ee8707 100644 --- a/litellm/proxy/utils.py +++ b/litellm/proxy/utils.py @@ -279,6 +279,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 @@ -8780,6 +8781,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"], @@ -8904,10 +8926,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 [ # mutable-ok: callers consume the public list[str] return contract + 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/unit/proxy/proxy_server/test_routes_model_info.py b/tests/unit/proxy/proxy_server/test_routes_model_info.py index fae4a6470cf..879f3dd069d 100644 --- a/tests/unit/proxy/proxy_server/test_routes_model_info.py +++ b/tests/unit/proxy/proxy_server/test_routes_model_info.py @@ -596,6 +596,81 @@ def test_model_info_team_key_cannot_see_other_teams_byok_model( assert _BYOK_INTERNAL_NAME not in surfaced_names +# --------------------------------------------------------------------------- +# GET /v1/model/info — customer/end-user allowlist narrowing (customer analogue of #42159) +# --------------------------------------------------------------------------- + + +@pytest.fixture +def customer_scope_router(monkeypatch): + """Router with three plain deployments so only the customer narrowing selects them.""" + model_names = ["kimi", "gpt-4o", "claude-3"] + deployments = [ + { + "model_name": name, + "litellm_params": {"model": name}, + "model_info": {"id": f"dep-{name}", "db_model": False}, + } + for name in model_names + ] + router = MagicMock() + router.model_list = deployments + router.get_model_names = MagicMock(return_value=model_names) + router.get_model_access_groups = MagicMock(return_value={}) + router.get_model_list_from_model_alias = MagicMock(return_value=[]) + router.get_model_list = MagicMock(return_value=[]) + router.model_group_alias = {} + monkeypatch.setattr(proxy_server, "llm_router", router) + monkeypatch.setattr(proxy_server, "llm_model_list", deployments) + monkeypatch.setattr(proxy_server, "user_model", None) + monkeypatch.setattr(proxy_server, "_get_proxy_model_info", lambda model: model) + yield router + + +def _patch_end_user_object(monkeypatch, models): + """Make the handler's cache-backed end-user load return a restricted (or unrestricted) customer.""" + from litellm.proxy._types import LiteLLM_EndUserTable + + async def _loader(**_kwargs): + if models is None: + return None + return LiteLLM_EndUserTable(user_id="c1", blocked=False, models=list(models)) + + monkeypatch.setattr("litellm.proxy.auth.auth_checks.get_end_user_object", _loader) + + +@pytest.mark.parametrize("path", ["/v1/model/info", "/model/info"]) +def test_v1_model_info_narrows_to_customer_allowed_models( + client, auth_as, customer_scope_router, mock_prisma, monkeypatch, path +): + """A restricted customer (models=['kimi']) sees only kimi from /v1/model/info.""" + monkeypatch.setattr(proxy_server, "prisma_client", mock_prisma) + _patch_end_user_object(monkeypatch, ["kimi"]) + + with auth_as(end_user_id="c1"): + response = client.get(path) + + assert response.status_code == 200, response.text + surfaced_names = [m.get("model_name") for m in response.json()["data"]] + assert surfaced_names == ["kimi"] + + +@pytest.mark.parametrize("path", ["/v1/model/info", "/model/info"]) +def test_v1_model_info_unrestricted_customer_sees_full_list( + client, auth_as, customer_scope_router, mock_prisma, monkeypatch, path +): + """A customer with an empty allowlist is unrestricted: the full model set is returned (regression guard).""" + monkeypatch.setattr(proxy_server, "prisma_client", mock_prisma) + _patch_end_user_object(monkeypatch, []) + + with auth_as(end_user_id="c1"): + response = client.get(path) + + assert response.status_code == 200, response.text + surfaced_names = sorted(m.get("model_name") for m in response.json()["data"]) + assert surfaced_names == ["claude-3", "gpt-4o", "kimi"] + + # --------------------------------------------------------------------------- # GET /model_group/info # --------------------------------------------------------------------------- diff --git a/tests/unit/proxy/utils/helpers/test_model_access.py b/tests/unit/proxy/utils/helpers/test_model_access.py index 7f77f938323..2ab06b8b5f6 100644 --- a/tests/unit/proxy/utils/helpers/test_model_access.py +++ b/tests/unit/proxy/utils/helpers/test_model_access.py @@ -592,3 +592,116 @@ async def test_listing_is_unchanged_without_an_agent_or_without_attached_groups( resolve_agent_ceiling=_agent_ceiling(None), ) assert (plain_key, agent_without_groups) == (["model-a", "model-b"], ["model-a", "model-b"]) + + +def _unrestricted_full_list(monkeypatch, model_names): + """Make the key/team path return the full router list so only the customer narrowing is exercised.""" + monkeypatch.setattr("litellm.proxy.auth.model_checks.get_key_models", lambda **_k: list(model_names)) + monkeypatch.setattr("litellm.proxy.auth.model_checks.get_team_models", lambda **_k: []) + monkeypatch.setattr( + "litellm.proxy.auth.model_checks.get_complete_model_list", + lambda **_k: list(model_names), + ) + + +def _customer(models): + from litellm.models.end_user import LiteLLM_EndUserTable + + return LiteLLM_EndUserTable(user_id="c1", blocked=False, models=list(models)) + + +def _patch_customer_object(monkeypatch, end_user_object): + async def _loader(**_kwargs): + return end_user_object + + monkeypatch.setattr("litellm.proxy.utils._get_customer_allowed_models", _loader) + + +def _customer_key(): + return UserAPIKeyAuth(api_key="sk-customer", user_id="user-1", end_user_id="c1", models=[], team_models=[]) + + +@pytest.mark.asyncio +async def test_get_available_models_for_user_narrows_listing_to_customer_allowed_models(monkeypatch): + model_names = ["kimi", "gpt-4o", "claude-3"] + _unrestricted_full_list(monkeypatch, model_names) + _patch_customer_object(monkeypatch, _customer(["kimi"])) + + result = await get_available_models_for_user( + user_api_key_dict=_customer_key(), + llm_router=_router_with_models(model_names), + general_settings={}, + user_model=None, + prisma_client=MagicMock(), + proxy_logging_obj=MagicMock(), + user_api_key_cache=MagicMock(), + ) + assert result == ["kimi"] + + +@pytest.mark.asyncio +async def test_get_available_models_for_user_is_unchanged_when_customer_is_unrestricted(monkeypatch): + model_names = ["kimi", "gpt-4o", "claude-3"] + _unrestricted_full_list(monkeypatch, model_names) + + # Case 1: no end-user resolved (end_user_id absent) -> full list. + _patch_customer_object(monkeypatch, None) + no_customer = await get_available_models_for_user( + user_api_key_dict=UserAPIKeyAuth(api_key="sk-plain", user_id="user-1", models=[], team_models=[]), + llm_router=_router_with_models(model_names), + general_settings={}, + user_model=None, + prisma_client=MagicMock(), + proxy_logging_obj=MagicMock(), + user_api_key_cache=MagicMock(), + ) + + # Case 2: end-user resolved but with an empty allowlist -> full list (unrestricted). + _patch_customer_object(monkeypatch, _customer([])) + empty_restriction = await get_available_models_for_user( + user_api_key_dict=_customer_key(), + llm_router=_router_with_models(model_names), + general_settings={}, + user_model=None, + prisma_client=MagicMock(), + proxy_logging_obj=MagicMock(), + user_api_key_cache=MagicMock(), + ) + + assert (no_customer, empty_restriction) == (model_names, model_names) + + +@pytest.mark.asyncio +async def test_get_available_models_for_user_customer_wildcard_expands_for_listing(monkeypatch): + model_names = ["anthropic/claude-3", "anthropic/claude-3-haiku", "gpt-4o"] + _unrestricted_full_list(monkeypatch, model_names) + _patch_customer_object(monkeypatch, _customer(["anthropic/*"])) + + result = await get_available_models_for_user( + user_api_key_dict=_customer_key(), + llm_router=_router_with_models(model_names), + general_settings={}, + user_model=None, + prisma_client=MagicMock(), + proxy_logging_obj=MagicMock(), + user_api_key_cache=MagicMock(), + ) + assert result == ["anthropic/claude-3", "anthropic/claude-3-haiku"] + + +@pytest.mark.asyncio +async def test_get_available_models_for_user_customer_sentinel_all_proxy_models_passthrough(monkeypatch): + model_names = ["kimi", "gpt-4o", "claude-3"] + _unrestricted_full_list(monkeypatch, model_names) + _patch_customer_object(monkeypatch, _customer(["all-proxy-models"])) + + result = await get_available_models_for_user( + user_api_key_dict=_customer_key(), + llm_router=_router_with_models(model_names), + general_settings={}, + user_model=None, + prisma_client=MagicMock(), + proxy_logging_obj=MagicMock(), + user_api_key_cache=MagicMock(), + ) + assert result == model_names From b1ef4c1b3a66920c4ef38c0b352886f7b9717ea3 Mon Sep 17 00:00:00 2001 From: Dhrumil Shah Date: Fri, 9 Oct 2026 16:14:48 +0000 Subject: [PATCH 2/6] fix(proxy): scope model discovery to customer allow-list via get_available_models_for_user Remove the buggy /model/info customer filtering and keep the narrowing in get_available_models_for_user, which backs /v1/models, /models and the /{id} variants. Replace the mocked unit tests that doubled litellm's own functions with a live e2e test under tests/e2e/access_control. --- litellm/proxy/proxy_server.py | 60 +--------- litellm/proxy/utils.py | 2 +- .../access_control/access_control_client.py | 50 +++++++- .../test_customer_model_scope_e2e.py | 82 +++++++++++++ tests/e2e/e2e_http.py | 7 ++ tests/e2e/models.py | 2 + .../proxy_server/test_routes_model_info.py | 75 ------------ .../proxy/utils/helpers/test_model_access.py | 113 ------------------ 8 files changed, 141 insertions(+), 250 deletions(-) create mode 100644 tests/e2e/access_control/test_customer_model_scope_e2e.py diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 7a21b8949ec..67fec38e39e 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -16295,37 +16295,11 @@ def _deployment_matches_allowed_model_names(model: dict[str, JsonValue], allowed return isinstance(team_public_model_name, str) and team_public_model_name in allowed_model_names -def _get_customer_allowed_v1_model_names( - customer_object: LiteLLM_EndUserTable, - user_api_key_dict: UserAPIKeyAuth, - llm_router: Router, -) -> set[str]: - """Public model names the customer/end-user is allowed to call, via the same inference-time gate.""" - from litellm.proxy.auth.auth_checks import customer_can_call_model - - return { - model - for model in llm_router.get_model_names() - if customer_can_call_model( - model=model, - end_user_object=customer_object, - llm_router=llm_router, - valid_token=user_api_key_dict, - ) - } - - def _get_v1_model_info_allowed_model_names( user_api_key_dict: UserAPIKeyAuth, llm_router: Router, - customer_object: LiteLLM_EndUserTable | None = None, ) -> set[str] | None: - """Return key/team allowlisted public model names, or None if unrestricted. - - When the caller is a restricted customer/end-user, intersect the key/team result with the - customer-allowed names (same inference-time gate). Unrestricted customer (None/empty models) - leaves the result unchanged. - """ + """Return key/team allowlisted public model names, or None if unrestricted.""" model_access_groups: Final = llm_router.get_model_access_groups() proxy_model_list: Final = llm_router.get_model_names() key_models: Final = get_key_models( @@ -16338,18 +16312,9 @@ def _get_v1_model_info_allowed_model_names( proxy_model_list=proxy_model_list, model_access_groups=model_access_groups, ) - customer_allowed: Final = ( - _get_customer_allowed_v1_model_names( - customer_object=customer_object, - user_api_key_dict=user_api_key_dict, - llm_router=llm_router, - ) - if customer_object is not None and customer_object.models - else None - ) if not key_models and not team_models: - return customer_allowed - key_team_allowed: Final = set( + return None + return set( get_complete_model_list( key_models=key_models, team_models=team_models, @@ -16360,9 +16325,6 @@ def _get_v1_model_info_allowed_model_names( return_wildcard_routes=False, ) ) - if customer_allowed is None: - return key_team_allowed - return key_team_allowed & customer_allowed def _filter_v1_model_info_deployments( @@ -16570,25 +16532,9 @@ async def model_info_v1( all_models = expand_wildcard_deployments_for_model_info(all_models) - # Narrow the listing by the customer/end-user allowlist (same gate inference enforces). - # Skip entirely when there is no end-user on the token or no DB to load from. - customer_object: LiteLLM_EndUserTable | None = None - if user_api_key_dict.end_user_id is not None and prisma_client is not None: - from litellm.proxy.auth.auth_checks import get_end_user_object - - customer_object = 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, - ) - allowed_model_names: Final = _get_v1_model_info_allowed_model_names( user_api_key_dict=user_api_key_dict, llm_router=llm_router, - customer_object=customer_object, ) all_models = _filter_v1_model_info_deployments( diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py index 3d220ee8707..a6a63be9db0 100644 --- a/litellm/proxy/utils.py +++ b/litellm/proxy/utils.py @@ -8939,7 +8939,7 @@ async def get_available_models_for_user( from litellm.proxy.auth.auth_checks import customer_can_call_model - return [ # mutable-ok: callers consume the public list[str] return contract + return [ model for model in visible_models if customer_can_call_model( diff --git a/tests/e2e/access_control/access_control_client.py b/tests/e2e/access_control/access_control_client.py index 90ddf061bca..2de1a788e15 100644 --- a/tests/e2e/access_control/access_control_client.py +++ b/tests/e2e/access_control/access_control_client.py @@ -9,14 +9,20 @@ from pydantic import BaseModel, ValidationError from proxy_client import ProxyClient from e2e_metadata import step -from e2e_http import NoBody, StreamingResponse, is_ok, unwrap +from e2e_http import EndUserHeaders, NoBody, StreamingResponse, is_ok, unwrap from models import ( ChatBody, ChatMessage, + CustomerInfoParams, + CustomerNewBody, + CustomerResponse, KeyGenerateBody, LiteLLMParamsBody, ModelInfoBody, + ModelListEntry, ModelNewBody, + ModelsListParams, + ModelsListResponse, TeamDeleteBody, TeamInfoParams, TeamInfoResponse, @@ -62,9 +68,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: @@ -148,6 +152,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..9e7fd7ec9d9 --- /dev/null +++ b/tests/e2e/access_control/test_customer_model_scope_e2e.py @@ -0,0 +1,82 @@ +"""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. +""" + +from __future__ import annotations + +import pytest + +from access_control_client import AccessControlClient +from e2e_config import unique_marker +from lifecycle import ResourceManager +from e2e_metadata import Domain, Subject, meta + +pytestmark = pytest.mark.e2e + +ALLOWED_MODEL = "kimi" +OTHER_MODEL = "gpt-4o" + + +class TestCustomerModelScope: + @meta(Subject(domain=Domain.PROXY_AUTH)) + def test_restricted_customer_sees_only_allowed_models( + self, client: AccessControlClient, resources: ResourceManager + ) -> None: + """A customer with models=[ALLOWED_MODEL] lists only that model, and its + recorded allow-list matches what discovery enforces.""" + customer_id = f"e2e-cust-scope-{unique_marker()}" + client.create_customer(customer_id, models=[ALLOWED_MODEL]) + resources.defer(lambda: client.proxy.delete_customers([customer_id])) + + recorded = client.customer_models(customer_id) + assert recorded == [ALLOWED_MODEL], f"customer must record its allow-list {[ALLOWED_MODEL]!r}, got {recorded!r}" + + listed = client.models_for_customer(customer_id) + assert ALLOWED_MODEL in listed, ( + f"restricted customer must see its allowed model {ALLOWED_MODEL!r} on /v1/models, got {listed!r}" + ) + assert OTHER_MODEL not in listed, ( + f"restricted customer must not see {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 + ) -> None: + """The narrowing is observable: the restricted customer's list is strictly + shorter than an unrestricted customer's list against the same proxy.""" + marker = unique_marker() + restricted = f"e2e-cust-restricted-{marker}" + unrestricted = f"e2e-cust-unrestricted-{marker}" + client.create_customer(restricted, models=[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 = client.models_for_customer(restricted) + unrestricted_models = 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}" + ) + + @meta(Subject(domain=Domain.PROXY_AUTH)) + def test_empty_allowlist_customer_is_not_deny_all( + self, client: AccessControlClient, resources: ResourceManager + ) -> 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 = f"e2e-cust-empty-{unique_marker()}" + client.create_customer(customer_id, models=None) + resources.defer(lambda: client.proxy.delete_customers([customer_id])) + + listed = client.models_for_customer(customer_id) + assert ALLOWED_MODEL in listed and OTHER_MODEL in listed, ( + f"empty-allowlist customer must see the full set including {ALLOWED_MODEL!r} and {OTHER_MODEL!r}, " + f"got {listed!r}" + ) diff --git a/tests/e2e/e2e_http.py b/tests/e2e/e2e_http.py index 94e135535ce..3b696e028dd 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 3ae200ff5e7..8bc2c6d6c99 100644 --- a/tests/e2e/models.py +++ b/tests/e2e/models.py @@ -191,10 +191,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/proxy_server/test_routes_model_info.py b/tests/unit/proxy/proxy_server/test_routes_model_info.py index 879f3dd069d..fae4a6470cf 100644 --- a/tests/unit/proxy/proxy_server/test_routes_model_info.py +++ b/tests/unit/proxy/proxy_server/test_routes_model_info.py @@ -596,81 +596,6 @@ def test_model_info_team_key_cannot_see_other_teams_byok_model( assert _BYOK_INTERNAL_NAME not in surfaced_names -# --------------------------------------------------------------------------- -# GET /v1/model/info — customer/end-user allowlist narrowing (customer analogue of #42159) -# --------------------------------------------------------------------------- - - -@pytest.fixture -def customer_scope_router(monkeypatch): - """Router with three plain deployments so only the customer narrowing selects them.""" - model_names = ["kimi", "gpt-4o", "claude-3"] - deployments = [ - { - "model_name": name, - "litellm_params": {"model": name}, - "model_info": {"id": f"dep-{name}", "db_model": False}, - } - for name in model_names - ] - router = MagicMock() - router.model_list = deployments - router.get_model_names = MagicMock(return_value=model_names) - router.get_model_access_groups = MagicMock(return_value={}) - router.get_model_list_from_model_alias = MagicMock(return_value=[]) - router.get_model_list = MagicMock(return_value=[]) - router.model_group_alias = {} - monkeypatch.setattr(proxy_server, "llm_router", router) - monkeypatch.setattr(proxy_server, "llm_model_list", deployments) - monkeypatch.setattr(proxy_server, "user_model", None) - monkeypatch.setattr(proxy_server, "_get_proxy_model_info", lambda model: model) - yield router - - -def _patch_end_user_object(monkeypatch, models): - """Make the handler's cache-backed end-user load return a restricted (or unrestricted) customer.""" - from litellm.proxy._types import LiteLLM_EndUserTable - - async def _loader(**_kwargs): - if models is None: - return None - return LiteLLM_EndUserTable(user_id="c1", blocked=False, models=list(models)) - - monkeypatch.setattr("litellm.proxy.auth.auth_checks.get_end_user_object", _loader) - - -@pytest.mark.parametrize("path", ["/v1/model/info", "/model/info"]) -def test_v1_model_info_narrows_to_customer_allowed_models( - client, auth_as, customer_scope_router, mock_prisma, monkeypatch, path -): - """A restricted customer (models=['kimi']) sees only kimi from /v1/model/info.""" - monkeypatch.setattr(proxy_server, "prisma_client", mock_prisma) - _patch_end_user_object(monkeypatch, ["kimi"]) - - with auth_as(end_user_id="c1"): - response = client.get(path) - - assert response.status_code == 200, response.text - surfaced_names = [m.get("model_name") for m in response.json()["data"]] - assert surfaced_names == ["kimi"] - - -@pytest.mark.parametrize("path", ["/v1/model/info", "/model/info"]) -def test_v1_model_info_unrestricted_customer_sees_full_list( - client, auth_as, customer_scope_router, mock_prisma, monkeypatch, path -): - """A customer with an empty allowlist is unrestricted: the full model set is returned (regression guard).""" - monkeypatch.setattr(proxy_server, "prisma_client", mock_prisma) - _patch_end_user_object(monkeypatch, []) - - with auth_as(end_user_id="c1"): - response = client.get(path) - - assert response.status_code == 200, response.text - surfaced_names = sorted(m.get("model_name") for m in response.json()["data"]) - assert surfaced_names == ["claude-3", "gpt-4o", "kimi"] - - # --------------------------------------------------------------------------- # GET /model_group/info # --------------------------------------------------------------------------- diff --git a/tests/unit/proxy/utils/helpers/test_model_access.py b/tests/unit/proxy/utils/helpers/test_model_access.py index 2ab06b8b5f6..7f77f938323 100644 --- a/tests/unit/proxy/utils/helpers/test_model_access.py +++ b/tests/unit/proxy/utils/helpers/test_model_access.py @@ -592,116 +592,3 @@ async def test_listing_is_unchanged_without_an_agent_or_without_attached_groups( resolve_agent_ceiling=_agent_ceiling(None), ) assert (plain_key, agent_without_groups) == (["model-a", "model-b"], ["model-a", "model-b"]) - - -def _unrestricted_full_list(monkeypatch, model_names): - """Make the key/team path return the full router list so only the customer narrowing is exercised.""" - monkeypatch.setattr("litellm.proxy.auth.model_checks.get_key_models", lambda **_k: list(model_names)) - monkeypatch.setattr("litellm.proxy.auth.model_checks.get_team_models", lambda **_k: []) - monkeypatch.setattr( - "litellm.proxy.auth.model_checks.get_complete_model_list", - lambda **_k: list(model_names), - ) - - -def _customer(models): - from litellm.models.end_user import LiteLLM_EndUserTable - - return LiteLLM_EndUserTable(user_id="c1", blocked=False, models=list(models)) - - -def _patch_customer_object(monkeypatch, end_user_object): - async def _loader(**_kwargs): - return end_user_object - - monkeypatch.setattr("litellm.proxy.utils._get_customer_allowed_models", _loader) - - -def _customer_key(): - return UserAPIKeyAuth(api_key="sk-customer", user_id="user-1", end_user_id="c1", models=[], team_models=[]) - - -@pytest.mark.asyncio -async def test_get_available_models_for_user_narrows_listing_to_customer_allowed_models(monkeypatch): - model_names = ["kimi", "gpt-4o", "claude-3"] - _unrestricted_full_list(monkeypatch, model_names) - _patch_customer_object(monkeypatch, _customer(["kimi"])) - - result = await get_available_models_for_user( - user_api_key_dict=_customer_key(), - llm_router=_router_with_models(model_names), - general_settings={}, - user_model=None, - prisma_client=MagicMock(), - proxy_logging_obj=MagicMock(), - user_api_key_cache=MagicMock(), - ) - assert result == ["kimi"] - - -@pytest.mark.asyncio -async def test_get_available_models_for_user_is_unchanged_when_customer_is_unrestricted(monkeypatch): - model_names = ["kimi", "gpt-4o", "claude-3"] - _unrestricted_full_list(monkeypatch, model_names) - - # Case 1: no end-user resolved (end_user_id absent) -> full list. - _patch_customer_object(monkeypatch, None) - no_customer = await get_available_models_for_user( - user_api_key_dict=UserAPIKeyAuth(api_key="sk-plain", user_id="user-1", models=[], team_models=[]), - llm_router=_router_with_models(model_names), - general_settings={}, - user_model=None, - prisma_client=MagicMock(), - proxy_logging_obj=MagicMock(), - user_api_key_cache=MagicMock(), - ) - - # Case 2: end-user resolved but with an empty allowlist -> full list (unrestricted). - _patch_customer_object(monkeypatch, _customer([])) - empty_restriction = await get_available_models_for_user( - user_api_key_dict=_customer_key(), - llm_router=_router_with_models(model_names), - general_settings={}, - user_model=None, - prisma_client=MagicMock(), - proxy_logging_obj=MagicMock(), - user_api_key_cache=MagicMock(), - ) - - assert (no_customer, empty_restriction) == (model_names, model_names) - - -@pytest.mark.asyncio -async def test_get_available_models_for_user_customer_wildcard_expands_for_listing(monkeypatch): - model_names = ["anthropic/claude-3", "anthropic/claude-3-haiku", "gpt-4o"] - _unrestricted_full_list(monkeypatch, model_names) - _patch_customer_object(monkeypatch, _customer(["anthropic/*"])) - - result = await get_available_models_for_user( - user_api_key_dict=_customer_key(), - llm_router=_router_with_models(model_names), - general_settings={}, - user_model=None, - prisma_client=MagicMock(), - proxy_logging_obj=MagicMock(), - user_api_key_cache=MagicMock(), - ) - assert result == ["anthropic/claude-3", "anthropic/claude-3-haiku"] - - -@pytest.mark.asyncio -async def test_get_available_models_for_user_customer_sentinel_all_proxy_models_passthrough(monkeypatch): - model_names = ["kimi", "gpt-4o", "claude-3"] - _unrestricted_full_list(monkeypatch, model_names) - _patch_customer_object(monkeypatch, _customer(["all-proxy-models"])) - - result = await get_available_models_for_user( - user_api_key_dict=_customer_key(), - llm_router=_router_with_models(model_names), - general_settings={}, - user_model=None, - prisma_client=MagicMock(), - proxy_logging_obj=MagicMock(), - user_api_key_cache=MagicMock(), - ) - assert result == model_names From f3a81114011dcef9a0333f294234e58865c1a4f7 Mon Sep 17 00:00:00 2001 From: Dhrumil Shah Date: Fri, 9 Oct 2026 17:06:58 +0000 Subject: [PATCH 3/6] test(e2e): create customer-scope deployments in-test via /model/new The customer model-scope e2e referenced kimi/gpt-4o as module constants, but the harness proxy serves neither, so the restricted/unrestricted /v1/models assertions could not hold against a booted proxy. Each case now registers the deployments it names via register_model (polled to servable) under a per-run unique marker and tears them down in a fixture, mirroring the access-group suite, and sets the restricted customer's allow-list to the exact deployment name so narrowing is deterministic. --- .../test_customer_model_scope_e2e.py | 103 +++++++++++++----- 1 file changed, 76 insertions(+), 27 deletions(-) diff --git a/tests/e2e/access_control/test_customer_model_scope_e2e.py b/tests/e2e/access_control/test_customer_model_scope_e2e.py index 9e7fd7ec9d9..0ff5da7d328 100644 --- a/tests/e2e/access_control/test_customer_model_scope_e2e.py +++ b/tests/e2e/access_control/test_customer_model_scope_e2e.py @@ -6,77 +6,126 @@ 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 collections.abc import Iterator +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 -ALLOWED_MODEL = "kimi" -OTHER_MODEL = "gpt-4o" +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(), + ) + + +@pytest.fixture +def deployments(client: AccessControlClient) -> Iterator[ScopedDeployments]: + marker: Final = unique_marker() + scoped: Final = ScopedDeployments( + allowed_model=f"e2e-custmodel-allowed-{marker}", + other_model=f"e2e-custmodel-other-{marker}", + ) + created: Final = tuple( + client.proxy.register_model(_deployment(name)) for name in (scoped.allowed_model, scoped.other_model) + ) + try: + yield scoped + finally: + for model_id in created: + client.proxy.delete_model(model_id) class TestCustomerModelScope: @meta(Subject(domain=Domain.PROXY_AUTH)) def test_restricted_customer_sees_only_allowed_models( - self, client: AccessControlClient, resources: ResourceManager + self, client: AccessControlClient, resources: ResourceManager, deployments: ScopedDeployments ) -> None: - """A customer with models=[ALLOWED_MODEL] lists only that model, and its + """A customer with models=[allowed_model] lists only that model, and its recorded allow-list matches what discovery enforces.""" - customer_id = f"e2e-cust-scope-{unique_marker()}" - client.create_customer(customer_id, models=[ALLOWED_MODEL]) + 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 = client.customer_models(customer_id) - assert recorded == [ALLOWED_MODEL], f"customer must record its allow-list {[ALLOWED_MODEL]!r}, got {recorded!r}" - - listed = client.models_for_customer(customer_id) - assert ALLOWED_MODEL in listed, ( - f"restricted customer must see its allowed model {ALLOWED_MODEL!r} on /v1/models, got {listed!r}" + 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}" ) - assert OTHER_MODEL not in listed, ( - f"restricted customer must not see {OTHER_MODEL!r} it cannot call, got {listed!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 + 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 = unique_marker() - restricted = f"e2e-cust-restricted-{marker}" - unrestricted = f"e2e-cust-unrestricted-{marker}" - client.create_customer(restricted, models=[ALLOWED_MODEL]) + 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 = client.models_for_customer(restricted) - unrestricted_models = client.models_for_customer(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 + 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 = f"e2e-cust-empty-{unique_marker()}" + 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 = client.models_for_customer(customer_id) - assert ALLOWED_MODEL in listed and OTHER_MODEL in listed, ( - f"empty-allowlist customer must see the full set including {ALLOWED_MODEL!r} and {OTHER_MODEL!r}, " - f"got {listed!r}" + 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}" ) From 9a05fef0f7d0e1525a96fe72fc140e29d4d5dca1 Mon Sep 17 00:00:00 2001 From: Dhrumil Shah Date: Fri, 9 Oct 2026 17:25:23 +0000 Subject: [PATCH 4/6] test(e2e): defer each customer-scope deployment cleanup at creation --- .../test_customer_model_scope_e2e.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/tests/e2e/access_control/test_customer_model_scope_e2e.py b/tests/e2e/access_control/test_customer_model_scope_e2e.py index 0ff5da7d328..8b21973c012 100644 --- a/tests/e2e/access_control/test_customer_model_scope_e2e.py +++ b/tests/e2e/access_control/test_customer_model_scope_e2e.py @@ -15,7 +15,6 @@ create-and-clean pattern. from __future__ import annotations -from collections.abc import Iterator from dataclasses import dataclass from typing import Final @@ -49,20 +48,16 @@ def _deployment(model_name: str) -> ModelNewBody: @pytest.fixture -def deployments(client: AccessControlClient) -> Iterator[ScopedDeployments]: +def deployments(client: AccessControlClient, resources: ResourceManager) -> ScopedDeployments: marker: Final = unique_marker() scoped: Final = ScopedDeployments( allowed_model=f"e2e-custmodel-allowed-{marker}", other_model=f"e2e-custmodel-other-{marker}", ) - created: Final = tuple( - client.proxy.register_model(_deployment(name)) for name in (scoped.allowed_model, scoped.other_model) - ) - try: - yield scoped - finally: - for model_id in created: - client.proxy.delete_model(model_id) + for name in (scoped.allowed_model, scoped.other_model): + model_id: Final = client.proxy.register_model(_deployment(name)) + resources.defer(lambda mid=model_id: client.proxy.delete_model(mid)) + return scoped class TestCustomerModelScope: From 43452350391c8185d7158589f758cf3d9437a14f Mon Sep 17 00:00:00 2001 From: Dhrumil Shah Date: Fri, 9 Oct 2026 17:36:17 +0000 Subject: [PATCH 5/6] test(e2e): register deployments via helper to avoid Final-in-loop --- .../test_customer_model_scope_e2e.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/tests/e2e/access_control/test_customer_model_scope_e2e.py b/tests/e2e/access_control/test_customer_model_scope_e2e.py index 8b21973c012..dec712b84f0 100644 --- a/tests/e2e/access_control/test_customer_model_scope_e2e.py +++ b/tests/e2e/access_control/test_customer_model_scope_e2e.py @@ -47,17 +47,19 @@ def _deployment(model_name: str) -> ModelNewBody: ) +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() - scoped: Final = ScopedDeployments( - allowed_model=f"e2e-custmodel-allowed-{marker}", - other_model=f"e2e-custmodel-other-{marker}", + return ScopedDeployments( + allowed_model=_register(client, resources, f"e2e-custmodel-allowed-{marker}"), + other_model=_register(client, resources, f"e2e-custmodel-other-{marker}"), ) - for name in (scoped.allowed_model, scoped.other_model): - model_id: Final = client.proxy.register_model(_deployment(name)) - resources.defer(lambda mid=model_id: client.proxy.delete_model(mid)) - return scoped class TestCustomerModelScope: From fcab21b77556b6124b2c2207fa7353122a173cde Mon Sep 17 00:00:00 2001 From: Dhrumil Shah Date: Fri, 9 Oct 2026 19:08:46 +0000 Subject: [PATCH 6/6] test(proxy): cover customer model-scope narrowing for v1/models --- ...st_auth_checks_object_access_and_lookup.py | 110 ++++++++++++++++++ .../proxy/utils/helpers/test_model_access.py | 57 +++++++++ 2 files changed, 167 insertions(+) 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 e26845e5b2a..c2f555ec1cc 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 @@ -11128,3 +11128,113 @@ async def test_authoritative_group_grants_propagate_policy_outages( await get_agent_ids_from_access_groups(["group"], check_db_only=True) else: assert await get_agent_ids_from_access_groups(["group"]) == [] + + +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 + ) + + +@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 []) + + assert ( + customer_can_call_model( + model="any-model", + end_user_object=end_user_object, + llm_router=None, + valid_token=None, + ) + is True + ) + + +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 + ) + 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 + ) + + +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 7f77f938323..077d8f9e0b3 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 @@ -519,6 +520,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