The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): resolve blank chat_type to the gated slash scope
beardthelion568decfbccec
diff --git a/gateway/slash_access.py b/gateway/slash_access.pyindex 07204201e41..39cc0e7e7cd 100644--- a/gateway/slash_access.py+++ b/gateway/slash_access.py@@ -113,8 +113,17 @@ def policy_for_source(gateway_config: Any, source: Any) -> SlashAccessPolicy:if not isinstance(extra, dict):extra = platform_config if isinstance(platform_config, dict) else {}chat_type = getattr(source, "chat_type", None)- scope = "dm" if chat_type and chat_type.lower() in _DM_CHAT_TYPES else "group"- return policy_from_extra(extra, scope)+ normalized = str(chat_type).strip().lower() if chat_type is not None else ""+ if normalized:+ scope = "dm" if normalized in _DM_CHAT_TYPES else "group"+ return policy_from_extra(extra, scope)+ # Blank/None chat_type is ambiguous (relay frames may send "" / null; restored rows keep a+ # stored empty value verbatim). _DM_CHAT_TYPES lists "" but a truthiness guard made it+ # unreachable: resolve to whichever scope is gated so an ambiguous source never lands in an+ # ungated allow-everything scope; keep the historical group scope on a tie.+ dm_policy = policy_from_extra(extra, "dm")+ group_policy = policy_from_extra(extra, "group")+ return dm_policy if dm_policy.enabled and not group_policy.enabled else group_policy__all__ = ["SlashAccessPolicy", "policy_from_extra", "policy_for_source"]diff --git a/tests/gateway/test_slash_access.py b/tests/gateway/test_slash_access.pyindex 82a7ad2d90c..a354d3fac05 100644--- a/tests/gateway/test_slash_access.py+++ b/tests/gateway/test_slash_access.py@@ -126,3 +126,71 @@ class TestPolicyForSource:assert grp_p.enabled is Trueassert grp_p.can_run("999", "stop") is False # gated++class TestBlankChatType:+ """Blank/None chat_type (relay frames, restored rows) must not land in an+ ungated scope: resolve to whichever scope is gated, group on tie."""++ def _cfg(self, extra):+ return GatewayConfig(+ platforms={Platform.DISCORD: PlatformConfig(enabled=True, extra=extra)}+ )++ def _blank_src(self, chat_type):+ return SessionSource(+ platform=Platform.DISCORD, chat_id="A", chat_type=chat_type, user_id="999"+ )++ def test_blank_falls_into_gated_dm_scope(self):+ # DM-gated install: blank previously resolved to group, whose unset+ # admin list disabled gating entirely.+ cfg = self._cfg({"allow_admin_from": ["111"], "user_allowed_commands": ["status"]})+ for chat_type in ("", None, " "):+ p = policy_for_source(cfg, self._blank_src(chat_type))+ assert p.enabled is True, repr(chat_type)+ assert p.is_admin("111") is True+ assert p.can_run("999", "stop") is False+ assert p.can_run("999", "status") is True++ def test_blank_keeps_group_scope_when_only_group_gated(self):+ cfg = self._cfg({"group_allow_admin_from": ["222"]})+ p = policy_for_source(cfg, self._blank_src(""))+ assert p.enabled is True+ assert p.is_admin("222") is True+ assert p.can_run("999", "stop") is False++ def test_blank_keeps_group_scope_on_tie(self):+ # Both scopes gated: historical blank -> group resolution is preserved.+ cfg = self._cfg({+ "allow_admin_from": ["111"],+ "group_allow_admin_from": ["222"],+ })+ p = policy_for_source(cfg, self._blank_src(None))+ assert p.enabled is True+ assert p.is_admin("222") is True+ assert p.is_admin("111") is False++ def test_blank_disabled_when_neither_scope_gated(self):+ cfg = self._cfg({})+ p = policy_for_source(cfg, self._blank_src(""))+ assert p.enabled is False+ assert p.can_run("999", "stop") is True++ def test_relay_wire_blank_chat_type_is_gated(self):+ """E2e through the real producer: _event_from_wire honors a blank+ chat_type from the wire verbatim (src.get default only covers a MISSING+ key), so the policy must gate the blank source it produces."""+ from gateway.relay.ws_transport import _event_from_wire++ cfg = self._cfg({"allow_admin_from": ["111"]})+ for blank in ("", None):+ evt = _event_from_wire({+ "text": "/stop",+ "source": {"platform": "discord", "chat_id": "c1",+ "chat_type": blank, "user_id": "999"},+ })+ assert not evt.source.chat_type # producer really did emit blank+ p = policy_for_source(cfg, evt.source)+ assert p.enabled is True, repr(blank)+ assert p.can_run("999", "stop") is False+diff --git a/tests/gateway/test_slash_access_dispatch.py b/tests/gateway/test_slash_access_dispatch.pyindex ef323f12b87..536964a8216 100644--- a/tests/gateway/test_slash_access_dispatch.py+++ b/tests/gateway/test_slash_access_dispatch.py@@ -182,6 +182,24 @@ async def test_group_only_gating_leaves_dm_unrestricted():assert "Tier: unrestricted" in result+async def test_blank_chat_type_resolves_to_gated_scope():+ """A blank chat_type (relay frames can send ""/null; restored rows keep a+ stored empty value) used to resolve to group scope, so on a DM-only-gated+ install the source landed in an ungated scope and every command ran."""+ runner = _make_runner(+ platform_extra={+ "allow_admin_from": ["111"],+ "user_allowed_commands": ["status"],+ }+ )+ for blank in ("", None):+ result = await runner._handle_message(+ _make_event("/stop", _make_source(user_id="999", chat_type=blank))+ )+ assert "⛔" in result, repr(blank)++# ---------------------------------------------------------------------------# Plugin-registered slash commands are gated through the same path# ---------------------------------------------------------------------------