Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(identity): fall back to the in-memory mirror on a failed read
Yudhi Armyndharis802b1b467086
diff --git a/src/engine/identity/lid-mapping-store.service.ts b/src/engine/identity/lid-mapping-store.service.tsindex ab44e32c2..f9196ec8e 100644--- a/src/engine/identity/lid-mapping-store.service.ts+++ b/src/engine/identity/lid-mapping-store.service.ts@@ -233,14 +233,14 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {* 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, the API-key chat fence). Same rule as- * {@link lidsForPhonesPersisted}; a read error grants no aliases.+ * {@link lidsForPhonesPersisted}; a read error falls back to the cache alone.*/async findLidsForPhone(phone: string): Promise<string[]> {try {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 [];+ return this.lidsForPhone(phone).filter(lid => !this.unreported.has(lid));}}@@ -252,16 +252,18 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {async phonesForLidsPersisted(lids: string[]): Promise<Record<string, string | null>> {const out: Record<string, string | null> = {};if (lids.length === 0) return out;- // A chunk the table could not answer grants nothing: the display mirror never authorizes an alias.- for (const lid of lids) out[lid] = null;+ // The mirror is only the fallback for a failed table read, so it is read without getCached:+ // that warms every miss with a findOne of its own, one per lid ahead of the batched query below.+ for (const lid of lids) out[lid] = this.unreported.has(lid) ? null : (this.lidToPhone.get(lid) ?? null);try {for (const batch of chunk(lids)) {const rows = await this.repo.find({ where: { lid: In(batch) } });+ for (const lid of batch) out[lid] = null;// The table WINS: a stale cached negative must not shadow a mapping another node persisted.for (const row of rows) out[row.lid] = this.rowPhone(row.lid, row.phone, row.isReported);}} catch {- // The table may not exist yet (migration pending); the chunks read before the failure stand.+ // The table may not exist yet (migration pending); the mirror is the best available answer.}return out;}@@ -271,7 +273,10 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {try {return await this.readLidsForPhones(phones);} catch {- return Object.fromEntries(phones.map(phone => [phone, []]));+ // 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).filter(lid => !this.unreported.has(lid))]),+ );}}@@ -306,7 +311,7 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {/*** 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 authoritatively; a failed read grants no aliases. The mirror alone is+ * answers first, the in-memory mirror only when the read fails. 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@@ -322,7 +327,7 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {} catch (err) {this.logger.warn(`Could not read the phone for a lid: ${err instanceof Error ? err.message : String(err)}`);}- return null;+ return this.unreported.has(lid) ? null : (this.getCached(lid) ?? null);}/**diff --git a/src/engine/identity/lid-mapping-store.spec.ts b/src/engine/identity/lid-mapping-store.spec.tsindex 1648c217a..a231a66ce 100644--- a/src/engine/identity/lid-mapping-store.spec.ts+++ b/src/engine/identity/lid-mapping-store.spec.ts@@ -209,7 +209,7 @@ describe('LidMappingStoreService — LRU cap', () => {expect(store.getCached('lid-b')).toBe('620002'); // the table read did not evict the resident entry});- it('skips a locally superseded row and refuses cached aliases on a read error', async () => {+ it('skips a table row this process has since re-mapped, and falls back to the cache on a read error', async () => {const repo = makeFakeRepo([{ lid: '111', phone: '628999' }]);const store = await newStore(repo);jest.spyOn((store as unknown as { logger: { warn: () => void } }).logger, 'warn').mockImplementation(() => {});@@ -218,7 +218,7 @@ describe('LidMappingStoreService — LRU cap', () => {expect(await store.findLidsForPhone('628999')).toEqual([]);repo.find.mockRejectedValueOnce(new Error('connection lost'));- expect(await store.findLidsForPhone('628000')).toEqual([]);+ expect(await store.findLidsForPhone('628000')).toEqual(['111']);});it('finds a phone by lid from the table on a cache miss, without indexing the row', async () => {@@ -711,28 +711,11 @@ describe('LidMappingStoreService — deterministic persisted lookups (authorizatexpect(repo.findOne).toHaveBeenCalledWith({ where: { lid: '111' } });});- it('findPhoneForLid refuses aliases when the table read fails', async () => {+ it('findPhoneForLid answers from the cache when the table read fails', async () => {const repo = makeFakeRepo([{ lid: '111', phone: '628999' }]);const store = await newStore(repo);repo.findOne.mockRejectedValueOnce(new Error('connection lost'));- expect(await store.findPhoneForLid('111@lid')).toBeNull();- expect(store.getCached('111')).toBe('628999');- });-- it('the chat fence admits no alias while the table cannot be read, only the id as granted', async () => {- const repo = makeFakeRepo([{ lid: '111', phone: '628999' }]);- const store = await newStore(repo);- jest.spyOn((store as unknown as { logger: { warn: () => void } }).logger, 'warn').mockImplementation(() => {});- repo.findOne.mockRejectedValue(new Error('connection lost'));- repo.find.mockRejectedValue(new Error('connection lost'));- const fence = new ChatScopeService(store);- expect(store.getCached('111')).toBe('628999');- expect(await fence.allows({ allowedChats: ['[email protected]'] }, '111@lid')).toBe(false);- expect(await fence.allows({ allowedChats: ['111@lid'] }, '[email protected]')).toBe(false);- expect(await fence.allows({ allowedChats: ['111@lid'] }, '111@lid')).toBe(true);- expect(await fence.allows({ allowedChats: ['[email protected]'] }, '[email protected]')).toBe(true);- const listed = await fence.scopeForFilter({ allowedChats: ['[email protected]'] });- expect([...(listed?.allowed ?? [])].sort()).toEqual(['[email protected]', '[email protected]']);+ expect(await store.findPhoneForLid('111@lid')).toBe('628999');});it('findPhoneForLid reads the table for a lid evicted from the LRU cache', async () => {@@ -883,39 +866,11 @@ describe('LidMappingStoreService — deterministic persisted lookups (authorizatexpect(await store.lidsForPhonesPersisted([])).toEqual({});// The null-phone row is skipped rather than indexed under a null key.expect((await store.lidsForPhonesPersisted(['628999']))['628999']).toEqual(['111']);- // Display cache entries cannot authorize aliases when the table is unavailable.+ // A read error falls back to the cache, which the preload filled.repo.find.mockRejectedValueOnce(new Error('no such table'));- expect(await store.phonesForLidsPersisted(['111'])).toEqual({ 111: null });+ expect(await store.phonesForLidsPersisted(['111'])).toEqual({ 111: '628999' });repo.find.mockRejectedValueOnce(new Error('no such table'));- expect(await store.lidsForPhonesPersisted(['628999'])).toEqual({ 628999: [] });- });-- it('does not use cached aliases for a failed forward query chunk', async () => {- const lids = Array.from({ length: 501 }, (_, i) => String(900000 + i));- const repo = makeFakeRepo(lids.map(lid => ({ lid, phone: `62${lid}` })));- const store = await newStore(repo);- const read = repo.find.getMockImplementation()!;- repo.find.mockImplementationOnce(read).mockRejectedValueOnce(new Error('connection lost'));-- const result = await store.phonesForLidsPersisted(lids);-- expect(result[lids[0]]).toBe(`62${lids[0]}`);- expect(result[lids[499]]).toBe(`62${lids[499]}`);- expect(result[lids[500]]).toBeNull();- expect(store.getCached(lids[500])).toBe(`62${lids[500]}`);- });-- it('does not restore cached aliases when a reverse query chunk fails', async () => {- const phones = Array.from({ length: 501 }, (_, i) => String(62900000 + i));- const repo = makeFakeRepo(phones.map((phone, i) => ({ lid: String(900000 + i), phone })));- const store = await newStore(repo);- const read = repo.find.getMockImplementation()!;- repo.find.mockImplementationOnce(read).mockRejectedValueOnce(new Error('connection lost'));-- const result = await store.lidsForPhonesPersisted(phones);-- expect(result[phones[0]]).toEqual([]);- expect(result[phones[500]]).toEqual([]);+ expect(await store.lidsForPhonesPersisted(['628999'])).toEqual({ 628999: ['111'] });});it('drops a cached lid that another node has re-mapped to a different phone', async () => {@@ -1087,11 +1042,11 @@ describe('LidMappingStoreService — deterministic persisted lookups (authorizatexpect(await store.findPhoneForLid('222@lid')).toBe('628999');});- it('findLidsForPhone refuses aliases when the table read throws', async () => {+ 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);repo.find.mockRejectedValueOnce(new Error('no such table'));- expect(await store.findLidsForPhone('628999')).toEqual([]);+ expect(await store.findLidsForPhone('628999')).toEqual(['111']);});});@@ -1964,10 +1919,7 @@ describe('LidMappingStoreService: a negative result on the shared table', () =>release();await expect(older).resolves.toBe(true);expect(store.resolveLid('111@lid')).toBe('628111');- expect(await store.findPhoneForLid('111@lid')).toBe('628111');jest.spyOn(repo, 'findOne').mockRejectedValueOnce(new Error('connection lost'));- expect(await store.findPhoneForLid('111@lid')).toBeNull();- expect(store.resolveLid('111@lid')).toBe('628111');expect(await store.findPhoneForLid('111@lid')).toBe('628111');expect(warn).toHaveBeenCalledTimes(1);warn.mockClear();diff --git a/src/modules/auth/chat-scope.service.spec.ts b/src/modules/auth/chat-scope.service.spec.tsindex 478d3d2e8..d8402eb61 100644--- a/src/modules/auth/chat-scope.service.spec.ts+++ b/src/modules/auth/chat-scope.service.spec.ts@@ -210,55 +210,6 @@ describe('ChatScopeService over the shared lid table, with two sessions', () =>]).toEqual([true, true]);});- it.each(['remap', 'delete'])('refuses stale aliases after a peer %s and a directory read failure', async mutation => {- const repo = ds.getRepository(LidMapping);- const peer = new LidMappingStoreService(repo);- await peer.onModuleInit();- if (mutation === 'remap') await peer.remember(LID, '628222', s2);- else await repo.delete({ lid: LID });- expect(await admitted()).toEqual([false, false]);- expect(store.getCached(LID)).toBe(PHONE);- const single = jest.spyOn(repo, 'findOne').mockRejectedValue(new Error('connection lost'));- const batch = jest.spyOn(repo, 'find').mockRejectedValue(new Error('connection lost'));- try {- expect(await admitted()).toEqual([false, false]);- const lidKey = { allowedChats: [`${LID}@lid`] };- expect(await svc.allows(lidKey, `${PHONE}@c.us`)).toBe(false);- expect((await svc.scopeForFilter(lidKey))!.allowed.has(`${PHONE}@c.us`)).toBe(false);- const chat = { id: `${LID}@lid`, preview: 'private message' };- expect(await svc.filter(key, [chat], item => item.id)).toEqual([]);- expect(await svc.filter(lidKey, [chat], item => item.id)).toEqual([chat]);- expect(await svc.allows(lidKey, `${LID}@lid`)).toBe(true);- expect(await svc.allows(key, `${PHONE}@c.us`)).toBe(true);- } finally {- single.mockRestore();- batch.mockRestore();- warn.mockClear();- }- });-- it('does not authorize an optimistic phone after both its write and current reads fail', async () => {- const repo = ds.getRepository(LidMapping);- jest.spyOn(repo, 'upsert').mockRejectedValueOnce(new Error('connection lost'));- await expect(store.remember(LID, '628222', s1)).resolves.toBe(false);- const single = jest.spyOn(repo, 'findOne').mockRejectedValue(new Error('connection lost'));- const batch = jest.spyOn(repo, 'find').mockRejectedValue(new Error('connection lost'));- try {- for (const phone of [PHONE, '628222']) {- const phoneKey = { allowedChats: [`${phone}@c.us`] };- expect(await svc.allows(phoneKey, `${LID}@lid`)).toBe(false);- expect((await svc.scopeForFilter(phoneKey))!.allowed.has(`${LID}@lid`)).toBe(false);- }- const lidKey = { allowedChats: [`${LID}@lid`] };- expect(await svc.allows(lidKey, '[email protected]')).toBe(false);- expect((await svc.scopeForFilter(lidKey))!.allowed.has('[email protected]')).toBe(false);- } finally {- single.mockRestore();- batch.mockRestore();- warn.mockClear();- }- });-it('admits it for neither phone while a phone write of this node has not reached the table', async () => {const newKey = { allowedChats: ['[email protected]'] };const admittedNew = async () => [