Skip to content

The agent that grows with you

fix(gateway): authorize a secondary bot's callbacks under its own profile scope

liuhao1024b483f0f65ba9
diff --git a/gateway/run_adapters.py b/gateway/run_adapters.py
index af0ece0a1b1..66033d797d1 100644
--- a/gateway/run_adapters.py
+++ b/gateway/run_adapters.py
@@ -1788,10 +1788,23 @@ class GatewayAdapterLifecycleMixin:
is consulted while allowlist reads stay under the transport home.
Without this an inline-button caller approved only in the routed profile's pairing store was denied
- (#86296), because the adapter's callback source was never route-stamped.
+ (#86296), because the adapter's callback source was never route-stamped. A secondary-owned bot's
+ callback runs outside the profile runtime scope (built at configure time, invoked from the
+ adapter's event loop), so it re-enters the owning profile's scope per call — the gate's scoped
+ env read otherwise falls back to os.environ (the default profile's env) and denies the
+ secondary's own allowlisted callers (#120639).
"""
from gateway.run import get_hermes_home
transport_home = Path(get_hermes_home()) if self._multiplex_on() and profile_name is None else None
+ # Prebuild the secondary's secret scope at configure time so entering it per callback never
+ # touches the adapter event loop with file IO; an unresolvable profile home keeps the
+ # credential-reads-fail-closed behavior of every other secondary handler.
+ secondary_scope = None
+ if profile_name:
+ profile_home = self._routed_profile_home(profile_name)
+ if profile_home is not UNRESOLVED_PROFILE_HOME:
+ from agent.secret_scope import build_profile_secret_scope
+ secondary_scope = (profile_home, build_profile_secret_scope(profile_home))
def check(
user_id: str, chat_type: Optional[str] = None, chat_id: Optional[str] = None, *,
@@ -1812,7 +1825,13 @@ class GatewayAdapterLifecycleMixin:
if adapter is not None:
source._transport_adapter_ref = _weakref.ref(adapter)
if transport_home is None:
- return self._is_user_authorized(source)
+ if secondary_scope is None:
+ return self._is_user_authorized(source)
+ # The cold-path message handler authorizes under ``_profile_runtime_scope``; a
+ # callback must read the same allowlist scope, not ambient os.environ.
+ from gateway.run import _profile_runtime_scope
+ with _profile_runtime_scope(secondary_scope[0], secondary_scope[1]):
+ return self._is_user_authorized(source)
# Canonicalize FIRST (callback sources never went through ``build_source``): the routed
# profile's pairing store is consulted, allowlists read under the transport home.
if self._canonicalize(source, primary_home=transport_home) is None:
diff --git a/tests/gateway/test_multiplex_interactive_auth.py b/tests/gateway/test_multiplex_interactive_auth.py
index bfa08a4d9b5..76ca0f353f8 100644
--- a/tests/gateway/test_multiplex_interactive_auth.py
+++ b/tests/gateway/test_multiplex_interactive_auth.py
@@ -110,6 +110,25 @@ def test_bot_sender_reaches_allow_bots_policy_through_callback(mux_home):
assert tg._is_user_authorized_from_message(msg(4343, False)) is False
+def test_secondary_owned_callback_reads_own_profile_allowlist(mux_home):
+ """#120639: a secondary profile's own bot authorizes inline-button callers
+ against the OWNING profile's allowlist — the same ``.env`` scope the
+ cold-path message handler reads — even though the callback fires outside
+ any profile runtime scope, straight off the adapter's event loop."""
+ runner = _runner(mux_home)
+ (mux_home / "profiles" / "secondary" / ".env").write_text("TELEGRAM_ALLOWED_USERS=555\n")
+ tg = _telegram(runner)
+ tg._hermes_profile_name = "secondary"
+ runner._profile_adapters = {"secondary": {Platform.TELEGRAM: tg}}
+ tg.set_authorization_check(
+ runner._make_adapter_auth_check(Platform.TELEGRAM, profile_name="secondary")
+ )
+
+ # No ambient profile scope: the adapter event loop invokes the callback bare.
+ assert tg._is_callback_user_authorized("555", chat_id="111", chat_type="private") is True
+ assert tg._is_callback_user_authorized("999", chat_id="111", chat_type="private") is False
+
+
def test_slack_interactive_auth_prefers_wired_profile_check(mux_home, monkeypatch):
"""#72657: a multiplexed Slack adapter's button gate resolves through the
wired ``_make_adapter_auth_check`` for its own profile; the DEFAULT