Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(control-api): propagate auth middleware errors on grant routes (F4)

Alfredo Lopezbb5ab65fe273
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index 84a7667dc..814af5f23 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.481",
+ "version": "0.1.482",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.481",
+ "version": "0.1.482",
"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.json
index 614262d48..d1aba19f9 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.481",
+ "version": "0.1.482",
"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/workflows/grants.routes.ts b/control-api/src/routes/admin/workflows/grants.routes.ts
index 3fd44fe22..8d517c257 100644
--- a/control-api/src/routes/admin/workflows/grants.routes.ts
+++ b/control-api/src/routes/admin/workflows/grants.routes.ts
@@ -26,13 +26,26 @@ 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)
+ 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 },
@@ -53,7 +66,8 @@ export function createAdminWorkflowGrantRoutes(gateway: K8sGateway): Router {
...WORKFLOW_GRANT_READ_RATE_LIMITS,
bindAdminWorkflowAuth,
asyncHandler(async (req: Request, res: Response) => {
- const caller = adminCaller(req)!
+ const caller = requireBoundAdminCaller(req, res)
+ if (!caller) return
try {
const users = await listWorkflowRecipeGrants(gateway, req.params.ns, req.params.name)
@@ -73,7 +87,8 @@ export function createAdminWorkflowGrantRoutes(gateway: K8sGateway): Router {
...WORKFLOW_GRANT_WRITE_RATE_LIMITS,
bindAdminWorkflowAuth,
asyncHandler(async (req: Request, res: Response) => {
- const caller = adminCaller(req)!
+ const caller = requireBoundAdminCaller(req, res)
+ if (!caller) return
try {
const result = await withAdministrativeTraceContext(req, caller.userId, () =>
@@ -118,7 +133,8 @@ export function createAdminWorkflowGrantRoutes(gateway: K8sGateway): Router {
...WORKFLOW_GRANT_READ_RATE_LIMITS,
bindAdminWorkflowAuth,
asyncHandler(async (req: Request, res: Response) => {
- const caller = adminCaller(req)!
+ const caller = requireBoundAdminCaller(req, res)
+ if (!caller) return
try {
const teams = await listWorkflowRecipeTeamGrants(gateway, req.params.ns, req.params.name)
@@ -138,7 +154,8 @@ export function createAdminWorkflowGrantRoutes(gateway: K8sGateway): Router {
...WORKFLOW_GRANT_WRITE_RATE_LIMITS,
bindAdminWorkflowAuth,
asyncHandler(async (req: Request, res: Response) => {
- const caller = adminCaller(req)!
+ const caller = requireBoundAdminCaller(req, res)
+ if (!caller) return
try {
const result = await withAdministrativeTraceContext(req, caller.userId, () =>
@@ -183,7 +200,8 @@ export function createAdminWorkflowGrantRoutes(gateway: K8sGateway): Router {
...WORKFLOW_GRANT_READ_RATE_LIMITS,
bindAdminWorkflowAuth,
asyncHandler(async (req: Request, res: Response) => {
- const caller = adminCaller(req)!
+ const caller = requireBoundAdminCaller(req, res)
+ if (!caller) return
try {
const teams = await listWorkflowRecipeApprovalTeams({
@@ -207,7 +225,8 @@ export function createAdminWorkflowGrantRoutes(gateway: K8sGateway): Router {
...WORKFLOW_GRANT_WRITE_RATE_LIMITS,
bindAdminWorkflowAuth,
asyncHandler(async (req: Request, res: Response) => {
- const caller = adminCaller(req)!
+ const caller = requireBoundAdminCaller(req, res)
+ if (!caller) return
try {
const result = await withAdministrativeTraceContext(req, caller.userId, () =>
@@ -246,7 +265,8 @@ export function createAdminWorkflowGrantRoutes(gateway: K8sGateway): Router {
...WORKFLOW_GRANT_WRITE_RATE_LIMITS,
bindAdminWorkflowAuth,
asyncHandler(async (req: Request, res: Response) => {
- const caller = adminCaller(req)!
+ const caller = requireBoundAdminCaller(req, res)
+ if (!caller) return
try {
const result = await withAdministrativeTraceContext(req, caller.userId, () =>
diff --git a/control-api/src/routes/workflows/shared/auth.ts b/control-api/src/routes/workflows/shared/auth.ts
index 8a99c0855..3bdc7fb32 100644
--- a/control-api/src/routes/workflows/shared/auth.ts
+++ b/control-api/src/routes/workflows/shared/auth.ts
@@ -48,10 +48,14 @@ export async function requireAdminWorkflowCallerMiddleware(
res: Response,
next: NextFunction
): Promise<void> {
- const caller = await requireAdminWorkflowCaller(req, res)
- if (!caller) return
- req.adminWorkflowCaller = caller
- next()
+ try {
+ const caller = await requireAdminWorkflowCaller(req, res)
+ if (!caller) return
+ req.adminWorkflowCaller = caller
+ next()
+ } catch (error) {
+ next(error)
+ }
}
export async function requireExternalWorkflowCaller(
diff --git a/control-api/test/routes.adminWorkflows.test.ts b/control-api/test/routes.adminWorkflows.test.ts
index 81d7c7cb4..4d7edd1c3 100644
--- a/control-api/test/routes.adminWorkflows.test.ts
+++ b/control-api/test/routes.adminWorkflows.test.ts
@@ -1934,6 +1934,22 @@ describe('routes/admin/workflows', () => {
await request(app).get(`/admin/workflows/${RECIPE_NS}/test-recipe/grants`).expect(401)
})
+ it('forwards admin session lookup failures to the error handler', async () => {
+ mockGetGrantsRateLimiterAllowed()
+ mockLiveAdminSession()
+ mockFindAdminById.mockRejectedValueOnce(new Error('db unavailable'))
+ const app = express()
+ app.use(express.json())
+ app.use(createWorkflowsAdminRouter(gateway as never))
+ app.use((_err, _req, res, _next) => {
+ res.status(500).json({ error: 'internal_error' })
+ })
+ await request(app)
+ .get(`/admin/workflows/${RECIPE_NS}/test-recipe/grants`)
+ .set('Authorization', 'Bearer admin-token')
+ .expect(500)
+ })
+
it('returns opaque 401 for a user-session caller', async () => {
await gateway.createResource('workflowrecipes', VALID_RECIPE as never, RECIPE_NS)
const app = makeApp(gateway)