From 9d711faaeaa1efbf27ca2a19391f9dae8245dbf5 Mon Sep 17 00:00:00 2001 From: 72004 Date: Fri, 21 Aug 2026 00:01:39 +0500 Subject: [PATCH 1/4] fix: order CLI SSO team choices by alias then id for a stable consent default --- litellm/proxy/management_endpoints/ui_sso.py | 17 ++++++- .../proxy/management_endpoints/test_ui_sso.py | 47 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/litellm/proxy/management_endpoints/ui_sso.py b/litellm/proxy/management_endpoints/ui_sso.py index c515108ab11..f35af961688 100644 --- a/litellm/proxy/management_endpoints/ui_sso.py +++ b/litellm/proxy/management_endpoints/ui_sso.py @@ -2266,6 +2266,18 @@ def _cli_sso_team_detail(team_row: Mapping[str, object]) -> CliSsoTeamDetail: ) +def _cli_sso_team_sort_key(detail: CliSsoTeamDetail) -> tuple[str, str]: + """Stable, predictable order for a user's team choices. + + The DB returns rows in an arbitrary order, so the consent page (and the + classic login team choice, which shares this lookup) would preselect + whichever team happened to come back first -- silently binding a user who + takes the default to a team they did not mean to pick. Order by alias + case-insensitively, with team_id as a deterministic tiebreaker. + """ + return ((detail.team_alias or "").casefold(), detail.team_id or "") + + async def fetch_cli_sso_team_details( prisma_client: PrismaClient, teams: Sequence[str], @@ -2281,7 +2293,10 @@ async def fetch_cli_sso_team_details( except Exception as e: verbose_proxy_logger.error("Error fetching team details for CLI SSO session: %s", e) return None - return tuple(_cli_sso_team_detail(team_row.model_dump()) for team_row in prisma_teams) + details: Final = ( + _cli_sso_team_detail(team_row.model_dump()) for team_row in prisma_teams + ) + return tuple(sorted(details, key=_cli_sso_team_sort_key)) def _cli_sso_session_teams(team_details: Sequence[CliSsoTeamDetail]) -> list[str]: diff --git a/tests/unit/proxy/management_endpoints/test_ui_sso.py b/tests/unit/proxy/management_endpoints/test_ui_sso.py index 4e128440703..f9efad48dff 100644 --- a/tests/unit/proxy/management_endpoints/test_ui_sso.py +++ b/tests/unit/proxy/management_endpoints/test_ui_sso.py @@ -9771,3 +9771,50 @@ async def test_cli_sso_login_never_serves_the_admin_login_form_on_a_ui_disabled_ body = response.body.decode() assert "Admin UI is Disabled" in body assert 'name="username"' not in body + + +@pytest.mark.asyncio +async def test_fetch_cli_sso_team_details_orders_by_alias_then_id(): + """CLI SSO team choices must come back in a stable order. + + The consent page preselects the first team, so an arbitrary DB order can + silently bind a user who takes the default to a team they did not mean to + pick. Teams must be ordered by alias case-insensitively, then team_id as a + deterministic tiebreaker. Regression for the CLI login consent page. + """ + from unittest.mock import AsyncMock, MagicMock, patch + + from litellm.proxy.management_endpoints import ui_sso as ui_sso_mod + + def _row(team_id, alias): + row = MagicMock() + row.model_dump.return_value = { + "team_id": team_id, + "team_alias": alias, + "models": [], + } + return row + + # Deliberately scrambled DB order, mixed case, plus an alias-less team. + rows = [ + _row("35551", "forge-team-b-35551"), + _row("10000", "Forge-Team-A-35551"), + _row("99999", None), + ] + repo = MagicMock() + repo.find_many = AsyncMock(return_value=rows) + + with patch.object(ui_sso_mod, "TeamRepository", return_value=MagicMock()), patch.object( + ui_sso_mod, "_team_detail_db", return_value=repo + ): + result = await ui_sso_mod.fetch_cli_sso_team_details( + prisma_client=MagicMock(), teams=["35551", "10000", "99999"] + ) + + assert result is not None + # None alias sorts first (""), then case-insensitive "Forge-Team-A" < "forge-team-b". + assert [d.team_alias for d in result] == [ + None, + "Forge-Team-A-35551", + "forge-team-b-35551", + ] From 0ff1962dc151fd1a24cde71de4f3c74ab24c2cc8 Mon Sep 17 00:00:00 2001 From: 72004 Date: Fri, 21 Aug 2026 00:19:33 +0500 Subject: [PATCH 2/4] chore: apply ruff format to ui_sso.py --- litellm/proxy/management_endpoints/ui_sso.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/litellm/proxy/management_endpoints/ui_sso.py b/litellm/proxy/management_endpoints/ui_sso.py index f35af961688..c6f6a775936 100644 --- a/litellm/proxy/management_endpoints/ui_sso.py +++ b/litellm/proxy/management_endpoints/ui_sso.py @@ -2293,9 +2293,7 @@ async def fetch_cli_sso_team_details( except Exception as e: verbose_proxy_logger.error("Error fetching team details for CLI SSO session: %s", e) return None - details: Final = ( - _cli_sso_team_detail(team_row.model_dump()) for team_row in prisma_teams - ) + details: Final = (_cli_sso_team_detail(team_row.model_dump()) for team_row in prisma_teams) return tuple(sorted(details, key=_cli_sso_team_sort_key)) From adc85bcea57500ba25e45a3c0cf37e27b49414b6 Mon Sep 17 00:00:00 2001 From: 72004 Date: Fri, 21 Aug 2026 00:45:53 +0500 Subject: [PATCH 3/4] test: cover CLI SSO team sort key branches directly --- .../proxy/management_endpoints/test_ui_sso.py | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/unit/proxy/management_endpoints/test_ui_sso.py b/tests/unit/proxy/management_endpoints/test_ui_sso.py index f9efad48dff..f5285c9c294 100644 --- a/tests/unit/proxy/management_endpoints/test_ui_sso.py +++ b/tests/unit/proxy/management_endpoints/test_ui_sso.py @@ -9818,3 +9818,23 @@ async def test_fetch_cli_sso_team_details_orders_by_alias_then_id(): "Forge-Team-A-35551", "forge-team-b-35551", ] + + +def test_cli_sso_team_sort_key_case_insensitive_with_id_tiebreaker(): + """Directly exercise the sort key, including the alias-None and team_id-None + fallbacks, so ordering is stable regardless of how the DB row is shaped. + """ + from litellm.proxy.management_endpoints import ui_sso as ui_sso_mod + + CliSsoTeamDetail = ui_sso_mod.CliSsoTeamDetail + key = ui_sso_mod._cli_sso_team_sort_key + + a = CliSsoTeamDetail(team_id="2", team_alias="Alpha", team_models=()) + b = CliSsoTeamDetail(team_id="1", team_alias="alpha", team_models=()) + c = CliSsoTeamDetail(team_id=None, team_alias=None, team_models=()) + + # Both `or ""` fallbacks are taken for c. + assert key(c) == ("", "") + # Case-insensitive alias equality falls back to team_id ("1" < "2"); + # the alias-less team sorts first. + assert [t.team_id for t in sorted([a, b, c], key=key)] == [None, "1", "2"] From a63a4be2f8c5144b41426a01680616f932f2e129 Mon Sep 17 00:00:00 2001 From: 72004 Date: Tue, 22 Sep 2026 18:38:26 +0500 Subject: [PATCH 4/4] docs(ui_sso): trim redundant sort-key docstring Drop the restatement of the sort mechanics (already evident from the return line) and keep only the non-obvious why. --- litellm/proxy/management_endpoints/ui_sso.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/litellm/proxy/management_endpoints/ui_sso.py b/litellm/proxy/management_endpoints/ui_sso.py index c6f6a775936..d551d51df67 100644 --- a/litellm/proxy/management_endpoints/ui_sso.py +++ b/litellm/proxy/management_endpoints/ui_sso.py @@ -2267,13 +2267,11 @@ def _cli_sso_team_detail(team_row: Mapping[str, object]) -> CliSsoTeamDetail: def _cli_sso_team_sort_key(detail: CliSsoTeamDetail) -> tuple[str, str]: - """Stable, predictable order for a user's team choices. - - The DB returns rows in an arbitrary order, so the consent page (and the - classic login team choice, which shares this lookup) would preselect - whichever team happened to come back first -- silently binding a user who - takes the default to a team they did not mean to pick. Order by alias - case-insensitively, with team_id as a deterministic tiebreaker. + """The DB returns team rows in an arbitrary order, so without a stable + sort the consent page (and the classic login team choice, which shares + this lookup) would preselect whichever team happened to come back first -- + silently binding a user who takes the default to a team they did not mean + to pick. """ return ((detail.team_alias or "").casefold(), detail.team_id or "")