Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): purge a deleted session's name-keyed dirs on PostgreSQL

Yudhi Armyndharise417a602674a
diff --git a/src/modules/session/session-content-lifetime.spec.ts b/src/modules/session/session-content-lifetime.spec.ts
index ff593e9d0..d90242e95 100644
--- a/src/modules/session/session-content-lifetime.spec.ts
+++ b/src/modules/session/session-content-lifetime.spec.ts
@@ -67,10 +67,12 @@ describe('Session content lifetime', () => {
let controls: SessionEngineControls;
let engines: EngineRegistry;
let hooks: HookManager;
+ let purgeSessionData: jest.Mock;
const config = new ConfigService({});
const logger = { log: jest.fn(), warn: jest.fn(), error: jest.fn(), debug: jest.fn() };
beforeEach(async () => {
+ purgeSessionData = jest.fn().mockResolvedValue(undefined);
db = new DataSource(
process.env.DATABASE_TYPE === 'postgres'
? {
@@ -102,7 +104,7 @@ describe('Session content lifetime', () => {
});
controls = new SessionEngineControls({
sessionRepository: db.getRepository(Session),
- engineFactory: { purgeSessionData: () => Promise.resolve() },
+ engineFactory: { purgeSessionData },
engines,
sessionErrors: state,
sessionRestrictions: state,
@@ -558,6 +560,36 @@ describe('Session content lifetime', () => {
expect(ordered).toEqual([...ordered].sort((a, b) => a - b));
}, 60000);
+ // The auth directories a session's name keyed before 0.23.5 go with it, unless that name is another session's id.
+ it('purges the directories its name keyed and withholds a name that is another session id', async () => {
+ const repo = db.getRepository(Session);
+ const plain = await repo.save({ name: 'legacy-name', config: {} });
+ const other = await repo.save({ name: 'other', config: {} });
+ const namedAfterOther = await repo.save({ name: other.id, config: {} });
+
+ await controls.delete(plain.id);
+ await controls.delete(namedAfterOther.id);
+
+ expect(purgeSessionData.mock.calls).toEqual([
+ [plain.id, 'legacy-name'],
+ [namedAfterOther.id, undefined],
+ ]);
+ });
+
+ // An import accepts any safe key as a session id, which only SQLite can store.
+ (process.env.DATABASE_TYPE === 'postgres' ? it.skip : it)(
+ 'withholds a name that is the id an import gave another session',
+ async () => {
+ const repo = db.getRepository(Session);
+ await repo.insert({ id: 'imported-key', name: 'imported', config: {} });
+ const namedAfterKey = await repo.save({ name: 'imported-key', config: {} });
+
+ await controls.delete(namedAfterKey.id);
+
+ expect(purgeSessionData.mock.calls).toEqual([[namedAfterKey.id, undefined]]);
+ },
+ );
+
// The SQLite driver resolves every statement within the same tick, so the yield between the batches is the
// only thing that lets timers, requests and engine callbacks run during a long delete. Each batch statement
// queues a macrotask; the next one must find it already run. (On PostgreSQL the driver's own I/O gives the
diff --git a/src/modules/session/session-engine-controls.ts b/src/modules/session/session-engine-controls.ts
index c2f5d3d65..1296bfce2 100644
--- a/src/modules/session/session-engine-controls.ts
+++ b/src/modules/session/session-engine-controls.ts
@@ -7,6 +7,7 @@ import {
ServiceUnavailableException,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
+import { isUUID } from 'class-validator';
import { EntityTarget, In, Repository, DataSource } from 'typeorm';
import { Session, SessionStatus } from './entities/session.entity';
import { Message } from '../message/entities/message.entity';
@@ -930,10 +931,14 @@ export class SessionEngineControls {
// carried before 0.23.5, and the boot migration keeps a legacy one it could not rename. Unless
// 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.
+ // failed lookup withholds the name rather than fail a delete that has already committed. On
+ // PostgreSQL the id column is a uuid, so a name of any other shape is no session's id there, and
+ // looking it up would fail rather than miss.
// 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);
+ const nameIsAnId =
+ (dataSource.options.type !== 'postgres' || isUUID(session.name)) &&
+ (await this.sessionRepository.exists({ where: { id: session.name } }).catch(() => true));
await this.engineFactory.purgeSessionData(id, nameIsAnId ? undefined : session.name);
} catch (error) {
// The session survives, but the write scopes closed above stay retired for good: a bulk batch