Skip to content

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

TypeScript

fix(control-api): isolate standalone authorize limiter buckets

Alfredo Lopez8def7cde0d4f
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index cc624b3db..f64de5231 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.599",
+ "version": "0.1.600",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.599",
+ "version": "0.1.600",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/grok-provider-attempt-contract": "file:../packages/grok-provider-attempt-contract",
diff --git a/control-api/package.json b/control-api/package.json
index 944361935..20033d94b 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.599",
+ "version": "0.1.600",
"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/workflows/shared/rateLimit.ts b/control-api/src/routes/workflows/shared/rateLimit.ts
index c595a2151..18e5a2553 100644
--- a/control-api/src/routes/workflows/shared/rateLimit.ts
+++ b/control-api/src/routes/workflows/shared/rateLimit.ts
@@ -4,7 +4,10 @@ import { createHash } from 'node:crypto'
import { rateLimitMiddleware } from '../../../middleware/rateLimitMiddleware.js'
import { verifyAdminToken } from '../../../utils/auth/adminAuthToken.js'
import { verifyExternalSessionToken } from '../../../utils/auth/externalSessionAuthToken.js'
-import { verifyMcpHostAccessJwt } from '../../../utils/auth/mcpHostJwtToken.js'
+import {
+ mcpHostVerifiedRateLimitPrincipal,
+ verifyMcpHostAccessJwt,
+} from '../../../utils/auth/mcpHostJwtToken.js'
import { CONTROL_UI_ADMIN_SESSION_COOKIE, readCookie } from '../../../utils/auth/sessionCookies.js'
import { extractBearerToken } from '../../../utils/extractBearerToken.js'
@@ -197,17 +200,20 @@ export function codexOAuthCallbackRateLimits() {
/**
* Authorize limiter key:
- * - Verified mcp-host access JWT (attached claims or cryptographic verify) → per-sub
+ * - Verified mcp-host access JWT → per-recipe `sub`, except standalone
+ * 1st-party hosts which share that sentinel and must key by `hostRefs[0]`
* - Missing or unverified bearer → client IP (rotation cannot mint buckets)
*/
export function mcpHostAttemptRateLimitKey(req: Request): string {
- const attached = req.mcpHostJwt
- if (attached?.sub) return `llm_provider_attempt:${attached.sub}`
+ const attached = mcpHostVerifiedRateLimitPrincipal(req.mcpHostJwt)
// Always verify. Gating on bearer truthiness is a user-controlled skip of
// the security check (CodeQL js/user-controlled-bypass). Empty or forged
// tokens return null and share the IP bucket.
- const verified = verifyMcpHostAccessJwt(extractBearerToken(req))
- if (verified?.sub) return `llm_provider_attempt:${verified.sub}`
+ const verified = mcpHostVerifiedRateLimitPrincipal(
+ verifyMcpHostAccessJwt(extractBearerToken(req))
+ )
+ const principal = attached ?? verified
+ if (principal) return `llm_provider_attempt:${principal}`
return `llm_provider_attempt:ip:${ipKeyGenerator(req.ip ?? 'unknown')}`
}
diff --git a/control-api/src/utils/auth/mcpHostJwtToken.ts b/control-api/src/utils/auth/mcpHostJwtToken.ts
index 801613dc7..b06d68bff 100644
--- a/control-api/src/utils/auth/mcpHostJwtToken.ts
+++ b/control-api/src/utils/auth/mcpHostJwtToken.ts
@@ -549,6 +549,29 @@ export function verifyMcpHostControlJwt(token: string): McpHostControlClaims | n
}
}
+/**
+ * Verified-claims identity for mcp-host rate-limit buckets.
+ * Standalone 1st-party tokens share `sub = <hostsNamespace>/standalone`;
+ * per-host isolation lives in `hostRefs[0]`, matching the refresh limiter.
+ */
+export function mcpHostVerifiedRateLimitPrincipal(
+ claims:
+ | Pick<McpHostAccessClaims, 'recipeNamespace' | 'recipeName' | 'hostRefs'>
+ | Pick<McpHostRefreshClaims, 'recipeNamespace' | 'recipeName' | 'hostRefs'>
+ | null
+ | undefined
+): string | null {
+ if (!claims) return null
+ if (claims.recipeNamespace === config.hostsNamespace) {
+ const primaryHostRef = claims.hostRefs[0]?.trim()
+ if (!primaryHostRef) return null
+ return `${claims.recipeNamespace}/host/${primaryHostRef}`
+ }
+ const recipeName = claims.recipeName.trim()
+ if (!recipeName) return null
+ return `${claims.recipeNamespace}/${recipeName}`
+}
+
export function getMcpHostRefreshRateLimitKey(
token: string,
opts: { expiredGraceSeconds?: number } = {}
@@ -577,13 +600,7 @@ export function getMcpHostRefreshRateLimitKey(
return null
}
- if (claims.recipeNamespace === config.hostsNamespace) {
- const primaryHostRef = claims.hostRefs[0]?.trim()
- if (!primaryHostRef) return null
- return `${claims.recipeNamespace}/host/${primaryHostRef}`
- }
-
- return `${claims.recipeNamespace}/${claims.recipeName}`
+ return mcpHostVerifiedRateLimitPrincipal(claims)
} catch {
return null
}
diff --git a/control-api/test/workflows.shared.rateLimit.test.ts b/control-api/test/workflows.shared.rateLimit.test.ts
index b5ab06d7b..4d3003f6b 100644
--- a/control-api/test/workflows.shared.rateLimit.test.ts
+++ b/control-api/test/workflows.shared.rateLimit.test.ts
@@ -1,6 +1,7 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import express from 'express'
import request from 'supertest'
+import { config } from '../src/config.js'
import {
adminCodexReadRateLimits,
adminCodexWriteRateLimits,
@@ -168,6 +169,37 @@ describe('routes/workflows/shared/rateLimit', () => {
)
})
+ it('mcpHostAttemptRateLimitKey isolates standalone hosts that share the sentinel sub', () => {
+ const tokenA = issueMcpHostAccessJwt(config.hostsNamespace, 'standalone', ['chatllm'], {
+ workflowControlScopes: ['llm:codex:execute'],
+ }).token
+ const tokenARotated = issueMcpHostAccessJwt(config.hostsNamespace, 'standalone', ['chatllm'], {
+ workflowControlScopes: ['llm:codex:execute'],
+ }).token
+ const tokenB = issueMcpHostAccessJwt(config.hostsNamespace, 'standalone', ['trader'], {
+ workflowControlScopes: ['llm:codex:execute'],
+ }).token
+ const reqFor = (token: string) =>
+ ({
+ ip: '203.0.113.10',
+ header: (name: string) =>
+ name.toLowerCase() === 'authorization' ? `Bearer ${token}` : undefined,
+ }) as express.Request
+
+ expect(mcpHostAttemptRateLimitKey(reqFor(tokenA))).toBe(
+ `llm_provider_attempt:${config.hostsNamespace}/host/chatllm`
+ )
+ expect(mcpHostAttemptRateLimitKey(reqFor(tokenARotated))).toBe(
+ `llm_provider_attempt:${config.hostsNamespace}/host/chatllm`
+ )
+ expect(mcpHostAttemptRateLimitKey(reqFor(tokenB))).toBe(
+ `llm_provider_attempt:${config.hostsNamespace}/host/trader`
+ )
+ expect(mcpHostAttemptRateLimitKey(reqFor(tokenA))).not.toBe(
+ mcpHostAttemptRateLimitKey(reqFor(tokenB))
+ )
+ })
+
it('llmProviderAttemptAuthorizeRateLimits reuses one IP PG bucket for rotating unverified bearers', async () => {
mockCheckAndIncrement.mockReset()
mockCheckAndIncrement.mockResolvedValue({