Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): drop lid mappings stored with a broadcast id as phone

Yudhi Armyndharisf71ce1669cc6
diff --git a/docs/05-database-design.md b/docs/05-database-design.md
index 6e16c0143..f2558f6bc 100644
--- a/docs/05-database-design.md
+++ b/docs/05-database-design.md
@@ -884,7 +884,8 @@ src/database/migrations/ # data connection (pluggable)
├── 1786650000000-AddChatStateObserved.ts # chat_states.observed (fields a row has seen)
├── 1786700000000-AddMessagesSessionChatCreatedAtIndex.ts # (sessionId, chatId, createdAt): chat thread pages
├── 1786800000000-AddBaileysStoredMessagesSessionCreatedIdIndex.ts # (sessionId, createdAt, id) index for the Baileys store cap trim
-└── 1786900000000-ScrubRevokedMessageContent.ts # clears content kept on rows revoked by earlier releases
+├── 1786900000000-ScrubRevokedMessageContent.ts # clears content kept on rows revoked by earlier releases
+└── 1786950000000-ScrubNonPhoneLidMappings.ts # drops lid_mappings rows where earlier releases stored a broadcast id as the phone
```
> [!NOTE]
diff --git a/src/database/migrations/1786950000000-ScrubNonPhoneLidMappings.ts b/src/database/migrations/1786950000000-ScrubNonPhoneLidMappings.ts
new file mode 100644
index 000000000..f0c3f802a
--- /dev/null
+++ b/src/database/migrations/1786950000000-ScrubNonPhoneLidMappings.ts
@@ -0,0 +1,33 @@
+import { MigrationInterface, QueryRunner } from 'typeorm';
+
+/**
+ * Drop the `lid_mappings` rows earlier releases wrote with a broadcast id as the phone.
+ *
+ * A status or broadcast-list message carries the sender's lid in `remoteJidAlt`, and earlier releases
+ * paired it with the chat (`status@broadcast` or `<list>@broadcast`) instead of the sender, storing
+ * "status" or the list id as that lid's phone number. The lid then resolved to `[email protected]` or
+ * `<list>@c.us`. New writes pair it with the sender; this removes the rows written before that:
+ * every phone that is not all digits, and every list id those releases filed a received list message
+ * under. A deleted row is only a cache miss, so the next lookup asks WhatsApp again. A null phone is a
+ * cached negative result and stays.
+ *
+ * Idempotent, and `down` is a no-op: the removed values were wrong.
+ */
+export class ScrubNonPhoneLidMappings1786950000000 implements MigrationInterface {
+ name = 'ScrubNonPhoneLidMappings1786950000000';
+
+ public async up(queryRunner: QueryRunner): Promise<void> {
+ if (!(await queryRunner.hasTable('lid_mappings'))) return;
+ const notDigits =
+ queryRunner.dataSource.options.type === 'postgres' ? `"phone" !~ '^[0-9]+$'` : `"phone" GLOB '*[^0-9]*'`;
+ await queryRunner.query(`DELETE FROM "lid_mappings" WHERE ${notDigits} OR "phone" = ''`);
+ if (!(await queryRunner.hasTable('messages'))) return;
+ await queryRunner.query(
+ `DELETE FROM "lid_mappings" WHERE "phone" IN (SELECT DISTINCT substr("chatId", 1, length("chatId") - 10) FROM "messages" WHERE "chatId" LIKE '%@broadcast')`,
+ );
+ }
+
+ public async down(): Promise<void> {
+ // Nothing to restore.
+ }
+}
diff --git a/src/database/migrations/__tests__/1786950000000-ScrubNonPhoneLidMappings.spec.ts b/src/database/migrations/__tests__/1786950000000-ScrubNonPhoneLidMappings.spec.ts
new file mode 100644
index 000000000..e9dfb1a6c
--- /dev/null
+++ b/src/database/migrations/__tests__/1786950000000-ScrubNonPhoneLidMappings.spec.ts
@@ -0,0 +1,75 @@
+import { DataSource, Repository } from 'typeorm';
+import { LidMapping } from '../../../engine/identity/lid-mapping.entity';
+import { Message, MessageDirection, MessageStatus } from '../../../modules/message/entities/message.entity';
+import { ScrubNonPhoneLidMappings1786950000000 } from '../1786950000000-ScrubNonPhoneLidMappings';
+
+// A real SQLite database: the statements themselves are what is under test.
+describe('ScrubNonPhoneLidMappings migration', () => {
+ const migration = new ScrubNonPhoneLidMappings1786950000000();
+ let ds: DataSource;
+ let mappings: Repository<LidMapping>;
+
+ beforeEach(async () => {
+ ds = new DataSource({
+ type: 'better-sqlite3',
+ database: ':memory:',
+ entities: [LidMapping, Message],
+ synchronize: true,
+ });
+ await ds.initialize();
+ mappings = ds.getRepository(LidMapping);
+ });
+
+ afterEach(async () => {
+ await ds.destroy();
+ });
+
+ const message = (chatId: string) =>
+ ds.getRepository(Message).save({
+ sessionId: 's1',
+ chatId,
+ waMessageId: `wa-${chatId}`,
+ from: chatId,
+ body: 'hi',
+ type: 'text',
+ direction: MessageDirection.INCOMING,
+ status: MessageStatus.SENT,
+ timestamp: 1,
+ });
+
+ it('drops a status or broadcast-list id stored as a phone and keeps real phones and negative results', async () => {
+ await mappings.save([
+ { lid: '111', phone: 'status', sessionId: 's1' },
+ { lid: '222', phone: '1612345678', sessionId: 's1' },
+ { lid: '333', phone: '628111', sessionId: 's1' },
+ { lid: '444', phone: null, sessionId: 's1' },
+ ]);
+ await message('1612345678@broadcast');
+ await message('status@broadcast');
+ await message('[email protected]');
+
+ const runner = ds.createQueryRunner();
+ await migration.up(runner);
+ await migration.up(runner); // idempotent
+ await runner.release();
+
+ const rows = await mappings.find({ order: { lid: 'ASC' } });
+ expect(rows.map(r => [r.lid, r.phone])).toEqual([
+ ['333', '628111'],
+ ['444', null],
+ ]);
+ });
+
+ it('skips a database without the tables', async () => {
+ const empty = new DataSource({ type: 'better-sqlite3', database: ':memory:' });
+ await empty.initialize();
+ const runner = empty.createQueryRunner();
+
+ await expect(migration.up(runner)).resolves.toBeUndefined();
+ await expect(migration.down()).resolves.toBeUndefined();
+
+ await runner.release();
+ await empty.destroy();
+ });
+});