Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(identity): drop a cached lid another node has re-mapped

Yudhi Armyndharisc77e5295e191
diff --git a/src/engine/identity/lid-mapping-store.service.ts b/src/engine/identity/lid-mapping-store.service.ts
index 2662a270f..bb432811a 100644
--- a/src/engine/identity/lid-mapping-store.service.ts
+++ b/src/engine/identity/lid-mapping-store.service.ts
@@ -186,22 +186,16 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {
* Reverse lookup that also reads the table. {@link lidsForPhone} only sees lids resident in the
* forward cache, and nothing phone-keyed ever warms it, so a mapping past the preload cap or
* evicted by the LRU would stay invisible to a phone filter for good. For callers that can await
- * (the message and status filters, the handover gate). Table rows are not indexed into the cache,
- * so a list query cannot evict hot forward entries. A lid this process has since re-mapped
- * elsewhere is skipped (last-write-wins), and a read error falls back to the cache alone.
+ * (the message and status filters, the handover gate, the API-key chat fence). Same rule as
+ * {@link lidsForPhonesPersisted}; a read error falls back to the cache alone.
*/
async findLidsForPhone(phone: string): Promise<string[]> {
- const lids = new Set(this.lidsForPhone(phone));
try {
- const rows = await this.repo.find({ select: { lid: true }, where: { phone } });
- for (const { lid } of rows) {
- const cached = this.lidToPhone.get(lid);
- if (cached === undefined || cached === phone) lids.add(lid);
- }
+ return (await this.readLidsForPhones([phone]))[phone];
} catch (err) {
this.logger.warn(`Could not read lids for a phone: ${err instanceof Error ? err.message : String(err)}`);
+ return this.lidsForPhone(phone);
}
- return [...lids];
}
/**
@@ -227,23 +221,37 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {
/** Batched reverse lookup, companion to {@link phonesForLidsPersisted}: phone digits -> lids. */
async lidsForPhonesPersisted(phones: string[]): Promise<Record<string, string[]>> {
- const out: Record<string, string[]> = {};
- if (phones.length === 0) return out;
- for (const phone of phones) out[phone] = this.lidsForPhone(phone);
try {
- for (const batch of chunk(phones)) {
- const rows = await this.repo.find({ where: { phone: In(batch) } });
- for (const row of rows) {
- if (!row.phone) continue;
- const list = out[row.phone] ?? [];
- if (!list.includes(row.lid)) list.push(row.lid);
- out[row.phone] = list;
- }
- }
+ return await this.readLidsForPhones(phones);
} catch {
// The table may not exist yet (migration pending); the cache is the best available answer.
+ return Object.fromEntries(phones.map(phone => [phone, this.lidsForPhone(phone)]));
}
- return out;
+ }
+
+ /**
+ * A lid answers a phone only when the cache and the table do not disagree: a cached reverse entry
+ * whose row another node has since re-mapped to a different phone is dropped, and a row this
+ * process has since re-mapped elsewhere is skipped. Either side alone may vouch for it (the row
+ * can be past the cache cap, the cached write can still be in flight). Throws on a read error.
+ */
+ private async readLidsForPhones(phones: string[]): Promise<Record<string, string[]>> {
+ const out: Record<string, Set<string>> = {};
+ for (const phone of phones) out[phone] = new Set(this.lidsForPhone(phone));
+ const cachedLids = [...new Set(phones.flatMap(phone => [...out[phone]]))];
+ for (const batch of chunk(cachedLids)) {
+ for (const row of await this.repo.find({ where: { lid: In(batch) } })) {
+ for (const phone of phones) if (row.phone !== phone) out[phone].delete(row.lid);
+ }
+ }
+ for (const batch of chunk(phones)) {
+ for (const { lid, phone } of await this.repo.find({ where: { phone: In(batch) } })) {
+ if (!phone || !out[phone]) continue;
+ const cached = this.lidToPhone.get(lid);
+ if (cached === undefined || cached === phone) out[phone].add(lid);
+ }
+ }
+ return Object.fromEntries(phones.map(phone => [phone, [...out[phone]]]));
}
/**
diff --git a/src/engine/identity/lid-mapping-store.spec.ts b/src/engine/identity/lid-mapping-store.spec.ts
index 9415f7c35..fb19029ba 100644
--- a/src/engine/identity/lid-mapping-store.spec.ts
+++ b/src/engine/identity/lid-mapping-store.spec.ts
@@ -595,6 +595,22 @@ describe('LidMappingStoreService — deterministic persisted lookups (authorizat
expect(await store.lidsForPhonesPersisted(['628999'])).toEqual({ 628999: ['111'] });
});
+ it('drops a cached lid that another node has re-mapped to a different phone', async () => {
+ const repo = makeFakeRepo([{ lid: '111', phone: '628999' }]);
+ const store = await newStore(repo);
+ repo.rows[0].phone = '628000'; // another node re-mapped 111; this node's cache still says 628999
+ expect(store.lidsForPhone('628999')).toEqual(['111']);
+ expect(await store.findLidsForPhone('628999')).toEqual([]);
+ expect(await store.lidsForPhonesPersisted(['628999'])).toEqual({ 628999: [] });
+ // A cached lid with no row yet (its write still in flight) still answers.
+ await store.remember('222', '628999');
+ repo.rows.splice(
+ repo.rows.findIndex(r => r.lid === '222'),
+ 1,
+ );
+ expect(await store.findLidsForPhone('628999')).toEqual(['222']);
+ });
+
it('findLidsForPhone falls back to the cache when the table read throws', async () => {
const repo = makeFakeRepo([{ lid: '111', phone: '628999' }]);
const store = await newStore(repo);
diff --git a/src/modules/auth/chat-scope.service.spec.ts b/src/modules/auth/chat-scope.service.spec.ts
index dab44f047..8b22b2840 100644
--- a/src/modules/auth/chat-scope.service.spec.ts
+++ b/src/modules/auth/chat-scope.service.spec.ts
@@ -1,5 +1,7 @@
import { ChatScopeService } from './chat-scope.service';
+import { FindOperator, Repository } from 'typeorm';
import { LidMappingStoreService } from '../../engine/identity/lid-mapping-store.service';
+import { LidMapping } from '../../engine/identity/lid-mapping.entity';
const PHONE = '919999999999';
const LID = '555000111';
@@ -48,6 +50,28 @@ describe('ChatScopeService', () => {
expect(await svc.filter({ allowedChats: null }, rows, r => r.id)).toEqual(rows);
});
+ it('does not admit a phone whose lid another node has re-mapped', async () => {
+ const rows = [{ lid: LID, phone: PHONE as string | null }];
+ const hit = (value: string | null, cond?: string | FindOperator<unknown>) =>
+ cond === undefined || (cond instanceof FindOperator ? (cond.value as unknown[]).includes(value) : value === cond);
+ const repo = {
+ find: jest.fn(
+ (o?: { where?: { lid?: string | FindOperator<unknown>; phone?: string | FindOperator<unknown> } }) =>
+ Promise.resolve(rows.filter(r => hit(r.lid, o?.where?.lid) && hit(r.phone, o?.where?.phone))),
+ ),
+ findOne: jest.fn((o: { where: { lid: string } }) =>
+ Promise.resolve(rows.find(r => r.lid === o.where.lid) ?? null),
+ ),
+ };
+ const store = new LidMappingStoreService(repo as unknown as Repository<LidMapping>);
+ await store.onModuleInit(); // the cache now indexes LID -> PHONE
+ rows[0].phone = '918888888888'; // another node re-mapped the lid in the shared table
+ const svc = new ChatScopeService(store);
+ expect(await svc.allows({ allowedChats: [`${LID}@lid`] }, `${PHONE}@c.us`)).toBe(false);
+ // Until this node learns the re-map, the disagreement fails closed for the new phone too.
+ expect(await svc.allows({ allowedChats: [`${LID}@lid`] }, '[email protected]')).toBe(false);
+ });
+
it('degrades to exact dialects when no lid directory is available', async () => {
const svc = new ChatScopeService();
expect(await svc.allows({ allowedChats: [`${PHONE}@c.us`] }, `${LID}@lid`)).toBe(false);
diff --git a/src/modules/integration/conversation-mapping.service.spec.ts b/src/modules/integration/conversation-mapping.service.spec.ts
index a19bd12d6..580ce99c2 100644
--- a/src/modules/integration/conversation-mapping.service.spec.ts
+++ b/src/modules/integration/conversation-mapping.service.spec.ts
@@ -1,10 +1,14 @@
-import { DataSource, Repository } from 'typeorm';
+import { DataSource, FindOperator, Repository } from 'typeorm';
import { ConversationMapping } from './entities/conversation-mapping.entity';
import { ConversationMappingConflict, ConversationMappingService, MappingKey } from './conversation-mapping.service';
import { AddIntegrationFabric1781900000000 } from '../../database/migrations/1781900000000-AddIntegrationFabric';
import { LidMappingStoreService } from '../../engine/identity/lid-mapping-store.service';
import { LidMapping } from '../../engine/identity/lid-mapping.entity';
+/** The `In([...])` condition a lid-table fake honours; an absent condition matches every row. */
+const inList = (value: string, cond?: FindOperator<string>): boolean =>
+ cond === undefined || (cond.value as unknown as string[]).includes(value);
+
describe('ConversationMappingService', () => {
let ds: DataSource;
let service: ConversationMappingService;
@@ -95,7 +99,8 @@ describe('ConversationMappingService', () => {
// real but its cache is empty, as for a mapping past the preload cap or evicted: only the table has it.
const table = [{ lid: '111', phone: '628999' }];
const lidStore = new LidMappingStoreService({
- find: ({ where }: { where: { phone: string } }) => Promise.resolve(table.filter(r => r.phone === where.phone)),
+ find: ({ where }: { where: { lid?: FindOperator<string>; phone?: FindOperator<string> } }) =>
+ Promise.resolve(table.filter(r => inList(r.lid, where.lid) && inList(r.phone, where.phone))),
findOne: ({ where }: { where: { lid: string } }) => Promise.resolve(table.find(r => r.lid === where.lid) ?? null),
} as unknown as Repository<LidMapping>);
const human = (chatId: string, provider = 'convB') =>
diff --git a/src/modules/message/message.service.spec.ts b/src/modules/message/message.service.spec.ts
index 4065a9ef6..d4ba9d2a4 100644
--- a/src/modules/message/message.service.spec.ts
+++ b/src/modules/message/message.service.spec.ts
@@ -1,6 +1,6 @@
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
-import { In, Repository } from 'typeorm';
+import { FindOperator, In, Repository } from 'typeorm';
import { BadRequestException, NotFoundException } from '@nestjs/common';
import { MessageService, spendInlineMediaBudget } from './message.service';
import { MessageSendService } from './message-send.service';
@@ -13,6 +13,10 @@ import { LidMappingStoreService } from '../../engine/identity/lid-mapping-store.
import { LidMapping } from '../../engine/identity/lid-mapping.entity';
import { SendPacingService } from './send-pacing.service';
+/** The `In([...])` condition a lid-table fake honours; an absent condition matches every row. */
+const inList = (value: string, cond?: FindOperator<string>): boolean =>
+ cond === undefined || (cond.value as unknown as string[]).includes(value);
+
/** Pacing is off by default in these tests; the governor's own spec covers its behaviour. */
const inertPacing = (): SendPacingService =>
({
@@ -333,7 +337,8 @@ describe('MessageService', () => {
// The mapping is only in the table: past the preload cap or evicted by the LRU.
const table = [{ lid: '111', phone: '628999' }];
const store = new LidMappingStoreService({
- find: ({ where }: { where: { phone: string } }) => Promise.resolve(table.filter(r => r.phone === where.phone)),
+ find: ({ where }: { where: { lid?: FindOperator<string>; phone?: FindOperator<string> } }) =>
+ Promise.resolve(table.filter(r => inList(r.lid, where.lid) && inList(r.phone, where.phone))),
} as unknown as Repository<LidMapping>);
const qb = makeFilteringQb();
(repository.createQueryBuilder as jest.Mock).mockReturnValue(qb);