Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): don't resurrect a session stopped/deleted during start() (#405)
Yudhi Armyndharis6755299f227f
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 81dfc38ba..faaea010a 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -52,6 +52,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0string was treated as a URL only with a lowercase `http://`/`https://` prefix, so a mixed-casescheme (e.g. `HTTPS://…`) was mistaken for base64 instead of being fetched through the SSRF-guardedpath — diverging from the Baileys engine. Both engines now use the same case-insensitive check. (#404)+- **A session stopped or deleted mid-startup is no longer resurrected to `READY`.** If `stop`/`delete`+ landed while `start()` was awaiting the engine's `initialize()`, the freshly-created engine was left+ registered and running. `start()` now re-checks the stopping flag after initialization and tears the+ engine down (mirroring the existing reconnect guard), so a concurrent stop/delete wins. (#405)### Securitydiff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.tsindex 42e528e61..e3915f058 100644--- a/src/modules/session/session.service.spec.ts+++ b/src/modules/session/session.service.spec.ts@@ -1213,6 +1213,26 @@ describe('SessionService', () => {});});+ describe('start() concurrent stop/delete guard', () => {+ it('tears down the just-initialized engine if a stop/delete lands during start() (no resurrection to READY)', async () => {+ const session = createMockSession();+ (repository.findOne as jest.Mock).mockResolvedValue(session);+ (repository.update as jest.Mock).mockResolvedValue({ affected: 1 });++ // Simulate a concurrent stop()/delete() landing WHILE engine.initialize() is in flight.+ mockEngine.initialize.mockImplementationOnce(() => {+ (service as unknown as { stoppingSessions: Set<string> }).stoppingSessions.add('sess-uuid-1');+ return Promise.resolve();+ });++ await service.start('sess-uuid-1');++ // The engine registered during init must be torn down + removed, not left READY.+ expect(mockEngine.destroy).toHaveBeenCalled();+ expect(service.getEngine('sess-uuid-1')).toBeUndefined();+ });+ });+// ── sendSeen (markChatRead) ───────────────────────────────────────describe('sendSeen', () => {diff --git a/src/modules/session/session.service.ts b/src/modules/session/session.service.tsindex a415855a1..9b9c20e8f 100644--- a/src/modules/session/session.service.ts+++ b/src/modules/session/session.service.ts@@ -402,6 +402,17 @@ export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicatthis.reconnectStates.set(id, { attempts: 0, timer: null, maxAttempts, baseDelay });await this.initializeEngine(id, session);++ // A stop()/delete() may have landed while we awaited engine.initialize() — if so, tear down the+ // engine we just registered so the session isn't resurrected to READY (mirrors the post-init+ // guard in executeReconnect; initialize()'s callbacks can also fire async after this returns).+ if (this.stoppingSessions.has(id)) {+ const resurrected = this.engines.get(id);+ if (resurrected) {+ await this.teardownEngineSafely(id, resurrected, e => e.destroy(), 'destroy');+ this.engines.delete(id);+ }+ }return this.findOne(id);} finally {this.initializingSessions.delete(id);