fix(users): preserve SSO admission when rejecting duplicate IDs

This commit is contained in:
Mrinal Chanshetty 2026-11-02 15:46:51 -08:00
parent 4674967246
commit fffcedc742
9 changed files with 109 additions and 41 deletions

View file

@ -605,9 +605,7 @@ async def new_user(
teams = check_if_default_team_set()
organization_ids: Final = cast(list[str] | None, data_json.pop("organizations", None))
response: Final = await generate_key_helper_fn(
request_type="user", **data_json, llm_router=None, query_type="create_data"
)
response: Final = await generate_key_helper_fn(request_type="user", **data_json, llm_router=None)
created_user_id: Final = cast(str | None, response.get("user_id", None))
if created_user_id is not None:
forget_missing_user(created_user_id)

View file

@ -5268,7 +5268,7 @@ async def generate_key_helper_fn(
tpm_limit: int | None = None,
rpm_limit: int | None = None,
tpd_limit: int | None = None,
query_type: Literal["insert_data", "update_data", "create_data"] = "insert_data",
query_type: Literal["insert_data", "update_data"] = "insert_data",
update_key_values: dict | None = None,
key_alias: str | None = None,
allowed_cache_controls: list | None = [],
@ -5479,28 +5479,12 @@ async def generate_key_helper_fn(
if prisma_client is not None:
if table_name is None or table_name == "user": # do not auto-create users for `/key/generate`
## CREATE USER (If necessary)
if query_type == "create_data":
from prisma.errors import ForeignKeyViolationError, UniqueViolationError
create_data: Final = cast( # cast-ok: locally built user fields have string keys
Mapping[str, object], user_data
)
try:
await UserRepository(prisma_client).create(prisma_client.jsonify_object(data=create_data))
except UniqueViolationError as exc:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail={"error": f"User with id {user_id} already exists"},
) from exc
except ForeignKeyViolationError as exc:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail={"error": "A referenced user resource does not exist."},
) from exc
elif query_type == "insert_data":
if query_type == "insert_data":
user_row = cast( # cast-ok: table_name="user" is the insert_data branch returning the user row
"prisma_models.LiteLLM_UserTable | None",
await prisma_client.insert_data(data=user_data, table_name="user"),
await prisma_client.insert_data(
data=user_data, table_name="user", allow_existing_user=request_type != "user"
),
)
if user_row is None:

View file

@ -6351,6 +6351,8 @@ class PrismaClient:
self,
data: Mapping[str, object],
table_name: Literal["user", "key", "config", "spend", "team", "user_notification"],
*,
allow_existing_user: bool = True,
):
"""
Add a key to the database. If it already exists, do nothing.
@ -6386,14 +6388,23 @@ class PrismaClient:
verbose_proxy_logger.info("Data Inserted into Keys Table")
return new_verification_token
elif table_name == "user":
from prisma.errors import UniqueViolationError
db_data = self.jsonify_object(data=data)
try:
new_user_row: Final = await UserRepository(self).upsert_user(db_data)
new_user_row: Final = await UserRepository(self).upsert_user(
db_data, allow_existing=allow_existing_user
)
if new_user_row is None:
raise HTTPException(
status_code=403,
detail="SSO for more than 5 users requires a LiteLLM Enterprise license. Existing users can still sign in.",
)
except UniqueViolationError as e:
raise HTTPException(
status_code=409,
detail={"error": f"User with id {db_data['user_id']} already exists"},
) from e
except Exception as e:
if (
"Foreign key constraint failed on the field: `LiteLLM_UserTable_organization_id_fkey (index)`"

View file

@ -168,7 +168,9 @@ class UserRepository(BaseRepository[LiteLLM_UserTable]):
"""
return await _count_billable_users(self.table)
async def upsert_user(self, data: Mapping[str, object]) -> "prisma_models.LiteLLM_UserTable | None":
async def upsert_user(
self, data: Mapping[str, object], *, allow_existing: bool = True
) -> "prisma_models.LiteLLM_UserTable | None":
"""Return the user row, or None when a new free SSO user would exceed capacity."""
scope: Final = _FREE_SSO_CREATION.get()
if (
@ -176,6 +178,8 @@ class UserRepository(BaseRepository[LiteLLM_UserTable]):
or (scope[0] is not None and scope[0] != data["user_id"])
or scope[1] is not asyncio.current_task()
):
if not allow_existing:
return await self.table.create(data=dict(data))
return await self.table.upsert(
where={"user_id": data["user_id"]}, data={"create": dict(data), "update": {}}
)

View file

@ -120,6 +120,7 @@ class BulkNewUserItem(NewUserRequest):
model_config = ConfigDict(extra="forbid", protected_namespaces=())
user_id: str | None = None
auto_create_key: bool = False
@field_validator("send_invite_email")

View file

@ -1,7 +1,7 @@
from collections.abc import Mapping
from typing import Final, Literal
from typing import Annotated, Final, Literal
from pydantic import Field, field_validator, model_validator
from pydantic import AfterValidator, Field, field_validator, model_validator
from litellm.models.user import LiteLLM_UserTable
from litellm.types.proxy.auth.user_roles import LitellmUserRoles
@ -15,7 +15,17 @@ class NewUserRequestTeam(LiteLLMPydanticObjectBase):
user_role: Literal["user", "admin"] = "user"
def _validate_custom_user_id(value: str) -> str:
normalized: Final = value.strip()
if not normalized:
raise ValueError("user_id must not be empty or contain only whitespace")
if "\x00" in normalized:
raise ValueError("user_id must not contain NUL characters")
return normalized
class NewUserRequest(GenerateRequestBase):
user_id: Annotated[str, AfterValidator(_validate_custom_user_id)] | None = None
tpm_limit: int | None = Field(default=None, ge=0)
rpm_limit: int | None = Field(default=None, ge=0)
max_budget: float | None = None
@ -37,18 +47,6 @@ class NewUserRequest(GenerateRequestBase):
organizations: list[str] | None = None
password: str | None = None
@field_validator("user_id")
@classmethod
def validate_user_id(cls, value: str | None) -> str | None:
if value is None:
return None
normalized: Final = value.strip()
if not normalized:
raise ValueError("user_id must not be empty or contain only whitespace")
if "\x00" in normalized:
raise ValueError("user_id must not contain NUL characters")
return normalized
@field_validator("password")
@classmethod
def password_not_supported(cls, value: str | None) -> str | None:

View file

@ -4783,7 +4783,9 @@ async def test_user_new_persists_model_max_budget(monkeypatch, model_max_budget,
models = []
class _FakePrisma:
async def insert_data(self, data, table_name):
async def insert_data(
self, data: Mapping[str, object], table_name: str, *, allow_existing_user: bool = True
) -> _FakeUserRow | SimpleNamespace:
if table_name == "user":
captured["user_data"] = dict(data)
return _FakeUserRow()

View file

@ -22,6 +22,7 @@ from litellm.proxy._types import (
LiteLLM_JWTAuth,
LiteLLM_UserTable,
NewUserResponse,
SSOUserDefinedValues,
ProxyException,
)
from litellm.proxy.auth.handle_jwt import JWTHandler
@ -52,6 +53,62 @@ from litellm.types.proxy.management_endpoints.ui_sso import (
from tests._master_key import MASTER_KEY
@pytest.mark.asyncio
@pytest.mark.parametrize(
("billable_users", "premium", "admitted"),
[(4, False, True), (5, False, False), (26, False, False), (26, True, True)],
)
async def test_sso_auto_creation_enforces_free_user_cap_through_new_user(
monkeypatch: pytest.MonkeyPatch, billable_users: int, premium: bool, admitted: bool
) -> None:
from litellm.proxy import proxy_server
from litellm.proxy.utils import PrismaClient
row: Final = LiteLLM_UserTable(user_id="new-sso-user", user_email="new-sso-user@example.test")
table: Final = SimpleNamespace(
find_first=AsyncMock(return_value=None),
find_unique=AsyncMock(return_value=None),
count=AsyncMock(side_effect=lambda where=None: billable_users if where is None else 0),
create=AsyncMock(return_value=row),
upsert=AsyncMock(return_value=row),
)
transaction: Final = MagicMock()
transaction.__aenter__ = AsyncMock(
return_value=SimpleNamespace(litellm_usertable=table, execute_raw=AsyncMock(), query_raw=AsyncMock())
)
transaction.__aexit__ = AsyncMock(return_value=None)
monkeypatch.setattr(
"prisma.Prisma",
lambda **kwargs: SimpleNamespace(litellm_usertable=table, tx=MagicMock(return_value=transaction)),
)
database: Final = PrismaClient(
database_url="postgresql://unused:unused@localhost/unused",
proxy_logging_obj=proxy_server.proxy_logging_obj,
)
monkeypatch.setattr(proxy_server, "prisma_client", database)
monkeypatch.setattr(proxy_server, "premium_user", premium)
monkeypatch.setattr("asyncio.sleep", AsyncMock())
values: Final[SSOUserDefinedValues] = {
"models": [],
"user_id": row.user_id,
"user_email": row.user_email,
"user_role": "internal_user_viewer",
"max_budget": None,
"budget_duration": None,
}
if admitted:
response: Final = await insert_sso_user(CustomOpenID(id=row.user_id, team_ids=[]), values)
assert response.user_id == row.user_id
assert table.create.await_count + table.upsert.await_count == 1
else:
with pytest.raises(ProxyException) as error:
await insert_sso_user(CustomOpenID(id=row.user_id, team_ids=[]), values)
assert str(error.value.code) == "403"
assert "SSO for more than 5 users" in error.value.message
table.create.assert_not_awaited()
table.upsert.assert_not_awaited()
def _sso_http_handler(route: Callable[[httpx.Request], httpx.Response] | None = None) -> AsyncHTTPHandler:
"""litellm's SSO httpx client answered in-process, by ``route`` or by the active respx mock."""
handler = AsyncHTTPHandler()

View file

@ -191,6 +191,19 @@ async def _run(prisma, users, caller=ADMIN, license=None, generate_key=_no_keys)
)
@pytest.mark.asyncio
@pytest.mark.parametrize("user_id", [None, "", " employee-42 ", " \t"])
async def test_bulk_keeps_existing_user_id_generation_and_whitespace_behavior(user_id: str | None) -> None:
prisma: Final = _FakePrisma()
response: Final = await _run(prisma, [{"user_id": user_id}])
result: Final = response.data[0]
assert result.success
assert result.user_id
assert result.user_id in prisma.db.litellm_usertable.rows
if user_id:
assert result.user_id == user_id
@pytest.mark.asyncio
async def test_creates_users_and_team_membership_in_every_store():
prisma = _FakePrisma(teams=[_team("t1", [Member(user_id="existing", role="admin")]), _team("t2")])