Skip to content

fix(scim): prune deleted user from teams' members_with_roles - #34180

Merged
yassin-berriai merged 1 commit into
litellm_internal_stagingfrom
litellm_scim_delete_user_mwr
Jul 22, 2026
Merged

yassin-berriai merged 1 commit into
litellm_internal_stagingfrom
litellm_scim_delete_user_mwr

Conversation

@yassin-berriai

@yassin-berriai yassin-berriai commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Relevant issues

#34162

Linear ticket

Part of LIT-4676

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • My PR passes all CI/CD checks (e.g., lint, format, unit tests)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

Independent e2e (Devin): after DELETE /Users the deleted user is pruned from the team's members_with_roles and no longer returned by GET /Groups (parent left the stale member).

proof

SCIM delete_user used to remove the user from the legacy team.members String[] column and delete their LiteLLM_TeamMembership rows, but it never pruned members_with_roles, which is the source of truth ScimTransformations.transform_litellm_team_to_scim_group reads for GET /Groups/{id}. So a deleted user kept showing up as a dangling member reference on every team they belonged to. The prune now runs before the user row is deleted, through team_member_delete, and a genuine prune failure surfaces instead of being swallowed, so we never report a successful delete while leaving a stale member (SCIM DELETE is idempotent, so the IdP retries). This bug lives entirely in the SCIM management path with no LLM inference, so the faithful end-user reproduction is curl against a live proxy plus a direct look at the members_with_roles column in Postgres

Live proxy on 127.0.0.1:4292, backed by a local Postgres, master-key auth

Before (bug), captured at commit 2b2ae4ca491278ed9e88227bb166a5c8c3c29279

# create user + group with the user as a member, then:
$ docker exec pg psql -U litellm -d litellm -t -c \
    "SELECT members_with_roles FROM \"LiteLLM_TeamTable\" WHERE team_id='team-1';"
 [{"role": "user", "user_id": "user-1", "user_email": null}]

$ curl -s -o /dev/null -w "delete http=%{http_code}\n" -X DELETE \
    127.0.0.1:4292/scim/v2/Users/user-1 -H "Authorization: Bearer $KEY"
delete http=204

$ docker exec pg psql -U litellm -d litellm -t -c \
    "SELECT members_with_roles FROM \"LiteLLM_TeamTable\" WHERE team_id='team-1';"
 [{"role": "user", "user_id": "user-1", "user_email": null}]   # BUG: user still present

$ curl -s 127.0.0.1:4292/scim/v2/Groups/team-1 -H "Authorization: Bearer $KEY"
members: [{'value': 'user-1', 'display': 'user-1'}]            # BUG: dangling member

$ curl ... /scim/v2/Users/user-1   ->  GET user http=404       # the user really is gone

After (fixed), captured at commit 0358d8739fe6e47de2d7030dfafa2b1e260bb10e

Scenario A, normal delete leaves no dangling member

$ docker exec pg psql ... "SELECT members_with_roles ... team_id='teamA';"
 [{"role": "user", "user_id": "userA", "user_email": null}]

$ curl -X DELETE .../scim/v2/Users/userA        ->  delete http=204

$ docker exec pg psql ... "SELECT members_with_roles ... team_id='teamA';"
 []                                              # fixed: pruned from source of truth

$ curl .../scim/v2/Groups/teamA                 ->  members: []
$ curl .../scim/v2/Users/userA                  ->  GET user http=404

Scenario B, a prune-path failure surfaces and the user is kept (fault injected by corrupting the team's members_with_roles so the prune cannot complete)

$ docker exec pg psql ... "SELECT members_with_roles ... team_id='teamB';"
 [{"role": "user", "user_id": "userB", "user_email": null}]

$ docker exec pg psql -c "UPDATE \"LiteLLM_TeamTable\" SET members_with_roles='[42]'::jsonb WHERE team_id='teamB';"

$ curl -X DELETE .../scim/v2/Users/userB
delete http=500
{"error":{"message":"1 validation error for LiteLLM_TeamTable ... members_with_roles.0 ...","type":"internal_server_error", ...}}

$ curl .../scim/v2/Users/userB                  ->  GET user http=200   # user kept, not silently dropped with a dangling member

Type

🐛 Bug Fix

Changes

delete_user now prunes each of the user's teams before deleting the user row. For every team the user belongs to whose members_with_roles actually contains them, it calls team_member_delete (with a PROXY_ADMIN UserAPIKeyAuth, the same pattern the sibling SCIM group code uses), which keeps members_with_roles, user.teams, and LiteLLM_TeamMembership consistent, so the source of truth GET /Groups/{id} reads no longer references a deleted user. Teams the user is not a member of are skipped, so a stale legacy team.members entry can't turn the delete into an error. The legacy team.members cleanup stays because that column is still queried elsewhere (list_team_v2)

The prune runs directly rather than through patch_team_membership, which logs and suppresses every team_member_delete error. Under that helper a genuine DB failure mid-prune left a dangling member while the endpoint still returned 204. Routing directly lets a real failure propagate through the endpoint's exception handler so it fails loudly and the user row is not deleted; SCIM DELETE is idempotent, so the IdP retries. patch_team_membership is unchanged for its other callers

Three regression tests cover it: test_delete_user_prunes_members_with_roles (a member is removed via team_member_delete and the user is deleted), test_delete_user_surfaces_prune_failure_and_keeps_user (a raised prune error surfaces and the user row is not deleted), and test_delete_user_skips_teams_where_not_a_member (a user absent from members_with_roles is a no-op and the delete still succeeds). All three fail on the pre-fix behavior and pass with the fix

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

@yassin-berriai

Copy link
Copy Markdown
Contributor Author

@greptileai

@greptile-apps

greptile-apps Bot commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR removes deleted SCIM users from team membership data. The main changes are:

  • Prunes matching members_with_roles entries before deleting the user
  • Propagates membership cleanup failures instead of returning success
  • Adds tests for successful cleanup, cleanup failures, and stale legacy membership

Confidence Score: 5/5

This looks safe to merge.

  • The direct cleanup call no longer swallows membership pruning failures.
  • User deletion occurs only after all matching team cleanups complete.
  • No blocking issue was found in the updated code.

Important Files Changed

Filename Overview
litellm/proxy/management_endpoints/scim/scim_v2.py Prunes each matching team membership before deleting the SCIM user and stops deletion when pruning fails
tests/test_litellm/proxy/management_endpoints/scim/test_scim_v2_endpoints.py Adds tests for membership cleanup delegation, failure propagation, and stale legacy membership

Reviews (3): Last reviewed commit: "fix(scim): surface delete_user team-prun..." | Re-trigger Greptile

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@greptile-apps

greptile-apps Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR updates SCIM user deletion to clean up team membership state. The main changes are:

  • Removes the deleted user from each known team through the shared membership helper
  • Keeps the legacy team member cleanup in place
  • Adds a focused test for the new helper call

Confidence Score: 4/5

A failed team cleanup can leave a dangling member after the user deletion reports success.

  • The successful path calls the correct existing membership helper.
  • Per-team helper failures are logged and suppressed before the user row is deleted.
  • The new test mocks the operation responsible for the persisted cleanup.

litellm/proxy/management_endpoints/scim/scim_v2.py and its SCIM endpoint test

Important Files Changed

Filename Overview
litellm/proxy/management_endpoints/scim/scim_v2.py Adds shared team-membership cleanup to SCIM user deletion, but per-team failures can be suppressed before the user is deleted
tests/test_litellm/proxy/management_endpoints/scim/test_scim_v2_endpoints.py Adds a single-team delegation test without checking the resulting persisted membership state

Reviews (2): Last reviewed commit: "fix(scim): prune deleted user from teams..." | Re-trigger Greptile

@codecov

codecov Bot commented Jul 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_scim_delete_user_mwr (9e702e8) with litellm_internal_staging (0fcaadf)1

Open in CodSpeed

Footnotes

  1. No successful run was found on litellm_internal_staging (d514497) during the generation of this report, so 0fcaadf was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@yassin-berriai

Copy link
Copy Markdown
Contributor Author

@greptileai please review the current head 0358d87

SCIM delete_user removed the user from the legacy team.members column and deleted
their team membership rows, but never pruned members_with_roles, which is the
source of truth ScimTransformations reads for GET /Groups/{id}. A deleted user
therefore lingered as a dangling member reference on every team they belonged to

Prune each of the user's teams directly via team_member_delete before deleting
the user row, and only for teams whose members_with_roles actually contain the
user, so a real DB failure surfaces (the endpoint fails loudly and the user is
kept; SCIM DELETE is idempotent, so the IdP retries) while a user who was never
in a team's members_with_roles stays a no-op. patch_team_membership is left
unchanged for its other callers
@yassin-berriai
yassin-berriai force-pushed the litellm_scim_delete_user_mwr branch from 0358d87 to 9e702e8 Compare July 22, 2026 21:10
@yassin-berriai
yassin-berriai merged commit 482f051 into litellm_internal_staging Jul 22, 2026
80 checks passed
@yassin-berriai
yassin-berriai deleted the litellm_scim_delete_user_mwr branch July 22, 2026 21:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants