Skip to content

The agent that grows with you

fix(gateway): apply WhatsApp identity aliases to cloud pairing revoke

fangliquanflqb35f219aedde
diff --git a/gateway/authz_mixin.py b/gateway/authz_mixin.py
index 49972eee67c..f1bb24bb8a8 100644
--- a/gateway/authz_mixin.py
+++ b/gateway/authz_mixin.py
@@ -751,8 +751,9 @@ class GatewayAuthorizationMixin:
if "@" in user_id:
check_ids.add(user_id.split("@")[0])
- # WhatsApp: resolve phone↔LID aliases from bridge session mapping files
- if source.platform == Platform.WHATSAPP:
+ # WhatsApp (Baileys + Cloud): resolve phone↔LID / JID aliases so
+ # device-suffix and bare-phone allowlist entries match the same principal.
+ if source.platform in {Platform.WHATSAPP, Platform.WHATSAPP_CLOUD}:
normalized_allowed_ids = set()
for allowed_id in allowed_ids:
normalized_allowed_ids.update(_expand_whatsapp_auth_aliases(allowed_id))
diff --git a/gateway/pairing.py b/gateway/pairing.py
index 4ac42688fb3..4e4ec14f39c 100644
--- a/gateway/pairing.py
+++ b/gateway/pairing.py
@@ -113,10 +113,15 @@ def _split_allowlist(raw: str) -> list:
return [uid.strip() for uid in raw.split(",") if uid.strip()]
+def _platform_uses_whatsapp_identity(platform: str) -> bool:
+ """True for Baileys WhatsApp and Meta Cloud — same phone/JID identity rules."""
+ return (platform or "").strip().lower() in {"whatsapp", "whatsapp_cloud"}
+
+
def _normalize_user_id(platform: str, user_id: str) -> str:
"""Normalize platform-specific user IDs before persisting / comparing them."""
raw_user_id = str(user_id or "").strip()
- if platform == "whatsapp":
+ if _platform_uses_whatsapp_identity(platform):
return normalize_whatsapp_identifier(raw_user_id) or raw_user_id
return raw_user_id
@@ -128,7 +133,7 @@ def _user_id_aliases(platform: str, user_id: str) -> set[str]:
return set()
aliases = {raw_user_id, _normalize_user_id(platform, raw_user_id)}
- if platform == "whatsapp":
+ if _platform_uses_whatsapp_identity(platform):
aliases.update(expand_whatsapp_aliases(raw_user_id))
aliases.discard("")
return aliases
diff --git a/gateway/platforms/whatsapp_common.py b/gateway/platforms/whatsapp_common.py
index 04b64eed06a..1c93228e3db 100644
--- a/gateway/platforms/whatsapp_common.py
+++ b/gateway/platforms/whatsapp_common.py
@@ -140,15 +140,20 @@ class WhatsAppBehaviorMixin:
Source precedence matches construction: explicit config wins over any
env carrier. When the adapter was seeded from an env var, re-read that
same key so pairing approve/revoke takes effect without restart
- (including an empty value while the key is still present). Config-seeded
- adapters keep the in-memory snapshot, which pairing revoke purges in
- place — a lower-precedence or stale env value must not broaden access.
+ (including an empty value while the key is still present). When the key
+ is absent — sole-entry revoke calls ``remove_env_value`` — treat the
+ allowlist as empty instead of falling back to the construction-time
+ snapshot. Config-seeded adapters keep the in-memory snapshot, which
+ pairing revoke purges in place — a lower-precedence or stale env value
+ must not broaden access.
"""
source = getattr(self, "_dm_allowlist_source", None)
if isinstance(source, str) and source != "config":
if source in os.environ:
return self._coerce_allow_list(os.environ.get(source, ""))
- return set(self._allow_from or ())
+ # Key removed (e.g. sole-entry pairing revoke) — do not revive the
+ # stale construction snapshot.
+ return set()
return set(self._allow_from or ())
# ------------------------------------------------------------------ JID helpers
diff --git a/tests/gateway/test_pairing_allowlist_bypass.py b/tests/gateway/test_pairing_allowlist_bypass.py
index 9487fd5ba2e..4f49f20be26 100644
--- a/tests/gateway/test_pairing_allowlist_bypass.py
+++ b/tests/gateway/test_pairing_allowlist_bypass.py
@@ -29,6 +29,7 @@ def _isolate_env(monkeypatch):
"TELEGRAM_GROUP_ALLOWED_USERS",
"TELEGRAM_GROUP_ALLOWED_CHATS",
"WHATSAPP_ALLOWED_USERS",
+ "WHATSAPP_CLOUD_ALLOWED_USERS",
"GATEWAY_ALLOW_ALL_USERS",
"GATEWAY_ALLOWED_USERS",
):
@@ -174,6 +175,28 @@ def test_revoke_whatsapp_removes_all_alias_forms_from_allowlist(store, monkeypat
assert saved.get("WHATSAPP_ALLOWED_USERS") == "keeper"
+def test_revoke_whatsapp_cloud_device_jid_removes_bare_allowlist_entry(store, monkeypatch):
+ """Cloud pairing uses platform whatsapp_cloud — same JID/phone alias rules."""
+ monkeypatch.setenv("WHATSAPP_CLOUD_ALLOWED_USERS", "already,15551234567")
+ saved = {}
+ import hermes_cli.config as cfg
+
+ monkeypatch.setattr(
+ cfg,
+ "save_env_value",
+ lambda k, v: (saved.__setitem__(k, v), os.environ.__setitem__(k, v)),
+ )
+ monkeypatch.setattr(cfg, "remove_env_value", lambda k: os.environ.pop(k, None))
+
+ store._approve_user("whatsapp_cloud", "[email protected]", "")
+ assert store.is_approved("whatsapp_cloud", "[email protected]") is True
+
+ assert store.revoke("whatsapp_cloud", "15551234567:[email protected]") is True
+ assert store.is_approved("whatsapp_cloud", "[email protected]") is False
+ assert saved.get("WHATSAPP_CLOUD_ALLOWED_USERS") == "already"
+ assert os.environ.get("WHATSAPP_CLOUD_ALLOWED_USERS") == "already"
+
+
def test_revoke_whatsapp_preserves_wildcard_allowlist_entry(store, monkeypatch):
monkeypatch.setenv("WHATSAPP_ALLOWED_USERS", "*,15551234567")
saved = {}
@@ -330,3 +353,20 @@ def test_whatsapp_live_allowlist_rereads_env_when_env_seeded(monkeypatch):
monkeypatch.setenv("WHATSAPP_ALLOWED_USERS", "")
assert adapter._is_dm_intake_allowed("15551234567") is False
assert adapter._is_dm_allowed("15551234567") is False
+
+
+def test_whatsapp_live_allowlist_denies_when_env_key_removed(monkeypatch):
+ """Sole-entry revoke pops the env key — must not revive the construction snapshot."""
+ from gateway.config import PlatformConfig
+ from plugins.platforms.whatsapp.adapter import WhatsAppAdapter
+
+ monkeypatch.setenv("WHATSAPP_ALLOWED_USERS", "15551234567")
+ adapter = WhatsAppAdapter(
+ PlatformConfig(enabled=True, extra={"dm_policy": "allowlist"})
+ )
+ assert adapter._allow_from == {"15551234567"}
+
+ monkeypatch.delenv("WHATSAPP_ALLOWED_USERS", raising=False)
+ assert adapter._live_dm_allow_from() == set()
+ assert adapter._is_dm_intake_allowed("15551234567") is False
+ assert adapter._is_dm_allowed("15551234567") is False
diff --git a/tests/gateway/test_whatsapp_cloud_allowed_users.py b/tests/gateway/test_whatsapp_cloud_allowed_users.py
index 6c665b575b9..3b2b52437b2 100644
--- a/tests/gateway/test_whatsapp_cloud_allowed_users.py
+++ b/tests/gateway/test_whatsapp_cloud_allowed_users.py
@@ -114,3 +114,16 @@ def test_cloud_allowed_users_live_reread_when_env_seeded(monkeypatch):
monkeypatch.setenv("WHATSAPP_CLOUD_ALLOWED_USERS", "")
assert adapter._is_dm_allowed("15551234567") is False
+
+
+def test_cloud_live_allowlist_denies_when_env_key_removed(monkeypatch):
+ """Sole-entry revoke removes the key — stale construction snapshot must not authorize."""
+ adapter = _build_adapter(
+ monkeypatch,
+ {"WHATSAPP_CLOUD_ALLOWED_USERS": "15551234567"},
+ )
+ assert "15551234567" in adapter._allow_from
+
+ monkeypatch.delenv("WHATSAPP_CLOUD_ALLOWED_USERS", raising=False)
+ assert adapter._live_dm_allow_from() == set()
+ assert adapter._is_dm_allowed("15551234567") is False