Skip to content

The agent that grows with you

fix(telegram): resolve button-caller authorization via the injected auth check, not handler introspection

elphamale4346721117b6
diff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.py
index 436a52cb8da..28bf515ec25 100644
--- a/plugins/platforms/telegram/adapter.py
+++ b/plugins/platforms/telegram/adapter.py
@@ -1213,18 +1213,39 @@ class TelegramAdapter(BasePlatformAdapter):
if not normalized_user_id:
return False
+ normalized_chat_type = str(chat_type or "dm").strip().lower() or "dm"
+ if normalized_chat_type == "private":
+ normalized_chat_type = "dm"
+ elif normalized_chat_type == "supergroup":
+ normalized_chat_type = "forum" if thread_id is not None else "group"
+
+ # Preferred path: the auth callback GatewayRunner injects at
+ # connection time (set_authorization_check), which delegates to the
+ # full _is_user_authorized chain -- env allowlists, group allowlists,
+ # pairing store, allow-all flags. Unlike the __self__ introspection
+ # below, this also works for a secondary multiplexed adapter, whose
+ # _message_handler is a profile closure with no __self__ (the same
+ # gap the admin-tier check had -- resolved the same way). The getattr
+ # tolerates partially-constructed adapters (object.__new__ in tests)
+ # that never ran BasePlatformAdapter.__init__.
+ if getattr(self, "_authorization_check", None) is not None:
+ injected = self._is_sender_authorized(
+ normalized_user_id,
+ chat_type=normalized_chat_type,
+ chat_id=str(chat_id or normalized_user_id),
+ )
+ if injected is not None:
+ return injected
+
+ # Legacy path: resolve the runner off the bound message handler.
+ # Still reachable for adapters wired without set_authorization_check
+ # (bare-adapter tests, direct embedding).
runner = getattr(getattr(self, "_message_handler", None), "__self__", None)
auth_fn = getattr(runner, "_is_user_authorized", None)
if callable(auth_fn):
try:
from gateway.session import SessionSource
- normalized_chat_type = str(chat_type or "dm").strip().lower() or "dm"
- if normalized_chat_type == "private":
- normalized_chat_type = "dm"
- elif normalized_chat_type == "supergroup":
- normalized_chat_type = "forum" if thread_id is not None else "group"
-
source = SessionSource(
platform=Platform.TELEGRAM,
chat_id=str(chat_id or normalized_user_id),
diff --git a/tests/gateway/test_telegram_callback_auth_fail_closed.py b/tests/gateway/test_telegram_callback_auth_fail_closed.py
index ee92721e4e3..db7d60d2a8e 100644
--- a/tests/gateway/test_telegram_callback_auth_fail_closed.py
+++ b/tests/gateway/test_telegram_callback_auth_fail_closed.py
@@ -87,3 +87,46 @@ class TestCallbackAuthFailClosed:
assert adapter._is_callback_user_authorized("12345") is True
+class TestCallbackAuthPrefersInjectedCheck:
+ """_is_callback_user_authorized must use the auth callback GatewayRunner
+ injects via set_authorization_check before the _message_handler.__self__
+ introspection.
+
+ A secondary multiplexed adapter's _message_handler is a profile closure
+ (no __self__), so the introspection path resolves to nothing and the old
+ code fell through to the env-only fallback — which knows nothing about
+ profile config allowlists or the pairing store. The injected callback is
+ registered for every gateway-connected adapter, including multiplexed
+ secondaries, and delegates to the full _is_user_authorized chain.
+ """
+
+ def test_injected_check_used_when_handler_is_a_closure(self, monkeypatch):
+ """Multiplexed shape: closure handler (no __self__) + injected check
+ registered → the injected check decides, not the env fallback."""
+ monkeypatch.delenv("TELEGRAM_ALLOWED_USERS", raising=False)
+ monkeypatch.delenv("GATEWAY_ALLOW_ALL_USERS", raising=False)
+ adapter = _make_adapter()
+ adapter._message_handler = lambda *a, **kw: None # no __self__
+ seen = {}
+
+ def _check(user_id, chat_type=None, chat_id=None):
+ seen.update(user_id=user_id, chat_type=chat_type, chat_id=chat_id)
+ return user_id == "999"
+
+ adapter._authorization_check = _check
+
+ # Env fallback would deny (empty allowlist); the injected check allows.
+ assert adapter._is_callback_user_authorized(
+ "999", chat_id="777", chat_type="supergroup"
+ ) is True
+ assert seen == {"user_id": "999", "chat_type": "group", "chat_id": "777"}
+
+ def test_injected_check_deny_wins_over_env_allowlist(self, monkeypatch):
+ """The injected check is authoritative when registered — an env
+ allowlist entry must not override its deny."""
+ monkeypatch.setenv("TELEGRAM_ALLOWED_USERS", "12345")
+ adapter = _make_adapter()
+ adapter._message_handler = lambda *a, **kw: None
+ adapter._authorization_check = lambda user_id, chat_type=None, chat_id=None: False
+
+ assert adapter._is_callback_user_authorized("12345") is False