Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

test(security): bind the queue-dashboard audit attribution and its mount order

Yudhi Armyndharis365e4644db12
diff --git a/src/common/security/bull-board-auth.middleware.spec.ts b/src/common/security/bull-board-auth.middleware.spec.ts
index da0de2cf4..adb380a5b 100644
--- a/src/common/security/bull-board-auth.middleware.spec.ts
+++ b/src/common/security/bull-board-auth.middleware.spec.ts
@@ -7,6 +7,9 @@ import { ApiKeyRole } from '../../modules/auth/entities/api-key.entity';
import { AuditService } from '../../modules/audit/audit.service';
import { AuditAction } from '../../modules/audit/entities/audit-log.entity';
import { KeyRateLimiter } from '../../modules/mcp/mcp-rate-limit';
+import * as fs from 'fs';
+import * as path from 'path';
+import { runWithRequestId, getRequestActor } from '../services/request-context';
const res = {} as Response;
@@ -389,3 +392,60 @@ describe('BullBoardAuthMiddleware audit trail', () => {
expect(next).toHaveBeenCalledWith(expect.any(UnauthorizedException));
});
});
+
+/**
+ * The queue dashboard is the most privileged UI in the deployment, and its two post-authentication
+ * denials wrote an audit row with no credential on it — the same gap ApiKeyGuard had. The fix stamps
+ * the resolved actor before those checks, and it works only because requestContextMiddleware is
+ * installed ahead of this mount in main.ts. Both halves are pinned here: the stamp, and the ordering
+ * it silently depends on.
+ */
+describe('a queue-dashboard denial is attributable to the credential', () => {
+ const denialActor = async (apiKey: Record<string, unknown>, hasPermission: boolean) => {
+ const authService = {
+ validateApiKey: jest.fn().mockResolvedValue(apiKey),
+ hasPermission: jest.fn().mockReturnValue(hasPermission),
+ };
+ const mw = new BullBoardAuthMiddleware(
+ authService as unknown as AuthService,
+ { get: () => undefined } as unknown as ConfigService,
+ );
+ const req = {
+ headers: { 'x-api-key': 'k' },
+ query: {},
+ method: 'GET',
+ originalUrl: '/api/admin/queues',
+ ip: '203.0.113.44',
+ socket: { remoteAddress: '203.0.113.44' },
+ } as unknown as Request;
+ const next = jest.fn();
+ let actor: ReturnType<typeof getRequestActor>;
+ await runWithRequestId('req-1', async () => {
+ await mw.use(req, {} as Response, next);
+ actor = getRequestActor();
+ });
+ expect(next).toHaveBeenCalledWith(expect.any(ForbiddenException));
+ return actor;
+ };
+
+ it('stamps the key when the role is insufficient', async () => {
+ const actor = await denialActor({ id: 'key-1', name: 'Ops key', allowedSessions: [] }, false);
+ expect(actor).toMatchObject({ apiKeyId: 'key-1', apiKeyName: 'Ops key' });
+ });
+
+ it('stamps the key when a session-scoped key is refused', async () => {
+ const actor = await denialActor({ id: 'key-2', name: 'Tenant A', allowedSessions: ['s1'] }, true);
+ expect(actor).toMatchObject({ apiKeyId: 'key-2', apiKeyName: 'Tenant A' });
+ });
+
+ // The stamp is a no-op outside a request-context scope, so the mount order in main.ts is load
+ // bearing. Nothing else binds it; a reordering would silently return the null rows this fixed.
+ it('keeps requestContextMiddleware installed ahead of the queue-dashboard mount', () => {
+ const main = fs.readFileSync(path.join(__dirname, '..', '..', 'main.ts'), 'utf8');
+ const contextAt = main.indexOf('app.use(requestContextMiddleware)');
+ const boardAt = main.indexOf('new BullBoardAuthMiddleware');
+ expect(contextAt).toBeGreaterThan(-1);
+ expect(boardAt).toBeGreaterThan(-1);
+ expect(contextAt).toBeLessThan(boardAt);
+ });
+});