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

_is_callback_user_authorized resolved the gateway's auth chain through
_message_handler.__self__. For a secondary multiplexed adapter the
message handler is a per-profile closure with no __self__, so the
introspection silently fell through to the env-only fallback -- which
knows nothing about config allowlists or the pairing store, denying
every button caller on that profile (fail-closed, but wrong).

Prefer the auth callback GatewayRunner already injects at connection
time via set_authorization_check (registered for primary and multiplexed
adapters alike, delegating to the full _is_user_authorized chain), and
keep the introspection plus env fallback for adapters wired without it.
Same resolution pattern the admin-tier gate uses.
This commit is contained in:
elphamale
2026-07-16 14:03:57 +03:00
committed by Teknium
parent 4afbecb429
commit 4346721117
2 changed files with 70 additions and 6 deletions
+27 -6
View File
@@ -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),
@@ -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