Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(audit): answer 400 for a repeated or unknown audit filter
Yudhi Armyndharis7a512ff17595
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.mdindex 2201ea108..50332cbd0 100644--- a/docs/06-api-specification.md+++ b/docs/06-api-specification.md@@ -5262,7 +5262,7 @@ List audit-log entries, newest first. API-key lifecycle changes, session lifecycUnlike the other list routes this one is **not** a bare array: `data` is the page and `total` the unpaginated match count. Nullable columns (`apiKeyId`, `sessionId`, `metadata`, `errorMessage`, …) are `null` when the event has no such dimension. `userAgent` and `statusCode` are reserved columns nothing populates, so rows carry `null`. `method` and `path` are populated only where an emitter passes them explicitly (API-key auth failures, key lifecycle changes, queue-board mutations); session/message-flow rows like the sample leave them `null`.-**Errors:** `401` missing/invalid API key · `403` key role below ADMIN+**Errors:** `400` repeated `action` or `severity`, or a value not in its list · `401` missing/invalid API key · `403` key role below ADMIN### 6.4.11 Administration (Infrastructure, Plugins, MCP)diff --git a/openapi.json b/openapi.jsonindex 2bd3421b2..038338e97 100644--- a/openapi.json+++ b/openapi.json@@ -5,6 +5,51 @@"get": {"operationId": "AuditController_findAll","parameters": [+ {+ "name": "sessionId",+ "required": false,+ "in": "query",+ "schema": {+ "type": "string"+ }+ },+ {+ "name": "apiKeyId",+ "required": false,+ "in": "query",+ "schema": {+ "type": "string"+ }+ },+ {+ "name": "limit",+ "required": false,+ "in": "query",+ "schema": {+ "type": "number"+ }+ },+ {+ "name": "offset",+ "required": false,+ "in": "query",+ "schema": {+ "type": "number"+ }+ },+ {+ "name": "severity",+ "required": false,+ "in": "query",+ "schema": {+ "enum": [+ "info",+ "warn",+ "error"+ ],+ "type": "string"+ }+ },{"name": "action","required": false,@@ -54,51 +99,6 @@],"type": "string"}- },- {- "name": "severity",- "required": false,- "in": "query",- "schema": {- "enum": [- "info",- "warn",- "error"- ],- "type": "string"- }- },- {- "name": "sessionId",- "required": false,- "in": "query",- "schema": {- "type": "string"- }- },- {- "name": "apiKeyId",- "required": false,- "in": "query",- "schema": {- "type": "string"- }- },- {- "name": "limit",- "required": false,- "in": "query",- "schema": {- "type": "number"- }- },- {- "name": "offset",- "required": false,- "in": "query",- "schema": {- "type": "number"- }}],"responses": {@@ -111,6 +111,9 @@}}}+ },+ "400": {+ "description": "action or severity is repeated or not one of the listed values"}},"summary": "List audit logs with optional filters",diff --git a/src/modules/audit/audit.controller.spec.ts b/src/modules/audit/audit.controller.spec.tsindex 1e457bd47..09901f11b 100644--- a/src/modules/audit/audit.controller.spec.ts+++ b/src/modules/audit/audit.controller.spec.ts@@ -1,5 +1,8 @@+import { BadRequestException } from '@nestjs/common';import { Reflector } from '@nestjs/core';import { AuditController } from './audit.controller';+import { AuditService } from './audit.service';+import { AuditAction, AuditSeverity } from './entities/audit-log.entity';import { REQUIRED_ROLE_KEY } from '../auth/decorators/auth.decorators';import { ApiKeyRole } from '../auth/entities/api-key.entity';@@ -15,3 +18,39 @@ describe('AuditController access control', () => {expect(role).toBe(ApiKeyRole.ADMIN);});});++// ?action=a&action=b reaches the handler as an array (an enum-typed query param is left alone by the+// global pipe) and bound as one SQL parameter it failed with a driver error, a 500. An unknown value+// silently matched nothing. Both are a malformed filter, so both answer 400.+describe('AuditController filter validation', () => {+ const findAll = jest.fn().mockResolvedValue({ data: [], total: 0 });+ const controller = new AuditController({ findAll } as unknown as AuditService);++ beforeEach(() => findAll.mockClear());++ it('rejects a repeated action or severity with 400', async () => {+ await expect(+ controller.findAll(undefined, [AuditAction.SESSION_CREATED, AuditAction.SESSION_DELETED] as never),+ ).rejects.toBeInstanceOf(BadRequestException);+ await expect(+ controller.findAll(undefined, undefined, [AuditSeverity.INFO, AuditSeverity.WARN] as never),+ ).rejects.toBeInstanceOf(BadRequestException);+ expect(findAll).not.toHaveBeenCalled();+ });++ it('rejects an unknown action or severity with 400', async () => {+ await expect(controller.findAll(undefined, 'nope' as never)).rejects.toBeInstanceOf(BadRequestException);+ await expect(controller.findAll(undefined, undefined, 'fatal' as never)).rejects.toBeInstanceOf(+ BadRequestException,+ );+ expect(findAll).not.toHaveBeenCalled();+ });++ it('passes a known action and severity through', async () => {+ await controller.findAll(undefined, AuditAction.SESSION_CREATED, AuditSeverity.WARN);+ expect(findAll).toHaveBeenCalledWith(+ { action: AuditAction.SESSION_CREATED, severity: AuditSeverity.WARN },+ undefined,+ );+ });+});diff --git a/src/modules/audit/audit.controller.ts b/src/modules/audit/audit.controller.tsindex afd13dcf9..635b2e55f 100644--- a/src/modules/audit/audit.controller.ts+++ b/src/modules/audit/audit.controller.ts@@ -1,4 +1,4 @@-import { Controller, Get, Query } from '@nestjs/common';+import { BadRequestException, Controller, Get, Query } from '@nestjs/common';import { ApiTags, ApiOperation, ApiResponse, ApiQuery } from '@nestjs/swagger';import { AuditListResponseDto } from './dto/audit-response.dto';import { AuditService, AuditQueryOptions } from './audit.service';@@ -21,18 +21,30 @@ export class AuditController {@ApiQuery({ name: 'limit', required: false, type: Number })@ApiQuery({ name: 'offset', required: false, type: Number })@ApiResponse({ status: 200, description: 'Paginated list of audit logs', type: AuditListResponseDto })+ @ApiResponse({ status: 400, description: 'action or severity is repeated or not one of the listed values' })async findAll(@CurrentApiKey() apiKey?: ApiKey,- @Query('action') action?: AuditAction,- @Query('severity') severity?: AuditSeverity,+ @Query('action') action?: AuditAction | AuditAction[],+ @Query('severity') severity?: AuditSeverity | AuditSeverity[],@Query('sessionId') sessionId?: string,@Query('apiKeyId') apiKeyId?: string,@Query('limit') limit?: string,@Query('offset') offset?: string,): Promise<{ data: AuditLog[]; total: number }> {+ // The global pipe leaves these enum-typed params alone, so ?action=a&action=b arrives as an array,+ // which the driver rejects as a 500, and an unknown value would silently match nothing. An empty+ // value still means no filter.+ if (action && !(Object.values(AuditAction) as unknown[]).includes(action)) {+ throw new BadRequestException('action must be a single value from the AuditAction list');+ }+ if (severity && !(Object.values(AuditSeverity) as unknown[]).includes(severity)) {+ throw new BadRequestException(+ `severity must be a single value, one of: ${Object.values(AuditSeverity).join(', ')}`,+ );+ }const options: AuditQueryOptions = {};- if (action) options.action = action;- if (severity) options.severity = severity;+ if (action) options.action = action as AuditAction;+ if (severity) options.severity = severity as AuditSeverity;if (sessionId) options.sessionId = sessionId;if (apiKeyId) options.apiKeyId = apiKeyId;if (limit) options.limit = parseInt(limit, 10);