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(agent): persist the bootstrap-key-seeded flag on legacy migration too

Wojciech Wojtyniak870211547d16
diff --git a/core/switch_core/main.py b/core/switch_core/main.py
index 7f091ce1e..777fecd54 100644
--- a/core/switch_core/main.py
+++ b/core/switch_core/main.py
@@ -549,7 +549,15 @@ async def _seed_agent_registration_bootstrap_key(
bootstrap_key = next(
(k for k in admin_keys if k.type == BOOTSTRAP_KEY_TYPE), None
)
- already_seeded = bool((admin.metadata_ or {}).get(_BOOTSTRAP_SEEDED_FLAG))
+ # Whether the flag is *already durably recorded* — read-only, used to
+ # decide whether a missing key means "revoked" vs. "never seeded".
+ # Deliberately not reused as a "did something get provisioned this
+ # call" flag: that conflated the migration branch (which provisions a
+ # key without having recorded the flag yet) with a true revocation,
+ # so the flag write below was skipped on the exact call that migrates
+ # a legacy key, leaving a revocation made shortly after unable to
+ # stick past the next restart.
+ flag_already_set = bool((admin.metadata_ or {}).get(_BOOTSTRAP_SEEDED_FLAG))
if legacy_key is not None and bootstrap_key is None:
legacy_key.type = BOOTSTRAP_KEY_TYPE
@@ -557,7 +565,6 @@ async def _seed_agent_registration_bootstrap_key(
legacy_key.key_hash = token_hash
legacy_key.encrypted_key = encrypted_key
bootstrap_key = legacy_key
- already_seeded = True
logger.info(
"Migrated the legacy admin-owned registration key to a "
"scoped agent-registration bootstrap key"
@@ -571,27 +578,27 @@ async def _seed_agent_registration_bootstrap_key(
"Rotated the agent-registration bootstrap key from "
"AGENT_REGISTRATION_TOKEN"
)
- elif already_seeded:
+ elif flag_already_set:
logger.warning(
"Agent-registration bootstrap key was revoked; not "
"reseeding it from AGENT_REGISTRATION_TOKEN. Mint per-user "
"registration keys from the gateway's API Keys page instead."
)
else:
- key = ApiKey(
+ bootstrap_key = ApiKey(
user_id=admin.id,
key_hash=token_hash,
encrypted_key=encrypted_key,
label=BOOTSTRAP_KEY_LABEL,
type=BOOTSTRAP_KEY_TYPE,
)
- await api_key_store.create(session, key)
+ await api_key_store.create(session, bootstrap_key)
logger.info(
"Seeded the agent-registration bootstrap key from "
"AGENT_REGISTRATION_TOKEN"
)
- if not already_seeded:
+ if bootstrap_key is not None and not flag_already_set:
meta = dict(admin.metadata_ or {})
meta[_BOOTSTRAP_SEEDED_FLAG] = True
admin.metadata_ = meta
diff --git a/core/tests/integration/test_registration_bootstrap_scope.py b/core/tests/integration/test_registration_bootstrap_scope.py
index ca2107187..28e584749 100644
--- a/core/tests/integration/test_registration_bootstrap_scope.py
+++ b/core/tests/integration/test_registration_bootstrap_scope.py
@@ -17,6 +17,7 @@ from __future__ import annotations
import pytest
+from switch_core.room_service import RoomCreateConfig
from tests.integration.conftest import REGISTRATION_TOKEN, Harness, SessionEnv
pytestmark = [pytest.mark.integration, pytest.mark.asyncio(loop_scope="session")]
@@ -63,3 +64,40 @@ async def test_bootstrap_token_registration_is_repeatable_and_stable(
agent_two = await harness.protocol.agent_store.get(session, second.agent_id)
assert agent_one is not None and agent_two is not None
assert agent_one.owner_id == agent_two.owner_id
+
+
+async def test_bootstrap_owned_agent_cannot_write_a_private_room_it_does_not_own(
+ harness: Harness, session_env: SessionEnv
+) -> None:
+ """Drives the actual authorization decision this fix closes, not just
+ the ownership attribute: before this fix, a bootstrap-registered agent's
+ owner was the admin, and `_resolve_acting_identity` would have resolved
+ it as `owner_is_admin=True` — an admin-equivalent bypass on every
+ private room in the deployment, including this one."""
+ bootstrap_result = await harness.register_agent_via_registration_token(
+ "colleague-agent", REGISTRATION_TOKEN
+ )
+ admin_owned = await harness.register_agent("admin-owned-agent")
+
+ private_room = await harness.room_service.create_room(
+ RoomCreateConfig(
+ name="admins-private-room",
+ description="owned by the admin, not the bootstrap account",
+ agent_ids=[admin_owned.agent_id],
+ owner_id=harness.owner_id,
+ created_by=harness.owner_id,
+ read_visibility="private",
+ write_visibility="private",
+ )
+ )
+
+ async with session_env.session_factory() as session: # type: ignore[operator]
+ # Control: the admin's own agent is the room's owner and must pass.
+ await harness.protocol._require_room_action(
+ session, admin_owned.agent_id, private_room.room.id, "write"
+ )
+
+ with pytest.raises(PermissionError):
+ await harness.protocol._require_room_action(
+ session, bootstrap_result.agent_id, private_room.room.id, "write"
+ )
diff --git a/core/tests/switch_core/test_main.py b/core/tests/switch_core/test_main.py
index d5f7c1aca..84cb23225 100644
--- a/core/tests/switch_core/test_main.py
+++ b/core/tests/switch_core/test_main.py
@@ -130,6 +130,46 @@ class TestSeedAgentRegistrationBootstrapKey:
assert bootstrap_keys[0].label == BOOTSTRAP_KEY_LABEL
assert not any(k.type == "registration" for k in keys)
+ async def test_revoking_a_migrated_legacy_key_survives_a_restart(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ """The durable-seeded flag must be recorded on the very call that
+ migrates a legacy key, not only on a later, unrelated call — an
+ operator can revoke (delete) the freshly migrated key immediately,
+ with no intervening restart to have backfilled the flag."""
+ user_store, api_key_store = UserStore(), ApiKeyStore()
+ admin_id = await _make_admin(session_factory)
+ config = _config("dev-test-token")
+
+ async with session_factory() as session:
+ legacy = ApiKey(
+ user_id=admin_id,
+ key_hash=_hash("dev-test-token"),
+ encrypted_key="irrelevant",
+ label=LEGACY_BOOTSTRAP_KEY_LABEL,
+ type="registration",
+ )
+ session.add(legacy)
+ await session.commit()
+
+ await _seed_agent_registration_bootstrap_key(
+ session_factory, user_store, api_key_store, config
+ )
+
+ async with session_factory() as session:
+ keys = await api_key_store.get_by_user(session, admin_id)
+ (bootstrap_key,) = [k for k in keys if k.type == BOOTSTRAP_KEY_TYPE]
+ await api_key_store.delete(session, bootstrap_key.id)
+ await session.commit()
+
+ await _seed_agent_registration_bootstrap_key(
+ session_factory, user_store, api_key_store, config
+ )
+
+ async with session_factory() as session:
+ keys = await api_key_store.get_by_user(session, admin_id)
+ assert not any(k.type == BOOTSTRAP_KEY_TYPE for k in keys)
+
async def test_rotating_the_token_updates_the_existing_key_in_place(
self, session_factory: async_sessionmaker[AsyncSession]
) -> None: