Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(control-api): meter workflow read, outputs, and trigger cookie paths (F4)
Alfredo Lopeza225d5e5f7ce
diff --git a/control-api/package-lock.json b/control-api/package-lock.jsonindex 9ce83f2ff..75abe2bfa 100644--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-api",- "version": "0.1.483",+ "version": "0.1.484","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.483",+ "version": "0.1.484","dependencies": {"@clerum/display-field": "file:../packages/display-field","@clerum/image-policy": "file:../packages/image-policy",diff --git a/control-api/package.json b/control-api/package.jsonindex 152683624..99ae29e39 100644--- a/control-api/package.json+++ b/control-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-api",- "version": "0.1.483",+ "version": "0.1.484","description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets","main": "dist/main.js","engines": {diff --git a/control-api/src/routes/admin/outputs.ts b/control-api/src/routes/admin/outputs.tsindex 3e7ecc5ec..2143f01ce 100644--- a/control-api/src/routes/admin/outputs.ts+++ b/control-api/src/routes/admin/outputs.ts@@ -9,7 +9,8 @@ import {} from '../../services/workflows/workflowRecipeAccessService.js'import { listWorkflowRunArtifacts } from '../../services/workflows/workflowRunArtifactService.js'import { listCanonicalRuns } from '../../services/workflows/workflowRunReadService.js'-import { requireAdminWorkflowCaller } from '../workflows/shared/auth.js'+import { bindAdminWorkflowAuth, requireBoundAdminWorkflowCaller } from '../workflows/shared/auth.js'+import { adminOutputsReadRateLimits } from '../workflows/shared/rateLimit.js'import { listHostArtifactsForHost } from './hostArtifacts.js'function isVisibleRecipe(resource: Record<string, unknown>): boolean {@@ -31,8 +32,10 @@ export function createAdminOutputsRouter(gateway: K8sGateway): Router {router.get('/admin/outputs',+ ...adminOutputsReadRateLimits(),+ bindAdminWorkflowAuth,asyncHandler(async (req, res) => {- const caller = await requireAdminWorkflowCaller(req, res)+ const caller = requireBoundAdminWorkflowCaller(req, res)if (!caller) returnconst [recipes, hosts] = await Promise.all([diff --git a/control-api/src/routes/admin/workflows/grants.routes.ts b/control-api/src/routes/admin/workflows/grants.routes.tsindex 62d4eb072..9d7e99b6f 100644--- a/control-api/src/routes/admin/workflows/grants.routes.ts+++ b/control-api/src/routes/admin/workflows/grants.routes.ts@@ -1,4 +1,4 @@-import { type NextFunction, type Request, type Response, Router } from 'express'+import { type Request, type Response, Router } from 'express'import { asyncHandler } from '../../../http/asyncHandler.js'import type { K8sGateway } from '../../../k8s.js'import { rootLogger } from '../../../observability/logger.js'@@ -14,8 +14,8 @@ import {revokeWorkflowRecipeApprovalTeam,} from '../../../services/workflows/workflowGrantManagementService.js'import {- type AdminWorkflowAuthedRequest,- requireAdminWorkflowCallerMiddleware,+ bindAdminWorkflowAuth,+ requireBoundAdminWorkflowCaller,} from '../../workflows/shared/auth.js'import {workflowGrantReadRateLimits,@@ -25,27 +25,6 @@ import {const BASE = '/admin/workflows'const logger = rootLogger.child({ module: 'admin-workflow-grants' })-function bindAdminWorkflowAuth(req: Request, res: Response, next: NextFunction): void {- void requireAdminWorkflowCallerMiddleware(req as AdminWorkflowAuthedRequest, res, next).catch(- next- )-}--function adminCaller(req: Request): AdminWorkflowAuthedRequest['adminWorkflowCaller'] {- return (req as AdminWorkflowAuthedRequest).adminWorkflowCaller-}--function requireBoundAdminCaller(req: Request, res: Response) {- const caller = adminCaller(req)- if (!caller) {- if (!res.headersSent) {- res.status(401).json({ error: 'Unauthorized' })- }- return null- }- return caller-}-function withAdministrativeTraceContext<T>(req: Request, operatorSub: string, work: () => T): T {return runWithAdministrativeRequestContext({ operatorSub, requestId: req.correlationId ?? null },@@ -66,7 +45,7 @@ export function createAdminWorkflowGrantRoutes(gateway: K8sGateway): Router {...workflowGrantReadRateLimits(),bindAdminWorkflowAuth,asyncHandler(async (req: Request, res: Response) => {- if (!requireBoundAdminCaller(req, res)) return+ if (!requireBoundAdminWorkflowCaller(req, res)) returntry {const users = await listWorkflowRecipeGrants(gateway, req.params.ns, req.params.name)@@ -86,7 +65,7 @@ export function createAdminWorkflowGrantRoutes(gateway: K8sGateway): Router {...workflowGrantWriteRateLimits(),bindAdminWorkflowAuth,asyncHandler(async (req: Request, res: Response) => {- const caller = requireBoundAdminCaller(req, res)+ const caller = requireBoundAdminWorkflowCaller(req, res)if (!caller) returntry {@@ -132,7 +111,7 @@ export function createAdminWorkflowGrantRoutes(gateway: K8sGateway): Router {...workflowGrantReadRateLimits(),bindAdminWorkflowAuth,asyncHandler(async (req: Request, res: Response) => {- if (!requireBoundAdminCaller(req, res)) return+ if (!requireBoundAdminWorkflowCaller(req, res)) returntry {const teams = await listWorkflowRecipeTeamGrants(gateway, req.params.ns, req.params.name)@@ -152,7 +131,7 @@ export function createAdminWorkflowGrantRoutes(gateway: K8sGateway): Router {...workflowGrantWriteRateLimits(),bindAdminWorkflowAuth,asyncHandler(async (req: Request, res: Response) => {- const caller = requireBoundAdminCaller(req, res)+ const caller = requireBoundAdminWorkflowCaller(req, res)if (!caller) returntry {@@ -198,7 +177,7 @@ export function createAdminWorkflowGrantRoutes(gateway: K8sGateway): Router {...workflowGrantReadRateLimits(),bindAdminWorkflowAuth,asyncHandler(async (req: Request, res: Response) => {- if (!requireBoundAdminCaller(req, res)) return+ if (!requireBoundAdminWorkflowCaller(req, res)) returntry {const teams = await listWorkflowRecipeApprovalTeams({@@ -222,7 +201,7 @@ export function createAdminWorkflowGrantRoutes(gateway: K8sGateway): Router {...workflowGrantWriteRateLimits(),bindAdminWorkflowAuth,asyncHandler(async (req: Request, res: Response) => {- const caller = requireBoundAdminCaller(req, res)+ const caller = requireBoundAdminWorkflowCaller(req, res)if (!caller) returntry {@@ -262,7 +241,7 @@ export function createAdminWorkflowGrantRoutes(gateway: K8sGateway): Router {...workflowGrantWriteRateLimits(),bindAdminWorkflowAuth,asyncHandler(async (req: Request, res: Response) => {- const caller = requireBoundAdminCaller(req, res)+ const caller = requireBoundAdminWorkflowCaller(req, res)if (!caller) returntry {diff --git a/control-api/src/routes/admin/workflows/read.routes.ts b/control-api/src/routes/admin/workflows/read.routes.tsindex dcdb67e9e..c3511645d 100644--- a/control-api/src/routes/admin/workflows/read.routes.ts+++ b/control-api/src/routes/admin/workflows/read.routes.ts@@ -10,7 +10,11 @@ import {isRecipeNamespaceAllowed,} from '../../../services/workflows/workflowRecipeAccessService.js'import { getWorkflowHealth } from '../../../services/workflows/workflowRunReadService.js'-import { requireAdminWorkflowCaller } from '../../workflows/shared/auth.js'+import {+ bindAdminWorkflowAuth,+ requireBoundAdminWorkflowCaller,+} from '../../workflows/shared/auth.js'+import { workflowAdminReadRateLimits } from '../../workflows/shared/rateLimit.js'const BASE = '/admin/workflows'@@ -19,8 +23,10 @@ export function createAdminWorkflowReadRoutes(gateway: K8sGateway): Router {router.get(BASE,+ ...workflowAdminReadRateLimits(),+ bindAdminWorkflowAuth,asyncHandler(async (req: Request, res: Response) => {- const caller = await requireAdminWorkflowCaller(req, res)+ const caller = requireBoundAdminWorkflowCaller(req, res)if (!caller) returnconst recipes = await getAuthorizedRecipeResources(caller, gateway)@@ -30,8 +36,10 @@ export function createAdminWorkflowReadRoutes(gateway: K8sGateway): Router {router.get(`${BASE}/:ns/:name`,+ ...workflowAdminReadRateLimits(),+ bindAdminWorkflowAuth,asyncHandler(async (req: Request, res: Response) => {- const caller = await requireAdminWorkflowCaller(req, res)+ const caller = requireBoundAdminWorkflowCaller(req, res)if (!caller) returnconst { ns, name } = req.params@@ -59,8 +67,10 @@ export function createAdminWorkflowReadRoutes(gateway: K8sGateway): Router {router.get(`${BASE}/:ns/:name/health`,+ ...workflowAdminReadRateLimits(),+ bindAdminWorkflowAuth,asyncHandler(async (req: Request, res: Response) => {- const caller = await requireAdminWorkflowCaller(req, res)+ const caller = requireBoundAdminWorkflowCaller(req, res)if (!caller) returnconst { ns, name } = req.paramsdiff --git a/control-api/src/routes/workflows/shared/auth.ts b/control-api/src/routes/workflows/shared/auth.tsindex 3bdc7fb32..0650a438f 100644--- a/control-api/src/routes/workflows/shared/auth.ts+++ b/control-api/src/routes/workflows/shared/auth.ts@@ -58,6 +58,29 @@ export async function requireAdminWorkflowCallerMiddleware(}}+export function adminWorkflowCaller(+ req: Request+): AdminWorkflowAuthedRequest['adminWorkflowCaller'] {+ return (req as AdminWorkflowAuthedRequest).adminWorkflowCaller+}++export function requireBoundAdminWorkflowCaller(req: Request, res: Response) {+ const caller = adminWorkflowCaller(req)+ if (!caller) {+ if (!res.headersSent) {+ res.status(401).json({ error: 'Unauthorized' })+ }+ return null+ }+ return caller+}++export function bindAdminWorkflowAuth(req: Request, res: Response, next: NextFunction): void {+ void requireAdminWorkflowCallerMiddleware(req as AdminWorkflowAuthedRequest, res, next).catch(+ next+ )+}+export async function requireExternalWorkflowCaller(req: Request,res: Responsediff --git a/control-api/src/routes/workflows/shared/rateLimit.ts b/control-api/src/routes/workflows/shared/rateLimit.tsindex 6c1731ea2..6ec5e340a 100644--- a/control-api/src/routes/workflows/shared/rateLimit.ts+++ b/control-api/src/routes/workflows/shared/rateLimit.ts@@ -7,6 +7,9 @@ import { extractBearerToken } from '../../../utils/extractBearerToken.js'const WORKFLOW_GRANT_READ_PER_MINUTE = 60const WORKFLOW_GRANT_WRITE_PER_MINUTE = 20+const WORKFLOW_ADMIN_READ_PER_MINUTE = 60+const ADMIN_OUTPUTS_READ_PER_MINUTE = 30+const WORKFLOW_TRIGGER_PER_MINUTE = 10/*** Credential surface matched by requireAdminWorkflowCaller: bearer for automation,@@ -86,21 +89,74 @@ export function workflowGrantWriteRateLimits() {return [workflowGrantWriteEdgeRateLimit(), workflowGrantWriteRateLimit()] as const}+function workflowAdminReadEdgeRateLimit() {+ return rateLimit({+ windowMs: 60_000,+ limit: WORKFLOW_ADMIN_READ_PER_MINUTE,+ standardHeaders: 'draft-7',+ legacyHeaders: false,+ keyGenerator: workflowGrantEdgeRateKey('workflow_admin_read_edge'),+ handler: workflowGrantEdgeRateLimitHandler,+ })+}++function adminOutputsReadEdgeRateLimit() {+ return rateLimit({+ windowMs: 60_000,+ limit: ADMIN_OUTPUTS_READ_PER_MINUTE,+ standardHeaders: 'draft-7',+ legacyHeaders: false,+ keyGenerator: workflowGrantEdgeRateKey('admin_outputs_read_edge'),+ handler: workflowGrantEdgeRateLimitHandler,+ })+}++export function workflowAdminReadRateLimits() {+ return [workflowAdminReadEdgeRateLimit(), workflowAdminReadRateLimit()] as const+}++export function adminOutputsReadRateLimits() {+ return [adminOutputsReadEdgeRateLimit(), adminOutputsReadRateLimit()] as const+}++function workflowTriggerRateLimitCredential(req: Request): string | null {+ const bearer = extractBearerToken(req)+ if (bearer) return bearer+ const userSessionToken = String(req.header('x-user-session-token') || '').trim()+ if (userSessionToken) return userSessionToken+ const cookie = readCookie(req, CONTROL_UI_ADMIN_SESSION_COOKIE)+ return cookie || null+}+export function workflowTriggerRateLimit() {return rateLimitMiddleware({bucketType: 'workflow_trigger',- maxPerMinute: 10,+ maxPerMinute: WORKFLOW_TRIGGER_PER_MINUTE,getBucketKey: (req: Request) => {- const bearer = extractBearerToken(req)- const userSessionToken = req.header('x-user-session-token')- const token = bearer || userSessionToken- if (!token) return null- const hash = createHash('sha256').update(token).digest('hex').slice(0, 32)+ const credential = workflowTriggerRateLimitCredential(req)+ if (!credential) return null+ const hash = createHash('sha256').update(credential).digest('hex').slice(0, 32)return `workflow_trigger:${hash}`},})}+function workflowAdminReadRateLimit() {+ return rateLimitMiddleware({+ bucketType: 'workflow_admin_read',+ maxPerMinute: WORKFLOW_ADMIN_READ_PER_MINUTE,+ getBucketKey: hashedAdminWorkflowCredentialBucket('workflow_admin_read'),+ })+}++function adminOutputsReadRateLimit() {+ return rateLimitMiddleware({+ bucketType: 'admin_outputs_read',+ maxPerMinute: ADMIN_OUTPUTS_READ_PER_MINUTE,+ getBucketKey: hashedAdminWorkflowCredentialBucket('admin_outputs_read'),+ })+}+export function workflowGrantReadRateLimit() {return rateLimitMiddleware({bucketType: 'workflow_grants_read',diff --git a/control-api/test/routes.adminWorkflows.test.ts b/control-api/test/routes.adminWorkflows.test.tsindex 4d7edd1c3..f05b79337 100644--- a/control-api/test/routes.adminWorkflows.test.ts+++ b/control-api/test/routes.adminWorkflows.test.ts@@ -534,6 +534,7 @@ describe('routes/admin/workflows', () => {})it('rejects mcp-host-control tokens on the admin workflows mount', async () => {+ mockRateLimiterAllowed()await gateway.createResource('workflowrecipes', VALID_RECIPE as never, RECIPE_NS)const app = makeApp(gateway)@@ -541,8 +542,6 @@ describe('routes/admin/workflows', () => {.get('/admin/workflows').set('Authorization', 'Bearer mcp-host-workflow-control-token').expect(401)-- expect(mockPoolQuery).not.toHaveBeenCalled()})})@@ -1230,6 +1229,7 @@ describe('routes/admin/workflows', () => {it('falls back to archived history for lastRun when no live runs remain', async () => {await gateway.createResource('workflowrecipes', VALID_RECIPE as never, RECIPE_NS)+ mockRateLimiterAllowed()mockPoolQuery// 1. listRunsByRecipe → no live workflow_runs.mockResolvedValueOnce({@@ -1278,6 +1278,7 @@ describe('routes/admin/workflows', () => {it('prefers a newer archived run over an older live run when computing lastRun', async () => {await gateway.createResource('workflowrecipes', VALID_RECIPE as never, RECIPE_NS)+ mockRateLimiterAllowed()mockPoolQuery// 1. listRunsByRecipe → one older live run still present.mockResolvedValueOnce({@@ -2496,15 +2497,16 @@ describe('routes/admin/workflows', () => {}it('rejects revoked administrator JTIs before business logic', async () => {+ mockRateLimiterAllowed()mockLiveAdminSession({ revoked: true })const res = await request(makeApp(gateway)).get('/admin/workflows').set('Authorization', 'Bearer admin-token')expectUnauthorized(res)- expect(mockPoolQuery).not.toHaveBeenCalled()})it('rejects tokens issued before password reset (stale sessionVersion)', async () => {+ mockRateLimiterAllowed()mockLiveAdminSession({ tokenSessionVersion: 1, adminSessionVersion: 2 })const res = await request(makeApp(gateway)).get('/admin/workflows')@@ -2513,6 +2515,7 @@ describe('routes/admin/workflows', () => {})it('rejects disabled administrator accounts', async () => {+ mockRateLimiterAllowed()mockLiveAdminSession({ status: 'disabled' })const res = await request(makeApp(gateway)).get('/admin/workflows')@@ -2521,6 +2524,7 @@ describe('routes/admin/workflows', () => {})it('rejects missing administrator records', async () => {+ mockRateLimiterAllowed()mockLiveAdminSession({ missingAdmin: true })const res = await request(makeApp(gateway)).get('/admin/workflows')@@ -2529,6 +2533,7 @@ describe('routes/admin/workflows', () => {})it('rejects stale HttpOnly cookie sessions', async () => {+ mockRateLimiterAllowed()mockLiveAdminSession({ tokenSessionVersion: 1, adminSessionVersion: 2 })const res = await request(makeApp(gateway)).get('/admin/workflows')@@ -2537,6 +2542,7 @@ describe('routes/admin/workflows', () => {})it('rejects stale bearer tokens even when a valid cookie is also present', async () => {+ mockRateLimiterAllowed()mockVerifyAdminToken.mockImplementation(token => {if (token === 'admin-token-stale') {return { ...ADMIN_CLAIMS, sessionVersion: 1 }@@ -2561,6 +2567,7 @@ describe('routes/admin/workflows', () => {})it('accepts a valid bearer token for admin workflow routes', async () => {+ mockRateLimiterAllowed()mockLiveAdminSession()const res = await request(makeApp(gateway)).get('/admin/workflows')@@ -2578,20 +2585,34 @@ describe('routes/admin/workflows', () => {headers?: Record<string, string>body?: Record<string, unknown>}> = [- { label: 'list workflows', method: 'get', path: '/admin/workflows', auth: 'bearer' },+ {+ label: 'list workflows',+ method: 'get',+ path: '/admin/workflows',+ auth: 'bearer',+ setup: async () => {+ mockRateLimiterAllowed()+ },+ },{label: 'workflow detail',method: 'get',path: `/admin/workflows/${RECIPE_NS}/test-recipe`,auth: 'bearer',- setup: seedRecipe,+ setup: async () => {+ mockRateLimiterAllowed()+ await seedRecipe()+ },},{label: 'workflow health',method: 'get',path: `/admin/workflows/${RECIPE_NS}/test-recipe/health`,auth: 'bearer',- setup: seedRecipe,+ setup: async () => {+ mockRateLimiterAllowed()+ await seedRecipe()+ },},{label: 'workflow trigger',@@ -2732,6 +2753,9 @@ describe('routes/admin/workflows', () => {path: '/admin/outputs',auth: 'cookie',useOutputsApp: true,+ setup: async () => {+ mockRateLimiterAllowed()+ },},]diff --git a/control-api/test/workflows.shared.rateLimit.test.ts b/control-api/test/workflows.shared.rateLimit.test.tsindex b7292ab9b..2953e2d1b 100644--- a/control-api/test/workflows.shared.rateLimit.test.ts+++ b/control-api/test/workflows.shared.rateLimit.test.ts@@ -68,6 +68,31 @@ describe('routes/workflows/shared/rateLimit', () => {await request(app).get('/grants').set('Authorization', 'Bearer bogus-3').expect(429)})+ it('workflowTriggerRateLimit meters cookie-only admin workflow triggers', async () => {+ mockCheckAndIncrement.mockReset()+ mockCheckAndIncrement.mockResolvedValue({+ allowed: true,+ remaining: 9,+ resetMs: Date.now() + 60_000,+ windowStartMs: Date.now(),+ count: 1,+ })++ const { workflowTriggerRateLimit } = await import('../src/routes/workflows/shared/rateLimit.js')+ const app = express()+ app.post('/trigger', workflowTriggerRateLimit(), (_req, res) => {+ res.status(200).json({ ok: true })+ })++ await request(app)+ .post('/trigger')+ .set('Cookie', 'control_ui_admin_session=admin-cookie-token')+ .expect(200)++ expect(mockCheckAndIncrement).toHaveBeenCalledOnce()+ expect(mockCheckAndIncrement.mock.calls[0]?.[0]).toMatch(/^workflow_trigger:[0-9a-f]{32}$/)+ })+it('workflowGrantReadRateLimit meters cookie-only admin workflow callers', async () => {mockCheckAndIncrement.mockReset()mockCheckAndIncrement.mockResolvedValue({