Skip to content

The agent that grows with you

fix(gateway): revalidate stored role grants

GodsBoyc5097da12b6e
diff --git a/gateway/authz_mixin.py b/gateway/authz_mixin.py
index 873966e3224..1b3103f49fb 100644
--- a/gateway/authz_mixin.py
+++ b/gateway/authz_mixin.py
@@ -583,7 +583,10 @@ class GatewayAuthorizationMixin:
# Compare with ``is True`` so the real bool field authorizes while a
# MagicMock source (test fixtures using ``object.__new__`` runners with
# mock sources) does not auto-truthy through this gate (see pitfall #13).
- if getattr(source, "role_authorized", False) is True:
+ if (
+ allow_adapter_delegation
+ and getattr(source, "role_authorized", False) is True
+ ):
return True
# Check pairing store. A pairing entry is a first-class authorization
diff --git a/tests/gateway/test_plugin_message_injection.py b/tests/gateway/test_plugin_message_injection.py
index c57399e699b..da054a4c487 100644
--- a/tests/gateway/test_plugin_message_injection.py
+++ b/tests/gateway/test_plugin_message_injection.py
@@ -225,6 +225,44 @@ async def test_dispatch_rechecks_current_authorization(raises):
adapter.handle_message.assert_not_awaited()
+async def test_dispatch_rejects_stored_role_only_authorization(monkeypatch):
+ """A stored adapter role grant must be revalidated against current core auth."""
+ for key in (
+ "DISCORD_ALLOWED_USERS",
+ "DISCORD_ALLOW_ALL_USERS",
+ "GATEWAY_ALLOWED_USERS",
+ "GATEWAY_ALLOW_ALL_USERS",
+ ):
+ monkeypatch.delenv(key, raising=False)
+
+ adapter = MagicMock(spec=BasePlatformAdapter)
+ adapter.handle_message = AsyncMock()
+ entry = _entry()
+ entry.session_key = "agent:main:discord:dm:42"
+ entry.platform = Platform.DISCORD
+ source = entry.origin
+ assert source is not None
+ source.platform = Platform.DISCORD
+ source.role_authorized = True
+
+ runner = _runner(entry)
+ runner.adapters = {Platform.DISCORD: adapter}
+ runner.config = GatewayConfig()
+ runner.pairing_store = MagicMock()
+ runner.pairing_store.is_approved.return_value = False
+ del runner._is_user_authorized
+
+ accepted = await runner._dispatch_plugin_message_injection(
+ session_key=entry.session_key,
+ content="wake up",
+ plugin_id="notify-plugin",
+ )
+
+ assert accepted is False
+ adapter.handle_message.assert_not_awaited()
+
+
@pytest.mark.asyncio
async def test_dispatch_stops_when_gateway_drains_during_lookup():
adapter = SimpleNamespace(handle_message=AsyncMock())