Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(identity): read the lid table first on every forward lookup

Yudhi Armyndharis2c1504b59d03
diff --git a/src/engine/identity/lid-mapping-store.service.ts b/src/engine/identity/lid-mapping-store.service.ts
index bb432811a..cc2c1be15 100644
--- a/src/engine/identity/lid-mapping-store.service.ts
+++ b/src/engine/identity/lid-mapping-store.service.ts
@@ -164,24 +164,6 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {
return set ? [...set] : [];
}
- /**
- * Deterministic forward lookup for authorization paths (the API-key chat fence): the persisted
- * table first, the in-memory mirror only when the table read fails or has no row. The mirror alone
- * is not enough there: it is LRU-evicted at `LID_MAPPING_CACHE_MAX` and can hold a stale negative
- * for a lid another node has since mapped, so a cache-first answer would let the same key and chat
- * pass or fail depending on cache residency. {@link findPhoneForLid} is the cache-first variant.
- */
- async resolveLidPersisted(jid: string): Promise<string | null> {
- const lid = userPart(jid);
- try {
- const row = await this.repo.findOne({ where: { lid } });
- if (row) return row.phone;
- } catch {
- // The table may not exist yet (migration pending); fall back to the mirror.
- }
- return this.getCached(lid) ?? null;
- }
-
/**
* 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
@@ -255,19 +237,23 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {
}
/**
- * Forward lookup that reads the table on a cache miss, for callers that can await (the handover
- * gate). {@link getCached} answers a miss with undefined and only warms in the background, which
- * is exactly when a mapping past the cap or evicted matters. Like {@link findLidsForPhone}, the row
- * is not indexed, and a read error answers null.
+ * Forward lookup for callers that can await: the API-key chat fence and every chat-bound read
+ * behind it (message history and media, the quoted body, status and handover filters). The table
+ * answers first, the in-memory mirror only when the read fails or has no row. The mirror alone is
+ * not enough: it is LRU-evicted at `LID_MAPPING_CACHE_MAX`, and nothing refreshes it when another
+ * node re-maps a lid in the shared table, so a cache-first answer could resolve the same lid to
+ * a phone the fence no longer sees, and a read behind the fence would serve that other chat. The
+ * row is not indexed into the cache, so a lookup cannot evict hot entries.
*/
- async findPhoneForLid(lid: string): Promise<string | null> {
- if (this.lidToPhone.has(lid)) return this.getCached(lid) ?? null;
+ async findPhoneForLid(jid: string): Promise<string | null> {
+ const lid = userPart(jid);
try {
- return (await this.repo.findOne({ where: { lid } }))?.phone ?? null;
+ const row = await this.repo.findOne({ where: { lid } });
+ if (row) return row.phone;
} catch (err) {
this.logger.warn(`Could not read the phone for a lid: ${err instanceof Error ? err.message : String(err)}`);
- return null;
}
+ return this.getCached(lid) ?? null;
}
async remember(lid: string, phone: string | null, sessionId?: string): Promise<void> {
diff --git a/src/engine/identity/lid-mapping-store.spec.ts b/src/engine/identity/lid-mapping-store.spec.ts
index fb19029ba..180ec2c73 100644
--- a/src/engine/identity/lid-mapping-store.spec.ts
+++ b/src/engine/identity/lid-mapping-store.spec.ts
@@ -467,13 +467,13 @@ describe('LidMappingStoreService — deterministic preload + repository fallback
});
describe('LidMappingStoreService — deterministic persisted lookups (authorization path)', () => {
- it('resolveLidPersisted answers from the cache when warm', async () => {
+ it('findPhoneForLid answers from the cache when warm', async () => {
const repo = makeFakeRepo([{ lid: '111', phone: '628999' }]);
const store = await newStore(repo);
- expect(await store.resolveLidPersisted('111@lid')).toBe('628999');
+ expect(await store.findPhoneForLid('111@lid')).toBe('628999');
});
- it('resolveLidPersisted reads the table for a lid evicted from the LRU cache', async () => {
+ it('findPhoneForLid reads the table for a lid evicted from the LRU cache', async () => {
process.env.LID_MAPPING_CACHE_MAX = '1';
try {
// Preload keeps one row; the other is persisted but not cached, so the table must answer.
@@ -482,8 +482,8 @@ describe('LidMappingStoreService — deterministic persisted lookups (authorizat
{ lid: '222', phone: '628888' },
]);
const store = await newStore(repo);
- expect(await store.resolveLidPersisted('111@lid')).toBe('628999');
- expect(await store.resolveLidPersisted('222@lid')).toBe('628888');
+ expect(await store.findPhoneForLid('111@lid')).toBe('628999');
+ expect(await store.findPhoneForLid('222@lid')).toBe('628888');
} finally {
delete process.env.LID_MAPPING_CACHE_MAX;
}
@@ -494,22 +494,31 @@ describe('LidMappingStoreService — deterministic persisted lookups (authorizat
const store = await newStore(repo);
await store.remember('222', null); // this node cached a negative
repo.rows[0].phone = '628888'; // another node has since mapped it in the table
- expect(await store.resolveLidPersisted('222@lid')).toBe('628888');
+ expect(await store.findPhoneForLid('222@lid')).toBe('628888');
expect(await store.phonesForLidsPersisted(['222'])).toEqual({ 222: '628888' });
});
- it('resolveLidPersisted returns null for a lid with no persisted row', async () => {
+ it('a cached mapping another node has re-mapped answers the table row, not the cache', async () => {
const repo = makeFakeRepo();
const store = await newStore(repo);
- expect(await store.resolveLidPersisted('999@lid')).toBeNull();
+ await store.remember('333', '628111'); // this node cached 333 -> 628111
+ repo.rows[0].phone = '628222'; // another node has since re-mapped it in the shared table
+ expect(store.getCached('333')).toBe('628111');
+ expect(await store.findPhoneForLid('333@lid')).toBe('628222');
+ });
+
+ it('findPhoneForLid returns null for a lid with no persisted row', async () => {
+ const repo = makeFakeRepo();
+ const store = await newStore(repo);
+ expect(await store.findPhoneForLid('999@lid')).toBeNull();
expect(repo.findOne).toHaveBeenCalledWith({ where: { lid: '999' } });
});
- it('resolveLidPersisted fails soft (null) when the table read throws', async () => {
+ it('findPhoneForLid fails soft (null) when the table read throws', async () => {
const repo = makeFakeRepo();
const store = await newStore(repo);
repo.findOne.mockRejectedValueOnce(new Error('no such table'));
- expect(await store.resolveLidPersisted('555@lid')).toBeNull();
+ expect(await store.findPhoneForLid('555@lid')).toBeNull();
});
it('findLidsForPhone reads the table for a lid evicted from the LRU cache', async () => {
diff --git a/src/modules/auth/chat-scope.service.spec.ts b/src/modules/auth/chat-scope.service.spec.ts
index 8b22b2840..b7d219a59 100644
--- a/src/modules/auth/chat-scope.service.spec.ts
+++ b/src/modules/auth/chat-scope.service.spec.ts
@@ -9,7 +9,7 @@ const LID = '555000111';
/** Deterministic persisted-lookup stand-in; the real service reads the lid table on a cache miss. */
function fakeStore(): LidMappingStoreService {
return {
- resolveLidPersisted: jest.fn((lid: string) => Promise.resolve(lid === LID ? PHONE : null)),
+ findPhoneForLid: jest.fn((lid: string) => Promise.resolve(lid === LID ? PHONE : null)),
findLidsForPhone: jest.fn((phone: string) => Promise.resolve(phone === PHONE ? [LID] : [])),
phonesForLidsPersisted: jest.fn((lids: string[]) =>
Promise.resolve(Object.fromEntries(lids.map(lid => [lid, lid === LID ? PHONE : null]))),
diff --git a/src/modules/auth/chat-scope.service.ts b/src/modules/auth/chat-scope.service.ts
index 0aaf8485a..78bc1d820 100644
--- a/src/modules/auth/chat-scope.service.ts
+++ b/src/modules/auth/chat-scope.service.ts
@@ -66,7 +66,7 @@ export class ChatScopeService {
const store = this.lidStore;
if (!store) return undefined;
return {
- resolveLid: userPart => store.resolveLidPersisted(userPart),
+ resolveLid: userPart => store.findPhoneForLid(userPart),
lidsForPhone: phone => store.findLidsForPhone(phone),
};
}
diff --git a/src/modules/message/message-send.service.spec.ts b/src/modules/message/message-send.service.spec.ts
index d1e3c07de..3cb5acb29 100644
--- a/src/modules/message/message-send.service.spec.ts
+++ b/src/modules/message/message-send.service.spec.ts
@@ -1,7 +1,8 @@
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { FindOperator, In, Repository } from 'typeorm';
-import type { LidMappingStoreService } from '../../engine/identity/lid-mapping-store.service';
+import { LidMappingStoreService } from '../../engine/identity/lid-mapping-store.service';
+import type { LidMapping } from '../../engine/identity/lid-mapping.entity';
import { BadRequestException, NotFoundException, PayloadTooLargeException } from '@nestjs/common';
import { MessageSendService } from './message-send.service';
import { Message, MessageDirection, MessageStatus } from './entities/message.entity';
@@ -1051,6 +1052,38 @@ describe('MessageSendService', () => {
expect(quoteOf()).toEqual({ id: 'wa-foreign', body: '' });
});
+ it('stores no body from the chat a stale cached lid mapping names', async () => {
+ // This node cached lid 999 -> 628111; another node has since re-mapped it to 628333 in the
+ // shared table. A reply to 999@lid must not read a quote out of chat 628111.
+ rows.push({ sessionId: 'sess-1', chatId: '[email protected]', waMessageId: 'wa-stale', body: 'chat 628111 only' });
+ const table = [{ lid: '999', phone: '628111' }];
+ const store = new LidMappingStoreService({
+ find: () => Promise.resolve([]),
+ findOne: ({ where }: { where: { lid: string } }) =>
+ Promise.resolve(table.find(r => r.lid === where.lid) ?? null),
+ upsert: () => Promise.resolve({}),
+ } as unknown as Repository<LidMapping>);
+ await store.remember('999', '628111');
+ table[0].phone = '628333';
+ const withStore = new MessageSendService(
+ repository as Repository<Message>,
+ sessionService as unknown as SessionService,
+ engines,
+ hookManager as HookManager,
+ templateService as unknown as TemplateService,
+ inertPacing(),
+ undefined,
+ undefined,
+ store,
+ );
+ try {
+ await withStore.reply('sess-1', { chatId: '999@lid', quotedMessageId: 'wa-stale', text: 'hi' });
+ expect(quoteOf()).toEqual({ id: 'wa-stale', body: '' });
+ } finally {
+ rows.pop();
+ }
+ });
+
it('still finds a quote stored under the lid form of the target chat', async () => {
const store = {
findPhoneForLid: jest.fn().mockResolvedValue(null),
diff --git a/src/modules/message/message.service.spec.ts b/src/modules/message/message.service.spec.ts
index d4ba9d2a4..b4a2d9915 100644
--- a/src/modules/message/message.service.spec.ts
+++ b/src/modules/message/message.service.spec.ts
@@ -510,6 +510,37 @@ describe('MessageService', () => {
return { qb, captured };
};
+ it('expands a @lid chatId to the phone the table names, not a stale cached one', async () => {
+ // This node cached lid 111 -> 628999; another node has since re-mapped it to 628777 in the
+ // shared table. The chat fence reads the table, so history must expand to the same phone or a
+ // key allowed only 111@lid would read chat 628999.
+ const table = [{ lid: '111', phone: '628999' }];
+ const store = new LidMappingStoreService({
+ 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),
+ upsert: () => Promise.resolve({}),
+ } as unknown as Repository<LidMapping>);
+ await store.remember('111', '628999');
+ table[0].phone = '628777';
+ const { qb, captured } = makeCaptureQb();
+ (repository.createQueryBuilder as jest.Mock).mockReturnValue(qb);
+ const withStore = new MessageService(
+ repository as Repository<Message>,
+ engines,
+ messageProjector as unknown as MessageProjector,
+ hookManager as HookManager,
+ store,
+ inertPacing(),
+ {} as MessageSendService,
+ );
+
+ await withStore.getMessages('sess-1', { chatId: '111@lid' });
+
+ expect(captured.chatIds).toEqual(['111@lid', '[email protected]', '[email protected]']);
+ });
+
it('does not expand a group chatId into the user dialects (fail-closed on the literal id)', async () => {
const { qb, captured } = makeCaptureQb();
(repository.createQueryBuilder as jest.Mock).mockReturnValue(qb);