Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): warn when a UUID-shaped name holds back an auth dir
Yudhi Armyndharis0959787d2e57
diff --git a/src/modules/session/session-auth-dir-migration.service.spec.ts b/src/modules/session/session-auth-dir-migration.service.spec.tsindex 5c02cfc3c..77e7b4546 100644--- a/src/modules/session/session-auth-dir-migration.service.spec.ts+++ b/src/modules/session/session-auth-dir-migration.service.spec.ts@@ -205,26 +205,60 @@ describe('SessionAuthDirMigration', () => {])('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([+ const migration = buildMigration([{ id: bobId, name: 'bob' },{ id: ALICE_ID, name: bobId },- ]).onModuleInit();+ ]);+ const warn = jest+ .spyOn((migration as unknown as { logger: { warn: jest.Mock } }).logger, 'warn')+ .mockImplementation(() => undefined);++ await migration.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);+ // Known to be another session's login, so nothing tells the operator to hand it over.+ expect(warn).not.toHaveBeenCalled();});- 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');+ // A UUID-shaped name's legacy directory cannot be told apart from a deleted session's id-keyed+ // leftover, so it is not moved; but on an upgrade it may be this session's own login, which must+ // not be stranded without a word.+ it('never moves a directory named by a UUID-shaped name, and names it so it can be moved by hand', async () => {+ const uuidName = '0f9e8d7c-6b5a-4938-8271-605f4e3d2c1b';+ seed(path.join(baileysDir, uuidName), 'baileys-legacy');+ const migration = buildMigration([{ id: ALICE_ID, name: uuidName }]);+ const warn = jest+ .spyOn((migration as unknown as { logger: { warn: jest.Mock } }).logger, 'warn')+ .mockImplementation(() => undefined);- await buildMigration([{ id: ALICE_ID, name: deletedId }]).onModuleInit();+ await migration.onModuleInit();- expect(markerAt(path.join(baileysDir, deletedId))).toBe('baileys-deleted');+ expect(markerAt(path.join(baileysDir, uuidName))).toBe('baileys-legacy');expect(fs.existsSync(path.join(baileysDir, ALICE_ID))).toBe(false);+ expect(warn).toHaveBeenCalledTimes(1);+ expect(warn).toHaveBeenCalledWith(+ expect.stringContaining(path.join(baileysDir, uuidName)),+ expect.objectContaining({ action: 'auth_dir_migration_ambiguous_name', engine: 'baileys' }),+ );+ });++ it('says nothing about a UUID-shaped name once its session has an id-keyed directory', async () => {+ const uuidName = '0f9e8d7c-6b5a-4938-8271-605f4e3d2c1b';+ seed(path.join(baileysDir, uuidName), 'baileys-leftover');+ seed(path.join(baileysDir, ALICE_ID), 'baileys-alice');+ const migration = buildMigration([{ id: ALICE_ID, name: uuidName }]);+ const warn = jest+ .spyOn((migration as unknown as { logger: { warn: jest.Mock } }).logger, 'warn')+ .mockImplementation(() => undefined);++ await migration.onModuleInit();++ expect(markerAt(path.join(baileysDir, uuidName))).toBe('baileys-leftover');+ expect(markerAt(path.join(baileysDir, ALICE_ID))).toBe('baileys-alice');+ expect(warn).not.toHaveBeenCalled();});it('skips the query and the filesystem entirely when there are no sessions', async () => {diff --git a/src/modules/session/session-auth-dir-migration.service.ts b/src/modules/session/session-auth-dir-migration.service.tsindex 903bac1f1..28f00dc17 100644--- a/src/modules/session/session-auth-dir-migration.service.ts+++ b/src/modules/session/session-auth-dir-migration.service.ts@@ -65,10 +65,10 @@ 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.+ // account to this row. The exact ids catch a row imported with an id that is not UUID-shaped;+ // migrateBase holds back the other UUID-shaped names, as EngineFactory.purgeSessionData does.const ids = new Set(rows.map(row => row.id));- const movable = sessions.filter(row => !isUUID(row.name) && !ids.has(row.name));+ const movable = sessions.filter(row => !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';@@ -114,6 +114,19 @@ export class SessionAuthDirMigration implements OnModuleInit {const legacy = path.basename(dirFor(session.name));const target = path.basename(dirFor(session.id));if (legacy === target || !entries.has(legacy)) continue;+ if (isUUID(session.name)) {+ // Its directory cannot be told apart from a deleted session's id-keyed leftover, so it is+ // not moved; but on an upgrade from a name-keyed release it may be this session's own login.+ if (!entries.has(target)) {+ this.logger.warn(+ `Session "${session.name}" has a UUID-shaped name, so "${path.join(base, legacy)}" was not moved ` ++ `onto its session id. If it holds this session's ${engine} login, move it to ` ++ `"${path.join(base, target)}" before starting the session; otherwise the session starts at a QR code.`,+ { sessionId: session.id, action: 'auth_dir_migration_ambiguous_name', engine, legacy, target },+ );+ }+ continue;+ }found.push(session.name);if (entries.has(target)) {this.logger.warn(