Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): scope GET /sessions to the calling key's allowedSessions (#324)
Yudhi Armyndharis6e8068f63806
diff --git a/src/modules/session/session.controller.ts b/src/modules/session/session.controller.tsindex 062ac2e1e..822b0f0d6 100644--- a/src/modules/session/session.controller.ts+++ b/src/modules/session/session.controller.ts@@ -15,8 +15,8 @@ import { Session } from './entities/session.entity';import { ChatSummary } from '../../engine/interfaces/whatsapp-engine.interface';import { AuditService } from '../audit/audit.service';import { AuditAction } from '../audit/entities/audit-log.entity';-import { RequireRole } from '../auth/decorators/auth.decorators';-import { ApiKeyRole } from '../auth/entities/api-key.entity';+import { RequireRole, CurrentApiKey } from '../auth/decorators/auth.decorators';+import { ApiKey, ApiKeyRole } from '../auth/entities/api-key.entity';@ApiTags('sessions')@Controller('sessions')@@ -67,8 +67,10 @@ export class SessionController {description: 'List of sessions',type: [SessionResponseDto],})- async findAll(): Promise<SessionResponseDto[]> {- const sessions = await this.sessionService.findAll();+ async findAll(@CurrentApiKey() apiKey?: ApiKey): Promise<SessionResponseDto[]> {+ // Scope to the key's allowedSessions so a session-restricted key cannot enumerate every+ // session. A null/empty allowlist (e.g. ADMIN) still lists all.+ const sessions = await this.sessionService.findAll(apiKey?.allowedSessions);return sessions.map(s => this.transformSession(s));}diff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.tsindex 3201ca078..a0bbc172c 100644--- a/src/modules/session/session.service.spec.ts+++ b/src/modules/session/session.service.spec.ts@@ -1,6 +1,6 @@import { Test, TestingModule } from '@nestjs/testing';import { getRepositoryToken, getDataSourceToken } from '@nestjs/typeorm';-import { Repository, DataSource } from 'typeorm';+import { Repository, DataSource, In } from 'typeorm';import { NotFoundException, ConflictException, BadRequestException } from '@nestjs/common';import { SessionService } from './session.service';import { Session, SessionStatus } from './entities/session.entity';@@ -185,6 +185,28 @@ describe('SessionService', () => {expect(result).toHaveLength(2);expect(repository.find).toHaveBeenCalledWith({ order: { createdAt: 'DESC' } });});++ it('scopes results to a session-restricted key (F-02)', async () => {+ (repository.find as jest.Mock).mockResolvedValue([]);++ await service.findAll(['sess-1', 'sess-2']);++ expect(repository.find).toHaveBeenCalledWith({+ where: { id: In(['sess-1', 'sess-2']) },+ order: { createdAt: 'DESC' },+ });+ });++ it('returns all sessions for an unrestricted key (null/empty allowlist)', async () => {+ (repository.find as jest.Mock).mockResolvedValue([]);++ await service.findAll(null);+ await service.findAll([]);++ expect(repository.find).toHaveBeenCalledTimes(2);+ expect(repository.find).toHaveBeenNthCalledWith(1, { order: { createdAt: 'DESC' } });+ expect(repository.find).toHaveBeenNthCalledWith(2, { order: { createdAt: 'DESC' } });+ });});describe('findOne', () => {diff --git a/src/modules/session/session.service.ts b/src/modules/session/session.service.tsindex 476d0a14c..569aa4473 100644--- a/src/modules/session/session.service.ts+++ b/src/modules/session/session.service.ts@@ -8,7 +8,7 @@ import {OnApplicationBootstrap,} from '@nestjs/common';import { InjectRepository, InjectDataSource } from '@nestjs/typeorm';-import { Repository, In, Not, IsNull, DataSource } from 'typeorm';+import { Repository, In, Not, IsNull, DataSource, FindManyOptions } from 'typeorm';import { Session, SessionStatus } from './entities/session.entity';import { Message, MessageDirection, MessageStatus } from '../message/entities/message.entity';import { CreateSessionDto } from './dto';@@ -211,10 +211,15 @@ export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicatreturn saved;}- async findAll(): Promise<Session[]> {- const sessions = await this.sessionRepository.find({- order: { createdAt: 'DESC' },- });+ async findAll(allowedSessions?: string[] | null): Promise<Session[]> {+ // A session-restricted key only lists its own sessions; an unrestricted key (null/empty+ // allowlist) lists all — mirroring the ApiKeyGuard allowedSessions model so a scoped key+ // cannot enumerate every session through this aggregate route.+ const options: FindManyOptions<Session> = { order: { createdAt: 'DESC' } };+ if (allowedSessions && allowedSessions.length > 0) {+ options.where = { id: In(allowedSessions) };+ }+ const sessions = await this.sessionRepository.find(options);return sessions.map(session => this.attachLastError(session));}