Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): migrate a UUID-shaped session name's auth dirs at boot
Yudhi Armyndharis3c7e2122a9ca
diff --git a/src/modules/session/session-auth-dir-migration.service.spec.ts b/src/modules/session/session-auth-dir-migration.service.spec.tsindex 77e7b4546..38f641e6c 100644--- a/src/modules/session/session-auth-dir-migration.service.spec.ts+++ b/src/modules/session/session-auth-dir-migration.service.spec.ts@@ -223,42 +223,20 @@ describe('SessionAuthDirMigration', () => {expect(warn).not.toHaveBeenCalled();});- // 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 () => {+ // Only an exact live id is held back. On an upgrade from a release that keyed directories by name,+ // a session named after, say, a tenant UUID owns the directory its name points at, and leaving it+ // there would bring the session back at a QR code.+ it('moves the directories of a UUID-shaped name that is not a session id onto the session id', async () => {const uuidName = '0f9e8d7c-6b5a-4938-8271-605f4e3d2c1b';+ seed(path.join(sessionsDir, `session-${uuidName}`), 'wwjs-legacy');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 migration.onModuleInit();- 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' }),- );- });+ await buildMigration([{ id: ALICE_ID, name: uuidName }]).onModuleInit();- 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();+ expect(markerAt(path.join(sessionsDir, `session-${ALICE_ID}`))).toBe('wwjs-legacy');+ expect(markerAt(path.join(baileysDir, ALICE_ID))).toBe('baileys-legacy');+ expect(fs.existsSync(path.join(sessionsDir, `session-${uuidName}`))).toBe(false);+ expect(fs.existsSync(path.join(baileysDir, uuidName))).toBe(false);});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 28f00dc17..924ab283a 100644--- a/src/modules/session/session-auth-dir-migration.service.ts+++ b/src/modules/session/session-auth-dir-migration.service.ts@@ -4,7 +4,6 @@ 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';@@ -65,8 +64,8 @@ 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. 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.+ // account to this row. Only the exact ids are held back, whatever their shape: a UUID-shaped name+ // that is no session's id is an ordinary name, and on an upgrade its directory is its own login.const ids = new Set(rows.map(row => row.id));const movable = sessions.filter(row => !ids.has(row.name));@@ -114,19 +113,6 @@ 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(