Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): re-purge auth dirs when a start completes after its row was deleted (#952)

Yudhi Armyndharisbb90ace9b00f
diff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.ts
index 9af293027..716d01f87 100644
--- a/src/modules/session/session.service.spec.ts
+++ b/src/modules/session/session.service.spec.ts
@@ -1247,6 +1247,8 @@ describe('SessionService', () => {
it('tears down an engine created when a stop lands during init (post-init guard)', async () => {
const i = internals();
// Simulate a concurrent stop() during engine init: initialize() flips the teardown flag.
+ // The row itself survives a stop(), so the retirement must NOT purge the auth dirs.
+ (repository.findOne as jest.Mock).mockResolvedValue(createMockSession());
mockEngine.initialize.mockImplementation(() => {
i.stoppingSessions.add('sess-uuid-1');
return Promise.resolve();
@@ -1256,6 +1258,7 @@ describe('SessionService', () => {
expect(mockEngine.destroy).toHaveBeenCalled();
expect(i.engines.has('sess-uuid-1')).toBe(false);
+ expect(engineFactory.purgeSessionData).not.toHaveBeenCalled();
});
it('tears down an engine created when a delete lands during init (session row gone, mark cleared)', async () => {
@@ -1274,6 +1277,8 @@ describe('SessionService', () => {
expect(mockEngine.destroy).toHaveBeenCalled();
expect(i.engines.has('sess-uuid-1')).toBe(false);
+ // delete() purged BEFORE this re-init re-created the auth dir — the guard purges a second time.
+ expect(engineFactory.purgeSessionData).toHaveBeenCalledWith('test-session');
});
it('still re-initializes when the old engine destroy() hangs (time-bounded teardown)', async () => {
@@ -3574,6 +3579,8 @@ describe('SessionService', () => {
// The engine registered during init must be torn down + removed, not left READY.
expect(mockEngine.destroy).toHaveBeenCalled();
expect(service.getEngine('sess-uuid-1')).toBeUndefined();
+ // A stop() retirement must NOT purge: the row (and its credentials) is meant to survive.
+ expect(engineFactory.purgeSessionData).not.toHaveBeenCalled();
});
it('tears down the just-initialized engine if the session is deleted during start() (row gone, mark cleared)', async () => {
@@ -3595,6 +3602,75 @@ describe('SessionService', () => {
expect(mockEngine.destroy).toHaveBeenCalled();
expect(service.getEngine('sess-uuid-1')).toBeUndefined();
});
+
+ it('re-purges the auth dirs when a start completes after its row was deleted (init re-created them)', async () => {
+ const session = createMockSession();
+ (repository.findOne as jest.Mock).mockResolvedValue(session);
+ (repository.update as jest.Mock).mockResolvedValue({ affected: 1 });
+
+ // delete() runs its purge BEFORE this start's init resolves; the init re-creates the auth dir
+ // (both engines mkdir at init), so the retirement guard must purge a second time — keyed by
+ // session NAME, same as delete()'s purge — or the race leaves credentials behind.
+ mockEngine.initialize.mockImplementationOnce(() => {
+ (repository.findOne as jest.Mock).mockResolvedValue(null);
+ return Promise.resolve();
+ });
+
+ await expect(service.start('sess-uuid-1')).rejects.toThrow(NotFoundException);
+
+ expect(mockEngine.destroy).toHaveBeenCalled();
+ expect(engineFactory.purgeSessionData).toHaveBeenCalledWith('test-session');
+ });
+
+ it('emits no QR/status event for the retired engine once the post-init guard has run', async () => {
+ const session = createMockSession();
+ (repository.findOne as jest.Mock).mockResolvedValue(session);
+ (repository.update as jest.Mock).mockResolvedValue({ affected: 1 });
+
+ mockEngine.initialize.mockImplementationOnce(() => {
+ (repository.findOne as jest.Mock).mockResolvedValue(null);
+ return Promise.resolve();
+ });
+
+ await expect(service.start('sess-uuid-1')).rejects.toThrow(NotFoundException);
+
+ // The guard evicted the engine from the live map, so a late engine callback must no-op.
+ const callbacks = (mockEngine.initialize.mock.calls as [EngineEventCallbacks][])[0][0];
+ callbacks.onQRCode?.('qr-after-retire');
+ expect(eventsGateway.emitQRCode).not.toHaveBeenCalled();
+ expect(webhookService.dispatch).not.toHaveBeenCalledWith('sess-uuid-1', 'session.qr', expect.anything());
+ });
+
+ it('does not re-purge when the same name was re-created under a new id mid-race (the new row owns the dirs)', async () => {
+ const session = createMockSession();
+ (repository.findOne as jest.Mock).mockResolvedValue(session);
+ (repository.update as jest.Mock).mockResolvedValue({ affected: 1 });
+
+ // delete(old id) + create(same name) land during init: the old row is gone but a NEW row now
+ // owns the name — purging would wipe the fresh session's auth dir, so the guard must skip it.
+ mockEngine.initialize.mockImplementationOnce(() => {
+ (repository.findOne as jest.Mock).mockImplementation(({ where }: { where: { id?: string; name?: string } }) => {
+ if (where.name === 'test-session') return Promise.resolve(createMockSession({ id: 'sess-uuid-2' }));
+ return Promise.resolve(null);
+ });
+ return Promise.resolve();
+ });
+
+ await expect(service.start('sess-uuid-1')).rejects.toThrow(NotFoundException);
+
+ expect(mockEngine.destroy).toHaveBeenCalled();
+ expect(engineFactory.purgeSessionData).not.toHaveBeenCalled();
+ });
+
+ it('does not purge anything on a normal start (session row present throughout)', async () => {
+ (repository.findOne as jest.Mock).mockResolvedValue(createMockSession());
+ (repository.update as jest.Mock).mockResolvedValue({ affected: 1 });
+
+ await service.start('sess-uuid-1');
+
+ expect(service.getEngine('sess-uuid-1')).toBeDefined();
+ expect(engineFactory.purgeSessionData).not.toHaveBeenCalled();
+ });
});
// ── sendSeen (markChatRead) ───────────────────────────────────────
diff --git a/src/modules/session/session.service.ts b/src/modules/session/session.service.ts
index cae508d44..cbdd18d77 100644
--- a/src/modules/session/session.service.ts
+++ b/src/modules/session/session.service.ts
@@ -637,6 +637,9 @@ export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicat
await this.teardownEngineSafely(id, resurrected, e => e.destroy(), 'destroy');
if (this.isLiveEngine(id, resurrected)) this.engines.delete(id);
}
+ // A delete() that raced this start purged the on-disk auth dirs BEFORE this init re-created
+ // them — purge again so the window leaves no credential residue behind (no-op for a stop()).
+ await this.purgeAuthDirsIfDeleted(id, session.name);
}
return this.findOne(id);
} finally {
@@ -1997,6 +2000,32 @@ export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicat
return (await this.sessionRepository.findOne({ where: { id } })) == null;
}
+ /**
+ * Re-purge a retired session's on-disk auth dirs when its row was deleted while a slow
+ * engine.initialize() was still in flight. A start()/(re)connect that lands between delete()'s
+ * engine eviction and its row removal initializes a fresh engine that RE-CREATES the auth dir
+ * purgeSessionData just emptied (both engines mkdir at init); the post-init guard tears the engine
+ * down, but engine teardown never touches the on-disk dirs, so without this second purge the race
+ * leaves live WhatsApp credentials behind — and a later same-name recreate would silently re-link
+ * them. Gated two ways so ONLY the delete race purges: a stop() retirement still has its row (its
+ * credentials must survive), and a row re-created under the SAME name now owns those dirs, so
+ * purging would wipe the fresh session's link. Best-effort: a failure is logged, never thrown —
+ * the retirement path must still surface the deleted session as NotFound.
+ */
+ private async purgeAuthDirsIfDeleted(id: string, name: string): Promise<void> {
+ try {
+ if ((await this.sessionRepository.findOne({ where: { id } })) != null) return;
+ if ((await this.sessionRepository.findOne({ where: { name } })) != null) return;
+ await this.engineFactory.purgeSessionData(name);
+ } catch (error) {
+ this.logger.warn('Failed to re-purge session auth dirs after a start/delete race', {
+ sessionId: id,
+ action: 'engine_repurge_failed',
+ error: error instanceof Error ? error.message : String(error),
+ });
+ }
+ }
+
private async executeReconnect(id: string, session: Session, state: ReconnectState): Promise<void> {
// The session may have been stopped/deleted before this fired — don't resurrect it.
if (this.stoppingSessions.has(id)) {
@@ -2035,6 +2064,9 @@ export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicat
await this.teardownEngineSafely(id, resurrected, e => e.destroy(), 'destroy');
if (this.isLiveEngine(id, resurrected)) this.engines.delete(id);
}
+ // Same start/delete window as start()'s post-init guard: this re-init re-created auth dirs
+ // delete() had already purged — purge again so no credentials outlive the row.
+ await this.purgeAuthDirsIfDeleted(id, session.name);
return;
}
} catch (error: unknown) {