Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions gateway/platforms/weixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1227,14 +1227,14 @@ def __init__(self, config: PlatformConfig):
)
self._rate_limit_circuit_until = 0.0
self._rate_limit_events: List[float] = []
self._dm_policy = str(extra.get("dm_policy") or os.getenv("WEIXIN_DM_POLICY", "pairing")).strip().lower()
self._group_policy = str(extra.get("group_policy") or os.getenv("WEIXIN_GROUP_POLICY", "disabled")).strip().lower()
self._dm_policy = str(extra.get("dm_policy") or _wx_secret("WEIXIN_DM_POLICY", "pairing")).strip().lower()
self._group_policy = str(extra.get("group_policy") or _wx_secret("WEIXIN_GROUP_POLICY", "disabled")).strip().lower()
allow_from = extra.get("allow_from")
if allow_from is None:
allow_from = os.getenv("WEIXIN_ALLOWED_USERS", "")
allow_from = _wx_secret("WEIXIN_ALLOWED_USERS", "")
group_allow_from = extra.get("group_allow_from")
if group_allow_from is None:
group_allow_from = os.getenv("WEIXIN_GROUP_ALLOWED_USERS", "")
group_allow_from = _wx_secret("WEIXIN_GROUP_ALLOWED_USERS", "")
self._allow_from = self._coerce_list(allow_from)
self._group_allow_from = self._coerce_list(group_allow_from)
self._split_multiline_messages = _coerce_bool(
Expand Down
13 changes: 7 additions & 6 deletions gateway/platforms/yuanbao.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
next_seq_no,
)
from gateway.session import build_session_key
from gateway.authz_mixin import _platform_gate_env

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -1282,9 +1283,9 @@ def __init__(
self._group_allow_from = group_allow_from

def _open_dm_opted_in(self) -> bool:
if os.getenv("GATEWAY_ALLOW_ALL_USERS", "").lower() in {"true", "1", "yes"}:
if _platform_gate_env("GATEWAY_ALLOW_ALL_USERS", "").lower() in {"true", "1", "yes"}:
return True
return os.getenv("YUANBAO_ALLOW_ALL_USERS", "").lower() in {"true", "1", "yes"}
return _platform_gate_env("YUANBAO_ALLOW_ALL_USERS", "").lower() in {"true", "1", "yes"}

def is_dm_allowed(self, sender_id: str) -> bool:
"""Strict DM authorization — pairing does not imply access."""
Expand Down Expand Up @@ -4946,23 +4947,23 @@ def __init__(self, config: PlatformConfig, **kwargs: Any) -> None:
# ------------------------------------------------------------------
dm_policy: str = (
_extra.get("dm_policy")
or os.getenv("YUANBAO_DM_POLICY", "pairing")
or _platform_gate_env("YUANBAO_DM_POLICY", "pairing")
).strip().lower()

_dm_allow_from_raw: str = (
_extra.get("dm_allow_from")
or os.getenv("YUANBAO_DM_ALLOW_FROM", "")
or _platform_gate_env("YUANBAO_DM_ALLOW_FROM", "")
)
dm_allow_from: list[str] = [x.strip() for x in _dm_allow_from_raw.split(",") if x.strip()]

group_policy: str = (
_extra.get("group_policy")
or os.getenv("YUANBAO_GROUP_POLICY", "pairing")
or _platform_gate_env("YUANBAO_GROUP_POLICY", "pairing")
).strip().lower()

_group_allow_from_raw: str = (
_extra.get("group_allow_from")
or os.getenv("YUANBAO_GROUP_ALLOW_FROM", "")
or _platform_gate_env("YUANBAO_GROUP_ALLOW_FROM", "")
)
group_allow_from: list[str] = [x.strip() for x in _group_allow_from_raw.split(",") if x.strip()]

Expand Down
2 changes: 1 addition & 1 deletion gateway/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2750,7 +2750,7 @@ def _own_policy_open_startup_violation(config) -> Optional[str]:
).strip().lower()
if dm_policy != "open" and group_policy != "open":
continue
gateway_allow_all = os.getenv(
gateway_allow_all = _getenv(
"GATEWAY_ALLOW_ALL_USERS", ""
).lower() in {"true", "1", "yes"}
platform_opted_in = gateway_allow_all or (
Expand Down
6 changes: 3 additions & 3 deletions plugins/platforms/wecom/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,18 @@ def __init__(self, config: PlatformConfig):
or os.getenv("WECOM_WEBSOCKET_URL", DEFAULT_WS_URL)
).strip() or DEFAULT_WS_URL

self._dm_policy = str(extra.get("dm_policy") or os.getenv("WECOM_DM_POLICY", "pairing")).strip().lower()
self._dm_policy = str(extra.get("dm_policy") or _get_scoped_secret("WECOM_DM_POLICY", "pairing")).strip().lower()
# dm_policy already honors WECOM_DM_POLICY, so the allowlist must honor
# WECOM_ALLOWED_USERS too. Without the env fallback an env-only setup
# (dm_policy=allowlist via env, no config extra) runs with an empty
# allowlist and drops every authorized DM at intake.
self._allow_from = _coerce_list(
extra.get("allow_from")
or extra.get("allowFrom")
or os.getenv("WECOM_ALLOWED_USERS", "")
or _get_scoped_secret("WECOM_ALLOWED_USERS", "")
)

self._group_policy = str(extra.get("group_policy") or os.getenv("WECOM_GROUP_POLICY", "pairing")).strip().lower()
self._group_policy = str(extra.get("group_policy") or _get_scoped_secret("WECOM_GROUP_POLICY", "pairing")).strip().lower()
self._group_allow_from = _coerce_list(extra.get("group_allow_from") or extra.get("groupAllowFrom"))
self._groups = extra.get("groups") if isinstance(extra.get("groups"), dict) else {}

Expand Down
36 changes: 36 additions & 0 deletions tests/gateway/test_multiplex_profile_authz.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,42 @@ def fake_is_user_authorized(source):
assert captured["profile"] == "coder"


def test_startup_guard_gateway_allow_all_reads_scope_not_environ(monkeypatch):
"""The GATEWAY_ALLOW_ALL_USERS opt-in check inside the startup guard
must honor the active profile secret scope (#93522): the default
profile's env-only opt-in must not leak into a secondary profile that
never opted in, and a secondary profile's own scoped opt-in must be
honored."""
from agent import secret_scope
from gateway.run import _own_policy_open_startup_violation

_clear_auth_env(monkeypatch)
cfg = GatewayConfig(multiplex_profiles=True)
cfg.platforms = {
Platform.WECOM: PlatformConfig(enabled=True, extra={"dm_policy": "open"}),
}

previous_multiplex = secret_scope.is_multiplex_active()
secret_scope.set_multiplex_active(True)
monkeypatch.setenv("GATEWAY_ALLOW_ALL_USERS", "true")
try:
token = secret_scope.set_secret_scope({"SOMETHING_ELSE": "x"})
try:
violation = _own_policy_open_startup_violation(cfg)
finally:
secret_scope.reset_secret_scope(token)
assert violation is not None, "default profile's env opt-in must not leak into the scoped secondary profile"

token = secret_scope.set_secret_scope({"GATEWAY_ALLOW_ALL_USERS": "true"})
try:
violation = _own_policy_open_startup_violation(cfg)
finally:
secret_scope.reset_secret_scope(token)
assert violation is None, "the secondary profile's own scoped opt-in must be honored"
finally:
secret_scope.set_multiplex_active(previous_multiplex)


def test_secondary_open_policy_fails_startup_guard(monkeypatch):
"""Secondary profiles must pass the same open-policy startup guard."""
from gateway.run import _own_policy_open_startup_violation
Expand Down
47 changes: 47 additions & 0 deletions tests/gateway/test_wecom.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,53 @@ def test_declares_non_editable_message_capability(self):
assert WeComAdapter.SUPPORTS_MESSAGE_EDITING is False


class TestWeComAdapterAuthzScope:
"""dm_policy/allowlist reads must honor the profile secret scope under
multiplexing (#93522): a secondary profile's own scope is authoritative
and must not inherit the default profile's process-env authorization."""

@pytest.fixture()
def multiplex_on(self):
from agent import secret_scope

previous = secret_scope.is_multiplex_active()
secret_scope.set_multiplex_active(True)
try:
yield
finally:
secret_scope.set_multiplex_active(previous)

def test_scoped_construction_reads_authz_from_scope_not_environ(self, multiplex_on, monkeypatch):
from agent import secret_scope
from plugins.platforms.wecom.adapter import WeComAdapter

monkeypatch.setenv("WECOM_DM_POLICY", "pairing")
monkeypatch.setenv("WECOM_ALLOWED_USERS", "default-user")
token = secret_scope.set_secret_scope(
{"WECOM_DM_POLICY": "allowlist", "WECOM_ALLOWED_USERS": "scoped-user"}
)
try:
adapter = WeComAdapter(PlatformConfig(enabled=True))
finally:
secret_scope.reset_secret_scope(token)
assert adapter._dm_policy == "allowlist"
assert adapter._allow_from == ["scoped-user"]

def test_scoped_miss_does_not_admit_default_profiles_allowlist(self, multiplex_on, monkeypatch):
from agent import secret_scope
from plugins.platforms.wecom.adapter import WeComAdapter

monkeypatch.setenv("WECOM_DM_POLICY", "allowlist")
monkeypatch.setenv("WECOM_ALLOWED_USERS", "default-user")
token = secret_scope.set_secret_scope({"SOMETHING_ELSE": "x"})
try:
adapter = WeComAdapter(PlatformConfig(enabled=True))
finally:
secret_scope.reset_secret_scope(token)
assert adapter._dm_policy == "pairing"
assert adapter._allow_from == []


class TestWeComConnect:

@pytest.mark.asyncio
Expand Down
46 changes: 46 additions & 0 deletions tests/gateway/test_weixin_secret_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,49 @@ def test_multiplex_unscoped_construction_falls_back_to_environ(
secret_scope.reset_secret_scope(token)
assert adapter._account_id == "env-account"
assert adapter._token == "env-token"


class TestWeixinAdapterAuthzScope:
"""Authorization reads (dm_policy/allowlists) must follow the same
scoped-secret rules as the credential reads above (#93522): a secondary
profile's own scope is authoritative, and the default profile's
process-env values must not leak into it."""

def test_scoped_construction_reads_authz_from_scope_not_environ(
self, multiplex_on, monkeypatch
):
monkeypatch.setenv("WEIXIN_DM_POLICY", "pairing")
monkeypatch.setenv("WEIXIN_ALLOWED_USERS", "default-user")
monkeypatch.setenv("WEIXIN_GROUP_ALLOWED_USERS", "default-group-user")
token = secret_scope.set_secret_scope(
{
"WEIXIN_DM_POLICY": "allowlist",
"WEIXIN_ALLOWED_USERS": "scoped-user",
"WEIXIN_GROUP_ALLOWED_USERS": "scoped-group-user",
}
)
try:
adapter = WeixinAdapter(PlatformConfig(enabled=True))
finally:
secret_scope.reset_secret_scope(token)
assert adapter._dm_policy == "allowlist"
assert adapter._allow_from == ["scoped-user"]
assert adapter._group_allow_from == ["scoped-group-user"]
assert adapter._is_dm_allowed("scoped-user") is True
assert adapter._is_dm_allowed("default-user") is False

def test_scoped_miss_does_not_admit_default_profiles_allow_all(
self, multiplex_on, monkeypatch
):
"""A secondary profile with no allowlist of its own must fail closed,
not inherit the default profile's env-only allowlist/opt-in."""
monkeypatch.setenv("WEIXIN_DM_POLICY", "allowlist")
monkeypatch.setenv("WEIXIN_ALLOWED_USERS", "default-user")
token = secret_scope.set_secret_scope({"SOMETHING_ELSE": "x"})
try:
adapter = WeixinAdapter(PlatformConfig(enabled=True))
finally:
secret_scope.reset_secret_scope(token)
assert adapter._dm_policy == "pairing"
assert adapter._allow_from == []
assert adapter._is_dm_allowed("default-user") is False
72 changes: 72 additions & 0 deletions tests/gateway/test_yuanbao_secret_scope.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""Yuanbao authorization-scope regression tests (#93522).

``dm_policy``/``group_policy``/allowlist reads and the ``AccessPolicy``
allow-all opt-in must honor the active profile secret scope under
multiplexing: a secondary profile's own scope is authoritative and must
not inherit the default profile's process-env authorization config.
"""

import pytest

from agent import secret_scope
from gateway.config import PlatformConfig
from gateway.platforms.yuanbao import AccessPolicy, YuanbaoAdapter


@pytest.fixture()
def multiplex_on():
previous = secret_scope.is_multiplex_active()
secret_scope.set_multiplex_active(True)
try:
yield
finally:
secret_scope.set_multiplex_active(previous)


class TestYuanbaoAdapterAuthzScope:
def test_scoped_construction_reads_authz_from_scope_not_environ(self, multiplex_on, monkeypatch):
monkeypatch.setenv("YUANBAO_DM_POLICY", "pairing")
monkeypatch.setenv("YUANBAO_DM_ALLOW_FROM", "default-user")
token = secret_scope.set_secret_scope(
{"YUANBAO_DM_POLICY": "allowlist", "YUANBAO_DM_ALLOW_FROM": "scoped-user"}
)
try:
adapter = YuanbaoAdapter(PlatformConfig(enabled=True))
finally:
secret_scope.reset_secret_scope(token)
assert adapter._access_policy._dm_policy == "allowlist"
assert adapter._access_policy._dm_allow_from == ["scoped-user"]

def test_scoped_miss_does_not_admit_default_profiles_allowlist(self, multiplex_on, monkeypatch):
monkeypatch.setenv("YUANBAO_DM_POLICY", "allowlist")
monkeypatch.setenv("YUANBAO_DM_ALLOW_FROM", "default-user")
token = secret_scope.set_secret_scope({"SOMETHING_ELSE": "x"})
try:
adapter = YuanbaoAdapter(PlatformConfig(enabled=True))
finally:
secret_scope.reset_secret_scope(token)
assert adapter._access_policy._dm_policy == "pairing"
assert adapter._access_policy._dm_allow_from == []


class TestYuanbaoAccessPolicyOpenDmOptIn:
def test_scoped_allow_all_admits(self, multiplex_on, monkeypatch):
monkeypatch.delenv("GATEWAY_ALLOW_ALL_USERS", raising=False)
monkeypatch.delenv("YUANBAO_ALLOW_ALL_USERS", raising=False)
policy = AccessPolicy(dm_policy="open", dm_allow_from=[], group_policy="pairing", group_allow_from=[])
token = secret_scope.set_secret_scope({"YUANBAO_ALLOW_ALL_USERS": "true"})
try:
assert policy._open_dm_opted_in() is True
finally:
secret_scope.reset_secret_scope(token)

def test_default_profiles_allow_all_does_not_leak_into_scoped_miss(self, multiplex_on, monkeypatch):
"""The default profile's env-only GATEWAY_ALLOW_ALL_USERS must not
admit a secondary profile that never opted in."""
monkeypatch.setenv("GATEWAY_ALLOW_ALL_USERS", "true")
policy = AccessPolicy(dm_policy="open", dm_allow_from=[], group_policy="pairing", group_allow_from=[])
token = secret_scope.set_secret_scope({"SOMETHING_ELSE": "x"})
try:
assert policy._open_dm_opted_in() is False
finally:
secret_scope.reset_secret_scope(token)
Loading