Skip to content

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

TypeScript

feat(control-api): u5 membership gate on mcp-oauth authorize-url mint

Diego Zacarias0c925cb09ef8
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index 7a69f5263..9ccaa8511 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.437",
+ "version": "0.1.438",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.437",
+ "version": "0.1.438",
"dependencies": {
"@clerum/image-policy": "file:../packages/image-policy",
"@clerum/llm-providers": "file:../packages/llm-providers",
diff --git a/control-api/package.json b/control-api/package.json
index 9ea171700..4275b8035 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.437",
+ "version": "0.1.438",
"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/internal/oauth.ts b/control-api/src/routes/internal/oauth.ts
index 9cbef863e..b729f5551 100644
--- a/control-api/src/routes/internal/oauth.ts
+++ b/control-api/src/routes/internal/oauth.ts
@@ -21,6 +21,7 @@ import {
} from '../../oauth/mcpServerOAuthSpec.js'
import { deleteOAuthGrant } from '../../oauth/store.js'
import { getAccessToken } from '../../oauth/tokenHelper.js'
+import { getUserContexts } from '../../services/directory/index.js'
import { K8sNotFoundError } from '../../services/resourceService.js'
import { buildPublicCallbackUrl } from '../external/oauthCallback.js'
@@ -173,6 +174,30 @@ export function createInternalOAuthRouter(gateway: K8sGateway): Router {
}
}
+ // DEC-U5-1 (+ security fix): defence in depth — fail EARLY (before
+ // sending the user to the provider) unless the consenting user is a
+ // member of the server's Context. This runs for EVERY grantScope, not
+ // just `context`: even a per-user server lives inside a Context, and
+ // connect shares the `mcp:server:invoke` scope with invoke — which
+ // additionally requires the server to be in the caller's per-context
+ // allowlist. Without a universal gate, any user could start consent for
+ // (and enumerate) another Context's integration on a `user`-scope server.
+ //
+ // `spec.contextRef` is CRD-required + singular, so every server has one;
+ // a server that somehow lacks it cannot be membership-verified → fail
+ // closed. Same rule + primitive as the callback bootstrap
+ // (getUserContexts → user_contexts) so membership lives in ONE place
+ // (D4). Deliberately NOT resolveInvocableMcpServersForContexts: that
+ // applies U3's grant-presence gate, which filters out servers WITHOUT a
+ // grant — exactly the ones connect exists to bootstrap (chicken-and-egg).
+ if (!resolved.contextRef) {
+ return res.status(403).json({ error: 'context_membership_denied' })
+ }
+ const { contextIds } = await getUserContexts(userId)
+ if (!contextIds.includes(resolved.contextRef)) {
+ return res.status(403).json({ error: 'context_membership_denied' })
+ }
+
const oauthClientId = resolved.decl.id
const redirectUri = buildPublicCallbackUrl(req, oauthClientId, config.oauthCallbackBaseUrl)
diff --git a/control-api/test/routes.internal.mcpOauthAuthorizeUrl.test.ts b/control-api/test/routes.internal.mcpOauthAuthorizeUrl.test.ts
index d3bcd2b3d..dbb0deb57 100644
--- a/control-api/test/routes.internal.mcpOauthAuthorizeUrl.test.ts
+++ b/control-api/test/routes.internal.mcpOauthAuthorizeUrl.test.ts
@@ -84,6 +84,9 @@ describe('POST /api/v1/internal/mcp-oauth/authorize-url (U5)', () => {
it('mints an authorize-URL whose signed state binds the forwarded userId + the SERVER oauthClientId', async () => {
seedOauthServer(gateway, { name: 'gdrive', grantScope: 'user' })
+ // Membership check runs for EVERY scope: user-7 is a member of the server's
+ // Context (default contextRef ctx-9).
+ mockPoolQuery.mockResolvedValue({ rows: [{ context_id: 'ctx-9' }], rowCount: 1 })
const res = await post(app)
.set('Authorization', 'Bearer dev-rpc-proxy-token')
.set('x-service-token', 'rpc-proxy')
@@ -155,8 +158,10 @@ describe('POST /api/v1/internal/mcp-oauth/authorize-url (U5)', () => {
expect(res.body.error).toBe('context_mismatch')
})
- it('context server: accepts a matching body contextId and mints the URL', async () => {
+ it('context server: accepts a matching body contextId from a MEMBER and mints the URL', async () => {
seedOauthServer(gateway, { name: 'gdrive', grantScope: 'context', contextRef: 'ctx-A' })
+ // Membership check (getUserContexts → user_contexts): user-2 is a member of ctx-A.
+ mockPoolQuery.mockResolvedValue({ rows: [{ context_id: 'ctx-A' }], rowCount: 1 })
const res = await post(app)
.set('Authorization', 'Bearer dev-rpc-proxy-token')
.set('x-service-token', 'rpc-proxy')
@@ -165,6 +170,61 @@ describe('POST /api/v1/internal/mcp-oauth/authorize-url (U5)', () => {
expect(res.body.authorizeUrl).toContain('https://accounts.google.com/')
})
+ // T5 (DEC-U5-1): a NON-member of a context-identity server's Context must be
+ // rejected at the MINT boundary — fail early, never sent to the provider.
+ it('context server: a NON-member is rejected 403 at mint, with NO authorizeUrl', async () => {
+ seedOauthServer(gateway, { name: 'gdrive', grantScope: 'context', contextRef: 'ctx-A' })
+ // getUserContexts returns contexts that do NOT include ctx-A.
+ mockPoolQuery.mockResolvedValue({
+ rows: [{ context_id: 'ctx-other' }, { context_id: 'ctx-else' }],
+ rowCount: 2,
+ })
+ const res = await post(app)
+ .set('Authorization', 'Bearer dev-rpc-proxy-token')
+ .set('x-service-token', 'rpc-proxy')
+ .send({ mcpServerName: 'gdrive', userId: 'user-nomember' })
+ expect(res.status).toBe(403)
+ expect(res.body.error).toBe('context_membership_denied')
+ // Observable: no authorize URL was minted.
+ expect(res.body.authorizeUrl).toBeUndefined()
+ })
+
+ // Security fix: the per-user flavor ALSO runs the membership gate now — a
+ // user-scope server still lives in a Context, and connect shares the invoke
+ // scope. A MEMBER mints fine.
+ it('user flavor: a MEMBER of the server Context mints OK', async () => {
+ seedOauthServer(gateway, { name: 'gdrive', grantScope: 'user', contextRef: 'ctx-9' })
+ mockPoolQuery.mockResolvedValue({ rows: [{ context_id: 'ctx-9' }], rowCount: 1 })
+ const res = await post(app)
+ .set('Authorization', 'Bearer dev-rpc-proxy-token')
+ .set('x-service-token', 'rpc-proxy')
+ .send({ mcpServerName: 'gdrive', userId: 'user-7' })
+ expect(res.status).toBe(200)
+ expect(res.body.authorizeUrl).toContain('https://accounts.google.com/')
+ // The membership lookup DID run (universal gate).
+ const membershipQuery = mockPoolQuery.mock.calls.find(
+ ([sql]) => typeof sql === 'string' && sql.includes('FROM user_contexts')
+ )
+ expect(membershipQuery).toBeDefined()
+ expect(membershipQuery?.[1]).toEqual(['user-7'])
+ })
+
+ // Security fix (the asymmetry hole): a `user`-scope server whose Context the
+ // user is NOT in must be rejected — no consent for another Context's
+ // integration, no cross-context enumeration oracle.
+ it('user flavor: a NON-member is rejected 403 at mint, with NO authorizeUrl', async () => {
+ seedOauthServer(gateway, { name: 'gdrive', grantScope: 'user', contextRef: 'ctx-9' })
+ // user-8 is a member of other Contexts, but NOT ctx-9.
+ mockPoolQuery.mockResolvedValue({ rows: [{ context_id: 'ctx-other' }], rowCount: 1 })
+ const res = await post(app)
+ .set('Authorization', 'Bearer dev-rpc-proxy-token')
+ .set('x-service-token', 'rpc-proxy')
+ .send({ mcpServerName: 'gdrive', userId: 'user-8' })
+ expect(res.status).toBe(403)
+ expect(res.body.error).toBe('context_membership_denied')
+ expect(res.body.authorizeUrl).toBeUndefined()
+ })
+
it('503 integration_not_configured when the client_id Secret is absent', async () => {
// Server exists but no google-creds Secret seeded.
void gateway.createResource(
@@ -184,6 +244,8 @@ describe('POST /api/v1/internal/mcp-oauth/authorize-url (U5)', () => {
},
MCP_NS
)
+ // Member of ctx-9 so we reach the Secret read (past the membership gate).
+ mockPoolQuery.mockResolvedValue({ rows: [{ context_id: 'ctx-9' }], rowCount: 1 })
const res = await post(app)
.set('Authorization', 'Bearer dev-rpc-proxy-token')
.set('x-service-token', 'rpc-proxy')