mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(scim): clear every group or member on a PATCH remove with no value or filter
RFC 7644 section 3.5.2.2 says a remove on a multi-valued attribute with no filter removes all of its values. A bare remove on a user's groups or a group's members answered 200 and removed nothing, so a deprovisioned user kept every team and its team keys
This commit is contained in:
parent
a44e44b980
commit
454f235ad4
5 changed files with 150 additions and 2 deletions
|
|
@ -2253,7 +2253,13 @@ def _is_group_path(path: str | None) -> bool:
|
|||
return isinstance(path, str) and path.lower().startswith("groups")
|
||||
|
||||
|
||||
def _removes_every_value(op: SCIMPatchOperation, attribute: str) -> bool:
|
||||
return op.op == "remove" and op.value is None and (op.path or "").lower() == attribute
|
||||
|
||||
|
||||
def _apply_group_operation(teams: tuple[str, ...], op: SCIMPatchOperation) -> tuple[str, ...]:
|
||||
if _removes_every_value(op, "groups"):
|
||||
return ()
|
||||
listed: Final = _extract_group_values(op.value)
|
||||
group_values: Final = listed if listed or op.value is not None else _extract_ids_from_path_filter(op.path, "groups")
|
||||
if op.op == "replace":
|
||||
|
|
@ -3270,8 +3276,11 @@ async def _process_group_patch_operations(
|
|||
)
|
||||
|
||||
if op_type == "remove":
|
||||
final_members = final_members - await _member_ids_to_drop(
|
||||
patched_members, frozenset(final_members), prisma_client
|
||||
final_members = (
|
||||
set[str]()
|
||||
if _removes_every_value(op, "members")
|
||||
else final_members
|
||||
- await _member_ids_to_drop(patched_members, frozenset(final_members), prisma_client)
|
||||
)
|
||||
else:
|
||||
member_result = await _resolve_group_member_ids(
|
||||
|
|
|
|||
|
|
@ -385,6 +385,10 @@ PATCH_SHAPES: Final[Mapping[str, PatchShape]] = {
|
|||
],
|
||||
expected=lambda users: (users[0], users[3]),
|
||||
),
|
||||
"remove-every-member": PatchShape(
|
||||
operations=lambda users: [{"op": "remove", "path": "members"}],
|
||||
expected=lambda users: (),
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -463,6 +463,51 @@ def test_profile_put_without_groups_keeps_memberships_and_a_new_groups_list_move
|
|||
_assert_serving(gateway, model_b, key_b)
|
||||
|
||||
|
||||
def test_patch_remove_on_groups_with_no_value_removes_the_user_from_every_team(gateway: Gateway) -> None:
|
||||
with gateway.scenario() as scenario:
|
||||
model: Final = scenario.model()
|
||||
teams: Final = (scenario.team(models=[model]), scenario.team(models=[model]))
|
||||
user_name: Final = f"scim-remove-groups-{uuid.uuid4().hex}@example.com"
|
||||
created: Final = _scim_request(
|
||||
gateway,
|
||||
"POST",
|
||||
"/scim/v2/Users",
|
||||
{
|
||||
"schemas": [SCIM_CORE_USER_SCHEMA],
|
||||
"userName": user_name,
|
||||
"emails": [{"value": user_name, "primary": True}],
|
||||
"groups": [{"value": team} for team in teams],
|
||||
},
|
||||
)
|
||||
scenario.cleanups.callback(_delete_user_if_present, gateway, user_name)
|
||||
assert created.status_code == 201, created.text
|
||||
key: Final = _key_with_cleanup(scenario, user_id=user_name, team_id=teams[0], models=[model])
|
||||
_assert_serving(gateway, model, key)
|
||||
|
||||
removed: Final = _scim_request(
|
||||
gateway,
|
||||
"PATCH",
|
||||
f"/scim/v2/Users/{user_name}",
|
||||
{
|
||||
"schemas": ["urn:ietf:params:scim:api:messages:2.0:PatchOp"],
|
||||
"Operations": [{"op": "remove", "path": "groups"}],
|
||||
},
|
||||
)
|
||||
|
||||
assert removed.status_code == 200, removed.text
|
||||
assert not ScimUserResponse.model_validate(removed.json()).groups, removed.text
|
||||
readback: Final = _scim_request(gateway, "GET", f"/scim/v2/Users/{user_name}")
|
||||
assert not ScimUserResponse.model_validate(readback.json()).groups, readback.text
|
||||
assert not any(_has_team_member(gateway, team, user_name) for team in teams)
|
||||
refused: Final = gateway.request(
|
||||
"POST",
|
||||
"/v1/chat/completions",
|
||||
{"model": model, "messages": [{"role": "user", "content": "SCIM removed every group"}]},
|
||||
key=key,
|
||||
)
|
||||
assert refused.status_code == 401, refused.text
|
||||
|
||||
|
||||
def test_scim_create_persists_external_id_as_the_sso_user_id(gateway: Gateway) -> None:
|
||||
pytest.skip(
|
||||
"BUG: POST /scim/v2/Users drops externalId, so the 201 resource and GET readback return externalId null and "
|
||||
|
|
|
|||
|
|
@ -507,6 +507,65 @@ def test_apply_patch_ops_remove_group_filtered_path_without_value():
|
|||
assert final_teams == ("team-2",)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("path", ["groups", "Groups"])
|
||||
def test_apply_patch_ops_remove_groups_without_value_or_filter_removes_every_team(path):
|
||||
"""RFC 7644 section 3.5.2.2: a remove on a multi-valued attribute with no
|
||||
filter removes all of its values"""
|
||||
user = LiteLLM_UserTable(
|
||||
user_id="user-all",
|
||||
user_email="all@example.com",
|
||||
teams=["team-1", "team-2"],
|
||||
metadata={},
|
||||
)
|
||||
patch_ops = SCIMPatchOp(Operations=[SCIMPatchOperation(op="remove", path=path)])
|
||||
|
||||
_, final_teams = _apply_patch_ops(existing_user=user, patch_ops=patch_ops)
|
||||
|
||||
assert final_teams == ()
|
||||
|
||||
|
||||
def test_apply_patch_ops_remove_groups_then_add_keeps_only_the_added_team():
|
||||
user = LiteLLM_UserTable(
|
||||
user_id="user-all",
|
||||
user_email="all@example.com",
|
||||
teams=["team-1", "team-2"],
|
||||
metadata={},
|
||||
)
|
||||
patch_ops = SCIMPatchOp(
|
||||
Operations=[
|
||||
SCIMPatchOperation(op="remove", path="groups"),
|
||||
SCIMPatchOperation(op="add", path="groups", value=[{"value": "team-3"}]),
|
||||
]
|
||||
)
|
||||
|
||||
_, final_teams = _apply_patch_ops(existing_user=user, patch_ops=patch_ops)
|
||||
|
||||
assert final_teams == ("team-3",)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"operation",
|
||||
[
|
||||
SCIMPatchOperation(op="remove", path="groups", value=[]),
|
||||
SCIMPatchOperation(op="add", path="groups"),
|
||||
],
|
||||
ids=["remove-empty-list", "add-without-value"],
|
||||
)
|
||||
def test_apply_patch_ops_groups_op_naming_no_team_removes_none(operation):
|
||||
user = LiteLLM_UserTable(
|
||||
user_id="user-all",
|
||||
user_email="all@example.com",
|
||||
teams=["team-1", "team-2"],
|
||||
metadata={},
|
||||
)
|
||||
|
||||
_, final_teams = _apply_patch_ops(
|
||||
existing_user=user, patch_ops=SCIMPatchOp(Operations=[operation])
|
||||
)
|
||||
|
||||
assert final_teams == ("team-1", "team-2")
|
||||
|
||||
|
||||
def test_apply_patch_ops_add_group_filtered_path_without_value():
|
||||
"""A filtered add path with no body value adds the team id from the filter."""
|
||||
user = LiteLLM_UserTable(
|
||||
|
|
|
|||
|
|
@ -3773,6 +3773,37 @@ async def test_process_group_patch_operations_remove_uses_members_with_roles(moc
|
|||
assert final_members == {"keep-user"}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("path", ["members", "Members"])
|
||||
async def test_process_group_patch_operations_remove_members_without_value_or_filter_removes_every_member(
|
||||
mocker, path
|
||||
):
|
||||
"""RFC 7644 section 3.5.2.2: a remove on a multi-valued attribute with no
|
||||
filter removes all of its values"""
|
||||
existing_team = LiteLLM_TeamTable(
|
||||
team_id="team-1",
|
||||
team_alias="Team One",
|
||||
members=[],
|
||||
members_with_roles=[
|
||||
Member(user_id="member-1", role="user"),
|
||||
Member(user_id="member-2", role="user"),
|
||||
],
|
||||
)
|
||||
patch_ops = SCIMPatchOp(
|
||||
schemas=["urn:ietf:params:scim:api:messages:2.0:PatchOp"],
|
||||
Operations=[SCIMPatchOperation(op="remove", path=path)],
|
||||
)
|
||||
|
||||
_, final_members, replace_target = await _process_group_patch_operations(
|
||||
patch_ops=patch_ops,
|
||||
existing_team=existing_team,
|
||||
prisma_client=mocker.MagicMock(),
|
||||
)
|
||||
|
||||
assert final_members == set()
|
||||
assert replace_target is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_groups_reports_members_from_members_with_roles(mocker):
|
||||
"""GET /Groups must report members from members_with_roles (the source of
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue