From c46f0909f36c8a70e8ee04c6cf40abe6d2b83c82 Mon Sep 17 00:00:00 2001 From: devops Date: Sat, 24 Oct 2026 04:01:18 +0000 Subject: [PATCH] Fix orchestration actor role normalization at HTTP boundary --- .../orchestration/orchestration.controller.ts | 11 +++++-- .../prepare-workflow.http.spec.ts | 32 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/backend/src/orchestration/orchestration.controller.ts b/backend/src/orchestration/orchestration.controller.ts index 8acf3079..3f95ee6d 100644 --- a/backend/src/orchestration/orchestration.controller.ts +++ b/backend/src/orchestration/orchestration.controller.ts @@ -97,13 +97,20 @@ export class OrchestrationController { ) {} private actor(req: any): WorkflowIdentity { const id = req?.user?.id ?? req?.user?.sub; + // Match RolesGuard aliases without promoting an admin to a reviewer. + const normalized = typeof req?.user?.role === 'string' + ? req.user.role.toLowerCase().replace(/[_-]/g, '') + : ''; + const role = normalized === 'admin' ? 'Admin' + : ['superadmin', 'adminsuper'].includes(normalized) ? 'SUPER_ADMIN' + : undefined; if ( typeof id !== 'string' || !id || - !['Admin', 'SUPER_ADMIN'].includes(req?.user?.role) + !role ) throw new ForbiddenException('Authenticated workflow identity required'); - return { id, role: req.user.role }; + return { id, role }; } private safe(row: any, req: any) { const view = project(row); diff --git a/backend/src/orchestration/prepare-workflow.http.spec.ts b/backend/src/orchestration/prepare-workflow.http.spec.ts index 5fa16b40..795f6382 100644 --- a/backend/src/orchestration/prepare-workflow.http.spec.ts +++ b/backend/src/orchestration/prepare-workflow.http.spec.ts @@ -68,6 +68,38 @@ describe('bounded workflow HTTP contracts', () => { afterAll(async () => { await app.close(); }); + it.each([ + ['ADMIN', 'Admin'], + ['admin', 'Admin'], + ['Admin', 'Admin'], + ['SUPER_ADMIN', 'SUPER_ADMIN'], + ['super-admin', 'SUPER_ADMIN'], + ['SuperAdmin', 'SUPER_ADMIN'], + ['ADMIN_SUPER', 'SUPER_ADMIN'], + ])('canonicalizes guard-supported %s on task reads to %s', async (role, canonical) => { + for (const path of ['tasks', 'tasks/t']) { + workflow.project.mockClear(); + const response = await request(app.getHttpServer()) + .get('/admin/orchestration/' + path) + .set('Authorization', role) + .expect(200); + expect(workflow.project).toHaveBeenCalledWith(t, { + id: 'verified-subject', role: canonical, + }); + const task = Array.isArray(response.body) ? response.body[0] : response.body; + expect(task.availableActions).toEqual(['SUBMIT_GATE1']); + } + }); + it('denies ordinary users before workflow projection', async () => { + workflow.project.mockClear(); + for (const path of ['tasks', 'tasks/t', 'overview']) { + await request(app.getHttpServer()) + .get('/admin/orchestration/' + path) + .set('Authorization', 'User_PetOwner') + .expect(403); + } + expect(workflow.project).not.toHaveBeenCalled(); + }); it('accepts existing proposal DTO, derives actor from JWT sub and returns projected task', async () => { const dto = { key: 'k',