fix(whatsapp): normalize user JIDs for group allowlists (#838)

Thanks @peschee.

Co-authored-by: Peter Siska <63866+peschee@users.noreply.github.com>
This commit is contained in:
Peter Steinberger
2026-01-14 23:24:21 +00:00
parent fd41000bc3
commit 57b4865ab3
6 changed files with 297 additions and 14 deletions
+34 -1
View File
@@ -1,6 +1,10 @@
import { describe, expect, it } from "vitest";
import { isWhatsAppGroupJid, normalizeWhatsAppTarget } from "./normalize.js";
import {
isWhatsAppGroupJid,
isWhatsAppUserJid,
normalizeWhatsAppTarget,
} from "./normalize.js";
describe("normalizeWhatsAppTarget", () => {
it("preserves group JIDs", () => {
@@ -24,6 +28,25 @@ describe("normalizeWhatsAppTarget", () => {
expect(normalizeWhatsAppTarget("1555123@s.whatsapp.net")).toBe("+1555123");
});
it("normalizes user JIDs with device suffix to E.164", () => {
// This is the bug fix: JIDs like "41796666864:0@s.whatsapp.net" should
// normalize to "+41796666864", not "+417966668640" (extra digit from ":0")
expect(normalizeWhatsAppTarget("41796666864:0@s.whatsapp.net")).toBe(
"+41796666864",
);
expect(normalizeWhatsAppTarget("1234567890:123@s.whatsapp.net")).toBe(
"+1234567890",
);
// Without device suffix still works
expect(normalizeWhatsAppTarget("41796666864@s.whatsapp.net")).toBe(
"+41796666864",
);
});
it("normalizes LID JIDs to E.164", () => {
expect(normalizeWhatsAppTarget("123456789@lid")).toBe("+123456789");
});
it("rejects invalid targets", () => {
expect(normalizeWhatsAppTarget("wat")).toBeNull();
expect(normalizeWhatsAppTarget("whatsapp:")).toBeNull();
@@ -37,6 +60,16 @@ describe("normalizeWhatsAppTarget", () => {
});
});
describe("isWhatsAppUserJid", () => {
it("detects user JIDs with various formats", () => {
expect(isWhatsAppUserJid("41796666864:0@s.whatsapp.net")).toBe(true);
expect(isWhatsAppUserJid("1234567890@s.whatsapp.net")).toBe(true);
expect(isWhatsAppUserJid("123456789@lid")).toBe(true);
expect(isWhatsAppUserJid("123456789-987654321@g.us")).toBe(false);
expect(isWhatsAppUserJid("+1555123")).toBe(false);
});
});
describe("isWhatsAppGroupJid", () => {
it("detects group JIDs with or without prefixes", () => {
expect(isWhatsAppGroupJid("120363401234567890@g.us")).toBe(true);
+31
View File
@@ -1,5 +1,8 @@
import { normalizeE164 } from "../utils.js";
const WHATSAPP_USER_JID_RE = /^(\d+)(?::\d+)?@s\.whatsapp\.net$/i;
const WHATSAPP_LID_RE = /^(\d+)@lid$/i;
function stripWhatsAppTargetPrefixes(value: string): string {
let candidate = value.trim();
for (;;) {
@@ -21,6 +24,27 @@ export function isWhatsAppGroupJid(value: string): boolean {
return /^[0-9]+(-[0-9]+)*$/.test(localPart);
}
/**
* Check if value looks like a WhatsApp user JID (e.g. "41796666864:0@s.whatsapp.net" or "123@lid").
*/
export function isWhatsAppUserJid(value: string): boolean {
const candidate = stripWhatsAppTargetPrefixes(value);
return WHATSAPP_USER_JID_RE.test(candidate) || WHATSAPP_LID_RE.test(candidate);
}
/**
* Extract the phone number from a WhatsApp user JID.
* "41796666864:0@s.whatsapp.net" -> "41796666864"
* "123456@lid" -> "123456"
*/
function extractUserJidPhone(jid: string): string | null {
const userMatch = jid.match(WHATSAPP_USER_JID_RE);
if (userMatch) return userMatch[1];
const lidMatch = jid.match(WHATSAPP_LID_RE);
if (lidMatch) return lidMatch[1];
return null;
}
export function normalizeWhatsAppTarget(value: string): string | null {
const candidate = stripWhatsAppTargetPrefixes(value);
if (!candidate) return null;
@@ -28,6 +52,13 @@ export function normalizeWhatsAppTarget(value: string): string | null {
const localPart = candidate.slice(0, candidate.length - "@g.us".length);
return `${localPart}@g.us`;
}
// Handle user JIDs (e.g. "41796666864:0@s.whatsapp.net")
if (isWhatsAppUserJid(candidate)) {
const phone = extractUserJidPhone(candidate);
if (!phone) return null;
const normalized = normalizeE164(phone);
return normalized.length > 1 ? normalized : null;
}
const normalized = normalizeE164(candidate);
return normalized.length > 1 ? normalized : null;
}