Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(db): scope the default-bridge lookup to the bound tenant (CHOO-2623)

Wojciech Wojtyniak82d1eaaaa4fd
diff --git a/core/switch_core/db/stores/collaboration_bridge_store.py b/core/switch_core/db/stores/collaboration_bridge_store.py
index 812e5e919..c62e355a4 100644
--- a/core/switch_core/db/stores/collaboration_bridge_store.py
+++ b/core/switch_core/db/stores/collaboration_bridge_store.py
@@ -1,7 +1,7 @@
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
-from switch_core.db.models import CollaborationBridge
+from switch_core.db.models import CollaborationBridge, require_tenant_id
class CollaborationBridgeStore:
@@ -29,19 +29,38 @@ class CollaborationBridgeStore:
async def get_default(self, session: AsyncSession) -> CollaborationBridge | None:
"""The bridge new rooms land on when none is named, or None when the
- instance has not nominated one."""
+ bound tenant has not nominated one.
+
+ Scoped explicitly rather than left to row-level security: `is_default`
+ is unique per tenant, not globally
+ (`ix_collaboration_bridges_single_default`), so the moment a second
+ tenant has a default bridge, an unfiltered `one_or_none()` here
+ matches both rows and raises `MultipleResultsFound` — a 500 out of
+ `create_room` and `POST /gateway/collaborations/{id}/default`, for
+ every tenant, not just the one that just onboarded. Filtering here
+ also keeps `set_default` from reading another tenant's default as its
+ own and demoting it (see there)."""
result = await session.execute(
- select(CollaborationBridge).where(CollaborationBridge.is_default.is_(True))
+ select(CollaborationBridge).where(
+ CollaborationBridge.tenant_id == require_tenant_id(),
+ CollaborationBridge.is_default.is_(True),
+ )
)
return result.scalars().one_or_none()
async def set_default(
self, session: AsyncSession, bridge_id: str
) -> CollaborationBridge:
- """Nominate ``bridge_id`` as the instance default, demoting whichever
- bridge held it before. The demotion is flushed first because a partial
- unique index enforces a single default — writing the new one while the
- old is still set would collide."""
+ """Nominate ``bridge_id`` as the bound tenant's default, demoting
+ whichever bridge held it before. The demotion is flushed first
+ because a partial unique index enforces a single default per tenant —
+ writing the new one while the old is still set would collide.
+
+ ``current`` comes from `get_default`, which is scoped to the bound
+ tenant — load-bearing here, not just for `create_room`: an unscoped
+ `get_default` would read *another* tenant's default as "the current
+ one" whenever the bound tenant had none of its own yet, and this
+ method would then demote a bridge it was never asked about."""
bridge = await session.get(CollaborationBridge, bridge_id)
if bridge is None:
raise ValueError(f"Bridge not found: {bridge_id}")
diff --git a/core/tests/switch_core/db/stores/test_collaboration_bridge_store.py b/core/tests/switch_core/db/stores/test_collaboration_bridge_store.py
index d1cfd655d..5bb9cd2ea 100644
--- a/core/tests/switch_core/db/stores/test_collaboration_bridge_store.py
+++ b/core/tests/switch_core/db/stores/test_collaboration_bridge_store.py
@@ -6,8 +6,9 @@ import uuid
import pytest
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
-from switch_core.db.models import Client, CollaborationBridge
+from switch_core.db.models import Client, CollaborationBridge, Tenant
from switch_core.db.stores.collaboration_bridge_store import CollaborationBridgeStore
+from switch_core.tenant_context import tenant_scope
async def _make_bridge(session: AsyncSession) -> str:
@@ -29,6 +30,12 @@ async def _make_bridge(session: AsyncSession) -> str:
return bridge.id
+async def _make_bridge_for_tenant(session: AsyncSession, tenant_id: str) -> str:
+ """A bridge filed under `tenant_id` rather than the ambient tenant."""
+ with tenant_scope(tenant_id):
+ return await _make_bridge(session)
+
+
@pytest.mark.asyncio
async def test_agent_greetings_enabled_defaults_true(
session_factory: async_sessionmaker[AsyncSession],
@@ -152,6 +159,78 @@ async def test_set_default_unknown_bridge_raises(
await store.set_default(session, "does-not-exist")
+async def test_get_default_does_not_choke_on_a_second_tenants_default(
+ session_factory: async_sessionmaker[AsyncSession],
+) -> None:
+ """`is_default` is unique per tenant, not globally — two tenants may each
+ have nominated one. Before scoping the read, an unfiltered `.one_or_none()`
+ matched both rows the moment a second tenant had a default and raised
+ `MultipleResultsFound`, a 500 out of `create_room` for every tenant, not
+ just the one that just onboarded.
+ """
+ store = CollaborationBridgeStore()
+ other_tenant = f"tenant-{uuid.uuid4().hex[:8]}"
+
+ async with session_factory() as session:
+ session.add(Tenant(id=other_tenant, slug=other_tenant, name=other_tenant))
+ await session.flush()
+ own_bridge = await _make_bridge(session)
+ other_bridge = await _make_bridge_for_tenant(session, other_tenant)
+ await store.set_default(session, own_bridge)
+ with tenant_scope(other_tenant):
+ await store.set_default(session, other_bridge)
+ await session.commit()
+
+ async with session_factory() as session:
+ default = await store.get_default(session)
+ assert default is not None
+ assert default.id == own_bridge
+
+ async with session_factory() as session:
+ with tenant_scope(other_tenant):
+ default = await store.get_default(session)
+ assert default is not None
+ assert default.id == other_bridge
+
+
+async def test_set_default_does_not_demote_another_tenants_default(
+ session_factory: async_sessionmaker[AsyncSession],
+) -> None:
+ """The dangerous shape of the same bug: an unscoped `get_default` reads
+ another tenant's default as "the current one" whenever the bound tenant
+ has none of its own yet, and `set_default` then demotes a bridge it was
+ never asked about — silently disabling another tenant's default the first
+ time this tenant nominates its own.
+ """
+ store = CollaborationBridgeStore()
+ other_tenant = f"tenant-{uuid.uuid4().hex[:8]}"
+
+ async with session_factory() as session:
+ session.add(Tenant(id=other_tenant, slug=other_tenant, name=other_tenant))
+ await session.flush()
+ other_bridge = await _make_bridge_for_tenant(session, other_tenant)
+ with tenant_scope(other_tenant):
+ await store.set_default(session, other_bridge)
+
+ own_bridge = await _make_bridge(session)
+ await store.set_default(session, own_bridge)
+ await session.commit()
+
+ async with session_factory() as session:
+ with tenant_scope(other_tenant):
+ other_default = await store.get(session, other_bridge)
+ assert other_default is not None
+ assert other_default.is_default is True, (
+ "the other tenant's default was demoted by a write it was never part of"
+ )
+
+ own_default = await store.get(session, own_bridge)
+ assert own_default is not None
+ assert own_default.is_default is True
+
+
@pytest.mark.asyncio
async def test_set_channel_team_merges_rather_than_replaces(
session_factory: async_sessionmaker[AsyncSession],
diff --git a/core/tests/switch_core/test_room_service_default_bridge_multi_tenant.py b/core/tests/switch_core/test_room_service_default_bridge_multi_tenant.py
new file mode 100644
index 000000000..ea3498f60
--- /dev/null
+++ b/core/tests/switch_core/test_room_service_default_bridge_multi_tenant.py
@@ -0,0 +1,201 @@
+"""`create_room` resolves the instance default bridge — now per tenant
+(CHOO-2623).
+
+`CollaborationBridgeStore.get_default` used to select `is_default` with no
+tenant filter and finish in `.one_or_none()`. That was correct as long as at
+most one bridge anywhere was ever marked default. The partial unique index
+backing `is_default` became `unique (tenant_id) where is_default`
+(`ix_collaboration_bridges_single_default`), so the moment a second tenant
+nominates its own default, the same unfiltered query matches both rows and
+raises `MultipleResultsFound` — a 500 out of `create_room`, for *every*
+tenant, including the one that already had a working default before the
+second ever onboarded.
+
+Exercised through `RoomService.create_room` against real Postgres
+(`CollaborationBridgeStore` and `RoomStore` are the production ones) rather
+than by calling `get_default` directly, because "does the room a caller asked
+for actually get created" is what regressed and what a fix has to restore.
+Only `_matrix_admin` and the collaboration bridge's own runtime handle
+(`_collab_lifecycle`) are faked — Matrix and a live bridge connection are not
+this bug's concern, and each room's `bridge_id` still has to satisfy the real
+composite foreign key to `collaboration_bridges (tenant_id, id)`, so a fix
+that resolved the *wrong* tenant's default would fail loudly here too.
+"""
+
+from __future__ import annotations
+
+import uuid
+from typing import Any
+
+import pytest
+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
+
+from switch_core.db.models import Client, CollaborationBridge, Tenant
+from switch_core.db.stores.agent_store import AgentStore
+from switch_core.db.stores.collaboration_bridge_store import CollaborationBridgeStore
+from switch_core.db.stores.room_store import RoomStore
+from switch_core.room_service import RoomCreateConfig, RoomService
+from switch_core.tenant_context import tenant_scope
+
+pytestmark = pytest.mark.no_ambient_tenant
+
+
+class _FakeMatrix:
+ """Provisioning, which room_service calls with no session open."""
+
+ def __init__(self) -> None:
+ self._n = 0
+ self.invited: list[tuple[str, str]] = []
+
+ async def create_room(self, name: str, topic: str) -> str:
+ self._n += 1
+ return f"!room-{self._n}:switch.local"
+
+ async def invite_to_room(self, room_id: str, user_id: str) -> None:
+ self.invited.append((room_id, user_id))
+
+
+class _FakeAdapter:
+ async def create_channel(
+ self, name: str, topic: str, *, channel_type: str = "channel_public"
+ ) -> str:
+ return f"chan-{uuid.uuid4().hex[:8]}"
+
+ async def add_agents_to_channel(
+ self, channel_id: str, agent_names: list[str]
+ ) -> None:
+ return None
+
+ async def ensure_channel_subscriptions(
+ self, channels: list[tuple[str, str]]
+ ) -> None:
+ return None
+
+
+class _FakeBridgeCore:
+ """The running half of a bridge — `create_room` never opens a session on
+ this, so it does not need to be the real `BridgeCore`."""
+
+ def __init__(self, matrix_user_id: str) -> None:
+ self._bridge_client_matrix_user_id = matrix_user_id
+ self.adapter = _FakeAdapter()
+ self.mappings: list[tuple[str, str, str, str]] = []
+
+ def begin_provisioning(self, external_channel_id: str) -> None:
+ return None
+
+ def end_provisioning(self, external_channel_id: str) -> None:
+ return None
+
+ def add_room_mapping(
+ self,
+ room_id: str,
+ matrix_room_id: str,
+ external_channel_id: str,
+ tenant_id: str,
+ ) -> None:
+ self.mappings.append((room_id, matrix_room_id, external_channel_id, tenant_id))
+
+
+class _FakeLifecycle:
+ def __init__(self, bridges: dict[str, _FakeBridgeCore]) -> None:
+ self._bridges = bridges
+
+ def get(self, bridge_id: str) -> Any:
+ return self._bridges.get(bridge_id)
+
+
+class _NoRunningClients:
+ """No agents, no already-running system clients — keeps the test to
+ exactly the bridge-resolution path the bug is in."""
+
+ def get_by_agent_id(self, agent_id: str) -> Any:
+ return None
+
+ def get_by_type(self, client_type: str, tenant_id: str) -> list[Any]:
+ return []
+
+
+async def _make_default_bridge(session: AsyncSession, *, tenant_id: str) -> str:
+ """A bridge client and a bridge nominated default, filed under `tenant_id`."""
+ with tenant_scope(tenant_id):
+ client = Client(
+ matrix_user_id=f"@bridge-{uuid.uuid4().hex[:8]}:switch.local",
+ display_name="bridge client",
+ type="bridge",
+ )
+ session.add(client)
+ await session.flush()
+ bridge = CollaborationBridge(
+ type="mattermost",
+ display_name="MM",
+ client_id=client.id,
+ status="active",
+ is_default=True,
+ )
+ session.add(bridge)
+ await session.flush()
+ return bridge.id
+
+
+def _service(
+ session_factory: async_sessionmaker[AsyncSession],
+ *,
+ bridges: dict[str, _FakeBridgeCore],
+ matrix: _FakeMatrix,
+) -> RoomService:
+ svc = object.__new__(RoomService)
+ svc._session_factory = session_factory # type: ignore[assignment]
+ svc._room_store = RoomStore() # type: ignore[assignment]
+ svc._agent_store = AgentStore() # type: ignore[assignment]
+ svc._client_lifecycle = _NoRunningClients() # type: ignore[assignment]
+ svc._collab_lifecycle = _FakeLifecycle(bridges) # type: ignore[assignment]
+ svc._collab_bridge_store = CollaborationBridgeStore() # type: ignore[assignment]
+ svc._matrix_admin = matrix # type: ignore[assignment]
+ return svc
+
+
+async def test_create_room_succeeds_for_each_tenants_own_default_bridge(
+ session_factory: async_sessionmaker[AsyncSession],
+) -> None:
+ tenant_a = f"tenant-{uuid.uuid4().hex[:8]}"
+ tenant_b = f"tenant-{uuid.uuid4().hex[:8]}"
+
+ async with session_factory() as session:
+ session.add_all(
+ [
+ Tenant(id=tenant_a, slug=tenant_a, name="A"),
+ Tenant(id=tenant_b, slug=tenant_b, name="B"),
+ ]
+ )
+ await session.flush()
+ bridge_a = await _make_default_bridge(session, tenant_id=tenant_a)
+ bridge_b = await _make_default_bridge(session, tenant_id=tenant_b)
+ await session.commit()
+
+ matrix = _FakeMatrix()
+ svc = _service(
+ session_factory,
+ bridges={
+ bridge_a: _FakeBridgeCore("@bot-a:switch.local"),
+ bridge_b: _FakeBridgeCore("@bot-b:switch.local"),
+ },
+ matrix=matrix,
+ )
+
+ # Before the fix this raised MultipleResultsFound out of
+ # `CollaborationBridgeStore.get_default` — for tenant A too, even though
+ # tenant A's own default bridge was never ambiguous on its own.
+ with tenant_scope(tenant_a):
+ result_a = await svc.create_room(
+ RoomCreateConfig(name="Room A", description="d")
+ )
+ with tenant_scope(tenant_b):
+ result_b = await svc.create_room(
+ RoomCreateConfig(name="Room B", description="d")
+ )
+
+ assert result_a.room.bridge_id == bridge_a
+ assert result_a.room.tenant_id == tenant_a
+ assert result_b.room.bridge_id == bridge_b
+ assert result_b.room.tenant_id == tenant_b