Skip to content

The agent that grows with you

fix(whatsapp): adapter reads WHATSAPP_GROUP_ALLOWED_USERS for the group allowlist (#72529)

sal477afd24209e
diff --git a/plugins/platforms/whatsapp/adapter.py b/plugins/platforms/whatsapp/adapter.py
index 81185b4663a..eec8836de43 100644
--- a/plugins/platforms/whatsapp/adapter.py
+++ b/plugins/platforms/whatsapp/adapter.py
@@ -275,7 +275,15 @@ class WhatsAppAdapter(WhatsAppBehaviorMixin, BasePlatformAdapter):
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_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"))
+ # Group allowlist: config keys win, then the profile-scoped env CSV — mirroring the DM
+ # path (_select_dm_allowlist) so a group_allow_from set only via WHATSAPP_GROUP_ALLOWED_USERS
+ # reaches the adapter (#72529: the env carrier was bridged to the Node bridge but never read here,
+ # so env-only installs silently ran group gating with an empty allowlist).
+ self._group_allow_from = self._coerce_allow_list(
+ extra.get("group_allow_from") if "group_allow_from" in extra
+ else (extra.get("groupAllowFrom") if "groupAllowFrom" in extra
+ else (_wenv("WHATSAPP_GROUP_ALLOW_FROM") or _wenv("WHATSAPP_GROUP_ALLOWED_USERS") or None))
+ )
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"}
self._mention_patterns = self._compile_mention_patterns()
diff --git a/tests/gateway/test_whatsapp_group_allowlist_env.py b/tests/gateway/test_whatsapp_group_allowlist_env.py
new file mode 100644
index 00000000000..6f27e9f373f
--- /dev/null
+++ b/tests/gateway/test_whatsapp_group_allowlist_env.py
@@ -0,0 +1,99 @@
+"""Regression test for #72529 — the WhatsApp adapter never read WHATSAPP_GROUP_ALLOWED_USERS.
+
+The Node bridge receives WHATSAPP_GROUP_ALLOWED_USERS via _BRIDGE_PASSTHROUGH_ENV, and the
+YAML bridge maps config.yaml ``whatsapp.group_allow_from`` onto the same env carrier — but
+``WhatsAppAdapter.__init__`` seeded ``_group_allow_from`` from ``extra`` alone. An install
+that configured the group allowlist only through the documented env var (or relied on the
+YAML bridge to populate it) silently ran group gating with an empty allowlist: with
+``group_policy: allowlist`` every group message was rejected, and with ``group_policy: open``
+nothing changed — the triage on #72529 flagged exactly this half as "no PR yet".
+
+The DM path already had the right shape (``_select_dm_allowlist`` reads config by key
+*presence*, then the scoped env). This test pins the group path to the same precedence:
+
+1. ``extra["group_allow_from"]`` (explicit list or CSV) wins, even when empty-ish forms differ;
+2. the legacy ``groupAllowFrom`` key follows;
+3. with neither config key present, WHATSAPP_GROUP_ALLOW_FROM / WHATSAPP_GROUP_ALLOWED_USERS
+ (profile-scoped, multiplex-safe) seed the allowlist;
+4. config wins over env — an env value must never broaden a config-seeded list.
+"""
+from unittest.mock import patch
+
+import pytest
+
+from gateway.config import Platform, PlatformConfig
+
+
+def _adapter_with_extra(extra):
+ from plugins.platforms.whatsapp.adapter import WhatsAppAdapter
+ return WhatsAppAdapter(PlatformConfig(enabled=True, extra=extra))
+
+
+class TestGroupAllowlistEnvFallback:
+ """Env-only group allowlists must reach ``_group_allow_from``."""
+
+ def test_env_only_group_allowlist_is_read(self, monkeypatch):
+ monkeypatch.setenv("WHATSAPP_GROUP_ALLOWED_USERS", "[email protected], [email protected]")
+ monkeypatch.delenv("WHATSAPP_GROUP_ALLOW_FROM", raising=False)
+ adapter = _adapter_with_extra({})
+ assert adapter._group_allow_from == {"[email protected]", "[email protected]"}
+
+ def test_group_allow_from_env_alias(self, monkeypatch):
+ """WHATSAPP_GROUP_ALLOW_FROM (the allow_from-style spelling) is honoured too."""
+ monkeypatch.setenv("WHATSAPP_GROUP_ALLOW_FROM", "[email protected]")
+ monkeypatch.delenv("WHATSAPP_GROUP_ALLOWED_USERS", raising=False)
+ adapter = _adapter_with_extra({})
+ assert adapter._group_allow_from == {"[email protected]"}
+
+ def test_config_group_allow_from_still_wins(self, monkeypatch):
+ """An explicit config list stays authoritative — the env value must not broaden it."""
+ monkeypatch.setenv("WHATSAPP_GROUP_ALLOWED_USERS", "[email protected]")
+ adapter = _adapter_with_extra({"group_allow_from": ["[email protected]"]})
+ assert adapter._group_allow_from == {"[email protected]"}
+
+ def test_legacy_group_allow_from_key_wins_over_env(self, monkeypatch):
+ monkeypatch.setenv("WHATSAPP_GROUP_ALLOWED_USERS", "[email protected]")
+ adapter = _adapter_with_extra({"groupAllowFrom": ["[email protected]"]})
+ assert adapter._group_allow_from == {"[email protected]"}
+
+ def test_empty_env_and_no_config_means_empty_allowlist(self, monkeypatch):
+ monkeypatch.delenv("WHATSAPP_GROUP_ALLOWED_USERS", raising=False)
+ monkeypatch.delenv("WHATSAPP_GROUP_ALLOW_FROM", raising=False)
+ adapter = _adapter_with_extra({})
+ assert adapter._group_allow_from == set()
+
+ def test_scoped_env_read_through_secret_scope(self, tmp_path, monkeypatch):
+ """The env read is profile-scoped (multiplex-safe) like every other WHATSAPP_* read."""
+ from agent import secret_scope as ss
+
+ (tmp_path / ".env").write_text("[email protected]\n")
+ monkeypatch.delenv("WHATSAPP_GROUP_ALLOWED_USERS", raising=False)
+ monkeypatch.delenv("WHATSAPP_GROUP_ALLOW_FROM", raising=False)
+
+ ss.set_multiplex_active(True)
+ tok = ss.set_secret_scope(ss.build_profile_secret_scope(tmp_path))
+ try:
+ adapter = _adapter_with_extra({})
+ assert adapter._group_allow_from == {"[email protected]"}
+ finally:
+ ss.reset_secret_scope(tok)
+ ss.set_multiplex_active(False)
+
+
+class TestGroupGatingUsesEnvSeededAllowlist:
+ """#72529's symptom: authorized group senders rejected because the allowlist was empty."""
+
+ def test_allowlisted_group_chat_passes_intake(self, monkeypatch):
+ """group_policy=allowlist + env-only allowlist → the group chat is admitted."""
+ monkeypatch.setenv("WHATSAPP_GROUP_ALLOWED_USERS", "[email protected]")
+ monkeypatch.setenv("WHATSAPP_GROUP_POLICY", "allowlist")
+ adapter = _adapter_with_extra({})
+ assert adapter._group_policy == "allowlist"
+ assert adapter._is_group_allowed("[email protected]") is True
+ assert adapter._is_group_allowed("[email protected]") is False
+
+ def test_open_group_policy_unaffected(self, monkeypatch):
+ monkeypatch.setenv("WHATSAPP_GROUP_POLICY", "open")
+ monkeypatch.delenv("WHATSAPP_GROUP_ALLOWED_USERS", raising=False)
+ adapter = _adapter_with_extra({})
+ assert adapter._is_group_allowed("[email protected]") is True