Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): purge a deleted session's auth dirs by its real id

Yudhi Armyndharis3674153a1253
diff --git a/src/modules/session/session-engine-controls.ts b/src/modules/session/session-engine-controls.ts
index 7489bd53f..184e428ee 100644
--- a/src/modules/session/session-engine-controls.ts
+++ b/src/modules/session/session-engine-controls.ts
@@ -599,8 +599,10 @@ export class SessionEngineControls {
// it is another session's id, whatever its shape (an import accepts any safe key): the dirs it
// names are then that session's live login, the same exact-id guard the migration keeps. A
// failed lookup withholds the name rather than fail a delete that has already committed.
+ // Keyed by `id`, not `session.id`: TypeORM clears a removed entity's primary key, so session.id
+ // is undefined here and the purge would refuse it as an unsafe key and remove nothing.
const nameIsAnId = await this.sessionRepository.exists({ where: { id: session.name } }).catch(() => true);
- await this.engineFactory.purgeSessionData(session.id, nameIsAnId ? undefined : session.name);
+ await this.engineFactory.purgeSessionData(id, nameIsAnId ? undefined : session.name);
} finally {
// Always clear the teardown mark so a later recreate/start with this id isn't suppressed. This
// stop mark was set after fence #1, so clearing it on a rejected 409 only undoes what THIS
diff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.ts
index 9107defe1..767105104 100644
--- a/src/modules/session/session.service.spec.ts
+++ b/src/modules/session/session.service.spec.ts
@@ -150,7 +150,11 @@ describe('SessionService', () => {
transaction: jest.fn().mockImplementation(async (cb: (manager: unknown) => Promise<unknown>) => {
const manager = {
save: jest.fn().mockImplementation((entity: unknown) => Promise.resolve(entity)),
- remove: jest.fn().mockResolvedValue(undefined),
+ // As TypeORM does: a removed entity comes back with its primary key cleared.
+ remove: jest.fn().mockImplementation((entity: { id?: string }) => {
+ entity.id = undefined;
+ return Promise.resolve(entity);
+ }),
delete: jest.fn().mockResolvedValue({ affected: 0 }),
};
return cb(manager);