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 mcp-oauth broker limiter by verified per-host principal

Diego Zacarias5cc46701b635
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index 716800e60..026eff3ee 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.601",
+ "version": "0.1.602",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.601",
+ "version": "0.1.602",
"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 a4c6f4544..8ec051e38 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.601",
+ "version": "0.1.602",
"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/mcpOauth.ts b/control-api/src/routes/mcpOauth.ts
index 191e634e6..c1339ed50 100644
--- a/control-api/src/routes/mcpOauth.ts
+++ b/control-api/src/routes/mcpOauth.ts
@@ -24,6 +24,7 @@ import { type Logger, rootLogger } from '../observability/logger.js'
import { K8sNotFoundError } from '../services/resourceService.js'
import {
type McpHostControlClaims,
+ mcpHostRateLimitBucketKey,
verifyMcpHostControlJwt,
} from '../utils/auth/mcpHostJwtToken.js'
import { extractBearerToken } from '../utils/extractBearerToken.js'
@@ -383,7 +384,13 @@ export function createMcpOauthRouter(gateway: K8sGateway): Router {
maxPerMinute: config.oauthBrokerRlPerMin,
getBucketKey: req => {
const claims = req.res?.locals?.mcpHostControl as McpHostControlClaims | undefined
- return claims ? `mcp-oauth:${claims.sub}` : 'mcp-oauth:unknown'
+ // Standalone 1st-party hosts all share sub=<hostsNamespace>/standalone,
+ // so keying by sub would collapse every standalone host into one bucket.
+ // The verified principal keys standalone hosts by hostRefs[0] instead,
+ // keeping each host isolated.
+ return (
+ mcpHostRateLimitBucketKey('mcp-oauth', claims, 'mcp-oauth:unknown') ?? 'mcp-oauth:unknown'
+ )
},
}),
async (req, res, next) => {
@@ -547,7 +554,13 @@ export function createMcpOauthRouter(gateway: K8sGateway): Router {
maxPerMinute: config.oauthBrokerRlPerMin,
getBucketKey: req => {
const claims = req.res?.locals?.mcpHostControl as McpHostControlClaims | undefined
- return claims ? `mcp-oauth:${claims.sub}` : 'mcp-oauth:unknown'
+ // Standalone 1st-party hosts all share sub=<hostsNamespace>/standalone,
+ // so keying by sub would collapse every standalone host into one bucket.
+ // The verified principal keys standalone hosts by hostRefs[0] instead,
+ // keeping each host isolated.
+ return (
+ mcpHostRateLimitBucketKey('mcp-oauth', claims, 'mcp-oauth:unknown') ?? 'mcp-oauth:unknown'
+ )
},
}),
async (req, res, next) => {
diff --git a/control-api/test/routes.mcpOauth.test.ts b/control-api/test/routes.mcpOauth.test.ts
index c6c4ba9f6..986089e42 100644
--- a/control-api/test/routes.mcpOauth.test.ts
+++ b/control-api/test/routes.mcpOauth.test.ts
@@ -112,6 +112,36 @@ describe('routes/mcp-oauth — POST /mcp-oauth/user-token (U1)', () => {
expect(res.status).toBe(401)
})
+ it('isolates the rate-limit bucket per standalone host (no sub collapse)', async () => {
+ // Every standalone 1st-party host shares sub=<hostsNamespace>/standalone, so
+ // keying the limiter by sub would drop all of them in one bucket. The bucket
+ // key must be the verified per-host principal (hostRefs[0]) instead.
+ seedOauthServer(gateway, { name: 'gdrive' })
+ vi.mocked(checkAndIncrement).mockClear()
+
+ const tokenA = issueMcpHostControlJwt('mcp-host', 'standalone', ['host-a'], {
+ scopes: ['oauth:user-token'],
+ }).token
+ const tokenB = issueMcpHostControlJwt('mcp-host', 'standalone', ['host-b'], {
+ scopes: ['oauth:user-token'],
+ }).token
+
+ await request(app)
+ .post('/api/v1/mcp-oauth/user-token')
+ .set('Authorization', `Bearer ${tokenA}`)
+ .send({ mcpServerName: 'gdrive', userId: 'user-1' })
+ await request(app)
+ .post('/api/v1/mcp-oauth/user-token')
+ .set('Authorization', `Bearer ${tokenB}`)
+ .send({ mcpServerName: 'gdrive', userId: 'user-1' })
+
+ const keys = vi.mocked(checkAndIncrement).mock.calls.map(call => call[0])
+ expect(keys).toHaveLength(2)
+ expect(keys[0]).toBe('mcp-oauth:mcp-host/host/host-a')
+ expect(keys[1]).toBe('mcp-oauth:mcp-host/host/host-b')
+ expect(keys[0]).not.toBe(keys[1])
+ })
+
it('403 with a valid control JWT that lacks the oauth:user-token scope', async () => {
seedOauthServer(gateway, { name: 'gdrive' })
const res = await request(app)