Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): never migrate an auth dir named by a session id
Yudhi Armyndharis480a00c91721
diff --git a/src/modules/session/session-auth-dir-migration.service.spec.ts b/src/modules/session/session-auth-dir-migration.service.spec.tsindex ea3846737..5c02cfc3c 100644--- a/src/modules/session/session-auth-dir-migration.service.spec.ts+++ b/src/modules/session/session-auth-dir-migration.service.spec.ts@@ -196,6 +196,37 @@ describe('SessionAuthDirMigration', () => {expect(fs.existsSync(path.join(baileysDir, BOB_ID))).toBe(false);});+ // The name rule lets a session be named after another session's id. The "legacy" directory that+ // name points at is then the other session's live id-keyed login, and moving it would hand that+ // WhatsApp account to the misnamed row.+ it.each([+ ['UUID-shaped and another session id', BOB_ID],+ ['another session id that is not UUID-shaped', 'imported-bob'],+ ])('leaves another session login alone when a name is %s', async (_case, bobId) => {+ seed(path.join(sessionsDir, `session-${bobId}`), 'wwjs-bob');+ seed(path.join(baileysDir, bobId), 'baileys-bob');++ await buildMigration([+ { id: bobId, name: 'bob' },+ { id: ALICE_ID, name: bobId },+ ]).onModuleInit();++ expect(markerAt(path.join(sessionsDir, `session-${bobId}`))).toBe('wwjs-bob');+ expect(markerAt(path.join(baileysDir, bobId))).toBe('baileys-bob');+ expect(fs.existsSync(path.join(sessionsDir, `session-${ALICE_ID}`))).toBe(false);+ expect(fs.existsSync(path.join(baileysDir, ALICE_ID))).toBe(false);+ });++ it('never moves a directory named by a UUID-shaped name, even when no session has that id', async () => {+ const deletedId = '0f9e8d7c-6b5a-4938-8271-605f4e3d2c1b';+ seed(path.join(baileysDir, deletedId), 'baileys-deleted');++ await buildMigration([{ id: ALICE_ID, name: deletedId }]).onModuleInit();++ expect(markerAt(path.join(baileysDir, deletedId))).toBe('baileys-deleted');+ expect(fs.existsSync(path.join(baileysDir, ALICE_ID))).toBe(false);+ });+it('skips the query and the filesystem entirely when there are no sessions', async () => {seed(path.join(sessionsDir, 'session-orphan'), 'orphan');diff --git a/src/modules/session/session-auth-dir-migration.service.ts b/src/modules/session/session-auth-dir-migration.service.tsindex e8cac0d13..903bac1f1 100644--- a/src/modules/session/session-auth-dir-migration.service.ts+++ b/src/modules/session/session-auth-dir-migration.service.ts@@ -4,6 +4,7 @@ import { Injectable, OnModuleInit } from '@nestjs/common';import { ConfigService } from '@nestjs/config';import { InjectRepository } from '@nestjs/typeorm';import { Repository } from 'typeorm';+import { isUUID } from 'class-validator';import { Session } from './entities/session.entity';import { baileysAuthDir, readAuthDirEntries, wwjsAuthDir } from '../../engine/auth-dir-paths';import { isSafeSessionName } from '../../common/utils/path-safety';@@ -62,16 +63,23 @@ export class SessionAuthDirMigration implements OnModuleInit {);}+ // The name rule lets a session be named after another session's id, and the "legacy" directory+ // that name points at is then that session's live id-keyed login: moving it would hand the+ // account to this row. Same guard as EngineFactory.purgeSessionData, plus the exact ids for a+ // row imported with an id that is not UUID-shaped.+ const ids = new Set(rows.map(row => row.id));+ const movable = sessions.filter(row => !isUUID(row.name) && !ids.has(row.name));+const sessionDataPath = this.configService.get<string>('engine.sessionDataPath') ?? './data/sessions';const authDir = this.configService.get<string>('engine.baileys.authDir') ?? './data/baileys';// Both engine shapes, whatever ENGINE_TYPE says: a session that ever ran under the other engine// still has a live auth directory there, and leaving it name-keyed would strand it (the same// reason EngineFactory.purgeSessionData removes both).const found = [- ...this.migrateBase('whatsapp-web.js', sessions, path.resolve(sessionDataPath), key =>+ ...this.migrateBase('whatsapp-web.js', movable, path.resolve(sessionDataPath), key =>wwjsAuthDir(sessionDataPath, key),),- ...this.migrateBase('baileys', sessions, authDir, key => baileysAuthDir(authDir, key)),+ ...this.migrateBase('baileys', movable, authDir, key => baileysAuthDir(authDir, key)),];this.warnOnCaseCollisions(sessions, new Set(found));}