Skip to content

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

TypeScript

fix(control-api): key authorize limiter by verified JWT, not bearer hash

Alfredo Lopez3d75bc86ca72
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index 461945ecf..fbc5590fc 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.589",
+ "version": "0.1.590",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.589",
+ "version": "0.1.590",
"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 a518fab2a..c16ca492b 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.589",
+ "version": "0.1.590",
"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 b449200d3..d00203a8c 100644
--- a/control-api/src/routes/workflows/shared/rateLimit.ts
+++ b/control-api/src/routes/workflows/shared/rateLimit.ts
@@ -4,6 +4,7 @@ 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 { CONTROL_UI_ADMIN_SESSION_COOKIE, readCookie } from '../../../utils/auth/sessionCookies.js'
import { extractBearerToken } from '../../../utils/extractBearerToken.js'
@@ -194,13 +195,18 @@ export function codexOAuthCallbackRateLimits() {
] as const
}
-function mcpHostAttemptRateLimitKey(req: Request): string {
- const claims = req.mcpHostJwt
- if (claims?.sub) return `llm_provider_attempt:${claims.sub}`
+/**
+ * Authorize limiter key:
+ * - Verified mcp-host access JWT (attached claims or cryptographic verify) → per-sub
+ * - 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 bearer = extractBearerToken(req)
if (bearer) {
- const hash = createHash('sha256').update(bearer).digest('hex').slice(0, 32)
- return `llm_provider_attempt:bearer:${hash}`
+ const verified = verifyMcpHostAccessJwt(bearer)
+ if (verified?.sub) return `llm_provider_attempt:${verified.sub}`
}
return `llm_provider_attempt:ip:${ipKeyGenerator(req.ip ?? 'unknown')}`
}
diff --git a/control-api/test/routes.mcp-host.llmProviderAttempts.createApp.test.ts b/control-api/test/routes.mcp-host.llmProviderAttempts.createApp.test.ts
new file mode 100644
index 000000000..667296841
--- /dev/null
+++ b/control-api/test/routes.mcp-host.llmProviderAttempts.createApp.test.ts
@@ -0,0 +1,85 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest'
+import { createServer } from 'node:http'
+import * as authorizer from '../src/services/llmProviderAttemptAuthorizer.js'
+import { MockGateway } from './mockGateway.js'
+
+vi.mock('../src/config.js', async importOriginal => {
+ const actual = await importOriginal<typeof import('../src/config.js')>()
+ return {
+ ...actual,
+ config: { ...actual.config, jsonBodyLimit: '1mb' },
+ }
+})
+
+vi.mock('../src/db.js', () => ({
+ pool: {
+ query: vi.fn().mockResolvedValue({ rows: [], rowCount: 0 }),
+ connect: vi.fn(),
+ },
+ withTransaction: vi.fn(),
+}))
+
+vi.mock('../src/services/notificationEmitter.js', () => ({
+ emitNotification: vi.fn().mockResolvedValue(undefined),
+ enqueueApprovalRequestedNotification: vi.fn().mockResolvedValue(undefined),
+ enqueueApprovalUpdatedNotification: vi.fn().mockResolvedValue(undefined),
+}))
+
+vi.mock('../src/services/rateLimiterService.js', () => ({
+ checkAndIncrement: vi.fn().mockResolvedValue({
+ allowed: true,
+ remaining: 59,
+ resetMs: Date.now() + 60_000,
+ windowStartMs: Date.now(),
+ count: 1,
+ }),
+}))
+
+vi.mock('../src/observability/metrics.js', async importOriginal => {
+ const actual = await importOriginal<typeof import('../src/observability/metrics.js')>()
+ return {
+ ...actual,
+ rateLimitHitsTotal: { inc: vi.fn() },
+ }
+})
+
+vi.mock('../src/services/llmProviderAttemptAuthorizer.js', async () => {
+ const actual = await vi.importActual<
+ typeof import('../src/services/llmProviderAttemptAuthorizer.js')
+ >('../src/services/llmProviderAttemptAuthorizer.js')
+ return {
+ ...actual,
+ authorizeLlmProviderAttempt: vi.fn(),
+ }
+})
+
+describe('createApp POST /api/v1/mcp-host/llm/provider-attempts/authorize', () => {
+ beforeEach(() => {
+ vi.mocked(authorizer.authorizeLlmProviderAttempt).mockReset()
+ })
+
+ it('returns 401 for an unauthenticated body over 24 MiB without calling authorize', async () => {
+ const { createApp } = await import('../src/app.js')
+ const app = createApp(new MockGateway() as never)
+ const listener = createServer(app).listen(0)
+ try {
+ const address = listener.address()
+ if (!address || typeof address === 'string') throw new Error('listener has no port')
+ const oversized = await fetch(
+ `http://127.0.0.1:${address.port}/api/v1/mcp-host/llm/provider-attempts/authorize`,
+ {
+ method: 'POST',
+ headers: { 'content-type': 'application/json' },
+ body: `{"pad":"${'x'.repeat(25 * 1024 * 1024)}"}`,
+ }
+ )
+ expect(oversized.status).toBe(401)
+ expect(oversized.status).not.toBe(413)
+ expect(authorizer.authorizeLlmProviderAttempt).not.toHaveBeenCalled()
+ } finally {
+ await new Promise<void>((resolve, reject) =>
+ listener.close(err => (err ? reject(err) : resolve()))
+ )
+ }
+ }, 30_000)
+})
diff --git a/control-api/test/routes.mcp-host.llmProviderAttempts.test.ts b/control-api/test/routes.mcp-host.llmProviderAttempts.test.ts
index 10a41e5bc..066407bbd 100644
--- a/control-api/test/routes.mcp-host.llmProviderAttempts.test.ts
+++ b/control-api/test/routes.mcp-host.llmProviderAttempts.test.ts
@@ -43,7 +43,8 @@ const HOST = 'research-host'
function buildApp() {
const app = express()
- // Production skips the global parser on this path; the route owns the 24 MiB JSON.
+ // Route-only harness: JWT-before-24-MiB-parser. createApp() skip of the
+ // global 150mb parser is locked in llmProviderAttempts.createApp.test.ts.
const api = express.Router()
api.use(
createMcpHostLlmProviderAttemptRoutes({
diff --git a/control-api/test/workflows.shared.rateLimit.test.ts b/control-api/test/workflows.shared.rateLimit.test.ts
index d7e59ac03..b5ab06d7b 100644
--- a/control-api/test/workflows.shared.rateLimit.test.ts
+++ b/control-api/test/workflows.shared.rateLimit.test.ts
@@ -9,6 +9,7 @@ import {
adminWorkflowTriggerRateLimit,
codexOAuthCallbackRateLimits,
llmProviderAttemptAuthorizeRateLimits,
+ mcpHostAttemptRateLimitKey,
mcpHostWorkflowTriggerRateLimit,
mcpHostWorkflowTriggerRateLimitCredential,
shouldSkipWorkflowGrantEdgeRateLimit,
@@ -21,6 +22,7 @@ import {
workflowTriggerRateLimit,
workflowTriggerRateLimitCredential,
} from '../src/routes/workflows/shared/rateLimit.js'
+import { issueMcpHostAccessJwt } from '../src/utils/auth/mcpHostJwtToken.js'
const mockCheckAndIncrement = vi.hoisted(() => vi.fn())
const mockVerifyAdminToken = vi.hoisted(() => vi.fn())
@@ -29,9 +31,13 @@ const mockVerifyExternalSessionToken = vi.hoisted(() => vi.fn())
vi.mock('../src/services/rateLimiterService.js', () => ({
checkAndIncrement: (...args: unknown[]) => mockCheckAndIncrement(...args),
}))
-vi.mock('../src/observability/metrics.js', () => ({
- rateLimitHitsTotal: { inc: vi.fn() },
-}))
+vi.mock('../src/observability/metrics.js', async importOriginal => {
+ const actual = await importOriginal<typeof import('../src/observability/metrics.js')>()
+ return {
+ ...actual,
+ rateLimitHitsTotal: { inc: vi.fn() },
+ }
+})
vi.mock('../src/utils/auth/adminAuthToken.js', () => ({
verifyAdminToken: (token: string) => mockVerifyAdminToken(token),
}))
@@ -108,6 +114,83 @@ describe('routes/workflows/shared/rateLimit', () => {
})
})
+ it('mcpHostAttemptRateLimitKey aggregates rotating unverified bearers on the same IP', () => {
+ const reqFor = (token: string) =>
+ ({
+ ip: '203.0.113.10',
+ header: (name: string) =>
+ name.toLowerCase() === 'authorization' ? `Bearer ${token}` : undefined,
+ }) as express.Request
+
+ expect(mcpHostAttemptRateLimitKey(reqFor('forged-a'))).toBe(
+ mcpHostAttemptRateLimitKey(reqFor('forged-b'))
+ )
+ expect(mcpHostAttemptRateLimitKey(reqFor('forged-a'))).toMatch(/^llm_provider_attempt:ip:/)
+ })
+
+ it('mcpHostAttemptRateLimitKey keys a missing bearer to the client IP', () => {
+ const req = {
+ ip: '203.0.113.10',
+ header: () => undefined,
+ } as express.Request
+
+ expect(mcpHostAttemptRateLimitKey(req)).toMatch(/^llm_provider_attempt:ip:/)
+ })
+
+ it('mcpHostAttemptRateLimitKey applies the per-sub bucket only after the JWT verifies', () => {
+ const tokenA = issueMcpHostAccessJwt('default', 'research-host', ['research-host'], {
+ workflowControlScopes: ['llm:codex:execute'],
+ }).token
+ const tokenARotated = issueMcpHostAccessJwt('default', 'research-host', ['research-host'], {
+ workflowControlScopes: ['llm:codex:execute'],
+ }).token
+ const tokenB = issueMcpHostAccessJwt('default', 'other-host', ['other-host'], {
+ 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:default/research-host'
+ )
+ expect(mcpHostAttemptRateLimitKey(reqFor(tokenARotated))).toBe(
+ 'llm_provider_attempt:default/research-host'
+ )
+ expect(mcpHostAttemptRateLimitKey(reqFor(tokenB))).toBe(
+ 'llm_provider_attempt:default/other-host'
+ )
+ expect(mcpHostAttemptRateLimitKey(reqFor(tokenA))).not.toBe(
+ mcpHostAttemptRateLimitKey(reqFor('forged-not-a-jwt'))
+ )
+ })
+
+ it('llmProviderAttemptAuthorizeRateLimits reuses one IP PG bucket for rotating unverified bearers', async () => {
+ mockCheckAndIncrement.mockReset()
+ mockCheckAndIncrement.mockResolvedValue({
+ allowed: true,
+ remaining: 59,
+ resetMs: Date.now() + 60_000,
+ windowStartMs: Date.now(),
+ count: 1,
+ })
+
+ const app = express()
+ app.post('/authorize', ...llmProviderAttemptAuthorizeRateLimits(), (_req, res) => {
+ res.status(200).json({ ok: true })
+ })
+
+ await request(app).post('/authorize').set('Authorization', 'Bearer forged-a').expect(200)
+ await request(app).post('/authorize').set('Authorization', 'Bearer forged-b').expect(200)
+
+ expect(mockCheckAndIncrement).toHaveBeenCalledTimes(2)
+ expect(mockCheckAndIncrement.mock.calls[0]?.[0]).toMatch(/^llm_provider_attempt:ip:/)
+ expect(mockCheckAndIncrement.mock.calls[0]?.[0]).toBe(mockCheckAndIncrement.mock.calls[1]?.[0])
+ })
+
it('rate-limit factories pair an edge backstop with the PG limiter', () => {
expect(workflowGrantReadRateLimits()).toHaveLength(2)
expect(workflowGrantWriteRateLimits()).toHaveLength(2)