The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(feishu): resolve DM admission config per-profile under multiplex (#86905)
SHT9f40d3bdd096
diff --git a/plugins/platforms/feishu/adapter.py b/plugins/platforms/feishu/adapter.pyindex 153f2d4d0c6..f9460201c09 100644--- a/plugins/platforms/feishu/adapter.py+++ b/plugins/platforms/feishu/adapter.py@@ -436,6 +436,9 @@ class FeishuAdapterSettings:group_rules: Dict[str, FeishuGroupRule] = field(default_factory=dict)allow_bots: str = "none" # "none" | "mentions" | "all"require_mention: bool = True+ # DM allow-all (FEISHU_ALLOW_ALL_USERS / GATEWAY_ALLOW_ALL_USERS), resolved+ # per-profile so multiplexed secondary adapters honor their own .env.+ allow_all_dm: bool = False@dataclass@@ -1591,7 +1594,9 @@ class FeishuAdapter(BasePlatformAdapter):# Env-only so adapter and gateway auth bypass share one source; yaml# feishu.allow_bots is bridged to this env var at config load.- allow_bots = os.getenv("FEISHU_ALLOW_BOTS", "none").strip().lower()+ # Scope-aware read: under multiplex a secondary profile's .env must+ # govern its own adapter (same pattern as app_secret below) — #86905.+ allow_bots = _get_scoped_secret("FEISHU_ALLOW_BOTS", "none").strip().lower()if allow_bots not in {"none", "mentions", "all"}:logger.warning("[Feishu] Unknown allow_bots=%r, falling back to 'none'. Valid: none, mentions, all.",@@ -1599,8 +1604,13 @@ class FeishuAdapter(BasePlatformAdapter):)allow_bots = "none"+ allow_all_dm = any(+ _get_scoped_secret(var, "").strip().lower() in {"true", "1", "yes"}+ for var in ("FEISHU_ALLOW_ALL_USERS", "GATEWAY_ALLOW_ALL_USERS")+ )+return FeishuAdapterSettings(- app_id=str(extra.get("app_id") or os.getenv("FEISHU_APP_ID", "")).strip(),+ app_id=str(extra.get("app_id") or _get_scoped_secret("FEISHU_APP_ID", "")).strip(),app_secret=str(extra.get("app_secret") or _get_scoped_secret("FEISHU_APP_SECRET", "")).strip(),domain_name=str(extra.get("domain") or os.getenv("FEISHU_DOMAIN", "feishu")).strip().lower(),connection_mode=str(@@ -1610,15 +1620,15 @@ class FeishuAdapter(BasePlatformAdapter):verification_token=str(extra.get("verification_token") or _get_scoped_secret("FEISHU_VERIFICATION_TOKEN", "")).strip(),- group_policy=os.getenv("FEISHU_GROUP_POLICY", "allowlist").strip().lower(),+ group_policy=_get_scoped_secret("FEISHU_GROUP_POLICY", "allowlist").strip().lower(),allowed_group_users=frozenset(item.strip()- for item in os.getenv("FEISHU_ALLOWED_USERS", "").split(",")+ for item in _get_scoped_secret("FEISHU_ALLOWED_USERS", "").split(",")if item.strip()),- bot_open_id=os.getenv("FEISHU_BOT_OPEN_ID", "").strip(),- bot_user_id=os.getenv("FEISHU_BOT_USER_ID", "").strip(),- bot_name=os.getenv("FEISHU_BOT_NAME", "").strip(),+ bot_open_id=_get_scoped_secret("FEISHU_BOT_OPEN_ID", "").strip(),+ bot_user_id=_get_scoped_secret("FEISHU_BOT_USER_ID", "").strip(),+ bot_name=_get_scoped_secret("FEISHU_BOT_NAME", "").strip(),dedup_cache_size=max(32,env_int("HERMES_FEISHU_DEDUP_CACHE_SIZE", _DEFAULT_DEDUP_CACHE_SIZE),@@ -1658,8 +1668,9 @@ class FeishuAdapter(BasePlatformAdapter):default_group_policy=default_group_policy,group_rules=group_rules,allow_bots=allow_bots,+ allow_all_dm=allow_all_dm,require_mention=_to_boolean(- extra.get("require_mention", os.getenv("FEISHU_REQUIRE_MENTION", "true"))+ extra.get("require_mention", _get_scoped_secret("FEISHU_REQUIRE_MENTION", "true"))),)@@ -1692,6 +1703,7 @@ class FeishuAdapter(BasePlatformAdapter):self._ws_ping_interval = settings.ws_ping_intervalself._ws_ping_timeout = settings.ws_ping_timeoutself._allow_bots = settings.allow_bots+ self._allow_all_dm = settings.allow_all_dmself._require_mention = settings.require_mentiondef _build_event_handler(self) -> Any:@@ -4390,9 +4402,10 @@ class FeishuAdapter(BasePlatformAdapter):return "bot_not_mentioned"if not is_group:- if os.getenv("FEISHU_ALLOW_ALL_USERS", "").strip().lower() in {"true", "1", "yes"}:- return None- if os.getenv("GATEWAY_ALLOW_ALL_USERS", "").strip().lower() in {"true", "1", "yes"}:+ # Snapshotted per-profile in _load_settings: _admit runs on the+ # lark_oapi WS thread with no secret scope, and a bare os.getenv+ # here would read the default profile's value (#86905).+ if self._allow_all_dm:return None# Empty FEISHU_ALLOWED_USERS is the pairing-mode default from setup:# forward DMs to gateway intake so the pairing handshake can run.diff --git a/tests/gateway/feishu_helpers.py b/tests/gateway/feishu_helpers.pyindex ae8a4bfc371..97771daaa35 100644--- a/tests/gateway/feishu_helpers.py+++ b/tests/gateway/feishu_helpers.py@@ -34,6 +34,7 @@ def make_adapter_skeleton(allow_bots: str = "none",require_mention: bool = True,group_policy: str = "allowlist",+ allow_all_dm: bool = False,) -> Any:from plugins.platforms.feishu.adapter import FeishuAdapter@@ -48,6 +49,7 @@ def make_adapter_skeleton(adapter._default_group_policy = group_policyadapter._allowed_group_users = frozenset()adapter._allow_bots = allow_bots+ adapter._allow_all_dm = allow_all_dmadapter._require_mention = require_mentionreturn adapterdiff --git a/tests/gateway/test_feishu_bot_admission.py b/tests/gateway/test_feishu_bot_admission.pyindex 09157c757e9..b396e90d299 100644--- a/tests/gateway/test_feishu_bot_admission.py+++ b/tests/gateway/test_feishu_bot_admission.py@@ -566,3 +566,77 @@ def test_handle_message_event_data_forwards_sender_when_admitted():assert captured.get("sender_id") is sender.sender_idassert captured.get("is_bot") is Trueassert captured.get("message_id") == "om_bot_ok"+++# --- Profile-scoped admission config (#86905) -------------------------------+++def test_dm_admission_config_resolves_from_profile_scope_under_multiplex(tmp_path, monkeypatch):+ """os.environ holds the DEFAULT profile's admission view; a secondary+ profile's .env must govern its own adapter — Feishu open_ids are+ app-scoped, so the default allow-list can never match the role app's+ senders, and the role profile's allow-all flag must be honored."""+ import agent.secret_scope as ss+ from plugins.platforms.feishu.adapter import FeishuAdapter++ monkeypatch.setenv("FEISHU_APP_ID", "cli_default")+ monkeypatch.setenv("FEISHU_APP_SECRET", "secret_default")+ monkeypatch.setenv("FEISHU_ALLOWED_USERS", "ou_default_view")+ monkeypatch.setenv("FEISHU_ALLOW_BOTS", "all")+ monkeypatch.delenv("GATEWAY_ALLOW_ALL_USERS", raising=False)+ monkeypatch.delenv("FEISHU_ALLOW_ALL_USERS", raising=False)+ (tmp_path / ".env").write_text(+ "FEISHU_APP_ID=cli_role\nFEISHU_APP_SECRET=secret_role\n"+ "FEISHU_ALLOWED_USERS=ou_role_view\n",+ encoding="utf-8",+ )++ ss.set_multiplex_active(True)+ tok = ss.set_secret_scope(ss.build_profile_secret_scope(tmp_path))+ try:+ settings = FeishuAdapter._load_settings(extra={})+ finally:+ ss.reset_secret_scope(tok)+ (tmp_path / ".env").write_text(+ "FEISHU_APP_ID=cli_role\nFEISHU_APP_SECRET=secret_role\nGATEWAY_ALLOW_ALL_USERS=true\n",+ encoding="utf-8",+ )+ tok = ss.set_secret_scope(ss.build_profile_secret_scope(tmp_path))+ try:+ allow_all = FeishuAdapter._load_settings(extra={})+ finally:+ ss.reset_secret_scope(tok)+ ss.set_multiplex_active(False)++ assert settings.app_id == "cli_role"+ assert settings.allowed_group_users == frozenset({"ou_role_view"})+ assert settings.allow_bots == "none" # default's "all" must not leak in+ assert settings.allow_all_dm is False++ # _admit runs on the WS thread with no scope: the snapshot must carry.+ adapter = object.__new__(FeishuAdapter)+ adapter._apply_settings(settings)+ assert adapter._admit(make_sender(open_id="ou_role_view"), make_message(chat_type="p2p")) is None+ assert adapter._admit(make_sender(open_id="ou_default_view"), make_message(chat_type="p2p")) == "dm_policy_rejected"++ assert allow_all.allow_all_dm is True+ adapter = object.__new__(FeishuAdapter)+ adapter._apply_settings(allow_all)+ assert adapter._admit(make_sender(open_id="ou_anyone"), make_message(chat_type="p2p")) is None+++def test_dm_admission_config_falls_back_to_os_environ_when_unscoped(monkeypatch):+ """Single-profile behavior unchanged: process env still configures DMs."""+ from plugins.platforms.feishu.adapter import FeishuAdapter++ monkeypatch.setenv("FEISHU_APP_ID", "cli_test")+ monkeypatch.setenv("FEISHU_APP_SECRET", "secret_test")+ monkeypatch.setenv("GATEWAY_ALLOW_ALL_USERS", "true")+ monkeypatch.setenv("FEISHU_ALLOWED_USERS", "ou_a,ou_b")++ settings = FeishuAdapter._load_settings(extra={})+ assert settings.allow_all_dm is True+ assert settings.allowed_group_users == frozenset({"ou_a", "ou_b"})+ adapter = object.__new__(FeishuAdapter)+ adapter._apply_settings(settings)+ assert adapter._admit(make_sender(open_id="ou_anyone"), make_message(chat_type="p2p")) is None