diff --git a/plugins/platforms/whatsapp/adapter.py b/plugins/platforms/whatsapp/adapter.py index b89d0f4abf..21d97ed961 100644 --- a/plugins/platforms/whatsapp/adapter.py +++ b/plugins/platforms/whatsapp/adapter.py @@ -15,7 +15,7 @@ from pathlib import Path from typing import Dict, Optional, Any from gateway.platforms._shared import ( - apply_yaml_bridge as _apply_yaml_bridge, get_scoped_secret, send_error + apply_yaml_bridge as _apply_yaml_bridge, extra_or_secret as _extra_or_secret, get_scoped_secret, send_error ) from hermes_cli._subprocess_compat import windows_detach_popen_kwargs from hermes_constants import (find_node_executable, get_hermes_dir, with_hermes_node_path) @@ -272,9 +272,9 @@ class WhatsAppAdapter(WhatsAppBehaviorMixin, BasePlatformAdapter): self._bridge_script: str = extra.get("bridge_script", str(self._DEFAULT_BRIDGE_DIR / "bridge.js")) self._session_path = Path(extra.get("session_path", get_hermes_dir("platforms/whatsapp/session", "whatsapp/session"))) self._reply_prefix: Optional[str] = extra.get("reply_prefix") - self._dm_policy = str(extra.get("dm_policy") or _wenv("WHATSAPP_DM_POLICY", "pairing")).strip().lower() + self._dm_policy = str(_extra_or_secret(extra, "dm_policy", "WHATSAPP_DM_POLICY", "pairing")).strip().lower() self._allow_from = self._coerce_allow_list(self._select_dm_allowlist(extra, ("WHATSAPP_ALLOWED_USERS",), _wenv)) - self._group_policy = str(extra.get("group_policy") or _wenv("WHATSAPP_GROUP_POLICY", "pairing")).strip().lower() + self._group_policy = str(_extra_or_secret(extra, "group_policy", "WHATSAPP_GROUP_POLICY", "pairing")).strip().lower() self._group_allow_from = self._coerce_allow_list(extra.get("group_allow_from") or extra.get("groupAllowFrom")) rr = extra.get("send_read_receipts", False) self._send_read_receipts = rr if isinstance(rr, bool) else str(rr or "").strip().lower() in {"1", "true", "yes", "on"} @@ -375,8 +375,10 @@ class WhatsAppAdapter(WhatsAppBehaviorMixin, BasePlatformAdapter): return False def _bridge_env(self) -> dict: - """Subprocess env: profile-resolved WHATSAPP_* values + profile-aware cache dirs.""" - # with_hermes_node_path() copies os.environ when called with no arg. + """Subprocess env: the adapter's EFFECTIVE profile policy + profile-resolved WHATSAPP_* values + cache dirs.""" + # with_hermes_node_path() copies os.environ when called with no arg: under a multiplexed secondary + # that copy carries the DEFAULT profile's WHATSAPP_* values, so every bridge-consumed key is + # re-resolved from this profile (dropped on a scoped miss), never inherited from the launch env. bridge_env = with_hermes_node_path() if self._reply_prefix is not None: bridge_env["WHATSAPP_REPLY_PREFIX"] = self._reply_prefix @@ -384,6 +386,17 @@ class WhatsAppAdapter(WhatsAppBehaviorMixin, BasePlatformAdapter): for _key, _v in [("WHATSAPP_MODE", _wenv("WHATSAPP_MODE", "self-chat"))] + [(k, _wenv(k)) for k in _BRIDGE_PASSTHROUGH_ENV]: if _v: bridge_env[_key] = _v + else: + bridge_env.pop(_key, None) + # bridge.js gates DMs BEFORE Python sees them: it must run the same dm_policy / allow_from the + # adapter resolved (scoped env → this profile's YAML → default), or a secondary's YAML + # ``dm_policy: pairing`` runs under the default profile's allowlist and drops valid pairing DMs. + bridge_env["WHATSAPP_DM_POLICY"] = self._dm_policy + allowed = ",".join(sorted(self._allow_from)) + if allowed: + bridge_env["WHATSAPP_ALLOWED_USERS"] = allowed + else: + bridge_env.pop("WHATSAPP_ALLOWED_USERS", None) # Without these the bridge hardcodes ~/.hermes/{image,audio,document}_cache (wrong under HERMES_HOME/profiles/cache layout). img_dir, audio_dir, _video_dir, doc_dir = _cache_dirs() bridge_env.update(HERMES_IMAGE_CACHE_DIR=str(img_dir), HERMES_AUDIO_CACHE_DIR=str(audio_dir), HERMES_DOCUMENT_CACHE_DIR=str(doc_dir)) diff --git a/tests/gateway/test_whatsapp_connect.py b/tests/gateway/test_whatsapp_connect.py index 756282ba29..0e3bfe28e0 100644 --- a/tests/gateway/test_whatsapp_connect.py +++ b/tests/gateway/test_whatsapp_connect.py @@ -54,6 +54,8 @@ def _make_adapter(): adapter._bridge_process = None adapter._reply_prefix = None adapter._send_read_receipts = False + adapter._dm_policy = "pairing" + adapter._allow_from = set() adapter._running = False adapter._message_handler = None adapter._fatal_error_code = None diff --git a/tests/gateway/test_whatsapp_stale_bridge.py b/tests/gateway/test_whatsapp_stale_bridge.py index 69d158ecbd..a35d0d71fb 100644 --- a/tests/gateway/test_whatsapp_stale_bridge.py +++ b/tests/gateway/test_whatsapp_stale_bridge.py @@ -54,6 +54,8 @@ def _make_adapter(bridge_script: str = "/tmp/test-bridge.js", adapter._bridge_process = None adapter._reply_prefix = None adapter._send_read_receipts = False + adapter._dm_policy = "pairing" + adapter._allow_from = set() adapter._running = False adapter._message_handler = None adapter._fatal_error_code = None