From 04c640a183040626ecceff5d7fc3854bbe9049cf Mon Sep 17 00:00:00 2001 From: fangliquanflq Date: Fri, 7 Aug 2026 23:28:17 +0800 Subject: [PATCH] fix(gateway): honor secondary adapters under profile runtime scope Multiplex turns enter _profile_runtime_scope, so get_active_profile_name() equals the secondary profile and the old authz lookup consulted empty self.adapters. Prefer _profile_adapters before the active-profile shortcut. --- gateway/authz_mixin.py | 14 ++++-- tests/gateway/test_multiplex_profile_authz.py | 46 +++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/gateway/authz_mixin.py b/gateway/authz_mixin.py index 22ec0ad820..b4b42f7176 100644 --- a/gateway/authz_mixin.py +++ b/gateway/authz_mixin.py @@ -204,11 +204,22 @@ class GatewayAuthorizationMixin: ``self.adapters``. ``SessionSource.profile`` selects which map to consult. When a stamped profile has its own adapter registry entry, the default profile's same-platform adapter must not be consulted as a fallback. + + Consult ``_profile_adapters`` *before* comparing against + ``_active_profile_name()``. Multiplex turns wrap authz in + ``_profile_runtime_scope``, which overrides ``HERMES_HOME`` so + ``get_active_profile_name()`` returns the secondary profile for the + duration of the turn. Treating that scoped name as "primary" would + look up ``self.adapters`` (empty for secondary-only platforms like + A2A) and default-deny an already-authenticated peer. """ if not platform: return None profile_name = (profile or "").strip() or None if profile_name and profile_name != "default": + profile_adapters = getattr(self, "_profile_adapters", None) or {} + if profile_name in profile_adapters: + return profile_adapters[profile_name].get(platform) # Adapter ownership is process-wide: only the profile the gateway # was LAUNCHED as owns ``self.adapters``. ``_active_profile_name()`` # reads the per-turn HERMES_HOME override, so inside a secondary @@ -226,9 +237,6 @@ class GatewayAuthorizationMixin: if profile_name == primary_profile: adapters = getattr(self, "adapters", None) or {} return adapters.get(platform) - profile_adapters = getattr(self, "_profile_adapters", None) or {} - if profile_name in profile_adapters: - return profile_adapters[profile_name].get(platform) # Fail closed: a stamped secondary profile with no registry entry # (e.g. its adapter failed to connect) must NOT fall back to the # default profile's adapter — that sends replies out the wrong bot. diff --git a/tests/gateway/test_multiplex_profile_authz.py b/tests/gateway/test_multiplex_profile_authz.py index e20176efcb..cd1bbf5a59 100644 --- a/tests/gateway/test_multiplex_profile_authz.py +++ b/tests/gateway/test_multiplex_profile_authz.py @@ -74,6 +74,52 @@ def test_active_profile_stamp_resolves_primary_adapter(monkeypatch): assert runner._authorization_adapter(Platform.WECOM, profile="dev") is default_adapter +def test_scoped_secondary_profile_still_uses_profile_adapters(monkeypatch): + """Runtime scope must not redirect secondary authz to primary adapters. + + ``_make_profile_message_handler`` wraps ``_handle_message`` in + ``_profile_runtime_scope``, which overrides HERMES_HOME so + ``get_active_profile_name()`` equals the secondary profile for that turn. + Authorization must still read ``_profile_adapters[profile]``, not the + empty primary ``self.adapters`` map — otherwise upstream-auth platforms + such as A2A default-deny an already-authenticated peer (#80884). A + secondary profile with NO registry entry still fails closed. + """ + from gateway.run import GatewayRunner + + _clear_auth_env(monkeypatch) + + runner = object.__new__(GatewayRunner) + runner.config = GatewayConfig(multiplex_profiles=True) + runner.adapters = {} + runner.pairing_store = MagicMock() + runner.pairing_store.is_approved.return_value = False + + secondary = SimpleNamespace( + authorization_is_upstream=True, + enforces_own_access_policy=False, + ) + runner._profile_adapters = {"beta": {Platform("a2a"): secondary}} + # Simulate the scoped turn: active profile name collapses to the secondary. + runner._active_profile_name = lambda: "beta" + + assert runner._authorization_adapter(Platform("a2a"), profile="beta") is secondary + + source = SessionSource( + platform=Platform("a2a"), + chat_id="a2a-context", + user_id="alpha", + user_name="alpha", + chat_type="dm", + profile="beta", + ) + assert runner._is_user_authorized(source) is True + + # Fail-closed guard is untouched: no registry entry -> no default fallback. + runner._profile_adapters = {"beta": {}} + assert runner._authorization_adapter(Platform("a2a"), profile="beta") is None + + def test_secondary_allowlist_dm_behavior_ignores_unauthorized(monkeypatch): """Unauthorized-DM behavior must read the secondary adapter's dm_policy.""" runner, _default_adapter, secondary_adapter = _make_multiplex_runner(monkeypatch)