Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.
Python agent-orchestrationaiai-agentschatopsdiscordlangchainmcpmicrosoft-teamsmulti-agentopen-sourceopenaiself-hostedslack
fix(gateway): fix flaky email-tiebreak test, widen the migration pre-flight, and add missing 503 coverage (CHOO-2624)
Wojciech Wojtyniak290c568d5323
diff --git a/core/switch_core/db/stores/user_store.py b/core/switch_core/db/stores/user_store.pyindex 406c5bc51..1ea987d91 100644--- a/core/switch_core/db/stores/user_store.py+++ b/core/switch_core/db/stores/user_store.py@@ -56,28 +56,32 @@ class UserStore:``users.email`` is still case-sensitively unique, so two rowsdiffering only in case can already exist from before this method- compared case-insensitively. A login must not turn that into a 500,- but silently guessing between two real accounts — one of which could- be an admin — is exactly the kind of quiet fallback CLAUDE.md rules- out: ordering is made deterministic (oldest account wins, as the- presumed original) and the ambiguity itself is logged loudly so it+ compared case-insensitively. A login must not turn that into a 500;+ neither should it silently guess between two real accounts — one of+ which could be an admin — since a random pick that happens to change+ between calls is a worse failure mode than a wrong-but-stable one, and+ a wrong one nobody hears about is worse than either. So the pick is+ made deterministic — ordered by ``id``, which is an arbitrary but+ stable tiebreak, not a claim that the lower id is the "real" or+ original account — and the ambiguity itself is logged loudly so itgets noticed and cleaned up rather than repeating unnoticed on everylogin."""result = await session.execute(select(User).where(func.lower(User.email) == email.lower())- .order_by(User.created_at, User.id)+ .order_by(User.id))users = result.scalars().all()if len(users) > 1:logger.error("Multiple users share email %r case-insensitively (ids: %s); "- "returning the oldest. This is pre-existing duplicate data, "- "not something this login caused — merge or rename the "- "extra account(s).",+ "returning %s (lowest id, an arbitrary but stable pick). This "+ "is pre-existing duplicate data, not something this login "+ "caused — merge or rename the extra account(s).",email,[u.id for u in users],+ users[0].id,)return users[0] if users else Nonediff --git a/core/switch_core/migrations/versions/04f27f37e474_oidc_identities_table.py b/core/switch_core/migrations/versions/04f27f37e474_oidc_identities_table.pyindex d7c9b621f..74c77e089 100644--- a/core/switch_core/migrations/versions/04f27f37e474_oidc_identities_table.py+++ b/core/switch_core/migrations/versions/04f27f37e474_oidc_identities_table.py@@ -26,11 +26,21 @@ out or forked into a second account by a changed email.considers two NULLs equal — so a separate partial unique index enforces atmost one such row per subject; without it, two different users sharing alegacy subject would both migrate cleanly and a login would then resolve to-whichever one the lookup happened to see first. Two users cannot share a-legacy subject in practice (each was some real person's login), so this-refuses to proceed rather than guess which one is right if it ever finds one:-that is a data problem for a human to resolve by hand, not one this migration-should paper over by silently picking a side.+whichever one the lookup happened to see first. The pre-flight check below+refuses to proceed rather than guess if it ever finds two users sharing an+``oidc_sub`` value at all, not only the no-issuer case: two full pairs+sharing the same ``(iss, sub)`` would otherwise die on the new unique+constraint with a bare violation instead of a message naming which users are+involved, and a legacy row sharing a subject with a full-pair row would+migrate cleanly on *both* sides and then never resolve on the legacy side+again — later logins would always match the full pair first, silently+orphaning whichever account only the legacy row named. Either way that is a+data problem for a human to resolve by hand, not one this migration should+paper over by silently picking a side. The check also catches — and blocks —+the rare legitimate case of two different issuers happening to assign the+same subject string to two different users: telling that apart from a real+collision automatically isn't implemented, so a human has to look and+disambiguate it by hand either way.The email column also gains a case-insensitive index: linking now matches anexisting account by email, and a compare that ignored case here would defeat@@ -54,9 +64,11 @@ def upgrade() -> None:duplicates = connection.execute(sa.text("""- SELECT metadata->>'oidc_sub' AS sub, count(*) AS n+ SELECT metadata->>'oidc_sub' AS sub,+ count(*) AS n,+ array_agg(metadata->>'oidc_iss') AS issuersFROM users- WHERE metadata ? 'oidc_sub' AND NOT (metadata ? 'oidc_iss')+ WHERE metadata ? 'oidc_sub'GROUP BY metadata->>'oidc_sub'HAVING count(*) > 1"""@@ -64,13 +76,25 @@ def upgrade() -> None:).fetchall()if duplicates:raise RuntimeError(- "Refusing to migrate: more than one user shares the same legacy "- "oidc_sub with no recorded issuer, which cannot be told apart by "- "subject alone: "- + ", ".join(f"{row.sub!r} ({row.n} users)" for row in duplicates)- + ". Resolve by hand — decide which account actually owns each "+ "Refusing to migrate: more than one user's oidc_sub metadata "+ "names the same subject, and this migration cannot safely tell "+ "them apart — two users sharing an identical (iss, sub) pair "+ "would violate the unique constraint this migration adds, and a "+ "legacy (no-issuer) row sharing a subject with a full pair would "+ "migrate cleanly but then never be reachable again, since a "+ "later login always matches the full pair first: "+ + ", ".join(+ f"{row.sub!r}: issuers {[iss or '<no issuer>' for iss in row.issuers]}"+ for row in duplicates+ )+ + ". This includes the rare legitimate case of two different "+ "issuers happening to assign the same subject string to two "+ "different users — telling that apart from a real collision "+ "automatically isn't implemented, so it blocks here too. Resolve "+ "by hand: for a real collision, decide which account owns the ""subject and clear oidc_sub from users.metadata for the "- "other(s) — then re-run this migration."+ "other(s); for a coincidence, disambiguate the values yourself "+ "(this migration will not do it for you) — then re-run.")op.create_table(diff --git a/core/tests/switch_core/db/stores/test_user_store_oidc.py b/core/tests/switch_core/db/stores/test_user_store_oidc.pyindex 39dd8ad3a..4b7ad6bcf 100644--- a/core/tests/switch_core/db/stores/test_user_store_oidc.py+++ b/core/tests/switch_core/db/stores/test_user_store_oidc.py@@ -158,25 +158,33 @@ class TestGetOrCreateOidcUser:# users.email is still case-sensitively unique, so two rows differing# only by case can exist from before get_by_email compared# case-insensitively (or from a path that bypasses this store- # entirely). A login must not crash on that, but silently guessing- # between two real accounts is exactly the quiet fallback CLAUDE.md- # rules out: the choice must be deterministic and disclosed.+ # entirely). A login must not crash on that, and it must not silently+ # guess between two real accounts either — a pick that could change+ # between calls, or that nobody is ever told about, is worse than a+ # stable pick that gets logged. This asserts stability and disclosure,+ # not "the right one": which id sorts lowest is arbitrary, so the test+ # doesn't assign either row a role that implies it should win.store = UserStore()async with session_factory() as session:- older = User(name="Old", email="[email protected]", role="admin")- newer = User(name="New", email="[email protected]", role="user")- await store.create(session, older)- await store.create(session, newer)+ first = User(name="First", email="[email protected]", role="user")+ second = User(name="Second", email="[email protected]", role="user")+ await store.create(session, first)+ await store.create(session, second)await session.commit()+ expected = min(first.id, second.id)with caplog.at_level(logging.ERROR, logger="switch_core.db.stores.user_store"):- found = await store.get_by_email(session, "[email protected]")+ # Called twice: the point being tested is that this is a+ # stable pick, not one that happens to vary between calls.+ first_call = await store.get_by_email(session, "[email protected]")+ second_call = await store.get_by_email(session, "[email protected]")- assert found is not None and found.id == older.id+ assert first_call is not None and first_call.id == expected+ assert second_call is not None and second_call.id == expectedassert any(- older.id in record.message and newer.id in record.message+ first.id in record.message and second.id in record.messagefor record in caplog.records)diff --git a/core/tests/switch_core/gateway/test_oidc_routes.py b/core/tests/switch_core/gateway/test_oidc_routes.pyindex 44a04b6c3..869c68f31 100644--- a/core/tests/switch_core/gateway/test_oidc_routes.py+++ b/core/tests/switch_core/gateway/test_oidc_routes.py@@ -9,7 +9,7 @@ from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmakerimport switch_core.gateway.oidc_routes as oidc_routesfrom switch_core.config import SwitchConfigfrom switch_core.db.models import OidcIdentity, User-from switch_core.db.stores.user_store import UserStore+from switch_core.db.stores.user_store import OidcIdentityRaceError, UserStorefrom switch_core.gateway.auth import hash_passwordfrom switch_core.gateway.auth_routes import auth_config@@ -358,6 +358,41 @@ class TestOidcCallback:assert user.role == "admin"assert user.email == "[email protected]"+ async def test_identity_race_contention_maps_to_503(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ monkeypatch: pytest.MonkeyPatch,+ ) -> None:+ # A raced login is transient contention, not a rejected security+ # decision (that's OidcIdentityConflictError's 409) — it must not+ # surface as a generic 500, and it must be told apart from a 409 so+ # an operator can distinguish a retry storm from an attack signal.+ token = {+ "userinfo": {+ "email": "[email protected]",+ "email_verified": True,+ "sub": "okta|race-503",+ "name": "Race",+ }+ }+ monkeypatch.setattr(oidc_routes, "_client", lambda: _FakeClient(token))++ class _AlwaysRacingStore:+ async def get_or_create_oidc_user(self, *args: object, **kwargs: object):+ raise OidcIdentityRaceError("raced twice in a row")++ async with session_factory() as session:+ with pytest.raises(HTTPException) as exc:+ await oidc_routes.oidc_callback(+ request=SimpleNamespace(), # type: ignore[arg-type]+ config=_config(),+ session=session,+ user_store=_AlwaysRacingStore(), # type: ignore[arg-type]+ )+ assert exc.value.status_code == 503+ assert exc.value.headers is not None+ assert exc.value.headers.get("Retry-After") == "1"+async def test_missing_email_claim_raises_401(self,session_factory: async_sessionmaker[AsyncSession],diff --git a/core/tests/switch_core/test_migration_oidc_identities_backfill.py b/core/tests/switch_core/test_migration_oidc_identities_backfill.pyindex 785c3bfa7..39a0fc970 100644--- a/core/tests/switch_core/test_migration_oidc_identities_backfill.py+++ b/core/tests/switch_core/test_migration_oidc_identities_backfill.py@@ -163,3 +163,89 @@ async def test_backfill_refuses_when_two_users_share_a_legacy_subject(assert table_exists.scalar() is Falsefinally:await engine.dispose()+++async def test_backfill_refuses_when_two_users_share_an_identical_full_pair(+ backfill_url: str,+) -> None:+ # Two users with the exact same (iss, sub) would otherwise die on the new+ # UNIQUE(iss, sub) constraint mid-migration with a bare violation and no+ # indication which users are involved; the pre-flight check must catch+ # this case too, not only the no-issuer one.+ engine = create_async_engine(backfill_url)+ try:+ async with engine.begin() as connection:+ await connection.run_sync(_upgrade_to(_PRE_MIGRATION_REVISION))++ async with engine.begin() as connection:+ await connection.execute(+ text(+ """+ INSERT INTO users (id, name, email, role, password_hash, metadata)+ VALUES+ ('full-a', 'A', '[email protected]', 'admin', NULL,+ '{"oidc_iss": "https://idp.example", "oidc_sub": "okta|shared-full"}'),+ ('full-b', 'B', '[email protected]', 'user', NULL,+ '{"oidc_iss": "https://idp.example", "oidc_sub": "okta|shared-full"}')+ """+ )+ )++ with pytest.raises(RuntimeError, match="okta\\|shared-full"):+ async with engine.begin() as connection:+ await connection.run_sync(_upgrade_to(_MIGRATION_UNDER_TEST))++ async with engine.connect() as connection:+ table_exists = await connection.execute(+ text(+ "SELECT to_regclass('public.oidc_identities') IS NOT NULL AS exists"+ )+ )+ assert table_exists.scalar() is False+ finally:+ await engine.dispose()+++async def test_backfill_refuses_when_a_legacy_row_shares_a_subject_with_a_full_pair(+ backfill_url: str,+) -> None:+ # The case that previously failed silently: a legacy (no-issuer) row and+ # a full-pair row sharing one subject would both migrate cleanly on their+ # own — no constraint stops either insert — and the legacy row would then+ # be permanently unreachable, since a later login always matches the+ # full pair via the exact (iss, sub) lookup first. The pre-flight must+ # catch this even though neither row alone, nor together, would trip+ # either unique index at insert time.+ engine = create_async_engine(backfill_url)+ try:+ async with engine.begin() as connection:+ await connection.run_sync(_upgrade_to(_PRE_MIGRATION_REVISION))++ async with engine.begin() as connection:+ await connection.execute(+ text(+ """+ INSERT INTO users (id, name, email, role, password_hash, metadata)+ VALUES+ ('orphaned-legacy', 'Legacy', '[email protected]',+ 'admin', NULL, '{"oidc_sub": "okta|mixed-shared"}'),+ ('winning-full', 'Full', '[email protected]', 'user',+ NULL,+ '{"oidc_iss": "https://idp.example", "oidc_sub": "okta|mixed-shared"}')+ """+ )+ )++ with pytest.raises(RuntimeError, match="okta\\|mixed-shared"):+ async with engine.begin() as connection:+ await connection.run_sync(_upgrade_to(_MIGRATION_UNDER_TEST))++ async with engine.connect() as connection:+ table_exists = await connection.execute(+ text(+ "SELECT to_regclass('public.oidc_identities') IS NOT NULL AS exists"+ )+ )+ assert table_exists.scalar() is False+ finally:+ await engine.dispose()