Skip to content

The agent that grows with you

fix(whatsapp): normalize device-qualified ids in every id comparison, bridge and Python

teknium18a3cded09cfa
diff --git a/contributors/emails/[email protected] b/contributors/emails/[email protected]
new file mode 100644
index 00000000000..5b807416920
--- /dev/null
+++ b/contributors/emails/[email protected]
@@ -0,0 +1 @@
+eranbes
diff --git a/gateway/platforms/whatsapp_common.py b/gateway/platforms/whatsapp_common.py
index 16bcd009932..5e5645f20df 100644
--- a/gateway/platforms/whatsapp_common.py
+++ b/gateway/platforms/whatsapp_common.py
@@ -138,10 +138,8 @@ class WhatsAppBehaviorMixin(OwnAccessPolicyMixin):
def _normalize_whatsapp_id(value: Optional[str]) -> str:
if not value:
return ""
- normalized = str(value).strip()
- if ":" in normalized and "@" in normalized:
- normalized = normalized.replace(":", "@", 1)
- return normalized
+ # Device-qualified ids (`<user>:<device>@lid`) must equal their bare form.
+ return re.sub(r":\d+(?=@)", "", str(value).strip())
@staticmethod
def _is_broadcast_chat(chat_id: str) -> bool:
diff --git a/scripts/whatsapp-bridge/bridge.js b/scripts/whatsapp-bridge/bridge.js
index 2101017c42c..c91cd112811 100644
--- a/scripts/whatsapp-bridge/bridge.js
+++ b/scripts/whatsapp-bridge/bridge.js
@@ -46,6 +46,7 @@ import {
inboundReadReceiptKeys,
inferMediaType,
mediaPayloadForFile,
+ normalizeWhatsAppId,
pollCreationMessageFromPayload,
pollUpdateForAggregation,
} from './bridge_helpers.js';
@@ -205,15 +206,6 @@ function trackSentMessageId(sent) {
rememberSentId(sent?.key?.id);
}
-function normalizeWhatsAppId(value) {
- if (!value) return '';
- // Strip the :<device> suffix (e.g. 447999674698:[email protected] -> [email protected])
- // so bot/mention/quoted ids compare consistently. The old .replace(':','@') produced a
- // malformed '...@14@lid', which never matched the bot's own id (breaking @mention and
- // reply-to-bot detection in groups).
- return String(value).replace(/:\d+@/, '@').replace(/:\d+$/, '');
-}
-
function redactWhatsAppId(value) {
const raw = String(value || '').trim();
if (!raw) return '';
diff --git a/scripts/whatsapp-bridge/bridge_helpers.js b/scripts/whatsapp-bridge/bridge_helpers.js
index b50c70e654b..33a42152c6b 100644
--- a/scripts/whatsapp-bridge/bridge_helpers.js
+++ b/scripts/whatsapp-bridge/bridge_helpers.js
@@ -15,7 +15,11 @@ export const MIME_MAP = {
export function normalizeWhatsAppId(value) {
if (!value) return '';
- return String(value).replace(':', '@');
+ // Baileys reports the bot's own ids device-qualified (`<user>:<device>@lid`), while
+ // inbound mentionedJid / contextInfo.participant are not. Drop the suffix so both
+ // forms compare equal; the old `':' -> '@'` swap produced `<user>@<device>@lid`,
+ // which never matched and silently broke @mention / reply-to-bot gating in groups.
+ return String(value).replace(/:\d+(?=@)/, '').replace(/:\d+$/, '');
}
function unwrapMessageEnvelopes(content) {
diff --git a/tests/gateway/test_whatsapp_group_gating.py b/tests/gateway/test_whatsapp_group_gating.py
index 96c5fd8c10c..47e905cac37 100644
--- a/tests/gateway/test_whatsapp_group_gating.py
+++ b/tests/gateway/test_whatsapp_group_gating.py
@@ -244,3 +244,18 @@ def test_broadcast_filter_runs_before_allowlist():
assert adapter._should_process_message(msg) is False
+
+
+def test_device_qualified_bot_ids_match_bare_mention_and_quote_ids():
+ """Baileys reports the bot's own ids as ``<user>:<device>@lid`` while inbound
+ mentionedJid / quoted participant ids are bare — both must normalize equal."""
+ adapter = _make_adapter(require_mention=True, group_policy="open")
+ device_qualified = ["447999674698:[email protected]", "116342762025117:14@lid"]
+
+ assert adapter._should_process_message(
+ _group_message("hi there", botIds=device_qualified, mentionedIds=["116342762025117@lid"])
+ ) is True
+ assert adapter._should_process_message(
+ _group_message("and this?", botIds=device_qualified, quotedParticipant="[email protected]")
+ ) is True
+ assert adapter._should_process_message(_group_message("hello everyone", botIds=device_qualified)) is False