From 671ebeac683e77019f5d13d0cd8042ebde43931b Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:52:29 -0800 Subject: [PATCH] fix(auth): keep empty access groups so a fully foreign group grants nothing A group whose members all belong to other teams was dropped from the team-filtered map, so its name leaked into /v1/models as a plain model. Keep the group with no members, and treat a grant that expands to nothing as no-default-models instead of unrestricted. All-team-models team keys get team_model_access_denied again, as on main. --- litellm/proxy/auth/auth_checks.py | 7 ++++++- litellm/proxy/auth/model_checks.py | 12 +++++------ ...st_auth_checks_object_access_and_lookup.py | 21 +++++++++++++++++-- tests/unit/proxy/auth/test_model_checks.py | 4 ++-- .../proxy/utils/helpers/test_model_access.py | 15 ++++++++++--- 5 files changed, 44 insertions(+), 15 deletions(-) diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.py index 6fad6f54189..a541f8cfe41 100644 --- a/litellm/proxy/auth/auth_checks.py +++ b/litellm/proxy/auth/auth_checks.py @@ -5853,12 +5853,17 @@ def raise_when_key_reaches_only_other_teams_deployments( ) if foreign_model is None: return + denied_object: Final = ( + "team" + if valid_token.team_id is not None and SpecialModelNames.all_team_models.value in (valid_token.models or ()) + else "key" + ) raise ModelAccessDeniedProxyException( message=model_access_denied_client_message(model=foreign_model), internal_message=( f"key not allowed to access model. Tried to access {foreign_model}, which is only deployed for other teams" ), - type=ProxyErrorTypes.get_model_access_error_type_for_object(object_type="key"), + type=ProxyErrorTypes.get_model_access_error_type_for_object(object_type=denied_object), param="model", code=status.HTTP_403_FORBIDDEN, ) diff --git a/litellm/proxy/auth/model_checks.py b/litellm/proxy/auth/model_checks.py index f060425f571..60f8953ed72 100644 --- a/litellm/proxy/auth/model_checks.py +++ b/litellm/proxy/auth/model_checks.py @@ -171,7 +171,10 @@ def _get_models_from_access_groups( member_models: Final = [ member for model in all_models if model in model_access_groups for member in model_access_groups[model] ] - return kept_models + member_models + expanded_models: Final = kept_models + member_models + if all_models and not expanded_models: + return [SpecialModelNames.no_default_models.value] + return expanded_models def _model_name_usable_by_team(llm_router: RegistryReader, model_name: str, team_id: str | None) -> bool: @@ -184,13 +187,8 @@ def _model_name_usable_by_team(llm_router: RegistryReader, model_name: str, team def access_groups_usable_by_team(llm_router: RegistryReader, team_id: str | None) -> Mapping[str, Sequence[str]]: return MappingProxyType( { - group: usable_group_models + group: tuple(name for name in model_names if _model_name_usable_by_team(llm_router, name, team_id)) for group, model_names in llm_router.registry().access_groups().items() - if ( - usable_group_models := tuple( - name for name in model_names if _model_name_usable_by_team(llm_router, name, team_id) - ) - ) } ) 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 d8aa500c895..2b1a623a359 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 @@ -3534,12 +3534,28 @@ async def test_enforce_key_access_keys_skipping_the_model_check_still_stop_at_ot @pytest.mark.asyncio -async def test_can_key_call_resolved_model_all_team_models_key_denied_other_teams_deployment(): +@pytest.mark.parametrize( + ("valid_token", "expected_error_type"), + [ + ( + UserAPIKeyAuth(api_key="sk-orphan", models=["all-team-models"], team_models=[]), + ProxyErrorTypes.key_model_access_denied, + ), + ( + UserAPIKeyAuth( + api_key="sk-team-a", models=["all-team-models"], team_id="team-a", team_models=["bedrock-group"] + ), + ProxyErrorTypes.team_model_access_denied, + ), + ], +) +async def test_can_key_call_resolved_model_all_team_models_key_denied_other_teams_deployment( + valid_token: UserAPIKeyAuth, expected_error_type: ProxyErrorTypes +): from litellm.proxy._types import ModelAccessDeniedProxyException from litellm.proxy.auth.auth_checks import can_key_call_resolved_model router = _make_router_with_global_and_team_b_deployment() - valid_token = UserAPIKeyAuth(api_key="sk-orphan", models=["all-team-models"], team_models=[]) with pytest.raises(ModelAccessDeniedProxyException) as exc: await can_key_call_resolved_model( @@ -3549,6 +3565,7 @@ async def test_can_key_call_resolved_model_all_team_models_key_denied_other_team llm_router=router, ) assert exc.value.code == "403" + assert exc.value.type == expected_error_type assert exc.value.internal_message == _OTHER_TEAMS_DEPLOYMENT_403 diff --git a/tests/unit/proxy/auth/test_model_checks.py b/tests/unit/proxy/auth/test_model_checks.py index aa3670b859a..7f1ad519161 100644 --- a/tests/unit/proxy/auth/test_model_checks.py +++ b/tests/unit/proxy/auth/test_model_checks.py @@ -3144,8 +3144,8 @@ def test_access_groups_usable_by_team_drops_other_teams_deployments() -> None: router: Final = _router_with_global_and_team_b_deployments() - assert access_groups_usable_by_team(router, None) == {"bedrock-group": ("bedrock-nova",)} - assert access_groups_usable_by_team(router, "team-a") == {"bedrock-group": ("bedrock-nova",)} + assert access_groups_usable_by_team(router, None) == {"bedrock-group": ("bedrock-nova",), "team-b-only": ()} + assert access_groups_usable_by_team(router, "team-a") == {"bedrock-group": ("bedrock-nova",), "team-b-only": ()} assert access_groups_usable_by_team(router, "team-b") == { "bedrock-group": ("bedrock-nova", "model_name_team-b_1111"), "team-b-only": ("model_name_team-b_2222",), diff --git a/tests/unit/proxy/utils/helpers/test_model_access.py b/tests/unit/proxy/utils/helpers/test_model_access.py index 3d8260191f8..31b018ec75f 100644 --- a/tests/unit/proxy/utils/helpers/test_model_access.py +++ b/tests/unit/proxy/utils/helpers/test_model_access.py @@ -390,14 +390,23 @@ async def test_get_available_models_for_user_admin_listing_for_a_team_scopes_gro @pytest.mark.asyncio -async def test_get_available_models_for_user_group_owned_by_another_team_lists_no_deployments(): +@pytest.mark.parametrize( + "user_api_key_dict", + [ + UserAPIKeyAuth(api_key="sk-teamless", models=["team-b-only"], team_id=None, team_models=[]), + UserAPIKeyAuth(api_key="sk-team-a", models=[], team_id="team-a", team_models=["team-b-only"]), + ], +) +async def test_get_available_models_for_user_group_owned_by_another_team_lists_no_deployments( + user_api_key_dict: UserAPIKeyAuth, +): result = await get_available_models_for_user( - user_api_key_dict=UserAPIKeyAuth(api_key="sk-teamless", models=["team-b-only"], team_id=None, team_models=[]), + user_api_key_dict=user_api_key_dict, llm_router=_router_with_global_and_team_b_deployments(), general_settings={}, user_model=None, ) - assert not {"bedrock-nova", "model_name_team-b_1111", "model_name_team-b_2222"} & set(result) + assert list(result) == [] @pytest.mark.asyncio