Skip to content

The agent that grows with you

refactor(discord): move the slash gate verdict to a sibling mixin; adapt the thread-starter test

unsupportedpastels56f798641888
diff --git a/plugins/platforms/discord/adapter.py b/plugins/platforms/discord/adapter.py
index acac9432823..cff1603fa22 100644
--- a/plugins/platforms/discord/adapter.py
+++ b/plugins/platforms/discord/adapter.py
@@ -1258,11 +1258,13 @@ def _read_discord_prompt_timeout() -> int:
from plugins.platforms.discord.adapter_media import DiscordMediaMixin
+from plugins.platforms.discord.adapter_slash_auth import DiscordSlashAuthMixin
from plugins.platforms.discord.adapter_thread_titles import DiscordThreadTitlesMixin, SemanticThreadRenames
from plugins.platforms.discord.adapter_voice_info import DiscordVoiceInfoMixin
-class DiscordAdapter(DiscordMediaMixin, DiscordThreadTitlesMixin, DiscordVoiceInfoMixin, BasePlatformAdapter):
+class DiscordAdapter(DiscordMediaMixin, DiscordThreadTitlesMixin, DiscordVoiceInfoMixin, DiscordSlashAuthMixin,
+ BasePlatformAdapter):
"""Discord bot adapter: guild/DM messages, threads, slash commands, button approvals, reactions."""
MAX_MESSAGE_LENGTH = 2000
@@ -4244,81 +4246,6 @@ class DiscordAdapter(DiscordMediaMixin, DiscordThreadTitlesMixin, DiscordVoiceIn
self.name,
)
- # ── Slash command authorization ─────────────────────────────────────
- # ``_check_slash_authorization`` mirrors the on_message gates one-for-one. No allowlist =>
- # fail closed unless allow-all; DISCORD_ALLOWED_CHANNELS alone authorizes per validated channel.
-
- def _evaluate_slash_authorization(
- self, interaction: "discord.Interaction",
- ) -> Tuple[bool, Optional[str]]:
- """Evaluate slash authorization without responding; returns ``(allowed, reason)``.
- Shared with side-effect-free callers (``/skill`` autocomplete returns [] per keystroke).
- Fail closed on malformed payloads: with an allowlist, a missing channel id/user REJECTS.
- """
- chan_obj = getattr(interaction, "channel", None)
- in_dm = isinstance(chan_obj, discord.DMChannel) if chan_obj is not None else False
- channel_ids: set = set()
- channel_keys: set = set()
- # Channel scope mirrors on_message; DMs use on_message's DM lockdown path instead.
- if not in_dm:
- chan_id_raw = getattr(interaction, "channel_id", None) or getattr(chan_obj, "id", None)
- if chan_id_raw is not None:
- channel_ids.add(str(chan_id_raw))
- # Threads: also test the parent channel, as on_message does.
- if isinstance(chan_obj, discord.Thread):
- parent_id = self._get_parent_channel_id(chan_obj)
- if parent_id:
- channel_ids.add(str(parent_id))
- # Name-form keys (ID, name, #name, parent) so name-based lists work for slash too.
- channel_keys = self._discord_channel_keys_from_channel(
- chan_obj,
- self._get_parent_channel_id(chan_obj)
- if isinstance(chan_obj, discord.Thread)
- else None,
- )
- allowed = self._get_allowed_channels()
- if allowed:
- if "*" not in allowed:
- if not channel_ids:
- # Channel policy configured but no resolvable channel id: fail closed.
- return (
- False, "channel id missing with DISCORD_ALLOWED_CHANNELS configured",
- )
- if not (channel_keys & allowed):
- return (False, "channel not in DISCORD_ALLOWED_CHANNELS")
- # Ignored beats allowed, including via a thread's parent.
- ignored = self._get_ignored_channels()
- if ignored and channel_ids:
- if "*" in ignored or (channel_keys & ignored):
- return (False, "channel in DISCORD_IGNORED_CHANNELS")
- # ── User / role allowlist (mirrors on_message line 681) ──
- user = getattr(interaction, "user", None)
- allowed_users = getattr(self, "_allowed_user_ids", set()) or set()
- allowed_roles = getattr(self, "_allowed_role_ids", set()) or set()
- if user is None or getattr(user, "id", None) is None:
- # No identifiable user: fail closed even with allow-all; downstream handlers need interaction.user.id.
- if allowed_users or allowed_roles:
- return (False, "missing interaction.user with allowlist configured")
- return (False, "missing interaction.user")
- user_id = str(user.id)
- # guild + is_dm scope the role check so the cross-guild DM bypass can't land via slash.
- # See #12136.
- interaction_guild = getattr(interaction, "guild", None)
- if not self._is_allowed_user(
- user_id, author=user, guild=interaction_guild, is_dm=in_dm,
- channel_ids=channel_keys if not in_dm else None,
- ):
- return (False, "user not in DISCORD_ALLOWED_USERS / DISCORD_ALLOWED_ROLES")
- return (True, None)
-
- def _slash_role_grant(self, interaction: "discord.Interaction") -> bool:
- """``role_authorized`` for an event built from ``interaction``, with the message path's
- meaning (_discord_message_admission): a role allowlist exists and THIS actor passes the gate.
- Re-evaluated for the interaction instead of trusted from call order, so a builder can never
- stamp the grant on an actor the slash gate would refuse."""
- return bool(getattr(self, "_allowed_role_ids", set())) and self._evaluate_slash_authorization(
- interaction)[0]
-
async def _check_slash_authorization(
self, interaction: "discord.Interaction", command_text: str,
) -> bool:
diff --git a/plugins/platforms/discord/adapter_slash_auth.py b/plugins/platforms/discord/adapter_slash_auth.py
new file mode 100644
index 00000000000..be23d38aeb9
--- /dev/null
+++ b/plugins/platforms/discord/adapter_slash_auth.py
@@ -0,0 +1,92 @@
+"""Slash-command authorization verdict for the Discord adapter: the on_message gates, side-effect free."""
+from __future__ import annotations
+
+from typing import Any, Callable, Optional, Tuple
+
+try:
+ import discord
+except ImportError: # the adapter does not start without discord.py
+ discord = None
+
+
+class DiscordSlashAuthMixin:
+ _get_parent_channel_id: Callable[[Any], Optional[str]]
+ _discord_channel_keys_from_channel: Callable[..., set]
+ _get_allowed_channels: Callable[[], set]
+ _get_ignored_channels: Callable[[], set]
+ _is_allowed_user: Callable[..., bool]
+
+ # ── Slash command authorization ─────────────────────────────────────
+ # ``_check_slash_authorization`` mirrors the on_message gates one-for-one. No allowlist =>
+ # fail closed unless allow-all; DISCORD_ALLOWED_CHANNELS alone authorizes per validated channel.
+
+ def _evaluate_slash_authorization(
+ self, interaction: "discord.Interaction",
+ ) -> Tuple[bool, Optional[str]]:
+ """Evaluate slash authorization without responding; returns ``(allowed, reason)``.
+ Shared with side-effect-free callers (``/skill`` autocomplete returns [] per keystroke).
+ Fail closed on malformed payloads: with an allowlist, a missing channel id/user REJECTS.
+ """
+ chan_obj = getattr(interaction, "channel", None)
+ in_dm = isinstance(chan_obj, discord.DMChannel) if chan_obj is not None else False
+ channel_ids: set = set()
+ channel_keys: set = set()
+ # Channel scope mirrors on_message; DMs use on_message's DM lockdown path instead.
+ if not in_dm:
+ chan_id_raw = getattr(interaction, "channel_id", None) or getattr(chan_obj, "id", None)
+ if chan_id_raw is not None:
+ channel_ids.add(str(chan_id_raw))
+ # Threads: also test the parent channel, as on_message does.
+ if isinstance(chan_obj, discord.Thread):
+ parent_id = self._get_parent_channel_id(chan_obj)
+ if parent_id:
+ channel_ids.add(str(parent_id))
+ # Name-form keys (ID, name, #name, parent) so name-based lists work for slash too.
+ channel_keys = self._discord_channel_keys_from_channel(
+ chan_obj,
+ self._get_parent_channel_id(chan_obj)
+ if isinstance(chan_obj, discord.Thread)
+ else None,
+ )
+ allowed = self._get_allowed_channels()
+ if allowed:
+ if "*" not in allowed:
+ if not channel_ids:
+ # Channel policy configured but no resolvable channel id: fail closed.
+ return (
+ False, "channel id missing with DISCORD_ALLOWED_CHANNELS configured",
+ )
+ if not (channel_keys & allowed):
+ return (False, "channel not in DISCORD_ALLOWED_CHANNELS")
+ # Ignored beats allowed, including via a thread's parent.
+ ignored = self._get_ignored_channels()
+ if ignored and channel_ids:
+ if "*" in ignored or (channel_keys & ignored):
+ return (False, "channel in DISCORD_IGNORED_CHANNELS")
+ # ── User / role allowlist (mirrors on_message line 681) ──
+ user = getattr(interaction, "user", None)
+ allowed_users = getattr(self, "_allowed_user_ids", set()) or set()
+ allowed_roles = getattr(self, "_allowed_role_ids", set()) or set()
+ if user is None or getattr(user, "id", None) is None:
+ # No identifiable user: fail closed even with allow-all; downstream handlers need interaction.user.id.
+ if allowed_users or allowed_roles:
+ return (False, "missing interaction.user with allowlist configured")
+ return (False, "missing interaction.user")
+ user_id = str(user.id)
+ # guild + is_dm scope the role check so the cross-guild DM bypass can't land via slash.
+ # See #12136.
+ interaction_guild = getattr(interaction, "guild", None)
+ if not self._is_allowed_user(
+ user_id, author=user, guild=interaction_guild, is_dm=in_dm,
+ channel_ids=channel_keys if not in_dm else None,
+ ):
+ return (False, "user not in DISCORD_ALLOWED_USERS / DISCORD_ALLOWED_ROLES")
+ return (True, None)
+
+ def _slash_role_grant(self, interaction: "discord.Interaction") -> bool:
+ """``role_authorized`` for an event built from ``interaction``, with the message path's
+ meaning (_discord_message_admission): a role allowlist exists and THIS actor passes the gate.
+ Re-evaluated for the interaction instead of trusted from call order, so a builder can never
+ stamp the grant on an actor the slash gate would refuse."""
+ return bool(getattr(self, "_allowed_role_ids", set())) and self._evaluate_slash_authorization(
+ interaction)[0]
diff --git a/tests/gateway/test_discord_slash_auth.py b/tests/gateway/test_discord_slash_auth.py
index d41693a4bd7..fcfc857cd5d 100644
--- a/tests/gateway/test_discord_slash_auth.py
+++ b/tests/gateway/test_discord_slash_auth.py
@@ -270,7 +270,9 @@ async def test_native_event_carries_this_actors_role_grant(adapter, builder, act
if builder == "slash":
dispatched.append(adapter._build_slash_event(interaction, "/reset"))
else:
- await adapter._dispatch_thread_session(interaction, "555", "topic", "hello")
+ thread = SimpleNamespace(id=555, name="topic", guild=interaction.guild, topic=None,
+ parent=SimpleNamespace(id=100, name="general", guild=interaction.guild))
+ await adapter._dispatch_thread_session(interaction, thread, "hello")
assert [_gateway_authorizes(event.source) for event in dispatched] == [gateway_admits]
diff --git a/tests/gateway/test_discord_voice_role_grant.py b/tests/gateway/test_discord_voice_role_grant.py
index 67320d08f0c..d9eb6cae050 100644
--- a/tests/gateway/test_discord_voice_role_grant.py
+++ b/tests/gateway/test_discord_voice_role_grant.py
@@ -5,6 +5,7 @@ from the ``/voice join`` copy, which never carries the per-event role grant, so
check refused every role-only speaker. The grant is recomputed for the current speaker against the
guild's current member, never inherited from whoever ran ``/voice join``.
"""
+from __future__ import annotations
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock
@@ -25,7 +26,9 @@ _ROLE, _GUILD, _TEXT, _JOINER = 1234, 1, 700, 42
([], 0), # the joiner holds the role, this speaker does not
(None, 0), # speaker no longer in the guild
])
-async def test_role_member_speech_passes_the_gateway_gate(monkeypatch, speaker_roles, dispatched):
+async def test_role_member_speech_passes_the_gateway_gate(
+ monkeypatch: pytest.MonkeyPatch, speaker_roles: list[int] | None, dispatched: int,
+) -> None:
for var in ("DISCORD_ALLOWED_USERS", "DISCORD_ALLOW_ALL_USERS", "GATEWAY_ALLOWED_USERS",
"GATEWAY_ALLOW_ALL_USERS"):
monkeypatch.delenv(var, raising=False)