Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(identity): let a negative clear a deleted session's lid phone
Yudhi Armyndharis495f9dee8f30
diff --git a/src/engine/identity/lid-mapping-store.service.ts b/src/engine/identity/lid-mapping-store.service.tsindex 94123ba4e..26341cb33 100644--- a/src/engine/identity/lid-mapping-store.service.ts+++ b/src/engine/identity/lid-mapping-store.service.ts@@ -1,6 +1,6 @@import { Injectable, OnModuleInit } from '@nestjs/common';import { InjectRepository } from '@nestjs/typeorm';-import { In, IsNull, Repository } from 'typeorm';+import { In, IsNull, Raw, Repository } from 'typeorm';import { LidMapping } from './lid-mapping.entity';import { userPart } from './wa-id';import { createLogger } from '../../common/services/logger.service';@@ -51,7 +51,7 @@ export interface LidMappingStore {findLidsForPhone?(phone: string): Promise<string[]>;/*** Write-through: update the cache + persist. A phone is last-write-wins. A `null` phone records a negative- * result, and replaces a stored phone only when the same session (or none) wrote it.+ * result, and replaces a stored phone only when the same session (or none, or one since deleted) wrote it.*/remember(lid: string, phone: string | null, sessionId?: string): Promise<void>;}@@ -301,10 +301,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); another 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 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.*/private async clear(lid: string, sessionId: string | null): Promise<void> {const updatedAt = new Date();@@ -318,6 +319,9 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {const replaceable = [{ 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)`) },...(sessionId ? [{ lid, sessionId }] : []),];const { affected } = await this.repo.update(replaceable, { phone: null, sessionId, updatedAt });@@ -330,8 +334,8 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {/*** Hand a deleted session's rows to no session. Its id never comes back (a re-created session gets a- * new one), so without this no later negative could clear a phone it stored. The rows stay: the table- * is shared. Never throws: the session is already gone.+ * new one); {@link clear} also treats an owner missing from `sessions` as none, which covers a delete+ * that never got here. The rows stay: the table is shared. Never throws: the session is already gone.*/async releaseSession(sessionId: string): Promise<void> {try {diff --git a/src/engine/identity/lid-mapping-store.spec.ts b/src/engine/identity/lid-mapping-store.spec.tsindex 41c7515ec..717ce9e3b 100644--- a/src/engine/identity/lid-mapping-store.spec.ts+++ b/src/engine/identity/lid-mapping-store.spec.ts@@ -1,6 +1,7 @@import { DataSource, FindOperator, In, Repository } from 'typeorm';import { LidMappingStoreService } from './lid-mapping-store.service';import { LidMapping } from './lid-mapping.entity';+import { Session } from '../../modules/session/entities/session.entity';/** A where-clause value: a literal, or a TypeORM `In([...])` FindOperator. */type Cond = string | FindOperator<unknown>;@@ -777,9 +778,13 @@ 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.+ 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;let repo: Repository<LidMapping>;let store: LidMappingStoreService;+ let warn: jest.SpyInstance;const row = (lid: string) => repo.findOne({ where: { lid } });/** Holds the next call of a repository method until `release` is called; `reached` settles on entry. */@@ -801,37 +806,43 @@ describe('LidMappingStoreService: a negative result on the shared table', () =>ds = await new DataSource({type: 'better-sqlite3',database: ':memory:',- entities: [LidMapping],+ entities: [LidMapping, Session],synchronize: true,}).initialize();+ await ds.getRepository(Session).insert([s1, s2, s3, s9].map(id => ({ id, name: id })));repo = ds.getRepository(LidMapping);store = new LidMappingStoreService(repo);+ // 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');await store.onModuleInit();});- afterEach(() => ds.destroy());+ afterEach(async () => {+ expect(warn).not.toHaveBeenCalled();+ await ds.destroy();+ });it('records a negative for a lid with no row', async () => {- await store.remember('333', null, 's1');+ await store.remember('333', null, s1);- expect(await row('333')).toMatchObject({ phone: null, sessionId: 's1' });+ expect(await row('333')).toMatchObject({ phone: null, sessionId: s1 });expect(await store.findPhoneForLid('333@lid')).toBeNull();});it('leaves the phone another session stored, in the table and in the cache', async () => {- await store.remember('111', '628111', 's1');- await store.remember('111', null, 's2');+ await store.remember('111', '628111', s1);+ await store.remember('111', null, s2);- expect(await row('111')).toMatchObject({ phone: '628111', sessionId: 's1' });+ expect(await row('111')).toMatchObject({ phone: '628111', sessionId: s1 });expect(store.getCached('111')).toBe('628111');expect(await store.lidsForPhonesPersisted(['628111'])).toEqual({ 628111: ['111'] });});it('clears the phone when the session that stored it answers no phone (#1058)', async () => {- await store.remember('111', '628111', 's1');- await store.remember('111', null, 's1');+ await store.remember('111', '628111', s1);+ await store.remember('111', null, s1);- expect(await row('111')).toMatchObject({ phone: null, sessionId: 's1' });+ expect(await row('111')).toMatchObject({ phone: null, sessionId: s1 });expect(store.lidsForPhone('628111')).toEqual([]);expect(store.getCached('111')).not.toBe('628111');expect(await store.findPhoneForLid('111@lid')).toBeNull();@@ -841,70 +852,82 @@ describe('LidMappingStoreService: a negative result on the shared table', () =>await repo.save({ lid: '111', phone: '628111', sessionId: null });await store.reload();- await store.remember('111', null, 's2');+ await store.remember('111', null, s2);++ expect(await row('111')).toMatchObject({ phone: null, sessionId: s2 });+ });++ it('clears a phone stored by a session that no longer exists', async () => {+ await repo.save({ lid: '111', phone: '628111', sessionId: gone });+ await store.reload();++ await store.remember('111', null, s2);- expect(await row('111')).toMatchObject({ phone: null, sessionId: 's2' });+ expect(await row('111')).toMatchObject({ phone: null, sessionId: s2 });+ expect(store.lidsForPhone('628111')).toEqual([]);+ expect(await store.findPhoneForLid('111@lid')).toBeNull();});it('drops a phone cached here once the table holds none, whichever session cleared it', async () => {- await store.remember('111', '628111', 's1');+ await store.remember('111', '628111', s1);await repo.update({ lid: '111' }, { phone: null }); // s1 answered no phone on another node- await store.remember('111', null, 's2');+ await store.remember('111', null, s2);- expect(await row('111')).toMatchObject({ phone: null, sessionId: 's2' });+ expect(await row('111')).toMatchObject({ phone: null, sessionId: s2 });expect(store.lidsForPhone('628111')).toEqual([]);});it('lets a later session clear a phone once the session that stored it is released', async () => {- await store.remember('111', '628111', 's1');- await store.remember('222', '628222', 's9');+ await store.remember('111', '628111', s1);+ await store.remember('222', '628222', s9);const stamp = new Date('2020-01-01T00:00:00.000Z');await repo.update({ lid: '111' }, { updatedAt: stamp });- await store.releaseSession('s1');+ await store.releaseSession(s1);expect(await row('111')).toMatchObject({ phone: '628111', sessionId: null, updatedAt: stamp });- expect(await row('222')).toMatchObject({ phone: '628222', sessionId: 's9' });- await store.remember('111', null, 's2');- expect(await row('111')).toMatchObject({ phone: null, sessionId: 's2' });+ expect(await row('222')).toMatchObject({ phone: '628222', sessionId: s9 });+ await store.remember('111', null, s2);+ expect(await row('111')).toMatchObject({ phone: null, sessionId: s2 });});it('releaseSession never throws', async () => {jest.spyOn(repo, 'update').mockRejectedValueOnce(new Error('connection lost'));- await expect(store.releaseSession('s1')).resolves.toBeUndefined();+ await expect(store.releaseSession(s1)).resolves.toBeUndefined();+ warn.mockClear();});it('a phone from any session still replaces the row', async () => {- await store.remember('111', null, 's1');- await store.remember('111', '628111', 's2');- await store.remember('111', '628222', 's3');+ await store.remember('111', null, s1);+ await store.remember('111', '628111', s2);+ await store.remember('111', '628222', s3);- expect(await row('111')).toMatchObject({ phone: '628222', sessionId: 's3' });+ expect(await row('111')).toMatchObject({ phone: '628222', sessionId: s3 });});it('keeps a phone stored while the negative of another session is being written', async () => {const update = hold('update');- const negative = store.remember('111', null, 's2');+ const negative = store.remember('111', null, s2);await update.reached; // the row exists (holding no phone); the conditional update has not run- await store.remember('111', '628111', 's1');+ await store.remember('111', '628111', s1);update.release();await negative;- expect(await row('111')).toMatchObject({ phone: '628111', sessionId: 's1' });+ expect(await row('111')).toMatchObject({ phone: '628111', sessionId: s1 });expect(store.getCached('111')).toBe('628111');});it('does not cache a negative over a phone still being written', async () => {const upsert = hold('upsert');- const phone = store.remember('111', '628111', 's1');+ const phone = store.remember('111', '628111', s1);await upsert.reached;- await store.remember('111', null, 's2'); // lands first: there is no row yet+ await store.remember('111', null, s2); // lands first: there is no row yetupsert.release();await phone;- expect(await row('111')).toMatchObject({ phone: '628111', sessionId: 's1' });+ expect(await row('111')).toMatchObject({ phone: '628111', sessionId: s1 });expect(store.getCached('111')).not.toBeNull();expect(await store.lidsForPhonesPersisted(['628111'])).toEqual({ 628111: ['111'] });});diff --git a/src/engine/identity/lid-mapping.entity.ts b/src/engine/identity/lid-mapping.entity.tsindex 4d7842f16..cc1c9c6d1 100644--- a/src/engine/identity/lid-mapping.entity.ts+++ b/src/engine/identity/lid-mapping.entity.ts@@ -8,9 +8,9 @@ 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) answers it, since another account may not see the- * number. `sessionId` is the session that last wrote the row - intentionally NOT a foreign key, since the- * row outlives any one session.+ * 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 session that last wrote the row - 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-filter@@ -23,7 +23,7 @@ export class LidMapping {@Column({ type: 'varchar', nullable: true })phone!: string | null;- /** The session that last wrote this row; a negative from another session leaves its phone. Not a foreign key. */+ /** The session that last wrote this row; a negative from another live session leaves its phone. Not a foreign key. */@Column({ type: 'varchar', nullable: true })sessionId!: string | null;diff --git a/src/modules/auth/chat-scope.service.spec.ts b/src/modules/auth/chat-scope.service.spec.tsindex 3a72789a9..7b6f5d8bc 100644--- a/src/modules/auth/chat-scope.service.spec.ts+++ b/src/modules/auth/chat-scope.service.spec.ts@@ -3,6 +3,7 @@ import { ChatScopeService } from './chat-scope.service';import { DataSource, FindOperator, Repository } from 'typeorm';import { LidMappingStoreService } from '../../engine/identity/lid-mapping-store.service';import { LidMapping } from '../../engine/identity/lid-mapping.entity';+import { Session } from '../session/entities/session.entity';const PHONE = '919999999999';const LID = '555000111';@@ -119,9 +120,11 @@ describe('ChatScopeService', () => {describe('ChatScopeService over the shared lid table, with two sessions', () => {const key = { allowedChats: [`${PHONE}@c.us`] };+ const [s1, s2] = ['00000001-0000-4000-8000-000000000000', '00000002-0000-4000-8000-000000000000'];let ds: DataSource;let store: LidMappingStoreService;let svc: ChatScopeService;+ let warn: jest.SpyInstance;const admitted = async (): Promise<[boolean, boolean]> => [await svc.allows(key, `${LID}@lid`),(await svc.scopeForFilter(key))!.allowed.has(`${LID}@lid`),@@ -131,27 +134,41 @@ describe('ChatScopeService over the shared lid table, with two sessions', () =>ds = await new DataSource({type: 'better-sqlite3',database: ':memory:',- entities: [LidMapping],+ entities: [LidMapping, Session],synchronize: true,}).initialize();+ await ds.getRepository(Session).insert([s1, s2].map(id => ({ id, name: id })));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');await store.onModuleInit();svc = new ChatScopeService(store);- await store.remember(LID, PHONE, 's1');+ await store.remember(LID, PHONE, s1);});- afterEach(() => ds.destroy());+ afterEach(async () => {+ expect(warn).not.toHaveBeenCalled();+ await ds.destroy();+ });it('still admits the lid of a phone after a session that cannot see the number answers no phone', async () => {expect(await admitted()).toEqual([true, true]);- await store.remember(LID, null, 's2');+ await store.remember(LID, null, s2);expect(await admitted()).toEqual([true, true]);});it('stops admitting it once the session that stored the phone answers no phone', async () => {- await store.remember(LID, null, 's1');+ await store.remember(LID, null, s1);++ 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 });++ await store.remember(LID, null, s2);expect(await admitted()).toEqual([false, false]);});