Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): refuse a pairing code for a number another session holds
Yudhi Armyndhariscb220230e663
diff --git a/src/modules/session/session-pairing-code.spec.ts b/src/modules/session/session-pairing-code.spec.tsnew file mode 100644index 000000000..4173dbc73--- /dev/null+++ b/src/modules/session/session-pairing-code.spec.ts@@ -0,0 +1,56 @@+import { ConflictException } from '@nestjs/common';+import { Not } from 'typeorm';+import { SessionService } from './session.service';+import { EngineRegistry } from '../../engine/engine-registry.service';+import { Session, SessionStatus } from './entities/session.entity';++const ID = '11111111-1111-4111-8111-111111111111';+const OTHER_PHONE = '628111000111';++function fixture(holderExists: boolean, ownPhone: string | null = null) {+ const row = { id: ID, name: 'pairing', status: SessionStatus.QR_READY, phone: ownPhone, config: {} } as Session;+ const repository = {+ findOne: jest.fn().mockResolvedValue(row),+ exists: jest.fn().mockResolvedValue(holderExists),+ };+ const state = { attachTo: <T>(value: T): T => value };+ const engine = { requestPairingCode: jest.fn().mockResolvedValue('ABCD1234') };+ const registry = new EngineRegistry();+ registry.set(ID, engine as never);+ const service = new SessionService(+ repository as never,+ {} as never,+ {} as never,+ registry,+ {} as never,+ state as never,+ state as never,+ {} as never,+ {} as never,+ {} as never,+ {} as never,+ );+ return { service, repository, engine };+}++describe('SessionService.requestPairingCode', () => {+ it('refuses a number another session of the gateway is linked with, without asking WhatsApp', async () => {+ const { service, repository, engine } = fixture(true);++ await expect(service.requestPairingCode(ID, OTHER_PHONE)).rejects.toBeInstanceOf(ConflictException);++ expect(repository.exists).toHaveBeenCalledWith({ where: { phone: OTHER_PHONE, id: Not(ID) } });+ expect(engine.requestPairingCode).not.toHaveBeenCalled();+ });++ it('asks for a code for a number no other session holds, including the session own number', async () => {+ const { service, engine } = fixture(false, OTHER_PHONE);++ await expect(service.requestPairingCode(ID, OTHER_PHONE)).resolves.toEqual({+ pairingCode: 'ABCD1234',+ status: SessionStatus.QR_READY,+ });++ expect(engine.requestPairingCode).toHaveBeenCalledWith(OTHER_PHONE);+ });+});diff --git a/src/modules/session/session.controller.ts b/src/modules/session/session.controller.tsindex 562ea6617..a0f42ad26 100644--- a/src/modules/session/session.controller.ts+++ b/src/modules/session/session.controller.ts@@ -516,14 +516,21 @@ export class SessionController {'On the whatsapp-web.js engine, only request a code for a number you are prepared to re-link. A request ' +'for a number that already has a linked session has been observed to end with WhatsApp revoking that ' +'device: the linked session logs out, its credentials are deleted and it falls back to `qr_ready`. ' +- 'Nothing refuses such a request, because the guards check the session state and never the number. ' ++ 'A number another session of this gateway is linked with is refused with `409`; WhatsApp is not asked, ' ++ 'and the gateway cannot see a link made on another gateway or only on the phone. ' +'Baileys was not affected in the same tests. Link by QR when a session of that number must stay up.',})@ApiParam({ name: 'sessionId', description: 'Session ID' })@ApiResponse({ status: 201, description: 'Pairing code generated', type: PairingCodeResponseDto })@ApiResponse({ status: 400, description: 'Session not started or already authenticated' })@ApiResponse({ status: 404, description: 'Session not found' })- @ApiResponse({ status: 409, description: PAIRING_NOT_READY_409 })+ @ApiResponse({+ status: 409,+ description:+ PAIRING_NOT_READY_409 ++ ' Also answered, with nothing sent to WhatsApp, when another session of this gateway is linked with the ' ++ 'requested number: log that session out or delete it first.',+ })@ApiResponse({ status: 503, description: PAIRING_TRANSPORT_503 })async requestPairingCode(@Param('sessionId', ParseUUIDPipe) id: string,diff --git a/src/modules/session/session.service.ts b/src/modules/session/session.service.tsindex dac1ef2ab..379179ffb 100644--- a/src/modules/session/session.service.ts+++ b/src/modules/session/session.service.ts@@ -1339,6 +1339,14 @@ export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicatif (session.status === SessionStatus.READY) {throw new BadRequestException('Session is already authenticated, no pairing needed');}+ // On whatsapp-web.js a code requested for a number that is already linked can make WhatsApp revoke that+ // device. The number is the caller's to choose, so a session this key may not touch could be logged out+ // through this one; a number another session holds is refused. The session's own number is its to re-link.+ if (await this.sessionRepository.exists({ where: { phone: phoneNumber, id: Not(id) } })) {+ throw new ConflictException(+ 'That number is linked to another session. Log that session out or delete it before pairing this one.',+ );+ }const pairingCode = await engine.requestPairingCode(phoneNumber);return { pairingCode, status: session.status };