Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(security): require ADMIN for the audit log and global stats reads (#330)
Yudhi Armyndharis240b243a93fa
diff --git a/src/modules/audit/audit.controller.spec.ts b/src/modules/audit/audit.controller.spec.tsnew file mode 100644index 000000000..cb5032a3b--- /dev/null+++ b/src/modules/audit/audit.controller.spec.ts@@ -0,0 +1,17 @@+import { Reflector } from '@nestjs/core';+import { AuditController } from './audit.controller';+import { REQUIRED_ROLE_KEY } from '../auth/decorators/auth.decorators';+import { ApiKeyRole } from '../auth/entities/api-key.entity';++// F-09 — the audit log is a security event trail; without a role gate any active key (incl. a+// read-only VIEWER) could read the entire trail. It must require ADMIN, matching the infra+// secrets/export routes.+describe('AuditController access control (F-09)', () => {+ it('GET /audit requires the ADMIN role', () => {+ // Read the handler off the prototype as an opaque object so the lint unbound-method rule+ // (which guards against detached method `this`) doesn't fire on a metadata-only lookup.+ const proto = AuditController.prototype as unknown as Record<string, object>;+ const role = new Reflector().get<ApiKeyRole | undefined>(REQUIRED_ROLE_KEY, proto.findAll);+ expect(role).toBe(ApiKeyRole.ADMIN);+ });+});diff --git a/src/modules/audit/audit.controller.ts b/src/modules/audit/audit.controller.tsindex 39b7a8742..5c1388673 100644--- a/src/modules/audit/audit.controller.ts+++ b/src/modules/audit/audit.controller.ts@@ -2,6 +2,8 @@ import { Controller, Get, Query } from '@nestjs/common';import { ApiTags, ApiOperation, ApiResponse, ApiQuery } from '@nestjs/swagger';import { AuditService, AuditQueryOptions } from './audit.service';import { AuditLog, AuditAction, AuditSeverity } from './entities/audit-log.entity';+import { RequireRole } from '../auth/decorators/auth.decorators';+import { ApiKeyRole } from '../auth/entities/api-key.entity';@ApiTags('audit')@Controller('audit')@@ -9,6 +11,7 @@ export class AuditController {constructor(private readonly auditService: AuditService) {}@Get()+ @RequireRole(ApiKeyRole.ADMIN)@ApiOperation({ summary: 'List audit logs with optional filters' })@ApiQuery({ name: 'action', required: false, enum: AuditAction })@ApiQuery({ name: 'severity', required: false, enum: AuditSeverity })diff --git a/src/modules/stats/stats.controller.spec.ts b/src/modules/stats/stats.controller.spec.tsnew file mode 100644index 000000000..274a92a84--- /dev/null+++ b/src/modules/stats/stats.controller.spec.ts@@ -0,0 +1,25 @@+import { Reflector } from '@nestjs/core';+import { StatsController } from './stats.controller';+import { REQUIRED_ROLE_KEY } from '../auth/decorators/auth.decorators';+import { ApiKeyRole } from '../auth/entities/api-key.entity';++// F-09 — the global stats routes aggregate across EVERY session and carry no scope param, so the+// ApiKeyGuard's allowedSessions fence doesn't apply. They must require ADMIN so a VIEWER / a+// session-restricted key can't read cross-tenant activity. The per-session route is left ungated:+// it carries :sessionId, so the guard already scopes a restricted key to its own sessions.+describe('StatsController access control (F-09)', () => {+ const reflector = new Reflector();+ // Opaque-object view of the prototype so the lint unbound-method rule doesn't fire on a+ // metadata-only handler lookup.+ const proto = StatsController.prototype as unknown as Record<string, object>;++ it.each(['getOverview', 'getMessageStats'] as const)('global stats route %s requires ADMIN', method => {+ const role = reflector.get<ApiKeyRole | undefined>(REQUIRED_ROLE_KEY, proto[method]);+ expect(role).toBe(ApiKeyRole.ADMIN);+ });++ it('per-session stats is not globally ADMIN-gated (scope-enforced by its :sessionId param)', () => {+ const role = reflector.get<ApiKeyRole | undefined>(REQUIRED_ROLE_KEY, proto.getSessionStats);+ expect(role).toBeUndefined();+ });+});diff --git a/src/modules/stats/stats.controller.ts b/src/modules/stats/stats.controller.tsindex edde5446d..b6532be40 100644--- a/src/modules/stats/stats.controller.ts+++ b/src/modules/stats/stats.controller.ts@@ -2,19 +2,25 @@ import { Controller, Get, Param, Query } from '@nestjs/common';import { ApiTags, ApiOperation } from '@nestjs/swagger';import { StatsService } from './stats.service';import { StatsQueryDto } from './dto/stats-query.dto';+import { RequireRole } from '../auth/decorators/auth.decorators';+import { ApiKeyRole } from '../auth/entities/api-key.entity';@ApiTags('Statistics')@Controller('stats')export class StatsController {constructor(private readonly statsService: StatsService) {}+ // Global, cross-session aggregates with no scope param — ADMIN-only so a VIEWER / session-+ // restricted key can't read cross-tenant activity. (Per-session stats below stays scope-gated.)@Get('overview')+ @RequireRole(ApiKeyRole.ADMIN)@ApiOperation({ summary: 'Get overall statistics' })async getOverview() {return this.statsService.getOverview();}@Get('messages')+ @RequireRole(ApiKeyRole.ADMIN)@ApiOperation({ summary: 'Get message statistics with time series' })async getMessageStats(@Query() query: StatsQueryDto) {return this.statsService.getMessageStats(query.period || '24h');