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): let a legitimate multi-issuer collision through the pre-flight (CHOO-2624)
Wojciech Wojtyniakc8e94fe6fc1c
diff --git a/core/switch_core/migrations/versions/04f27f37e474_oidc_identities_table.py b/core/switch_core/migrations/versions/04f27f37e474_oidc_identities_table.pyindex 74c77e089..979ffa7e1 100644--- a/core/switch_core/migrations/versions/04f27f37e474_oidc_identities_table.py+++ b/core/switch_core/migrations/versions/04f27f37e474_oidc_identities_table.py@@ -27,20 +27,22 @@ 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 towhichever 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.+refuses to proceed rather than guess whenever a subject shared by more than+one user is actually ambiguous: either the same ``(iss, sub)`` pair repeats+(a real collision — it would die on the new unique constraint anyway, but+with a bare violation instead of a message naming which users are involved),+or the group includes a NULL issuer (a legacy row sharing a subject with+another row of any kind, which would migrate cleanly on both sides and then+never resolve on the legacy side again, since a later login always matches a+recorded issuer first). 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.++A subject shared by rows that each carry a distinct, non-NULL issuer is not+ambiguous — a later login matches exactly one of them by its exact+``(iss, sub)`` pair — and the tenant work ahead is exactly what will make+that a normal shape once more than one issuer exists, so this migration must+not refuse it: the check allows that case through.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@@ -71,30 +73,32 @@ def upgrade() -> None:WHERE metadata ? 'oidc_sub'GROUP BY metadata->>'oidc_sub'HAVING count(*) > 1+ AND (+ count(*) FILTER (WHERE metadata->>'oidc_iss' IS NULL) > 0+ OR count(*) FILTER (WHERE metadata->>'oidc_iss' IS NOT NULL)+ <> count(DISTINCT metadata->>'oidc_iss')+ FILTER (WHERE metadata->>'oidc_iss' IS NOT NULL)+ )""")).fetchall()if duplicates:raise RuntimeError("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: "+ "names the same subject in a way that is genuinely ambiguous — "+ "either an identical (iss, sub) pair repeats (which would "+ "violate the unique constraint this migration adds anyway), or "+ "a legacy (no-issuer) row shares the subject with another row, "+ "which would migrate cleanly on both sides and then never be "+ "reachable again, since a later login always matches a recorded "+ "issuer 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 "+ + ". Resolve by hand — decide which account actually owns the ""subject and clear oidc_sub from users.metadata for the "- "other(s); for a coincidence, disambiguate the values yourself "- "(this migration will not do it for you) — then re-run."+ "other(s) — then re-run this migration.")op.create_table(diff --git a/core/tests/switch_core/test_migration_oidc_identities_backfill.py b/core/tests/switch_core/test_migration_oidc_identities_backfill.pyindex 39a0fc970..312f9d556 100644--- a/core/tests/switch_core/test_migration_oidc_identities_backfill.py+++ b/core/tests/switch_core/test_migration_oidc_identities_backfill.py@@ -165,6 +165,56 @@ async def test_backfill_refuses_when_two_users_share_a_legacy_subject(await engine.dispose()+async def test_backfill_allows_two_distinct_issuers_sharing_a_subject(+ backfill_url: str,+) -> None:+ # Two full-pair rows sharing a subject string but naming two different,+ # non-NULL issuers are not ambiguous: UNIQUE(iss, sub) is satisfied by+ # both, the NULL-issuer partial index doesn't apply to either, and a+ # later login matches exactly one of them by its exact (iss, sub) pair.+ # This is also the shape the coming multi-issuer tenant work will make+ # routine, so the pre-flight must not refuse it.+ 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+ ('issuer-a-user', 'A', '[email protected]', 'user',+ NULL,+ '{"oidc_iss": "https://idp-a.example", "oidc_sub": "shared-across-issuers"}'),+ ('issuer-b-user', 'B', '[email protected]', 'user',+ NULL,+ '{"oidc_iss": "https://idp-b.example", "oidc_sub": "shared-across-issuers"}')+ """+ )+ )++ async with engine.begin() as connection:+ await connection.run_sync(_upgrade_to(_MIGRATION_UNDER_TEST))++ async with engine.connect() as connection:+ identities = (+ await connection.execute(+ text(+ "SELECT user_id, iss, sub FROM oidc_identities ORDER BY user_id"+ )+ )+ ).all()+ finally:+ await engine.dispose()++ assert identities == [+ ("issuer-a-user", "https://idp-a.example", "shared-across-issuers"),+ ("issuer-b-user", "https://idp-b.example", "shared-across-issuers"),+ ]++async def test_backfill_refuses_when_two_users_share_an_identical_full_pair(backfill_url: str,) -> None: