From 00d9672ff35a665f704760aae7fe397205874cec Mon Sep 17 00:00:00 2001 From: Avani-prajapati Date: Wed, 21 Oct 2026 17:18:38 +0530 Subject: [PATCH 1/7] fix(proxy)!: restrict keys with empty models to their access groups A key with models: [] and access_group_ids was treated as unrestricted, because the empty list passed the native check before the groups were read. Such a key is now checked against its groups' models only, and is denied when the groups grant no models. The denial is the same key model-access denial a natively restricted key gets BREAKING CHANGE: keys with an empty models list and access_group_ids are now limited to the models of those access groups instead of every deployed model --- litellm/proxy/auth/auth_checks.py | 52 ++++++++- tests/unit/proxy/auth/test_auth_checks.py | 130 ++++++++++++++++++++++ 2 files changed, 178 insertions(+), 4 deletions(-) diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.py index 8887d86570a..5d258e3fff2 100644 --- a/litellm/proxy/auth/auth_checks.py +++ b/litellm/proxy/auth/auth_checks.py @@ -4970,8 +4970,16 @@ def _can_object_call_model( ): return True + _raise_model_access_denied(model=model, models=models, object_type=object_type) + + +def _raise_model_access_denied( + model: str, + models: Sequence[str], + object_type: Literal["user", "customer", "team", "key", "org", "project", "agent"], +) -> NoReturn: internal_message: Final = ( - f"{object_type} not allowed to access model. This {object_type} can only access models={models}. " + f"{object_type} not allowed to access model. This {object_type} can only access models={list(models)}. " f"Tried to access {model}" ) raise ModelAccessDeniedProxyException( @@ -5186,12 +5194,16 @@ def _resolve_key_models_for_auth_check(valid_token: UserAPIKeyAuth) -> list[str] If the key has no team_id, it inherits the full proxy model list (equivalent to an empty models field, i.e. unrestricted access). """ - models: Final = list(valid_token.models or []) + models: Final = _configured_key_models(valid_token) if SpecialModelNames.all_team_models.value in models: if valid_token.team_id is None: return [] return list(valid_token.team_models or []) - return models + return list(models) + + +def _configured_key_models(valid_token: UserAPIKeyAuth) -> tuple[str, ...]: + return tuple(valid_token.models or ()) def _model_access_groups_serving_model( @@ -5468,6 +5480,21 @@ async def stamp_matched_model_access_groups( return matched +async def _models_granted_by_groups_only( + model: str | Sequence[str], + access_group_ids: Sequence[str], + prisma_client: DatabaseClient | None, + object_type: Literal["key", "team"], +) -> tuple[str, ...]: + models_from_groups: Final = await get_models_from_access_groups( + access_group_ids=access_group_ids, + prisma_client=prisma_client, + ) + if models_from_groups: + return tuple(models_from_groups) + _raise_model_access_denied(model=str(model), models=(), object_type=object_type) + + async def can_key_call_model( model: str | list[str], llm_model_list: Sequence[object] | None, @@ -5487,6 +5514,24 @@ async def can_key_call_model( Raises: - Exception: If token not allowed to call model """ + key_access_group_ids: Final = valid_token.access_group_ids or [] + if not _configured_key_models(valid_token) and key_access_group_ids: + return can_object_call_model( + model=model, + llm_router=llm_router, + models=list( + await _models_granted_by_groups_only( + model=model, + access_group_ids=key_access_group_ids, + prisma_client=prisma_client, + object_type="key", + ) + ), + team_model_aliases=valid_token.team_model_aliases, + team_id=valid_token.team_id, + key_model_aliases=key_model_aliases_for_auth_check(valid_token), + object_type="key", + ) key_models: Final = _resolve_key_models_for_auth_check(valid_token=valid_token) try: return can_object_call_model( @@ -5500,7 +5545,6 @@ async def can_key_call_model( ) except ProxyException: # Fallback: check key's access_group_ids - key_access_group_ids: Final = valid_token.access_group_ids or [] if key_access_group_ids: models_from_groups: Final = await get_models_from_access_groups( access_group_ids=key_access_group_ids, diff --git a/tests/unit/proxy/auth/test_auth_checks.py b/tests/unit/proxy/auth/test_auth_checks.py index e1afa81d372..d77977254a3 100644 --- a/tests/unit/proxy/auth/test_auth_checks.py +++ b/tests/unit/proxy/auth/test_auth_checks.py @@ -33,6 +33,7 @@ from litellm.proxy._types import ( LiteLLM_TeamTable, Litellm_EntityType, LitellmUserRoles, + ProxyErrorTypes, ProxyException, ) from litellm.proxy.utils import PrismaClient @@ -1604,6 +1605,135 @@ async def test_can_key_call_model_via_access_group_ids(): ) +def _router_deploying(*model_names: str) -> litellm.Router: + return litellm.Router( + model_list=[ + {"model_name": name, "litellm_params": {"model": f"openai/{name}", "api_key": "test"}} + for name in model_names + ] + ) + + +async def _key_denial( + model: str, key: UserAPIKeyAuth, router: litellm.Router, group_models: list[str] +) -> ProxyException: + from litellm.proxy.auth.auth_checks import can_key_call_model + + with patch( + "litellm.proxy.auth.auth_checks.get_models_from_access_groups", + new_callable=AsyncMock, + return_value=group_models, + ): + with pytest.raises(ProxyException) as denial: + await can_key_call_model(model=model, llm_model_list=[], valid_token=key, llm_router=router) + return denial.value + + +@pytest.mark.asyncio +async def test_key_with_empty_models_and_group_is_allowed_only_group_models(): + from litellm.proxy.auth.auth_checks import can_key_call_model + + key = UserAPIKeyAuth(token="test-token", models=[], access_group_ids=["ag-gpt4"]) + with patch( + "litellm.proxy.auth.auth_checks.get_models_from_access_groups", + new_callable=AsyncMock, + return_value=["gpt-4"], + ): + result = await can_key_call_model( + model="gpt-4", + llm_model_list=[], + valid_token=key, + llm_router=_router_deploying("gpt-4", "claude-sonnet"), + ) + assert result is True + + +@pytest.mark.asyncio +async def test_key_with_empty_models_and_group_is_denied_deployed_model_outside_group_like_native_key(): + router = _router_deploying("gpt-4", "claude-sonnet") + group_restricted_key = UserAPIKeyAuth(token="test-token", models=[], access_group_ids=["ag-gpt4"]) + natively_restricted_key = UserAPIKeyAuth(token="test-token", models=["gpt-4"]) + + group_denial = await _key_denial("claude-sonnet", group_restricted_key, router, ["gpt-4"]) + native_denial = await _key_denial("claude-sonnet", natively_restricted_key, router, []) + + assert (group_denial.type, group_denial.code, group_denial.message) == ( + native_denial.type, + "403", + native_denial.message, + ) + assert group_denial.type == ProxyErrorTypes.key_model_access_denied + + +@pytest.mark.asyncio +@pytest.mark.parametrize("model", ["gpt-4", "claude-sonnet"]) +async def test_key_with_group_resolving_to_no_models_is_denied_every_model(model): + key = UserAPIKeyAuth(token="test-token", models=[], access_group_ids=["ag-empty"]) + + denial = await _key_denial(model, key, _router_deploying("gpt-4", "claude-sonnet"), []) + + assert (denial.type, denial.code) == (ProxyErrorTypes.key_model_access_denied, "403") + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "key_models, access_group_ids, model", + [ + (["gpt-4"], ["ag-claude"], "gpt-4"), + ([], [], "claude-sonnet"), + (["*"], ["ag-claude"], "gpt-3.5-turbo"), + (["all-proxy-models"], ["ag-claude"], "gpt-3.5-turbo"), + ], +) +async def test_key_allowed_natively_is_allowed_without_group_lookup(key_models, access_group_ids, model): + from litellm.proxy.auth.auth_checks import can_key_call_model + + key = UserAPIKeyAuth(token="test-token", models=key_models, access_group_ids=access_group_ids) + with patch( + "litellm.proxy.auth.auth_checks.get_models_from_access_groups", + new_callable=AsyncMock, + return_value=["claude-sonnet"], + ) as group_lookup: + result = await can_key_call_model( + model=model, + llm_model_list=[], + valid_token=key, + llm_router=_router_deploying("gpt-4", "claude-sonnet", "gpt-3.5-turbo"), + ) + assert result is True + assert group_lookup.await_count == 0, "a natively decided key must not pay for a group lookup" + + +@pytest.mark.asyncio +async def test_key_with_native_models_and_group_is_allowed_group_model(): + from litellm.proxy.auth.auth_checks import can_key_call_model + + key = UserAPIKeyAuth(token="test-token", models=["gpt-4"], access_group_ids=["ag-claude"]) + with patch( + "litellm.proxy.auth.auth_checks.get_models_from_access_groups", + new_callable=AsyncMock, + return_value=["claude-sonnet"], + ): + result = await can_key_call_model( + model="claude-sonnet", + llm_model_list=[], + valid_token=key, + llm_router=_router_deploying("gpt-4", "claude-sonnet", "gpt-3.5-turbo"), + ) + assert result is True + + +@pytest.mark.asyncio +async def test_key_with_native_models_and_group_is_denied_model_in_neither(): + key = UserAPIKeyAuth(token="test-token", models=["gpt-4"], access_group_ids=["ag-claude"]) + + denial = await _key_denial( + "gpt-3.5-turbo", key, _router_deploying("gpt-4", "claude-sonnet", "gpt-3.5-turbo"), ["claude-sonnet"] + ) + + assert (denial.type, denial.code) == (ProxyErrorTypes.key_model_access_denied, "403") + + # --------------------------------------------------------------------------- # _key_access_group_grants_model (key access group overriding team restriction) # --------------------------------------------------------------------------- From ffbd4b45bfcf9a6f8ee1702ee7bc813fa18d7897 Mon Sep 17 00:00:00 2001 From: Avani-prajapati Date: Wed, 21 Oct 2026 17:20:43 +0530 Subject: [PATCH 2/7] fix(proxy)!: restrict teams with empty models to their access groups A team with models: [] and access_group_ids was treated as unrestricted, because the empty list passed the native check before the groups were read. Such a team is now checked against its groups' models only, and is denied when the groups grant no models. The denial keeps the team model-access denial type. BREAKING CHANGE: teams with an empty models list and access_group_ids are now limited to the models of those access groups instead of every deployed model --- litellm/proxy/auth/auth_checks.py | 17 ++++ tests/unit/proxy/auth/test_auth_checks.py | 116 ++++++++++++++++++++++ 2 files changed, 133 insertions(+) diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.py index 5d258e3fff2..1f177cd41b3 100644 --- a/litellm/proxy/auth/auth_checks.py +++ b/litellm/proxy/auth/auth_checks.py @@ -5754,6 +5754,23 @@ async def can_team_access_model( 1. First checks native team-level model permissions (current implementation) 2. If not allowed natively, falls back to access_group_ids on the team """ + if team_object is not None and not team_object.models and team_object.access_group_ids: + return can_object_call_model( + model=model, + llm_router=llm_router, + models=list( + await _models_granted_by_groups_only( + model=model, + access_group_ids=team_object.access_group_ids, + prisma_client=prisma_client, + object_type="team", + ) + ), + team_model_aliases=team_model_aliases, + team_id=team_object.team_id, + key_model_aliases=key_model_aliases, + object_type="team", + ) try: return can_object_call_model( model=model, diff --git a/tests/unit/proxy/auth/test_auth_checks.py b/tests/unit/proxy/auth/test_auth_checks.py index d77977254a3..0f49945a422 100644 --- a/tests/unit/proxy/auth/test_auth_checks.py +++ b/tests/unit/proxy/auth/test_auth_checks.py @@ -1566,6 +1566,122 @@ async def test_can_team_access_model_access_group_ids_denied(): ) +async def _team_denial( + model: str, team: LiteLLM_TeamTable, router: litellm.Router, group_models: list[str] +) -> ProxyException: + from litellm.proxy.auth.auth_checks import can_team_access_model + + with patch( + "litellm.proxy.auth.auth_checks.get_models_from_access_groups", + new_callable=AsyncMock, + return_value=group_models, + ): + with pytest.raises(ProxyException) as denial: + await can_team_access_model(model=model, team_object=team, llm_router=router) + return denial.value + + +@pytest.mark.asyncio +async def test_team_with_empty_models_and_group_is_allowed_only_group_models(): + from litellm.proxy.auth.auth_checks import can_team_access_model + + team = LiteLLM_TeamTable(team_id="test-team", models=[], access_group_ids=["ag-gpt4"]) + with patch( + "litellm.proxy.auth.auth_checks.get_models_from_access_groups", + new_callable=AsyncMock, + return_value=["gpt-4"], + ): + result = await can_team_access_model( + model="gpt-4", team_object=team, llm_router=_router_deploying("gpt-4", "claude-sonnet") + ) + assert result is True + + +@pytest.mark.asyncio +async def test_team_with_empty_models_and_group_is_denied_deployed_model_outside_group_like_native_team(): + router = _router_deploying("gpt-4", "claude-sonnet") + group_restricted_team = LiteLLM_TeamTable(team_id="test-team", models=[], access_group_ids=["ag-gpt4"]) + natively_restricted_team = LiteLLM_TeamTable(team_id="test-team", models=["gpt-4"]) + + group_denial = await _team_denial("claude-sonnet", group_restricted_team, router, ["gpt-4"]) + native_denial = await _team_denial("claude-sonnet", natively_restricted_team, router, []) + + assert (group_denial.type, group_denial.code, group_denial.message) == ( + native_denial.type, + "403", + native_denial.message, + ) + assert group_denial.type == ProxyErrorTypes.team_model_access_denied + + +@pytest.mark.asyncio +@pytest.mark.parametrize("model", ["gpt-4", "claude-sonnet"]) +async def test_team_with_group_resolving_to_no_models_is_denied_every_model(model): + team = LiteLLM_TeamTable(team_id="test-team", models=[], access_group_ids=["ag-empty"]) + + denial = await _team_denial(model, team, _router_deploying("gpt-4", "claude-sonnet"), []) + + assert (denial.type, denial.code) == (ProxyErrorTypes.team_model_access_denied, "403") + + +@pytest.mark.asyncio +async def test_team_with_native_models_and_group_is_allowed_union_of_both(): + from litellm.proxy.auth.auth_checks import can_team_access_model + + team = LiteLLM_TeamTable(team_id="test-team", models=["gpt-4"], access_group_ids=["ag-claude"]) + router = _router_deploying("gpt-4", "claude-sonnet", "gpt-3.5-turbo") + with patch( + "litellm.proxy.auth.auth_checks.get_models_from_access_groups", + new_callable=AsyncMock, + return_value=["claude-sonnet"], + ): + native = await can_team_access_model(model="gpt-4", team_object=team, llm_router=router) + via_group = await can_team_access_model(model="claude-sonnet", team_object=team, llm_router=router) + assert (native, via_group) == (True, True) + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "team", + [LiteLLM_TeamTable(team_id="test-team", models=[]), None], +) +async def test_team_without_models_or_groups_is_unrestricted(team): + from litellm.proxy.auth.auth_checks import can_team_access_model + + with patch( + "litellm.proxy.auth.auth_checks.get_models_from_access_groups", + new_callable=AsyncMock, + return_value=[], + ) as group_lookup: + result = await can_team_access_model( + model="claude-sonnet", team_object=team, llm_router=_router_deploying("gpt-4", "claude-sonnet") + ) + assert result is True + assert group_lookup.await_count == 0 + + +@pytest.mark.asyncio +async def test_key_access_group_still_grants_model_group_restricted_team_denies(): + from litellm.proxy.auth.auth_checks import key_access_group_grants_model + + router = _router_deploying("gpt-4", "claude-sonnet") + team = LiteLLM_TeamTable(team_id="team-a", models=[], access_group_ids=["ag-team"]) + key = UserAPIKeyAuth(token="test-token", models=[], access_group_ids=["ag-key"], team_id="team-a") + + denial = await _team_denial("claude-sonnet", team, router, ["gpt-4"]) + with patch( + "litellm.proxy.auth.auth_checks.get_authorized_resources_from_key_access_groups", + new_callable=AsyncMock, + return_value=["claude-sonnet"], + ): + key_group_grants = await key_access_group_grants_model( + model="claude-sonnet", valid_token=key, team_object=team, llm_router=router + ) + + assert denial.type == ProxyErrorTypes.team_model_access_denied + assert key_group_grants is True + + # --------------------------------------------------------------------------- # can_key_call_model with access_group_ids fallback # --------------------------------------------------------------------------- From 16ac63c479954cc4a4e24eea5df0e133d68a0453 Mon Sep 17 00:00:00 2001 From: Avani-prajapati Date: Wed, 21 Oct 2026 17:23:36 +0530 Subject: [PATCH 3/7] fix(proxy)!: fail closed when access groups cannot be read A key or team restricted only by access groups skipped any group that was deleted or could not be loaded, so a failed lookup could widen access. The group-restricted path now reads groups in strict mode and denies with the usual model-access denial on any lookup failure. The native-first fallback stays non-strict. BREAKING CHANGE: a key or team restricted only by access groups is now denied when its groups cannot be loaded --- litellm/proxy/auth/auth_checks.py | 15 ++- tests/unit/proxy/auth/test_auth_checks.py | 124 ++++++++++++++++++++++ 2 files changed, 135 insertions(+), 4 deletions(-) diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.py index 1f177cd41b3..4cd8ed9a2f5 100644 --- a/litellm/proxy/auth/auth_checks.py +++ b/litellm/proxy/auth/auth_checks.py @@ -4702,6 +4702,8 @@ async def get_models_from_access_groups( prisma_client: DatabaseClient | None = None, user_api_key_cache: UserApiKeyCache | None = None, proxy_logging_obj: ProxyLogging | None = None, + *, + strict_errors: bool = False, ) -> list[str]: """ Collect model names from unified access groups. @@ -4713,6 +4715,7 @@ async def get_models_from_access_groups( prisma_client=prisma_client, user_api_key_cache=user_api_key_cache, proxy_logging_obj=proxy_logging_obj, + strict_errors=strict_errors, ) @@ -5486,10 +5489,14 @@ async def _models_granted_by_groups_only( prisma_client: DatabaseClient | None, object_type: Literal["key", "team"], ) -> tuple[str, ...]: - models_from_groups: Final = await get_models_from_access_groups( - access_group_ids=access_group_ids, - prisma_client=prisma_client, - ) + try: + models_from_groups: Final = await get_models_from_access_groups( + access_group_ids=access_group_ids, + prisma_client=prisma_client, + strict_errors=True, + ) + except Exception: # noqa: BLE001 # fail-closed: a group that cannot be loaded must deny, never widen access + _raise_model_access_denied(model=str(model), models=(), object_type=object_type) if models_from_groups: return tuple(models_from_groups) _raise_model_access_denied(model=str(model), models=(), object_type=object_type) diff --git a/tests/unit/proxy/auth/test_auth_checks.py b/tests/unit/proxy/auth/test_auth_checks.py index 0f49945a422..586e945a358 100644 --- a/tests/unit/proxy/auth/test_auth_checks.py +++ b/tests/unit/proxy/auth/test_auth_checks.py @@ -2167,6 +2167,130 @@ async def test_key_access_group_grants_model_when_get_access_object_raises(): p.stop() +def _group_lookup_outcomes() -> list: + from litellm.proxy.auth.auth_checks import AccessGroupNotFoundError + + return [ + pytest.param({"side_effect": AccessGroupNotFoundError("ag-1")}, id="group-deleted"), + pytest.param({"side_effect": RuntimeError("database unreachable")}, id="lookup-fails"), + pytest.param({"return_value": _fake_access_group("ag-1", access_model_names=[])}, id="group-grants-nothing"), + ] + + +@pytest.mark.asyncio +@pytest.mark.parametrize("lookup", _group_lookup_outcomes()) +@pytest.mark.parametrize("model", ["gpt-4", "claude-sonnet"]) +async def test_group_restricted_key_is_denied_when_groups_cannot_be_loaded(lookup, model): + from litellm.proxy.auth.auth_checks import can_key_call_model + + key = UserAPIKeyAuth(token="test-token", models=[], access_group_ids=["ag-1"]) + patches = _patch_proxy_server_globals() + [ + patch("litellm.proxy.auth.auth_checks.get_access_object", new_callable=AsyncMock, **lookup) + ] + for p in patches: + p.start() + try: + with pytest.raises(ProxyException) as denial: + await can_key_call_model( + model=model, + llm_model_list=[], + valid_token=key, + llm_router=_router_deploying("gpt-4", "claude-sonnet"), + ) + finally: + for p in patches: + p.stop() + + assert (denial.value.type, denial.value.code) == (ProxyErrorTypes.key_model_access_denied, "403") + + +@pytest.mark.asyncio +@pytest.mark.parametrize("lookup", _group_lookup_outcomes()) +@pytest.mark.parametrize("model", ["gpt-4", "claude-sonnet"]) +async def test_group_restricted_team_is_denied_when_groups_cannot_be_loaded(lookup, model): + team = LiteLLM_TeamTable(team_id="test-team", models=[], access_group_ids=["ag-1"]) + patches = _patch_proxy_server_globals() + [ + patch("litellm.proxy.auth.auth_checks.get_access_object", new_callable=AsyncMock, **lookup) + ] + for p in patches: + p.start() + try: + with pytest.raises(ProxyException) as denial: + await can_team_access_model( + model=model, team_object=team, llm_router=_router_deploying("gpt-4", "claude-sonnet") + ) + finally: + for p in patches: + p.stop() + + assert (denial.value.type, denial.value.code) == (ProxyErrorTypes.team_model_access_denied, "403") + + +@pytest.mark.asyncio +@pytest.mark.parametrize("failure", [RuntimeError("database unreachable"), "group-deleted"]) +@pytest.mark.parametrize("object_type", ["key", "team"]) +async def test_group_restricted_object_is_denied_when_one_of_its_groups_cannot_be_loaded(failure, object_type): + from litellm.proxy.auth.auth_checks import AccessGroupNotFoundError, can_key_call_model + + async def lookup(access_group_id: str, **_: object): + if access_group_id == "ag-ok": + return _fake_access_group("ag-ok", access_model_names=["gpt-4"]) + raise AccessGroupNotFoundError(access_group_id) if failure == "group-deleted" else failure + + router = _router_deploying("gpt-4", "claude-sonnet") + patches = _patch_proxy_server_globals() + [ + patch("litellm.proxy.auth.auth_checks.get_access_object", side_effect=lookup) + ] + for p in patches: + p.start() + try: + with pytest.raises(ProxyException) as denial: + if object_type == "key": + key = UserAPIKeyAuth(token="test-token", models=[], access_group_ids=["ag-ok", "ag-1"]) + await can_key_call_model(model="gpt-4", llm_model_list=[], valid_token=key, llm_router=router) + else: + team = LiteLLM_TeamTable(team_id="test-team", models=[], access_group_ids=["ag-ok", "ag-1"]) + await can_team_access_model(model="gpt-4", team_object=team, llm_router=router) + finally: + for p in patches: + p.stop() + + expected_type = { + "key": ProxyErrorTypes.key_model_access_denied, + "team": ProxyErrorTypes.team_model_access_denied, + }[object_type] + assert (denial.value.type, denial.value.code) == (expected_type, "403") + + +@pytest.mark.asyncio +async def test_natively_restricted_key_whose_group_fallback_fails_gets_the_native_denial(): + from litellm.proxy.auth.auth_checks import can_key_call_model + + key = UserAPIKeyAuth(token="test-token", models=["gpt-4"], access_group_ids=["ag-1"]) + patches = _patch_proxy_server_globals() + [ + patch( + "litellm.proxy.auth.auth_checks.get_access_object", + new_callable=AsyncMock, + side_effect=RuntimeError("database unreachable"), + ) + ] + for p in patches: + p.start() + try: + with pytest.raises(ProxyException) as denial: + await can_key_call_model( + model="claude-sonnet", + llm_model_list=[], + valid_token=key, + llm_router=_router_deploying("gpt-4", "claude-sonnet"), + ) + finally: + for p in patches: + p.stop() + + assert (denial.value.type, denial.value.code) == (ProxyErrorTypes.key_model_access_denied, "403") + + @pytest.fixture def db_success_hook() -> Iterator[AsyncMock]: hook: Final = AsyncMock() From 10a7daccabb0a5bb635fe4eb3092ad7b56bf2200 Mon Sep 17 00:00:00 2001 From: Avani-prajapati Date: Wed, 21 Oct 2026 17:32:45 +0530 Subject: [PATCH 4/7] refactor(proxy): share the group-only model check and log group load failures --- litellm/proxy/auth/auth_checks.py | 75 +++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 24 deletions(-) diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.py index 4cd8ed9a2f5..c82f0bea63c 100644 --- a/litellm/proxy/auth/auth_checks.py +++ b/litellm/proxy/auth/auth_checks.py @@ -4980,11 +4980,11 @@ def _raise_model_access_denied( model: str, models: Sequence[str], object_type: Literal["user", "customer", "team", "key", "org", "project", "agent"], + *, + cause: str | None = None, ) -> NoReturn: - internal_message: Final = ( - f"{object_type} not allowed to access model. This {object_type} can only access models={list(models)}. " - f"Tried to access {model}" - ) + detail: Final = f"This {object_type} can only access models={list(models)}." if cause is None else cause + internal_message: Final = f"{object_type} not allowed to access model. {detail} Tried to access {model}" raise ModelAccessDeniedProxyException( message=( customer_model_access_denied_client_message(model=model) @@ -5495,13 +5495,52 @@ async def _models_granted_by_groups_only( prisma_client=prisma_client, strict_errors=True, ) - except Exception: # noqa: BLE001 # fail-closed: a group that cannot be loaded must deny, never widen access - _raise_model_access_denied(model=str(model), models=(), object_type=object_type) + except Exception as e: # noqa: BLE001 # fail-closed: a group that cannot be loaded must deny, never widen access + verbose_proxy_logger.warning( + "%s model access denied, access groups could not be loaded. model=%s access_group_ids=%s error=%s", + object_type, + model, + list(access_group_ids), + e, + ) + _raise_model_access_denied( + model=str(model), + models=(), + object_type=object_type, + cause="Its access groups could not be loaded.", + ) if models_from_groups: return tuple(models_from_groups) _raise_model_access_denied(model=str(model), models=(), object_type=object_type) +async def _can_object_call_model_via_groups_only( + model: str | list[str], # mutable-ok: forwarded unchanged to can_object_call_model + access_group_ids: Sequence[str], + prisma_client: DatabaseClient | None, + llm_router: Router | None, + team_model_aliases: Mapping[str, str] | None, + team_id: str | None, + key_model_aliases: Mapping[str, str] | None, + object_type: Literal["key", "team"], +) -> Literal[True]: + granted_models: Final = await _models_granted_by_groups_only( + model=model, + access_group_ids=access_group_ids, + prisma_client=prisma_client, + object_type=object_type, + ) + return can_object_call_model( + model=model, + llm_router=llm_router, + models=list(granted_models), + team_model_aliases=team_model_aliases, + team_id=team_id, + key_model_aliases=key_model_aliases, + object_type=object_type, + ) + + async def can_key_call_model( model: str | list[str], llm_model_list: Sequence[object] | None, @@ -5523,17 +5562,11 @@ async def can_key_call_model( """ key_access_group_ids: Final = valid_token.access_group_ids or [] if not _configured_key_models(valid_token) and key_access_group_ids: - return can_object_call_model( + return await _can_object_call_model_via_groups_only( model=model, + access_group_ids=key_access_group_ids, + prisma_client=prisma_client, llm_router=llm_router, - models=list( - await _models_granted_by_groups_only( - model=model, - access_group_ids=key_access_group_ids, - prisma_client=prisma_client, - object_type="key", - ) - ), team_model_aliases=valid_token.team_model_aliases, team_id=valid_token.team_id, key_model_aliases=key_model_aliases_for_auth_check(valid_token), @@ -5762,17 +5795,11 @@ async def can_team_access_model( 2. If not allowed natively, falls back to access_group_ids on the team """ if team_object is not None and not team_object.models and team_object.access_group_ids: - return can_object_call_model( + return await _can_object_call_model_via_groups_only( model=model, + access_group_ids=team_object.access_group_ids, + prisma_client=prisma_client, llm_router=llm_router, - models=list( - await _models_granted_by_groups_only( - model=model, - access_group_ids=team_object.access_group_ids, - prisma_client=prisma_client, - object_type="team", - ) - ), team_model_aliases=team_model_aliases, team_id=team_object.team_id, key_model_aliases=key_model_aliases, From fd87e52b701ab7417e751f8817a7b3440ae3454e Mon Sep 17 00:00:00 2001 From: Avani-prajapati Date: Wed, 21 Oct 2026 17:32:45 +0530 Subject: [PATCH 5/7] test(proxy): cover group-only model access through a fake group store --- tests/unit/proxy/auth/test_auth_checks.py | 557 ++++++++-------------- 1 file changed, 200 insertions(+), 357 deletions(-) diff --git a/tests/unit/proxy/auth/test_auth_checks.py b/tests/unit/proxy/auth/test_auth_checks.py index 586e945a358..fb9fddbfae8 100644 --- a/tests/unit/proxy/auth/test_auth_checks.py +++ b/tests/unit/proxy/auth/test_auth_checks.py @@ -12,7 +12,7 @@ load_dotenv() from collections.abc import Awaitable, Callable, Iterator, Mapping, Sequence from copy import deepcopy from types import SimpleNamespace -from typing import Final +from typing import Final, Literal from unittest.mock import AsyncMock, MagicMock, patch import pytest, litellm @@ -40,6 +40,7 @@ from litellm.proxy.utils import PrismaClient import litellm.proxy.common_utils.auth_cache_invalidation_pubsub as pubsub_module from litellm.caching.in_memory_cache import InMemoryCache from litellm.proxy.auth.auth_checks import ( + can_key_call_model, can_team_access_model, delete_cache_key_objects, is_model_cost_zero, @@ -48,6 +49,8 @@ from litellm.proxy.auth.auth_checks import ( common_checks, ) from litellm.proxy.utils import ProxyLogging +from litellm._logging import verbose_proxy_logger +from litellm.proxy.auth.user_api_key_auth import enforce_key_and_fallback_model_access from litellm.proxy.utils import CallInfo from litellm.types.passthrough_endpoints.pass_through_endpoints import LITELLM_PASS_THROUGH_ENDPOINT_MARKER @@ -1566,122 +1569,6 @@ async def test_can_team_access_model_access_group_ids_denied(): ) -async def _team_denial( - model: str, team: LiteLLM_TeamTable, router: litellm.Router, group_models: list[str] -) -> ProxyException: - from litellm.proxy.auth.auth_checks import can_team_access_model - - with patch( - "litellm.proxy.auth.auth_checks.get_models_from_access_groups", - new_callable=AsyncMock, - return_value=group_models, - ): - with pytest.raises(ProxyException) as denial: - await can_team_access_model(model=model, team_object=team, llm_router=router) - return denial.value - - -@pytest.mark.asyncio -async def test_team_with_empty_models_and_group_is_allowed_only_group_models(): - from litellm.proxy.auth.auth_checks import can_team_access_model - - team = LiteLLM_TeamTable(team_id="test-team", models=[], access_group_ids=["ag-gpt4"]) - with patch( - "litellm.proxy.auth.auth_checks.get_models_from_access_groups", - new_callable=AsyncMock, - return_value=["gpt-4"], - ): - result = await can_team_access_model( - model="gpt-4", team_object=team, llm_router=_router_deploying("gpt-4", "claude-sonnet") - ) - assert result is True - - -@pytest.mark.asyncio -async def test_team_with_empty_models_and_group_is_denied_deployed_model_outside_group_like_native_team(): - router = _router_deploying("gpt-4", "claude-sonnet") - group_restricted_team = LiteLLM_TeamTable(team_id="test-team", models=[], access_group_ids=["ag-gpt4"]) - natively_restricted_team = LiteLLM_TeamTable(team_id="test-team", models=["gpt-4"]) - - group_denial = await _team_denial("claude-sonnet", group_restricted_team, router, ["gpt-4"]) - native_denial = await _team_denial("claude-sonnet", natively_restricted_team, router, []) - - assert (group_denial.type, group_denial.code, group_denial.message) == ( - native_denial.type, - "403", - native_denial.message, - ) - assert group_denial.type == ProxyErrorTypes.team_model_access_denied - - -@pytest.mark.asyncio -@pytest.mark.parametrize("model", ["gpt-4", "claude-sonnet"]) -async def test_team_with_group_resolving_to_no_models_is_denied_every_model(model): - team = LiteLLM_TeamTable(team_id="test-team", models=[], access_group_ids=["ag-empty"]) - - denial = await _team_denial(model, team, _router_deploying("gpt-4", "claude-sonnet"), []) - - assert (denial.type, denial.code) == (ProxyErrorTypes.team_model_access_denied, "403") - - -@pytest.mark.asyncio -async def test_team_with_native_models_and_group_is_allowed_union_of_both(): - from litellm.proxy.auth.auth_checks import can_team_access_model - - team = LiteLLM_TeamTable(team_id="test-team", models=["gpt-4"], access_group_ids=["ag-claude"]) - router = _router_deploying("gpt-4", "claude-sonnet", "gpt-3.5-turbo") - with patch( - "litellm.proxy.auth.auth_checks.get_models_from_access_groups", - new_callable=AsyncMock, - return_value=["claude-sonnet"], - ): - native = await can_team_access_model(model="gpt-4", team_object=team, llm_router=router) - via_group = await can_team_access_model(model="claude-sonnet", team_object=team, llm_router=router) - assert (native, via_group) == (True, True) - - -@pytest.mark.asyncio -@pytest.mark.parametrize( - "team", - [LiteLLM_TeamTable(team_id="test-team", models=[]), None], -) -async def test_team_without_models_or_groups_is_unrestricted(team): - from litellm.proxy.auth.auth_checks import can_team_access_model - - with patch( - "litellm.proxy.auth.auth_checks.get_models_from_access_groups", - new_callable=AsyncMock, - return_value=[], - ) as group_lookup: - result = await can_team_access_model( - model="claude-sonnet", team_object=team, llm_router=_router_deploying("gpt-4", "claude-sonnet") - ) - assert result is True - assert group_lookup.await_count == 0 - - -@pytest.mark.asyncio -async def test_key_access_group_still_grants_model_group_restricted_team_denies(): - from litellm.proxy.auth.auth_checks import key_access_group_grants_model - - router = _router_deploying("gpt-4", "claude-sonnet") - team = LiteLLM_TeamTable(team_id="team-a", models=[], access_group_ids=["ag-team"]) - key = UserAPIKeyAuth(token="test-token", models=[], access_group_ids=["ag-key"], team_id="team-a") - - denial = await _team_denial("claude-sonnet", team, router, ["gpt-4"]) - with patch( - "litellm.proxy.auth.auth_checks.get_authorized_resources_from_key_access_groups", - new_callable=AsyncMock, - return_value=["claude-sonnet"], - ): - key_group_grants = await key_access_group_grants_model( - model="claude-sonnet", valid_token=key, team_object=team, llm_router=router - ) - - assert denial.type == ProxyErrorTypes.team_model_access_denied - assert key_group_grants is True - - # --------------------------------------------------------------------------- # can_key_call_model with access_group_ids fallback # --------------------------------------------------------------------------- @@ -1721,135 +1608,6 @@ async def test_can_key_call_model_via_access_group_ids(): ) -def _router_deploying(*model_names: str) -> litellm.Router: - return litellm.Router( - model_list=[ - {"model_name": name, "litellm_params": {"model": f"openai/{name}", "api_key": "test"}} - for name in model_names - ] - ) - - -async def _key_denial( - model: str, key: UserAPIKeyAuth, router: litellm.Router, group_models: list[str] -) -> ProxyException: - from litellm.proxy.auth.auth_checks import can_key_call_model - - with patch( - "litellm.proxy.auth.auth_checks.get_models_from_access_groups", - new_callable=AsyncMock, - return_value=group_models, - ): - with pytest.raises(ProxyException) as denial: - await can_key_call_model(model=model, llm_model_list=[], valid_token=key, llm_router=router) - return denial.value - - -@pytest.mark.asyncio -async def test_key_with_empty_models_and_group_is_allowed_only_group_models(): - from litellm.proxy.auth.auth_checks import can_key_call_model - - key = UserAPIKeyAuth(token="test-token", models=[], access_group_ids=["ag-gpt4"]) - with patch( - "litellm.proxy.auth.auth_checks.get_models_from_access_groups", - new_callable=AsyncMock, - return_value=["gpt-4"], - ): - result = await can_key_call_model( - model="gpt-4", - llm_model_list=[], - valid_token=key, - llm_router=_router_deploying("gpt-4", "claude-sonnet"), - ) - assert result is True - - -@pytest.mark.asyncio -async def test_key_with_empty_models_and_group_is_denied_deployed_model_outside_group_like_native_key(): - router = _router_deploying("gpt-4", "claude-sonnet") - group_restricted_key = UserAPIKeyAuth(token="test-token", models=[], access_group_ids=["ag-gpt4"]) - natively_restricted_key = UserAPIKeyAuth(token="test-token", models=["gpt-4"]) - - group_denial = await _key_denial("claude-sonnet", group_restricted_key, router, ["gpt-4"]) - native_denial = await _key_denial("claude-sonnet", natively_restricted_key, router, []) - - assert (group_denial.type, group_denial.code, group_denial.message) == ( - native_denial.type, - "403", - native_denial.message, - ) - assert group_denial.type == ProxyErrorTypes.key_model_access_denied - - -@pytest.mark.asyncio -@pytest.mark.parametrize("model", ["gpt-4", "claude-sonnet"]) -async def test_key_with_group_resolving_to_no_models_is_denied_every_model(model): - key = UserAPIKeyAuth(token="test-token", models=[], access_group_ids=["ag-empty"]) - - denial = await _key_denial(model, key, _router_deploying("gpt-4", "claude-sonnet"), []) - - assert (denial.type, denial.code) == (ProxyErrorTypes.key_model_access_denied, "403") - - -@pytest.mark.asyncio -@pytest.mark.parametrize( - "key_models, access_group_ids, model", - [ - (["gpt-4"], ["ag-claude"], "gpt-4"), - ([], [], "claude-sonnet"), - (["*"], ["ag-claude"], "gpt-3.5-turbo"), - (["all-proxy-models"], ["ag-claude"], "gpt-3.5-turbo"), - ], -) -async def test_key_allowed_natively_is_allowed_without_group_lookup(key_models, access_group_ids, model): - from litellm.proxy.auth.auth_checks import can_key_call_model - - key = UserAPIKeyAuth(token="test-token", models=key_models, access_group_ids=access_group_ids) - with patch( - "litellm.proxy.auth.auth_checks.get_models_from_access_groups", - new_callable=AsyncMock, - return_value=["claude-sonnet"], - ) as group_lookup: - result = await can_key_call_model( - model=model, - llm_model_list=[], - valid_token=key, - llm_router=_router_deploying("gpt-4", "claude-sonnet", "gpt-3.5-turbo"), - ) - assert result is True - assert group_lookup.await_count == 0, "a natively decided key must not pay for a group lookup" - - -@pytest.mark.asyncio -async def test_key_with_native_models_and_group_is_allowed_group_model(): - from litellm.proxy.auth.auth_checks import can_key_call_model - - key = UserAPIKeyAuth(token="test-token", models=["gpt-4"], access_group_ids=["ag-claude"]) - with patch( - "litellm.proxy.auth.auth_checks.get_models_from_access_groups", - new_callable=AsyncMock, - return_value=["claude-sonnet"], - ): - result = await can_key_call_model( - model="claude-sonnet", - llm_model_list=[], - valid_token=key, - llm_router=_router_deploying("gpt-4", "claude-sonnet", "gpt-3.5-turbo"), - ) - assert result is True - - -@pytest.mark.asyncio -async def test_key_with_native_models_and_group_is_denied_model_in_neither(): - key = UserAPIKeyAuth(token="test-token", models=["gpt-4"], access_group_ids=["ag-claude"]) - - denial = await _key_denial( - "gpt-3.5-turbo", key, _router_deploying("gpt-4", "claude-sonnet", "gpt-3.5-turbo"), ["claude-sonnet"] - ) - - assert (denial.type, denial.code) == (ProxyErrorTypes.key_model_access_denied, "403") - - # --------------------------------------------------------------------------- # _key_access_group_grants_model (key access group overriding team restriction) # --------------------------------------------------------------------------- @@ -2167,128 +1925,213 @@ async def test_key_access_group_grants_model_when_get_access_object_raises(): p.stop() -def _group_lookup_outcomes() -> list: - from litellm.proxy.auth.auth_checks import AccessGroupNotFoundError - - return [ - pytest.param({"side_effect": AccessGroupNotFoundError("ag-1")}, id="group-deleted"), - pytest.param({"side_effect": RuntimeError("database unreachable")}, id="lookup-fails"), - pytest.param({"return_value": _fake_access_group("ag-1", access_model_names=[])}, id="group-grants-nothing"), - ] +GroupStore = Mapping[str, Sequence[str] | Exception | None] +GroupRestrictedObject = Literal["key", "team"] +DENIAL_TYPES: Final = { + "key": ProxyErrorTypes.key_model_access_denied, + "team": ProxyErrorTypes.team_model_access_denied, +} -@pytest.mark.asyncio -@pytest.mark.parametrize("lookup", _group_lookup_outcomes()) -@pytest.mark.parametrize("model", ["gpt-4", "claude-sonnet"]) -async def test_group_restricted_key_is_denied_when_groups_cannot_be_loaded(lookup, model): - from litellm.proxy.auth.auth_checks import can_key_call_model - - key = UserAPIKeyAuth(token="test-token", models=[], access_group_ids=["ag-1"]) - patches = _patch_proxy_server_globals() + [ - patch("litellm.proxy.auth.auth_checks.get_access_object", new_callable=AsyncMock, **lookup) - ] - for p in patches: - p.start() - try: - with pytest.raises(ProxyException) as denial: - await can_key_call_model( - model=model, - llm_model_list=[], - valid_token=key, - llm_router=_router_deploying("gpt-4", "claude-sonnet"), - ) - finally: - for p in patches: - p.stop() - - assert (denial.value.type, denial.value.code) == (ProxyErrorTypes.key_model_access_denied, "403") +def _router_deploying(*model_names: str, aliases: Mapping[str, str] | None = None) -> litellm.Router: + return litellm.Router( + model_list=[ + {"model_name": name, "litellm_params": {"model": f"openai/{name}", "api_key": "test"}} + for name in model_names + ], + model_group_alias=dict(aliases or {}), + ) -@pytest.mark.asyncio -@pytest.mark.parametrize("lookup", _group_lookup_outcomes()) -@pytest.mark.parametrize("model", ["gpt-4", "claude-sonnet"]) -async def test_group_restricted_team_is_denied_when_groups_cannot_be_loaded(lookup, model): - team = LiteLLM_TeamTable(team_id="test-team", models=[], access_group_ids=["ag-1"]) - patches = _patch_proxy_server_globals() + [ - patch("litellm.proxy.auth.auth_checks.get_access_object", new_callable=AsyncMock, **lookup) - ] - for p in patches: - p.start() - try: - with pytest.raises(ProxyException) as denial: - await can_team_access_model( - model=model, team_object=team, llm_router=_router_deploying("gpt-4", "claude-sonnet") - ) - finally: - for p in patches: - p.stop() +def _fake_group_prisma(groups: GroupStore) -> MagicMock: + async def find_unique(where: Mapping[str, str]) -> MagicMock | None: + outcome: Final = groups.get(where["access_group_id"]) + if isinstance(outcome, Exception): + raise outcome + if outcome is None: + return None + row: Final = MagicMock() + row.dict.return_value = { + "access_group_id": where["access_group_id"], + "access_group_name": where["access_group_id"], + "access_model_names": list(outcome), + } + return row - assert (denial.value.type, denial.value.code) == (ProxyErrorTypes.team_model_access_denied, "403") + prisma: Final = MagicMock() + prisma.db.litellm_accessgrouptable.find_unique = AsyncMock(side_effect=find_unique) + return prisma -@pytest.mark.asyncio -@pytest.mark.parametrize("failure", [RuntimeError("database unreachable"), "group-deleted"]) -@pytest.mark.parametrize("object_type", ["key", "team"]) -async def test_group_restricted_object_is_denied_when_one_of_its_groups_cannot_be_loaded(failure, object_type): - from litellm.proxy.auth.auth_checks import AccessGroupNotFoundError, can_key_call_model - - async def lookup(access_group_id: str, **_: object): - if access_group_id == "ag-ok": - return _fake_access_group("ag-ok", access_model_names=["gpt-4"]) - raise AccessGroupNotFoundError(access_group_id) if failure == "group-deleted" else failure - - router = _router_deploying("gpt-4", "claude-sonnet") - patches = _patch_proxy_server_globals() + [ - patch("litellm.proxy.auth.auth_checks.get_access_object", side_effect=lookup) - ] - for p in patches: - p.start() - try: - with pytest.raises(ProxyException) as denial: - if object_type == "key": - key = UserAPIKeyAuth(token="test-token", models=[], access_group_ids=["ag-ok", "ag-1"]) - await can_key_call_model(model="gpt-4", llm_model_list=[], valid_token=key, llm_router=router) - else: - team = LiteLLM_TeamTable(team_id="test-team", models=[], access_group_ids=["ag-ok", "ag-1"]) - await can_team_access_model(model="gpt-4", team_object=team, llm_router=router) - finally: - for p in patches: - p.stop() - - expected_type = { - "key": ProxyErrorTypes.key_model_access_denied, - "team": ProxyErrorTypes.team_model_access_denied, - }[object_type] - assert (denial.value.type, denial.value.code) == (expected_type, "403") +def _use_group_store(monkeypatch: pytest.MonkeyPatch, groups: GroupStore) -> MagicMock: + monkeypatch.setattr("litellm.proxy.proxy_server.user_api_key_cache", UserApiKeyCache()) + monkeypatch.setattr("litellm.proxy.proxy_server.proxy_logging_obj", MagicMock()) + return _fake_group_prisma(groups) -@pytest.mark.asyncio -async def test_natively_restricted_key_whose_group_fallback_fails_gets_the_native_denial(): - from litellm.proxy.auth.auth_checks import can_key_call_model - - key = UserAPIKeyAuth(token="test-token", models=["gpt-4"], access_group_ids=["ag-1"]) - patches = _patch_proxy_server_globals() + [ - patch( - "litellm.proxy.auth.auth_checks.get_access_object", - new_callable=AsyncMock, - side_effect=RuntimeError("database unreachable"), +async def _call_model_check( + object_type: GroupRestrictedObject, + model: str, + router: litellm.Router, + prisma: MagicMock, + models: list[str], + access_group_ids: list[str], +) -> Literal[True]: + if object_type == "key": + key: Final = UserAPIKeyAuth(token="test-token", models=models, access_group_ids=access_group_ids) + return await can_key_call_model( + model=model, llm_model_list=[], valid_token=key, llm_router=router, prisma_client=prisma ) - ] - for p in patches: - p.start() - try: - with pytest.raises(ProxyException) as denial: - await can_key_call_model( - model="claude-sonnet", - llm_model_list=[], - valid_token=key, - llm_router=_router_deploying("gpt-4", "claude-sonnet"), - ) - finally: - for p in patches: - p.stop() + team: Final = LiteLLM_TeamTable(team_id="test-team", models=models, access_group_ids=access_group_ids) + return await can_team_access_model(model=model, team_object=team, llm_router=router, prisma_client=prisma) - assert (denial.value.type, denial.value.code) == (ProxyErrorTypes.key_model_access_denied, "403") + +GROUP_MODEL_ROUTER_ALIASES: Final = {"smart": "gpt-4", "fast": "claude-sonnet"} + + +@pytest.mark.asyncio +@pytest.mark.parametrize("object_type", ["key", "team"]) +@pytest.mark.parametrize( + "groups, model", + [ + pytest.param({"ag-1": ["gpt-4"]}, "gpt-4", id="listed-model"), + pytest.param({"ag-1": ["gpt-*"]}, "gpt-4", id="wildcard-group-model"), + pytest.param({"ag-1": ["gpt-4"]}, "smart", id="alias-of-granted-model"), + pytest.param({"ag-1": ["gpt-4"], "ag-2": ["claude-sonnet"]}, "claude-sonnet", id="second-group"), + ], +) +async def test_object_with_empty_models_is_allowed_models_its_groups_grant( + monkeypatch: pytest.MonkeyPatch, object_type: GroupRestrictedObject, groups: GroupStore, model: str +) -> None: + prisma: Final = _use_group_store(monkeypatch, groups) + + result: Final = await _call_model_check( + object_type, + model, + _router_deploying("gpt-4", "claude-sonnet", aliases=GROUP_MODEL_ROUTER_ALIASES), + prisma, + [], + list(groups), + ) + + assert result is True + + +@pytest.mark.asyncio +@pytest.mark.parametrize("object_type", ["key", "team"]) +@pytest.mark.parametrize( + "groups, model", + [ + pytest.param({"ag-1": ["gpt-4"]}, "claude-sonnet", id="deployed-model-outside-group"), + pytest.param({"ag-1": ["gpt-*"]}, "claude-sonnet", id="outside-wildcard-group-model"), + pytest.param({"ag-1": ["gpt-4"]}, "fast", id="alias-of-ungranted-model"), + pytest.param({"ag-1": []}, "gpt-4", id="group-grants-nothing"), + ], +) +async def test_object_with_empty_models_is_denied_models_its_groups_do_not_grant( + monkeypatch: pytest.MonkeyPatch, object_type: GroupRestrictedObject, groups: GroupStore, model: str +) -> None: + prisma: Final = _use_group_store(monkeypatch, groups) + router: Final = _router_deploying("gpt-4", "claude-sonnet", aliases=GROUP_MODEL_ROUTER_ALIASES) + + with pytest.raises(ProxyException) as denial: + await _call_model_check(object_type, model, router, prisma, [], list(groups)) + + assert (denial.value.type, denial.value.code) == (DENIAL_TYPES[object_type], "403") + + +@pytest.mark.asyncio +@pytest.mark.parametrize("object_type", ["key", "team"]) +async def test_group_denial_is_indistinguishable_from_native_denial( + monkeypatch: pytest.MonkeyPatch, object_type: GroupRestrictedObject +) -> None: + prisma: Final = _use_group_store(monkeypatch, {"ag-1": ["gpt-4"]}) + router: Final = _router_deploying("gpt-4", "claude-sonnet") + + with pytest.raises(ProxyException) as group_denial: + await _call_model_check(object_type, "claude-sonnet", router, prisma, [], ["ag-1"]) + with pytest.raises(ProxyException) as native_denial: + await _call_model_check(object_type, "claude-sonnet", router, prisma, ["gpt-4"], []) + + assert (group_denial.value.type, group_denial.value.code, group_denial.value.message) == ( + native_denial.value.type, + native_denial.value.code, + native_denial.value.message, + ) + + +@pytest.mark.asyncio +@pytest.mark.parametrize("object_type", ["key", "team"]) +@pytest.mark.parametrize( + "groups, access_group_ids", + [ + pytest.param({"ag-1": None}, ["ag-1"], id="group-deleted"), + pytest.param({"ag-1": RuntimeError("database unreachable")}, ["ag-1"], id="lookup-fails"), + pytest.param({"ag-ok": ["gpt-4"], "ag-1": None}, ["ag-ok", "ag-1"], id="one-of-two-groups-deleted"), + pytest.param( + {"ag-ok": ["gpt-4"], "ag-1": RuntimeError("database unreachable")}, + ["ag-ok", "ag-1"], + id="one-of-two-lookups-fails", + ), + pytest.param({"ag-ok": ["gpt-4"]}, [""], id="blank-group-id"), + pytest.param({"ag-ok": ["gpt-4"]}, ["ag-ok", ""], id="blank-group-id-beside-valid-group"), + ], +) +async def test_object_with_empty_models_is_denied_when_a_group_cannot_be_loaded( + monkeypatch: pytest.MonkeyPatch, + object_type: GroupRestrictedObject, + groups: GroupStore, + access_group_ids: list[str], +) -> None: + prisma: Final = _use_group_store(monkeypatch, groups) + + with pytest.raises(ProxyException) as denial: + await _call_model_check(object_type, "gpt-4", _router_deploying("gpt-4"), prisma, [], access_group_ids) + + assert (denial.value.type, denial.value.code) == (DENIAL_TYPES[object_type], "403") + + +@pytest.mark.asyncio +async def test_group_load_failure_is_logged_with_group_ids_and_without_the_token( + monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture +) -> None: + prisma: Final = _use_group_store(monkeypatch, {"ag-1": RuntimeError("database unreachable")}) + verbose_proxy_logger.addHandler(caplog.handler) + try: + with caplog.at_level("WARNING", logger=verbose_proxy_logger.name), pytest.raises(ProxyException): + await _call_model_check("key", "gpt-4", _router_deploying("gpt-4"), prisma, [], ["ag-1"]) + finally: + verbose_proxy_logger.removeHandler(caplog.handler) + + assert "ag-1" in caplog.text + assert "test-token" not in caplog.text + + +@pytest.mark.asyncio +async def test_group_restricted_key_is_held_to_its_groups_on_the_request_path( + monkeypatch: pytest.MonkeyPatch, +) -> None: + prisma: Final = _use_group_store(monkeypatch, {"ag-1": ["gpt-4"]}) + monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", prisma) + router: Final = _router_deploying("gpt-4", "claude-sonnet") + monkeypatch.setattr("litellm.proxy.proxy_server.llm_router", router) + key: Final = UserAPIKeyAuth(token="test-token", models=[], access_group_ids=["ag-1"]) + + async def enforce(model: str) -> None: + await enforce_key_and_fallback_model_access( + valid_token=key, + request_data={"model": model, "messages": [{"role": "user", "content": "hi"}]}, + route="/v1/chat/completions", + request=None, + llm_model_list=router.model_list, + llm_router=router, + ) + + await enforce("gpt-4") + with pytest.raises(ProxyException) as denial: + await enforce("claude-sonnet") + + assert denial.value.type == ProxyErrorTypes.key_model_access_denied @pytest.fixture From c3369e03a608bc8a5770af2eb53c6af56e74ae6c Mon Sep 17 00:00:00 2001 From: Avani-prajapati Date: Wed, 21 Oct 2026 17:47:44 +0530 Subject: [PATCH 6/7] refactor(proxy): decide group-only model access in the same flow as the native check can_key_call_model and can_team_access_model now pick the models to check (the groups' models for a group-restricted key or team, the native models otherwise) and run one native check, instead of an early-return branch that duplicated it. The access-group fallback is skipped for group-restricted objects, since the groups were already the only grant. The allow-path test also asserts a deployed model outside the groups is denied, so it fails when the empty models list is read as unrestricted. --- litellm/proxy/auth/auth_checks.py | 92 ++++++++++------------- tests/unit/proxy/auth/test_auth_checks.py | 15 ++-- 2 files changed, 46 insertions(+), 61 deletions(-) diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.py index c82f0bea63c..a73e3ae4a82 100644 --- a/litellm/proxy/auth/auth_checks.py +++ b/litellm/proxy/auth/auth_checks.py @@ -5197,7 +5197,7 @@ def _resolve_key_models_for_auth_check(valid_token: UserAPIKeyAuth) -> list[str] If the key has no team_id, it inherits the full proxy model list (equivalent to an empty models field, i.e. unrestricted access). """ - models: Final = _configured_key_models(valid_token) + models: Final = _models_configured_on_key(valid_token) if SpecialModelNames.all_team_models.value in models: if valid_token.team_id is None: return [] @@ -5205,10 +5205,18 @@ def _resolve_key_models_for_auth_check(valid_token: UserAPIKeyAuth) -> list[str] return list(models) -def _configured_key_models(valid_token: UserAPIKeyAuth) -> tuple[str, ...]: +def _models_configured_on_key(valid_token: UserAPIKeyAuth) -> tuple[str, ...]: return tuple(valid_token.models or ()) +def _key_is_group_restricted(valid_token: UserAPIKeyAuth) -> bool: + return not _models_configured_on_key(valid_token) and bool(valid_token.access_group_ids) + + +def _team_is_group_restricted(team_object: LiteLLM_TeamTable | None) -> bool: + return team_object is not None and not team_object.models and bool(team_object.access_group_ids) + + def _model_access_groups_serving_model( model: str | Sequence[str], llm_router: Router, @@ -5514,33 +5522,6 @@ async def _models_granted_by_groups_only( _raise_model_access_denied(model=str(model), models=(), object_type=object_type) -async def _can_object_call_model_via_groups_only( - model: str | list[str], # mutable-ok: forwarded unchanged to can_object_call_model - access_group_ids: Sequence[str], - prisma_client: DatabaseClient | None, - llm_router: Router | None, - team_model_aliases: Mapping[str, str] | None, - team_id: str | None, - key_model_aliases: Mapping[str, str] | None, - object_type: Literal["key", "team"], -) -> Literal[True]: - granted_models: Final = await _models_granted_by_groups_only( - model=model, - access_group_ids=access_group_ids, - prisma_client=prisma_client, - object_type=object_type, - ) - return can_object_call_model( - model=model, - llm_router=llm_router, - models=list(granted_models), - team_model_aliases=team_model_aliases, - team_id=team_id, - key_model_aliases=key_model_aliases, - object_type=object_type, - ) - - async def can_key_call_model( model: str | list[str], llm_model_list: Sequence[object] | None, @@ -5561,18 +5542,19 @@ async def can_key_call_model( - Exception: If token not allowed to call model """ key_access_group_ids: Final = valid_token.access_group_ids or [] - if not _configured_key_models(valid_token) and key_access_group_ids: - return await _can_object_call_model_via_groups_only( - model=model, - access_group_ids=key_access_group_ids, - prisma_client=prisma_client, - llm_router=llm_router, - team_model_aliases=valid_token.team_model_aliases, - team_id=valid_token.team_id, - key_model_aliases=key_model_aliases_for_auth_check(valid_token), - object_type="key", + group_restricted: Final = _key_is_group_restricted(valid_token) + key_models: Final = ( + list( + await _models_granted_by_groups_only( + model=model, + access_group_ids=key_access_group_ids, + prisma_client=prisma_client, + object_type="key", + ) ) - key_models: Final = _resolve_key_models_for_auth_check(valid_token=valid_token) + if group_restricted + else _resolve_key_models_for_auth_check(valid_token=valid_token) + ) try: return can_object_call_model( model=model, @@ -5584,6 +5566,8 @@ async def can_key_call_model( object_type="key", ) except ProxyException: + if group_restricted: + raise # Fallback: check key's access_group_ids if key_access_group_ids: models_from_groups: Final = await get_models_from_access_groups( @@ -5794,30 +5778,34 @@ async def can_team_access_model( 1. First checks native team-level model permissions (current implementation) 2. If not allowed natively, falls back to access_group_ids on the team """ - if team_object is not None and not team_object.models and team_object.access_group_ids: - return await _can_object_call_model_via_groups_only( - model=model, - access_group_ids=team_object.access_group_ids, - prisma_client=prisma_client, - llm_router=llm_router, - team_model_aliases=team_model_aliases, - team_id=team_object.team_id, - key_model_aliases=key_model_aliases, - object_type="team", + team_access_group_ids: Final = (team_object.access_group_ids or []) if team_object else [] + group_restricted: Final = _team_is_group_restricted(team_object) + team_models: Final = ( + list( + await _models_granted_by_groups_only( + model=model, + access_group_ids=team_access_group_ids, + prisma_client=prisma_client, + object_type="team", + ) ) + if group_restricted + else (team_object.models if team_object else []) + ) try: return can_object_call_model( model=model, llm_router=llm_router, - models=team_object.models if team_object else [], + models=team_models, team_model_aliases=team_model_aliases, team_id=team_object.team_id if team_object else None, key_model_aliases=key_model_aliases, object_type="team", ) except ProxyException: + if group_restricted: + raise # Fallback: check team's access_group_ids - team_access_group_ids: Final = (team_object.access_group_ids or []) if team_object else [] if team_access_group_ids: models_from_groups: Final = await get_models_from_access_groups( access_group_ids=team_access_group_ids, diff --git a/tests/unit/proxy/auth/test_auth_checks.py b/tests/unit/proxy/auth/test_auth_checks.py index fb9fddbfae8..2723e87b1aa 100644 --- a/tests/unit/proxy/auth/test_auth_checks.py +++ b/tests/unit/proxy/auth/test_auth_checks.py @@ -2000,21 +2000,18 @@ GROUP_MODEL_ROUTER_ALIASES: Final = {"smart": "gpt-4", "fast": "claude-sonnet"} pytest.param({"ag-1": ["gpt-4"], "ag-2": ["claude-sonnet"]}, "claude-sonnet", id="second-group"), ], ) -async def test_object_with_empty_models_is_allowed_models_its_groups_grant( +async def test_object_with_empty_models_is_held_to_the_models_its_groups_grant( monkeypatch: pytest.MonkeyPatch, object_type: GroupRestrictedObject, groups: GroupStore, model: str ) -> None: prisma: Final = _use_group_store(monkeypatch, groups) + router: Final = _router_deploying("gpt-4", "claude-sonnet", "gemini-pro", aliases=GROUP_MODEL_ROUTER_ALIASES) - result: Final = await _call_model_check( - object_type, - model, - _router_deploying("gpt-4", "claude-sonnet", aliases=GROUP_MODEL_ROUTER_ALIASES), - prisma, - [], - list(groups), - ) + result: Final = await _call_model_check(object_type, model, router, prisma, [], list(groups)) + with pytest.raises(ProxyException) as denial: + await _call_model_check(object_type, "gemini-pro", router, prisma, [], list(groups)) assert result is True + assert denial.value.type == DENIAL_TYPES[object_type] @pytest.mark.asyncio From cd35b6cd2a0b1381b25b9d28cb55b0bc4f8e2058 Mon Sep 17 00:00:00 2001 From: Avani-prajapati Date: Wed, 21 Oct 2026 18:07:55 +0530 Subject: [PATCH 7/7] fix(proxy): strip control characters from the access group load failure log The warning logged when a group-restricted key or team's access groups cannot be loaded included the requested model and the access group ids, both user-controlled, so a crafted value could forge extra log lines. They are now stripped of CR, LF and other non-printable characters before logging. --- litellm/proxy/auth/auth_checks.py | 10 +++++++--- tests/unit/proxy/auth/test_auth_checks.py | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.py index a73e3ae4a82..c82f76f6b24 100644 --- a/litellm/proxy/auth/auth_checks.py +++ b/litellm/proxy/auth/auth_checks.py @@ -5491,6 +5491,10 @@ async def stamp_matched_model_access_groups( return matched +def _log_safe(value: object) -> str: + return "".join(ch for ch in str(value).replace("\r", "").replace("\n", "") if ch.isprintable()) + + async def _models_granted_by_groups_only( model: str | Sequence[str], access_group_ids: Sequence[str], @@ -5507,9 +5511,9 @@ async def _models_granted_by_groups_only( verbose_proxy_logger.warning( "%s model access denied, access groups could not be loaded. model=%s access_group_ids=%s error=%s", object_type, - model, - list(access_group_ids), - e, + _log_safe(model), + _log_safe(list(access_group_ids)), + _log_safe(e), ) _raise_model_access_denied( model=str(model), diff --git a/tests/unit/proxy/auth/test_auth_checks.py b/tests/unit/proxy/auth/test_auth_checks.py index 2723e87b1aa..556007cbc94 100644 --- a/tests/unit/proxy/auth/test_auth_checks.py +++ b/tests/unit/proxy/auth/test_auth_checks.py @@ -2104,6 +2104,26 @@ async def test_group_load_failure_is_logged_with_group_ids_and_without_the_token assert "test-token" not in caplog.text +@pytest.mark.asyncio +async def test_group_load_failure_log_cannot_be_split_by_user_controlled_model_or_group_id( + monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture +) -> None: + forged_group_id: Final = "ag-1\r\nFORGED LOG LINE\x1b[31m" + prisma: Final = _use_group_store(monkeypatch, {forged_group_id: RuntimeError("database unreachable")}) + verbose_proxy_logger.addHandler(caplog.handler) + try: + with caplog.at_level("WARNING", logger=verbose_proxy_logger.name), pytest.raises(ProxyException): + await _call_model_check( + "key", "gpt-4\nFORGED MODEL LINE", _router_deploying("gpt-4"), prisma, [], [forged_group_id] + ) + finally: + verbose_proxy_logger.removeHandler(caplog.handler) + + messages: Final = {record.getMessage() for record in caplog.records if "access groups" in record.getMessage()} + assert len(messages) == 1 + assert not any(ch in next(iter(messages)) for ch in "\r\n\x1b") + + @pytest.mark.asyncio async def test_group_restricted_key_is_held_to_its_groups_on_the_request_path( monkeypatch: pytest.MonkeyPatch,