Files
hermes-agent/tests/gateway/test_qqbot_credential_isolation.py
T
Da7-Tech e8c5cb5710 fix(qqbot): scope the authz, startup-validation, and direct-send QQ reads
Review follow-up: the adapter-level resolver alone left three paths
reading per-profile QQ_* values from raw os.getenv, so a secondary
multiplex profile's scoped opt-in or credentials were ignored (or the
primary's environ values leaked in):

- gateway/authz_mixin.py: route the per-platform allow-all flag and the
  per-platform/group allowlist + allow-bots reads through the
  scope-aware gateway.config._getenv. Deployment-global GATEWAY_* reads
  intentionally stay on os.getenv. This makes the same fix effective
  for every own-policy platform, not just QQ; unscoped behavior is
  byte-identical to os.getenv.
- gateway/run.py (_own_policy_open_startup_violation): resolve the
  per-platform dm/group policy and allow-all opt-in via _getenv; the
  secondary-profile caller already runs inside _profile_runtime_scope.
- tools/send_message_tool.py (_send_qqbot): the QQ_APP_ID /
  QQ_CLIENT_SECRET fallbacks now honor the active profile scope.

Tests: tests/gateway/test_qqbot_scope_paths.py covers all three paths
end-to-end (scope wins, no environ inheritance for non-opted profiles,
single-profile environ fallback unchanged); the STT suite now asserts
QQ_STT_BASE_URL and QQ_STT_MODEL scoping alongside the API key. All
five scoped-behavior tests fail on the previous commit and pass here.
2026-08-02 10:01:16 -07:00

126 lines
5.1 KiB
Python

"""Credential isolation for the QQ (qqbot) gateway adapter.
Covers the multiplex credential-collision class (same class as the
WeChat/weixin adapter tracked in #59662): the QQ adapter resolves its ``QQ_*``
settings through the active profile secret scope rather than raw ``os.getenv``,
so a secondary profile whose secret lives in its own ``.env`` (installed as an
isolated scope, not into ``os.environ``) does not fall back to the
default/primary profile's value.
Also guards the primary/active profile: it is constructed without a scope and
legitimately owns ``os.environ``, so the resolver must fall back to
``os.environ`` there (not fail closed) even when multiplexing is active —
otherwise the active profile's adapter would raise ``UnscopedSecretError`` on
construction and fail to start.
"""
import pytest
from agent import secret_scope as ss
from gateway.config import PlatformConfig
from gateway.platforms.qqbot.adapter import QQAdapter
@pytest.fixture(autouse=True)
def _reset_multiplex():
ss.set_multiplex_active(False)
yield
ss.set_multiplex_active(False)
def _make_adapter(extra=None):
return QQAdapter(PlatformConfig(enabled=True, extra=extra or {}))
class TestQQCredentialScope:
def test_credentials_read_scope_not_environ(self, monkeypatch):
# os.environ holds another profile's values; the scoped values must win.
monkeypatch.setenv("QQ_APP_ID", "global-app")
monkeypatch.setenv("QQ_CLIENT_SECRET", "global-secret")
ss.set_multiplex_active(True)
tok = ss.set_secret_scope(
{"QQ_APP_ID": "profileA-app", "QQ_CLIENT_SECRET": "profileA-secret"}
)
try:
adapter = _make_adapter()
finally:
ss.reset_secret_scope(tok)
assert adapter._app_id == "profileA-app"
assert adapter._client_secret == "profileA-secret"
def test_two_profiles_isolated(self):
ss.set_multiplex_active(True)
tok_a = ss.set_secret_scope({"QQ_CLIENT_SECRET": "secret-A"})
try:
a = _make_adapter()
finally:
ss.reset_secret_scope(tok_a)
tok_b = ss.set_secret_scope({"QQ_CLIENT_SECRET": "secret-B"})
try:
b = _make_adapter()
finally:
ss.reset_secret_scope(tok_b)
assert a._client_secret == "secret-A"
assert b._client_secret == "secret-B"
def test_single_profile_still_reads_environ(self, monkeypatch):
# No scope + multiplex inactive (default single-profile deployment):
# legacy os.environ behavior is preserved — no regression.
monkeypatch.setenv("QQ_CLIENT_SECRET", "legacy-secret")
adapter = _make_adapter()
assert adapter._client_secret == "legacy-secret"
def test_active_profile_no_scope_reads_environ_without_raising(self, monkeypatch):
# The primary/active profile is built with NO scope while multiplexing
# is active. A bare get_secret() would fail closed (UnscopedSecretError)
# and break its startup; the resolver must fall back to os.environ.
monkeypatch.setenv("QQ_APP_ID", "primary-app")
monkeypatch.setenv("QQ_CLIENT_SECRET", "primary-secret")
ss.set_multiplex_active(True)
assert ss.current_secret_scope() is None # no scope installed
adapter = _make_adapter() # must not raise
assert adapter._app_id == "primary-app"
assert adapter._client_secret == "primary-secret"
def test_explicit_config_extra_takes_precedence(self, monkeypatch):
# An explicit value in config.extra still wins over env/scope.
monkeypatch.setenv("QQ_CLIENT_SECRET", "env-secret")
adapter = _make_adapter(extra={"client_secret": "explicit"})
assert adapter._client_secret == "explicit"
class TestQQSttConfigScope:
def test_stt_api_key_reads_scope(self, monkeypatch):
monkeypatch.setenv("QQ_STT_API_KEY", "global-stt-key")
ss.set_multiplex_active(True)
tok = ss.set_secret_scope({"QQ_STT_API_KEY": "profileA-stt-key"})
try:
adapter = _make_adapter()
stt = adapter._resolve_stt_config()
finally:
ss.reset_secret_scope(tok)
assert stt is not None
assert stt["api_key"] == "profileA-stt-key"
def test_all_stt_values_read_scope(self, monkeypatch):
# Every QQ_STT_* value must come from the scope, not os.environ.
monkeypatch.setenv("QQ_STT_API_KEY", "global-stt-key")
monkeypatch.setenv("QQ_STT_BASE_URL", "https://global.example/v1")
monkeypatch.setenv("QQ_STT_MODEL", "global-asr")
ss.set_multiplex_active(True)
tok = ss.set_secret_scope(
{
"QQ_STT_API_KEY": "profileA-stt-key",
"QQ_STT_BASE_URL": "https://scoped.example/v1",
"QQ_STT_MODEL": "scoped-asr",
}
)
try:
adapter = _make_adapter()
stt = adapter._resolve_stt_config()
finally:
ss.reset_secret_scope(tok)
assert stt is not None
assert stt["api_key"] == "profileA-stt-key"
assert stt["base_url"] == "https://scoped.example/v1"
assert stt["model"] == "scoped-asr"