Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(engine): keep another session's auth when purging a UUID name

Yudhi Armyndharis218700a2a6a8
diff --git a/src/engine/engine.factory.spec.ts b/src/engine/engine.factory.spec.ts
index 3d9e5ef07..7f3f89a6d 100644
--- a/src/engine/engine.factory.spec.ts
+++ b/src/engine/engine.factory.spec.ts
@@ -328,6 +328,22 @@ describe('EngineFactory', () => {
expect(fs.existsSync(otherSession)).toBe(true);
});
+ // A name is free text that passes the session-name rule, so it can be another session's id. The
+ // legacy dirs it names are then that other session's live id-keyed credentials.
+ it("leaves another session's auth dirs alone when the deleted session is named after its id", async () => {
+ const { factory } = buildBothDirFactory('baileys');
+ const otherId = '0b5c3a52-6d1e-4c1a-9f0e-2a7b8c9d0e1f';
+ const otherWwjs = wwjsAuthDir(path.join(tmpRoot, 'sessions'), otherId);
+ const otherBaileys = baileysAuthDir(path.join(tmpRoot, 'baileys'), otherId);
+ fs.mkdirSync(otherWwjs, { recursive: true });
+ fs.mkdirSync(otherBaileys, { recursive: true });
+
+ await factory.purgeSessionData(SESSION_ID, otherId);
+
+ expect(fs.existsSync(otherWwjs)).toBe(true);
+ expect(fs.existsSync(otherBaileys)).toBe(true);
+ });
+
it('refuses to purge an unsafe session key (no rm on a traversal path)', async () => {
// A sibling that a '../' name would resolve to — it must survive the refused purge.
const sibling = path.join(tmpRoot, 'baileys-evil');
diff --git a/src/engine/engine.factory.ts b/src/engine/engine.factory.ts
index adfec8a8c..5e4a46a80 100644
--- a/src/engine/engine.factory.ts
+++ b/src/engine/engine.factory.ts
@@ -2,6 +2,7 @@ import * as fs from 'fs';
import * as path from 'path';
import { Injectable, OnModuleInit } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
+import { isUUID } from 'class-validator';
import { IWhatsAppEngine } from './interfaces/whatsapp-engine.interface';
import { WhatsAppWebJsAdapter } from './adapters/whatsapp-web-js.adapter';
import { PluginLoaderService, PluginType, IEnginePlugin, PluginManifest } from '../core/plugins';
@@ -181,7 +182,9 @@ export class EngineFactory implements OnModuleInit {
{ engine: 'whatsapp-web.js', dir: this.wwjsAuthDir(sessionId) },
{ engine: 'baileys', dir: this.baileysAuthDir(sessionId) },
];
- if (legacyName !== undefined && isSafeSessionName(legacyName)) {
+ // A UUID-shaped name is skipped: the name rule lets a session be named after another session's
+ // id, and the dirs that name points at are then that session's live id-keyed credentials.
+ if (legacyName !== undefined && isSafeSessionName(legacyName) && !isUUID(legacyName)) {
const legacyDirs = [
{ engine: 'whatsapp-web.js', dir: this.wwjsAuthDir(legacyName) },
{ engine: 'baileys', dir: this.baileysAuthDir(legacyName) },