Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(webhook): resolve a privacy id named by a filter rule, not only by the event
Yudhi Armyndharis6e212ee16710
diff --git a/src/modules/webhook/filters/filter-evaluator.spec.ts b/src/modules/webhook/filters/filter-evaluator.spec.tsindex 4c0902e1e..fa084b0d4 100644--- a/src/modules/webhook/filters/filter-evaluator.spec.ts+++ b/src/modules/webhook/filters/filter-evaluator.spec.ts@@ -193,6 +193,27 @@ describe('evaluateFilters', () => {const f = filters({ field: 'mentions', operator: 'is', value: ['628999'] });expect(evaluateFilters(f, 'message.received', msg({ mentionedIds: ['111@lid', '[email protected]'] }), resolve)).toBe(true);});++ // A rule can hold the lid itself: the dashboard's chat picker stores whatever id the chat+ // carries. Resolving only the payload side made the two stop agreeing as soon as the gateway+ // learned that lid's phone, which turns an exclusion into a delivery of the excluded chat.+ it('resolves a lid the RULE names, so it keeps matching once the phone is known', () => {+ const resolve = (jid: string): string | null => (jid.startsWith('111@lid') ? '628999' : null);+ const exclude = filters({ field: 'sender', operator: 'isNot', value: ['111@lid'] });+ const fromThatPerson = msg({ from: '[email protected]', author: '111@lid', isGroup: true });++ // Before the mapping is known, both sides stay a lid and the exclusion holds.+ expect(evaluateFilters(exclude, 'message.received', fromThatPerson)).toBe(false);+ // Once it is known, both sides resolve to the phone and the exclusion must still hold.+ expect(evaluateFilters(exclude, 'message.received', fromThatPerson, resolve)).toBe(false);+ });++ it('resolves a lid the rule names for an inclusion too', () => {+ const resolve = (jid: string): string | null => (jid.startsWith('111@lid') ? '628999' : null);+ const only = filters({ field: 'sender', operator: 'is', value: ['111@lid'] });+ const fromThatPerson = msg({ from: '[email protected]', author: '111@lid', isGroup: true });+ expect(evaluateFilters(only, 'message.received', fromThatPerson, resolve)).toBe(true);+ });});describe('chatId (conversation scoping so a webhook can allowlist specific groups)', () => {diff --git a/src/modules/webhook/filters/filter-evaluator.ts b/src/modules/webhook/filters/filter-evaluator.tsindex e2371dda2..6dece2b70 100644--- a/src/modules/webhook/filters/filter-evaluator.ts+++ b/src/modules/webhook/filters/filter-evaluator.ts@@ -20,14 +20,18 @@ export type LidResolver = (jid: string) => string | null;// the same neutral string (`<phone>@c.us` / `<id>@g.us` / `<lid>@lid`). So a phone filter now matches// the person across user dialects AND any lid resolving to that phone - previously a silent miss.const canonicalActor = (jid: string, resolve?: LidResolver): string => toNeutralJid(jid, resolve).toLowerCase();-const canonicalInput = (value: string): string => {+const canonicalInput = (value: string, resolve?: LidResolver): string => {// Bare digits are a phone-addressed user; anything else parses as a JID. Mirrors the engine's// user-input canonicalization (byte-identical to the former WaId.fromUserInput().toNeutral()).const trimmed = value.trim();if (trimmed && !trimmed.includes('@')) {return `${trimmed.replace(/\D/g, '') || trimmed}@c.us`.toLowerCase();}- return toNeutralJid(trimmed).toLowerCase();+ // Through the same resolver as the payload side. A rule can hold a lid: the dashboard's chat+ // picker stores whatever id the chat carries. Canonicalising only one side meant the two stopped+ // agreeing the moment the gateway learned that lid's phone, which silently flips an exclusion+ // open and an inclusion shut, with the rule still reading correctly in the UI.+ return toNeutralJid(trimmed, resolve).toLowerCase();};const toStringArray = (value: unknown): string[] =>@@ -44,7 +48,7 @@ function evaluateCondition(switch (def.kind) {case 'id': {- const candidates = new Set(toStringArray(value).map(canonicalInput));+ const candidates = new Set(toStringArray(value).map(v => canonicalInput(v, resolve)));const actual = typeof resolved === 'string' ? resolved : undefined;const isMatch = actual != null && candidates.has(canonicalActor(actual, resolve));return operator === 'isNot' ? !isMatch : isMatch;@@ -58,7 +62,7 @@ function evaluateCondition(}case 'idArray': {- const candidates = new Set(toStringArray(value).map(canonicalInput));+ const candidates = new Set(toStringArray(value).map(v => canonicalInput(v, resolve)));const actual = toStringArray(resolved).map(jid => canonicalActor(jid, resolve));const intersects = actual.some(v => candidates.has(v));return operator === 'isNot' ? !intersects : intersects;