Skip to content

The agent that grows with you

fix(slack): a rejected slash-command sender costs no Slack lookup

John Paul Solivabda86e96f6b3
diff --git a/plugins/platforms/slack/adapter.py b/plugins/platforms/slack/adapter.py
index 208e1db1d82..f65ff3aae1a 100644
--- a/plugins/platforms/slack/adapter.py
+++ b/plugins/platforms/slack/adapter.py
@@ -6073,14 +6073,21 @@ class SlackAdapter(BasePlatformAdapter):
"[Slack] Ignoring slash command from DM because Slack DMs are disabled: channel=%s user=%s",
channel_id, user_id)
return
+ # A sender the gateway rejects must not cost Slack lookups first (the message path's rule).
+ # In a channel the runner only drops them; in a DM it answers per unauthorized_dm_behavior
+ # (pairing code or decline), so it still gets the event, without names.
+ rejected = self._early_reject_unauthorized(user_id, channel_id, is_dm)
+ if rejected and not is_dm:
+ return
# A slash turn is a human turn: it re-pins the channel prompt and session context, so it
# must carry the same names and prompt as a message here or the next message flips them.
source = self.build_source(
chat_id=channel_id,
- chat_name=await self._resolve_channel_name(channel_id, team_id=team_id),
+ chat_name=None if rejected else await self._resolve_channel_name(channel_id, team_id=team_id),
chat_type="dm" if is_dm else "group",
user_id=user_id,
- user_name=await self._resolve_user_name(user_id, chat_id=channel_id, team_id=team_id),
+ user_name=None if rejected else await self._resolve_user_name(
+ user_id, chat_id=channel_id, team_id=team_id),
thread_id=thread_id, scope_id=team_id or None)
from gateway.platforms.base import resolve_channel_skills
event = MessageEvent(
diff --git a/tests/plugins/platforms/slack/test_slash_command_channel_prompt.py b/tests/plugins/platforms/slack/test_slash_command_channel_prompt.py
index 4f8a5a73dca..9fe9f3765ce 100644
--- a/tests/plugins/platforms/slack/test_slash_command_channel_prompt.py
+++ b/tests/plugins/platforms/slack/test_slash_command_channel_prompt.py
@@ -55,3 +55,20 @@ async def test_slash_turn_matches_message_turn_prompt_and_names(channel_id, chan
assert (message.source.chat_name, message.source.user_name) == (chat_name, "Alice")
assert (slash.source.chat_name, slash.source.user_name) == (
message.source.chat_name, message.source.user_name)
+
+
[email protected]("channel_id, reaches_runner", [("C_OPS", 0), ("D_ALICE", 1)], ids=["channel", "dm"])
+async def test_rejected_slash_sender_costs_no_slack_lookup(channel_id, reaches_runner):
+ """The message path rejects an unauthorized sender before any Slack lookup, and the names the
+ slash path now resolves must not cost one either. In a DM the runner still gets the event,
+ without names: it answers an unauthorized DM per ``unauthorized_dm_behavior`` (pairing code
+ or decline), and a slash command there is how an unpaired user gets that answer."""
+ adapter = _adapter(channel_id)
+ adapter.set_authorization_check(lambda *_args, **_kwargs: False)
+ await adapter._handle_slash_command(
+ {"command": "/hermes", "text": "what broke?", "user_id": "U_MALLORY",
+ "channel_id": channel_id, "team_id": "T1"})
+ adapter._app.client.users_info.assert_not_awaited()
+ adapter._app.client.conversations_info.assert_not_awaited()
+ assert adapter.handle_message.await_count == reaches_runner