mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
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.
This commit is contained in:
parent
305fe443b5
commit
671ebeac68
5 changed files with 44 additions and 15 deletions
|
|
@ -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,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
)
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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",),
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue