From 4b850ccbe29f8be2772247f5897a5fe5d12d6897 Mon Sep 17 00:00:00 2001 From: Praising Harris Ratnam Date: Sun, 18 Oct 2026 09:41:03 +0530 Subject: [PATCH] fix(auth_checks): fail closed on empty resolved access groups and preserve team union (#47105) --- litellm/proxy/auth/auth_checks.py | 52 +++++++-- tests/unit/proxy/auth/test_auth_checks.py | 135 ++++++++++++++++++++++ 2 files changed, 177 insertions(+), 10 deletions(-) diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.py index 398a69de9b5..c231a53a6a8 100644 --- a/litellm/proxy/auth/auth_checks.py +++ b/litellm/proxy/auth/auth_checks.py @@ -5411,11 +5411,33 @@ async def can_key_call_model( prisma_client=prisma_client, ) if models_from_groups is not None: - allowed_models = ( - [m for m in key_models if m in models_from_groups] - if key_models - else models_from_groups - ) + if not models_from_groups: + raise ModelAccessDeniedProxyException( + message=model_access_denied_client_message(model=model), + internal_message=( + f"key not allowed to access model. Key has access_group_ids={key_access_group_ids} " + f"which resolved to no models. Tried to access {model}" + ), + type=ProxyErrorTypes.get_model_access_error_type_for_object(object_type="key"), + param="model", + code=status.HTTP_403_FORBIDDEN, + ) + if key_models: + allowed_models = [m for m in key_models if m in models_from_groups] + if not allowed_models: + raise ModelAccessDeniedProxyException( + message=model_access_denied_client_message(model=model), + internal_message=( + f"key not allowed to access model. Key models={key_models} have no intersection " + f"with access_group_ids models={models_from_groups}. Tried to access {model}" + ), + type=ProxyErrorTypes.get_model_access_error_type_for_object(object_type="key"), + param="model", + code=status.HTTP_403_FORBIDDEN, + ) + else: + allowed_models = models_from_groups + return can_object_call_model( model=model, llm_router=llm_router, @@ -5632,11 +5654,21 @@ async def can_team_access_model( prisma_client=prisma_client, ) if models_from_groups is not None: - allowed_models = ( - [m for m in team_models if m in models_from_groups] - if team_models - else models_from_groups - ) + if team_models: + allowed_models = list(dict.fromkeys([*team_models, *models_from_groups])) + elif models_from_groups: + allowed_models = models_from_groups + else: + raise ModelAccessDeniedProxyException( + message=model_access_denied_client_message(model=model), + internal_message=( + f"team not allowed to access model. Team has access_group_ids={team_access_group_ids} " + f"which resolved to no models. Tried to access {model}" + ), + type=ProxyErrorTypes.get_model_access_error_type_for_object(object_type="team"), + param="model", + code=status.HTTP_403_FORBIDDEN, + ) return can_object_call_model( model=model, llm_router=llm_router, diff --git a/tests/unit/proxy/auth/test_auth_checks.py b/tests/unit/proxy/auth/test_auth_checks.py index e1afa81d372..fb56f6af573 100644 --- a/tests/unit/proxy/auth/test_auth_checks.py +++ b/tests/unit/proxy/auth/test_auth_checks.py @@ -1604,6 +1604,141 @@ async def test_can_key_call_model_via_access_group_ids(): ) +@pytest.mark.asyncio +async def test_can_key_call_model_empty_models_empty_access_groups_denied(): + """Test can_key_call_model fails closed (denies) when key has empty models and access_group_ids resolve to empty.""" + from unittest.mock import AsyncMock, patch + + from litellm.proxy.auth.auth_checks import can_key_call_model + from litellm.proxy._types import ProxyException + + user_api_key_object = UserAPIKeyAuth( + token="test-token", + models=[], + access_group_ids=["ag-empty"], + ) + router = litellm.Router( + model_list=[ + { + "model_name": "gpt-4", + "litellm_params": {"model": "openai/gpt-4", "api_key": "test"}, + } + ] + ) + + with patch( + "litellm.proxy.auth.auth_checks.get_models_from_access_groups", + new_callable=AsyncMock, + return_value=[], + ): + with pytest.raises(ProxyException): + await can_key_call_model( + model="gpt-4", + llm_model_list=[], + valid_token=user_api_key_object, + llm_router=router, + ) + + +@pytest.mark.asyncio +async def test_can_key_call_model_disjoint_models_and_access_groups_denied(): + """Test can_key_call_model denies when key models and access_group_ids models are disjoint.""" + from unittest.mock import AsyncMock, patch + + from litellm.proxy.auth.auth_checks import can_key_call_model + from litellm.proxy._types import ProxyException + + user_api_key_object = UserAPIKeyAuth( + token="test-token", + models=["gpt-3.5-turbo"], + access_group_ids=["ag-claude-only"], + ) + router = litellm.Router( + model_list=[ + { + "model_name": "claude-3", + "litellm_params": {"model": "anthropic/claude-3", "api_key": "test"}, + } + ] + ) + + with patch( + "litellm.proxy.auth.auth_checks.get_models_from_access_groups", + new_callable=AsyncMock, + return_value=["claude-3"], + ): + with pytest.raises(ProxyException): + await can_key_call_model( + model="claude-3", + llm_model_list=[], + valid_token=user_api_key_object, + llm_router=router, + ) + + +@pytest.mark.asyncio +async def test_can_team_access_model_empty_models_empty_access_groups_denied(): + """Test can_team_access_model fails closed (denies) when team has empty models and access_group_ids resolve to empty.""" + from unittest.mock import AsyncMock, patch + + from litellm.proxy.auth.auth_checks import can_team_access_model + from litellm.proxy._types import ProxyException + + team_object = LiteLLM_TeamTable( + team_id="test-team", + models=[], + access_group_ids=["ag-empty"], + ) + + with patch( + "litellm.proxy.auth.auth_checks.get_models_from_access_groups", + new_callable=AsyncMock, + return_value=[], + ): + with pytest.raises(ProxyException): + await can_team_access_model( + model="gpt-4", + team_object=team_object, + llm_router=None, + team_model_aliases=None, + ) + + +@pytest.mark.asyncio +async def test_can_team_access_model_union_preserves_both_team_and_group_models(): + """Test can_team_access_model preserves union semantics when team_models and access_group_ids are both set.""" + from unittest.mock import AsyncMock, patch + + from litellm.proxy.auth.auth_checks import can_team_access_model + + team_object = LiteLLM_TeamTable( + team_id="test-team", + 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-3"], + ): + # Access gpt-4 (from team_models) + assert await can_team_access_model( + model="gpt-4", + team_object=team_object, + llm_router=None, + team_model_aliases=None, + ) is True + + # Access claude-3 (from access_group_ids) + assert await can_team_access_model( + model="claude-3", + team_object=team_object, + llm_router=None, + team_model_aliases=None, + ) is True + + # --------------------------------------------------------------------------- # _key_access_group_grants_model (key access group overriding team restriction) # ---------------------------------------------------------------------------