Skip to content

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

TypeScript

fix(control-api): ip-only grant edge limiter and lazy limiter stacks (F4)

Alfredo Lopez91fcee8427a8
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index 814af5f23..9ce83f2ff 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.482",
+ "version": "0.1.483",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.482",
+ "version": "0.1.483",
"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 d1aba19f9..152683624 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.482",
+ "version": "0.1.483",
"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 8d517c257..62d4eb072 100644
--- a/control-api/src/routes/admin/workflows/grants.routes.ts
+++ b/control-api/src/routes/admin/workflows/grants.routes.ts
@@ -18,8 +18,8 @@ import {
requireAdminWorkflowCallerMiddleware,
} from '../../workflows/shared/auth.js'
import {
- WORKFLOW_GRANT_READ_RATE_LIMITS,
- WORKFLOW_GRANT_WRITE_RATE_LIMITS,
+ workflowGrantReadRateLimits,
+ workflowGrantWriteRateLimits,
} from '../../workflows/shared/rateLimit.js'
const BASE = '/admin/workflows'
@@ -63,11 +63,10 @@ export function createAdminWorkflowGrantRoutes(gateway: K8sGateway): Router {
router.get(
`${BASE}/:ns/:name/grants`,
- ...WORKFLOW_GRANT_READ_RATE_LIMITS,
+ ...workflowGrantReadRateLimits(),
bindAdminWorkflowAuth,
asyncHandler(async (req: Request, res: Response) => {
- const caller = requireBoundAdminCaller(req, res)
- if (!caller) return
+ if (!requireBoundAdminCaller(req, res)) return
try {
const users = await listWorkflowRecipeGrants(gateway, req.params.ns, req.params.name)
@@ -84,7 +83,7 @@ export function createAdminWorkflowGrantRoutes(gateway: K8sGateway): Router {
router.put(
`${BASE}/:ns/:name/grants`,
- ...WORKFLOW_GRANT_WRITE_RATE_LIMITS,
+ ...workflowGrantWriteRateLimits(),
bindAdminWorkflowAuth,
asyncHandler(async (req: Request, res: Response) => {
const caller = requireBoundAdminCaller(req, res)
@@ -130,11 +129,10 @@ export function createAdminWorkflowGrantRoutes(gateway: K8sGateway): Router {
router.get(
`${BASE}/:ns/:name/team-grants`,
- ...WORKFLOW_GRANT_READ_RATE_LIMITS,
+ ...workflowGrantReadRateLimits(),
bindAdminWorkflowAuth,
asyncHandler(async (req: Request, res: Response) => {
- const caller = requireBoundAdminCaller(req, res)
- if (!caller) return
+ if (!requireBoundAdminCaller(req, res)) return
try {
const teams = await listWorkflowRecipeTeamGrants(gateway, req.params.ns, req.params.name)
@@ -151,7 +149,7 @@ export function createAdminWorkflowGrantRoutes(gateway: K8sGateway): Router {
router.put(
`${BASE}/:ns/:name/team-grants`,
- ...WORKFLOW_GRANT_WRITE_RATE_LIMITS,
+ ...workflowGrantWriteRateLimits(),
bindAdminWorkflowAuth,
asyncHandler(async (req: Request, res: Response) => {
const caller = requireBoundAdminCaller(req, res)
@@ -197,11 +195,10 @@ export function createAdminWorkflowGrantRoutes(gateway: K8sGateway): Router {
router.get(
`/admin/workflow-recipes/:ns/:name/allowed-teams`,
- ...WORKFLOW_GRANT_READ_RATE_LIMITS,
+ ...workflowGrantReadRateLimits(),
bindAdminWorkflowAuth,
asyncHandler(async (req: Request, res: Response) => {
- const caller = requireBoundAdminCaller(req, res)
- if (!caller) return
+ if (!requireBoundAdminCaller(req, res)) return
try {
const teams = await listWorkflowRecipeApprovalTeams({
@@ -222,7 +219,7 @@ export function createAdminWorkflowGrantRoutes(gateway: K8sGateway): Router {
router.put(
`/admin/workflow-recipes/:ns/:name/allowed-teams/:teamId`,
- ...WORKFLOW_GRANT_WRITE_RATE_LIMITS,
+ ...workflowGrantWriteRateLimits(),
bindAdminWorkflowAuth,
asyncHandler(async (req: Request, res: Response) => {
const caller = requireBoundAdminCaller(req, res)
@@ -262,7 +259,7 @@ export function createAdminWorkflowGrantRoutes(gateway: K8sGateway): Router {
router.delete(
`/admin/workflow-recipes/:ns/:name/allowed-teams/:teamId`,
- ...WORKFLOW_GRANT_WRITE_RATE_LIMITS,
+ ...workflowGrantWriteRateLimits(),
bindAdminWorkflowAuth,
asyncHandler(async (req: Request, res: Response) => {
const caller = requireBoundAdminCaller(req, res)
diff --git a/control-api/src/routes/workflows/shared/rateLimit.ts b/control-api/src/routes/workflows/shared/rateLimit.ts
index ba8c8b8de..6c1731ea2 100644
--- a/control-api/src/routes/workflows/shared/rateLimit.ts
+++ b/control-api/src/routes/workflows/shared/rateLimit.ts
@@ -28,12 +28,16 @@ function hashedAdminWorkflowCredentialBucket(prefix: string) {
}
}
+/**
+ * Ingress-style edge key: IP-only so unverified bearer rotation cannot evade the
+ * backstop. Per-credential quotas remain on the PG limiter downstream.
+ */
+export function workflowGrantEdgeRateLimitKey(prefix: string, req: Request): string {
+ return `${prefix}:ip:${ipKeyGenerator(req.ip ?? 'unknown')}`
+}
+
function workflowGrantEdgeRateKey(prefix: string) {
- return (req: Request): string => {
- const bucket = hashedAdminWorkflowCredentialBucket(prefix)(req)
- if (bucket) return bucket
- return `${prefix}:ip:${ipKeyGenerator(req.ip ?? 'unknown')}`
- }
+ return (req: Request): string => workflowGrantEdgeRateLimitKey(prefix, req)
}
function workflowGrantEdgeRateLimitHandler(_req: Request, res: Response): void {
@@ -74,15 +78,13 @@ export function workflowGrantWriteEdgeRateLimit() {
})
}
-export const WORKFLOW_GRANT_READ_RATE_LIMITS = [
- workflowGrantReadEdgeRateLimit(),
- workflowGrantReadRateLimit(),
-] as const
+export function workflowGrantReadRateLimits() {
+ return [workflowGrantReadEdgeRateLimit(), workflowGrantReadRateLimit()] as const
+}
-export const WORKFLOW_GRANT_WRITE_RATE_LIMITS = [
- workflowGrantWriteEdgeRateLimit(),
- workflowGrantWriteRateLimit(),
-] as const
+export function workflowGrantWriteRateLimits() {
+ return [workflowGrantWriteEdgeRateLimit(), workflowGrantWriteRateLimit()] as const
+}
export function workflowTriggerRateLimit() {
return rateLimitMiddleware({
diff --git a/control-api/test/workflows.shared.rateLimit.test.ts b/control-api/test/workflows.shared.rateLimit.test.ts
index e1f03d306..b7292ab9b 100644
--- a/control-api/test/workflows.shared.rateLimit.test.ts
+++ b/control-api/test/workflows.shared.rateLimit.test.ts
@@ -1,8 +1,10 @@
import { describe, expect, it, vi } from 'vitest'
import express from 'express'
+import { rateLimit } from 'express-rate-limit'
import request from 'supertest'
import {
adminWorkflowRateLimitCredential,
+ workflowGrantEdgeRateLimitKey,
workflowGrantReadRateLimit,
} from '../src/routes/workflows/shared/rateLimit.js'
@@ -29,6 +31,43 @@ describe('routes/workflows/shared/rateLimit', () => {
expect(adminWorkflowRateLimitCredential(req)).toBe('admin-cookie-token')
})
+ it('workflowGrantEdgeRateLimitKey buckets by IP regardless of bearer token', () => {
+ const reqA = {
+ ip: '203.0.113.10',
+ header: () => 'Bearer token-a',
+ } as express.Request
+ const reqB = {
+ ip: '203.0.113.10',
+ header: () => 'Bearer token-b',
+ } as express.Request
+
+ expect(workflowGrantEdgeRateLimitKey('workflow_grants_read_edge', reqA)).toBe(
+ workflowGrantEdgeRateLimitKey('workflow_grants_read_edge', reqB)
+ )
+ })
+
+ it('edge backstop caps distinct bogus bearer tokens from the same IP', async () => {
+ const edgeLimit = rateLimit({
+ windowMs: 60_000,
+ limit: 2,
+ standardHeaders: false,
+ legacyHeaders: false,
+ keyGenerator: req => workflowGrantEdgeRateLimitKey('workflow_grants_read_edge', req),
+ handler: (_req, res) => {
+ res.status(429).json({ error: 'Too Many Requests' })
+ },
+ })
+
+ const app = express()
+ app.get('/grants', edgeLimit, (_req, res) => {
+ res.status(200).json({ ok: true })
+ })
+
+ await request(app).get('/grants').set('Authorization', 'Bearer bogus-1').expect(200)
+ await request(app).get('/grants').set('Authorization', 'Bearer bogus-2').expect(200)
+ await request(app).get('/grants').set('Authorization', 'Bearer bogus-3').expect(429)
+ })
+
it('workflowGrantReadRateLimit meters cookie-only admin workflow callers', async () => {
mockCheckAndIncrement.mockReset()
mockCheckAndIncrement.mockResolvedValue({