Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(identity): let any negative clear a logged-out session's lid phone
Yudhi Armyndharis68e85a6e61a0
diff --git a/src/engine/identity/lid-mapping-store.service.ts b/src/engine/identity/lid-mapping-store.service.tsindex 7cd742854..834cfe2c0 100644--- a/src/engine/identity/lid-mapping-store.service.ts+++ b/src/engine/identity/lid-mapping-store.service.ts@@ -71,8 +71,9 @@ export interface LidMappingStore {* Write-through: update the cache + persist. A phone is last-write-wins. A session reporting the phone* this node already holds takes the row, unless this node already counted a report of it from that* session: after s1, s2, s1 report one phone the owner is s2. A `null` phone records a negative result,- * and replaces a stored phone only when its owner (or none, or one since deleted) gives it. Never rejects;- * resolves false when the report did not reach the table (its write failed, or waits after one that did).+ * and replaces a stored phone only when its owner (or none, or one since deleted or logged out) gives it.+ * Never rejects; resolves false when the report did not reach the table (its write failed, or waits after+ * one that did).*/remember(lid: string, phone: string | null, sessionId?: string): Promise<boolean>;}@@ -377,11 +378,11 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {/*** Record that a session found no phone for a lid. The row is shared by every session, and one account* can see a contact's number while another cannot, so a negative replaces the row only when it holds- * no phone or the phone was written by this session (or by none, or by one no longer in `sessions`);- * another live session's mapping stays. The check is part of the update, not a read before it, so a- * phone stored meanwhile is never overwritten. When the row is cleared the cached entry is dropped- * rather than set: a phone written while the statements ran may be the newer value, and the next read- * takes whichever the table holds.+ * no phone or the phone was written by this session (or by none, or by one no longer linked: gone from+ * `sessions`, or logged out, which clears its `phone`); another linked session's mapping stays. The check+ * is part of the update, not a read before it, so a phone stored meanwhile is never overwritten. When the+ * row is cleared the cached entry is dropped rather than set: a phone written while the statements ran may+ * be the newer value, and the next read takes whichever the table holds.*/private async clear(lid: string, sessionId: string | null): Promise<void> {const updatedAt = new Date();@@ -396,8 +397,14 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {{ lid, phone: IsNull() },{ lid, sessionId: IsNull() },// A session deleted without its rows being released (an older release, a crash, a restored backup)- // owns them no more than a released one does. CAST: sessions.id is a uuid on PostgreSQL.- { lid, sessionId: Raw(owner => `${owner} NOT IN (SELECT CAST(s."id" AS TEXT) FROM sessions s)`) },+ // owns them no more than a released one does, and neither does one logged out: it reports nothing+ // until it is linked again. CAST: sessions.id is a uuid on PostgreSQL.+ {+ lid,+ sessionId: Raw(+ owner => `${owner} NOT IN (SELECT CAST(s."id" AS TEXT) FROM sessions s WHERE s."phone" IS NOT NULL)`,+ ),+ },...(sessionId ? [{ lid, sessionId }] : []),];const { affected } = await this.repo.update(replaceable, { phone: null, sessionId, updatedAt });diff --git a/src/engine/identity/lid-mapping-store.spec.ts b/src/engine/identity/lid-mapping-store.spec.tsindex 937db8fdf..980bf4150 100644--- a/src/engine/identity/lid-mapping-store.spec.ts+++ b/src/engine/identity/lid-mapping-store.spec.ts@@ -974,7 +974,7 @@ describe('LidMappingStoreService — deterministic persisted lookups (authorizat});describe('LidMappingStoreService: a negative result on the shared table', () => {- // Sessions that exist in `sessions`; `gone` names one that was deleted.+ // Linked sessions in `sessions` (each carries its account's phone); `gone` names one that was deleted.const [s1, s2, s3, s9] = ['1', '2', '3', '9'].map(n => `0000000${n}-0000-4000-8000-000000000000`);const gone = '0000000d-0000-4000-8000-000000000000';let ds: DataSource;@@ -1029,7 +1029,7 @@ describe('LidMappingStoreService: a negative result on the shared table', () =>? { ...server, schema, extra: { options: `-c search_path=${schema},public` }, entities, synchronize: true }: { type: 'better-sqlite3', database: ':memory:', entities, synchronize: true },).initialize();- await ds.getRepository(Session).insert([s1, s2, s3, s9].map(id => ({ id, name: id })));+ await ds.getRepository(Session).insert([s1, s2, s3, s9].map(id => ({ id, name: id, phone: `6290000000${id[7]}` })));repo = ds.getRepository(LidMapping);store = new LidMappingStoreService(repo);clock = 0;@@ -1091,18 +1091,40 @@ describe('LidMappingStoreService: a negative result on the shared table', () =>expect(await store.findPhoneForLid('111@lid')).toBeNull();});+ it('clears a phone stored by a session that has since logged out', async () => {+ await store.remember('111', '628111', s1);+ await ds.getRepository(Session).update({ id: s1 }, { phone: null }); // logout, or WhatsApp unlinked it++ await store.remember('111', null, s2);++ expect(await row('111')).toMatchObject({ phone: null, sessionId: s2 });+ expect(await store.findPhoneForLid('111@lid')).toBeNull();+ });++ it('keeps it again once that session is linked again', async () => {+ await store.remember('111', '628111', s1);+ await ds.getRepository(Session).update({ id: s1 }, { phone: null });+ await ds.getRepository(Session).update({ id: s1 }, { phone: '629000000001' });++ await store.remember('111', null, s2);++ expect(await row('111')).toMatchObject({ phone: '628111', sessionId: s1 });+ });+it('touches no other lid, whichever branch would let the negative replace that row', async () => {const stamp = new Date('2020-01-01T00:00:00.000Z');const others = [{ lid: '222', phone: '628222', sessionId: null }, // no owner{ lid: '444', phone: null, sessionId: s3 }, // holds no phone{ lid: '555', phone: '628555', sessionId: gone }, // owned by a deleted session+ { lid: '777', phone: '628777', sessionId: s9 }, // owned by a session that logged out{ lid: '666', phone: '628666', sessionId: s2 }, // owned by the session answering];await repo.insert([{ lid: '111', phone: '628111', sessionId: s2, updatedAt: stamp },...others.map(o => ({ ...o, updatedAt: stamp })),]);+ await ds.getRepository(Session).update({ id: s9 }, { phone: null });await store.reload();await store.remember('111', null, s2);diff --git a/src/engine/identity/lid-mapping.entity.ts b/src/engine/identity/lid-mapping.entity.tsindex a0d612d62..d2a00f5ed 100644--- a/src/engine/identity/lid-mapping.entity.ts+++ b/src/engine/identity/lid-mapping.entity.ts@@ -8,12 +8,12 @@ import { Column, Entity, Index, PrimaryColumn, UpdateDateColumn } from 'typeorm'* A phone is last-write-wins: a stored mapping is "best known, not forever" (WhatsApp recycles numbers),* so it's a cache WhatsApp can correct. `phone` is nullable to record a negative result (a lid we looked* up and could not resolve) so active lookups stay one-network-call-per-unknown-lid. A negative replaces a- * phone only when the session that wrote it (or none, or one since deleted) answers it, since another- * account may not see the number. `sessionId` is the row's owner, the session that last wrote it: a+ * phone only when the session that wrote it (or none, or one since deleted or logged out) answers it, since+ * another account may not see the number. `sessionId` is the row's owner, the session that last wrote it: a* negative, or a report of a phone, though a session this node already counted for that phone writes* nothing when it reports it again (LidMappingStoreService.seenBy), so after s1, s2, s1 report one phone- * the owner is s2. A deleted session owns nothing. Intentionally NOT a foreign key, since the row outlives- * any one session.+ * the owner is s2. A deleted or logged-out session owns nothing. Intentionally NOT a foreign key, since the row+ * outlives any one session.*/@Entity('lid_mappings')@Index(['phone']) // reverse lookup: phone -> lids, for the message from-filterdiff --git a/src/modules/auth/chat-scope.service.spec.ts b/src/modules/auth/chat-scope.service.spec.tsindex b236d45b6..e0170db8a 100644--- a/src/modules/auth/chat-scope.service.spec.ts+++ b/src/modules/auth/chat-scope.service.spec.ts@@ -148,7 +148,7 @@ describe('ChatScopeService over the shared lid table, with two sessions', () =>entities: [LidMapping, Session],synchronize: true,}).initialize();- await ds.getRepository(Session).insert([s1, s2].map(id => ({ id, name: id })));+ await ds.getRepository(Session).insert([s1, s2].map(id => ({ id, name: id, phone: `6290000000${id[7]}` })));store = new LidMappingStoreService(ds.getRepository(LidMapping));// A statement that fails is only logged, which would let a case pass on a write that never ran.warn = jest.spyOn((store as unknown as { logger: { warn: () => void } }).logger, 'warn');@@ -186,7 +186,7 @@ describe('ChatScopeService over the shared lid table, with two sessions', () =>it('still admits it after the latest session to report the phone is deleted, once an earlier one reports it again', async () => {const s3 = '00000003-0000-4000-8000-000000000000';- await ds.getRepository(Session).insert({ id: s3, name: s3 });+ await ds.getRepository(Session).insert({ id: s3, name: s3, phone: '629000000003' });await store.remember(LID, PHONE, s2);await ds.getRepository(Session).delete({ id: s2 });await store.releaseSession(s2);@@ -231,6 +231,14 @@ describe('ChatScopeService over the shared lid table, with two sessions', () =>]);});+ it('stops admitting it once the session that stored the phone logs out and another answers no phone', async () => {+ await ds.getRepository(Session).update({ id: s1 }, { phone: null });++ await store.remember(LID, null, s2);++ expect(await admitted()).toEqual([false, false]);+ });+it('stops admitting it once the session that stored the phone is deleted and another answers no phone', async () => {await ds.getRepository(Session).delete({ id: s1 });diff --git a/src/modules/session/session-lid-resolver.service.spec.ts b/src/modules/session/session-lid-resolver.service.spec.tsindex d3917995d..780274545 100644--- a/src/modules/session/session-lid-resolver.service.spec.ts+++ b/src/modules/session/session-lid-resolver.service.spec.ts@@ -284,7 +284,7 @@ describe('SessionLidResolver over the shared lid table', () => {}).initialize();const now = jest.spyOn(Date, 'now').mockReturnValue(1_000_000);try {- await ds.getRepository(Session).insert({ id: s1, name: s1 });+ await ds.getRepository(Session).insert({ id: s1, name: s1, phone: '629000000001' });const repo = ds.getRepository(LidMapping);const store = new LidMappingStoreService(repo);const warn = jest.spyOn((store as unknown as { logger: { warn: () => void } }).logger, 'warn');