Skip to content

The agent that grows with you

fix(slack): early auth reject checks the session thread's routed profile

Tekniumf41ac076956d
diff --git a/plugins/platforms/slack/adapter.py b/plugins/platforms/slack/adapter.py
index d0b0ff1a439..3ca74ef0ffa 100644
--- a/plugins/platforms/slack/adapter.py
+++ b/plugins/platforms/slack/adapter.py
@@ -4160,20 +4160,21 @@ class SlackAdapter(BasePlatformAdapter):
runner = getattr(getattr(self, "_message_handler", None), "__self__", None)
return getattr(runner, "_is_user_authorized", None)
- def _early_reject_unauthorized(self, user_id: str, channel_id: str, is_dm: bool) -> bool:
+ def _early_reject_unauthorized(
+ self, user_id: str, channel_id: str, is_dm: bool, *, thread_id: Optional[str] = None) -> bool:
"""True (logged) when the sender is definitively unauthorized. Injected profile-bound check
first (works under multiplex, where the handler has no ``__self__``), then runner
- introspection. Unknown (None) is NOT a rejection."""
+ introspection. Unknown (None) is NOT a rejection. ``thread_id``: thread-scoped profile_routes."""
chat_type = "dm" if is_dm else "group"
decision = (
- self._is_sender_authorized(user_id, chat_type, channel_id)
+ self._is_sender_authorized(user_id, chat_type, channel_id, thread_id=thread_id)
if user_id and getattr(self, "_authorization_check", None) is not None
else None)
auth_fn = self._runner_auth_fn()
if decision is None and user_id and callable(auth_fn):
source = self.build_source(
- chat_id=channel_id, chat_name="", chat_type=chat_type, user_id=user_id, user_name=""
- )
+ chat_id=channel_id, chat_name="", chat_type=chat_type, user_id=user_id, user_name="",
+ thread_id=thread_id)
decision = bool(auth_fn(source))
if decision is False:
logger.warning(
@@ -4621,9 +4622,9 @@ class SlackAdapter(BasePlatformAdapter):
is_one_to_one_dm = channel_type == "im"
# Reject unauthorized users before the expensive lookups/downloads;
# the runner's own auth check only runs after MessageEvent is built.
- if self._early_reject_unauthorized(user_id, channel_id, is_dm):
- return
thread_ts = self._session_thread_ts(event, ts, is_dm, assistant_meta)
+ if self._early_reject_unauthorized(user_id, channel_id, is_dm, thread_id=thread_ts):
+ return
bot_uid = self._team_bot_user_ids.get(team_id, self._bot_user_id)
# Mentions may live only in Block Kit blocks.
# See #52387.
@@ -6081,7 +6082,7 @@ class SlackAdapter(BasePlatformAdapter):
# 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)
+ rejected = self._early_reject_unauthorized(user_id, channel_id, is_dm, thread_id=thread_id)
if rejected and not is_dm:
return
source = self.build_source(
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 a7e18f9d5fb..4bc39becac9 100644
--- a/tests/plugins/platforms/slack/test_slash_command_channel_prompt.py
+++ b/tests/plugins/platforms/slack/test_slash_command_channel_prompt.py
@@ -118,6 +118,27 @@ async def test_slash_that_starts_no_turn_costs_no_slack_lookup(channel_id, autho
assert adapter.handle_message.await_count == reaches_runner
+async def test_thread_routed_sender_is_checked_against_the_routed_thread():
+ """A thread-scoped ``profile_routes`` entry points this thread at a profile whose pairing store
+ approved the caller; the default store did not. The gateway's auth callback resolves the store
+ from ``thread_id``, so the early reject must pass it on both entry points, or the caller's
+ ``/stop`` and messages in that thread are dropped before the runner sees them."""
+ routed_thread = "1700.000050"
+ adapter = _adapter("C_OPS")
+ adapter.set_authorization_check(
+ lambda _uid, _chat_type, _chat_id, *, thread_id=None, **_kw: thread_id == routed_thread)
+ await adapter._handle_slash_command(
+ {"command": "/stop", "text": "", "user_id": "U_ALICE", "channel_id": "C_OPS",
+ "team_id": "T1", "thread_ts": routed_thread})
+ await adapter._handle_slack_message(
+ {"text": "<@U_BOT> what broke?", "user": "U_ALICE", "channel": "C_OPS",
+ "channel_type": "channel", "ts": "1700.000100", "thread_ts": routed_thread}, {"team_id": "T1"})
+ handed = [c.args[0] for c in adapter.handle_message.await_args_list]
+ assert [(e.text, e.source.thread_id) for e in handed] == [
+ ("/stop", routed_thread), ("what broke?", routed_thread)]
+
+
@pytest.mark.asyncio
async def test_concurrent_slashes_reach_the_gateway_in_arrival_order():
"""Two ``/hermes queue`` slashes through the Bolt listener: the first is held on a cold