Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(control-api): gate mcp-oauth broker behind fail-closed flag (R1-H1)
Diego Zacarias037fb395c16f
diff --git a/control-api/package-lock.json b/control-api/package-lock.jsonindex be2a61d83..971556d02 100644--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-api",- "version": "0.1.468",+ "version": "0.1.469","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.468",+ "version": "0.1.469","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.jsonindex 6e03124d4..4d711e8cd 100644--- a/control-api/package.json+++ b/control-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-api",- "version": "0.1.468",+ "version": "0.1.469","description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets","main": "dist/main.js","engines": {diff --git a/control-api/src/config.ts b/control-api/src/config.tsindex 2a3d58848..4fff0c3b2 100644--- a/control-api/src/config.ts+++ b/control-api/src/config.ts@@ -95,6 +95,13 @@ type Config = {mcpHostJwtMaxHostRefs: numbermcpHostJwtMaxHostRefLength: numberoauthBrokerJwtTtlSec: number+ // OAuth mcp-server broker (POST /api/v1/mcp-oauth/user-token) kill-switch.+ // DEFAULT OFF (fail-closed): the endpoint must look absent in production+ // until the U4 mcp-host runtime lands, honoring the PR promise "do not enable+ // OAuth mcp-servers in production until U4 lands". When off the route returns+ // 404 not_found before any auth/token work. MCP_OAUTH_BROKER_ENABLED=true+ // opts a deploy in deliberately.+ mcpOauthBrokerEnabled: booleanuserApprovalRequestDefaultTtlSec: numberuserApprovalRequestMaxTtlSec: numberuserApprovalRequestExpiryIntervalMs: number@@ -749,6 +756,9 @@ export const config: Config = {DEFAULT_MCP_HOST_JWT_MAX_HOST_REF_LENGTH),oauthBrokerJwtTtlSec: Number(process.env.CONTROL_API_OAUTH_BROKER_JWT_TTL_SEC || 600),+ // Fail-closed kill-switch: same default-OFF idiom as+ // desktopGfsOperatorLinkingEnabled. Missing/'false' → OFF; only 'true' enables.+ mcpOauthBrokerEnabled: failClosedBooleanFromEnv('MCP_OAUTH_BROKER_ENABLED'),userApprovalRequestDefaultTtlSec: Number(process.env.WORKFLOW_APPROVAL_DEFAULT_TTL_SEC || 86400),userApprovalRequestMaxTtlSec: Number(process.env.WORKFLOW_APPROVAL_MAX_TTL_SEC || 604800),userApprovalRequestExpiryIntervalMs: Number(diff --git a/control-api/src/routes/mcpOauth.ts b/control-api/src/routes/mcpOauth.tsindex 2cfe2ed82..fef7c0481 100644--- a/control-api/src/routes/mcpOauth.ts+++ b/control-api/src/routes/mcpOauth.ts@@ -174,8 +174,22 @@ export function createMcpOauthRouter(gateway: K8sGateway): Router {next()}+ // Fail-closed kill-switch (finding R1-H1). While the OAuth mcp-server broker+ // is disabled the endpoint must look ABSENT — a 404 before any auth or token+ // work, not a 403 — so it cannot serve provider tokens in production until the+ // U4 mcp-host runtime lands. Read at request time so the flag is honored+ // without a restart in tests; default is OFF (config.mcpOauthBrokerEnabled).+ function requireBrokerEnabled(_req: Request, res: Response, next: NextFunction): void {+ if (!config.mcpOauthBrokerEnabled) {+ res.status(404).json({ error: 'not_found' })+ return+ }+ next()+ }+router.post('/mcp-oauth/user-token',+ requireBrokerEnabled,requireControlCaller,rateLimitMiddleware({bucketType: 'mcp_oauth_broker',diff --git a/control-api/test/routes.mcpOauth.test.ts b/control-api/test/routes.mcpOauth.test.tsindex 0636f310e..a05728f1f 100644--- a/control-api/test/routes.mcpOauth.test.ts+++ b/control-api/test/routes.mcpOauth.test.ts@@ -1,4 +1,4 @@-import { beforeEach, describe, expect, it, vi } from 'vitest'+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import request from 'supertest'import { createApp } from '../src/app.js'import { config } from '../src/config.js'@@ -86,12 +86,21 @@ function nonExpiredUserGrantRow(userId: string, expiresAt: Date) {describe('routes/mcp-oauth — POST /mcp-oauth/user-token (U1)', () => {let gateway: MockGatewaylet app: ReturnType<typeof createApp>+ const originalBrokerEnabled = config.mcpOauthBrokerEnabledbeforeEach(() => {gateway = new MockGateway(MCP_NS)app = createApp(gateway as never)mockPoolQuery.mockReset()mockPoolQuery.mockResolvedValue({ rows: [], rowCount: 0 })+ // These specs assert the broker's live behavior, so run them with the+ // kill-switch ON. The flag defaults OFF (fail-closed); the OFF→404 spec+ // below flips it back to exercise the disabled path.+ config.mcpOauthBrokerEnabled = true+ })++ afterEach(() => {+ config.mcpOauthBrokerEnabled = originalBrokerEnabled})it('401 without a control JWT', async () => {@@ -320,4 +329,60 @@ describe('routes/mcp-oauth — POST /mcp-oauth/user-token (U1)', () => {expect(res.status).toBe(400)expect(res.body.error).toBe('invalid_request')})++ // Finding R1-H1: the broker is gated behind a kill-switch that defaults OFF,+ // so it cannot serve provider tokens in production until the U4 mcp-host+ // runtime lands. When disabled, a fully well-formed AND authorized request —+ // valid control JWT, correct scope, seeded server, a grant row that would+ // otherwise resolve to a 200 token — must observe 404 not_found (the endpoint+ // looks absent), and grant resolution must never run (no oauth_grants query).+ describe('kill-switch (mcpOauthBrokerEnabled)', () => {+ it('OFF (default): a well-formed authorized request returns 404 not_found and never resolves a grant', async () => {+ config.mcpOauthBrokerEnabled = false+ seedOauthServer(gateway, { name: 'gdrive', grantScope: 'user' })+ // A grant that WOULD resolve to a 200 token if the endpoint served — so+ // the 404 can only come from the gate, not from a missing grant.+ const future = new Date(Date.now() + 3600_000)+ mockPoolQuery.mockResolvedValueOnce({+ rows: [nonExpiredUserGrantRow('user-1', future)],+ rowCount: 1,+ })++ const res = await request(app)+ .post('/api/v1/mcp-oauth/user-token')+ .set('Authorization', `Bearer ${controlToken()}`)+ .send({ mcpServerName: 'gdrive', userId: 'user-1', contextId: 'ctx-9' })++ // Load-bearing (T4): assert the observable HTTP response, not an internal+ // call count. This assertion fails against the pre-gate parent (which+ // always serves → 200 with the seeded grant).+ expect(res.status).toBe(404)+ expect(res.body.error).toBe('not_found')+ expect(res.body.token).toBeUndefined()+ // Never reached grant resolution — no oauth_grants lookup ran.+ const grantQuery = mockPoolQuery.mock.calls.find(+ ([sql]) => typeof sql === 'string' && sql.includes('FROM oauth_grants')+ )+ expect(grantQuery).toBeUndefined()+ })++ it('ON: the same request resolves normally (behavior unchanged)', async () => {+ config.mcpOauthBrokerEnabled = true+ seedOauthServer(gateway, { name: 'gdrive', grantScope: 'user' })+ const future = new Date(Date.now() + 3600_000)+ mockPoolQuery.mockResolvedValueOnce({+ rows: [nonExpiredUserGrantRow('user-1', future)],+ rowCount: 1,+ })++ const res = await request(app)+ .post('/api/v1/mcp-oauth/user-token')+ .set('Authorization', `Bearer ${controlToken()}`)+ .send({ mcpServerName: 'gdrive', userId: 'user-1', contextId: 'ctx-9' })++ expect(res.status).toBe(200)+ expect(res.body.token).toBe('GDRIVE-ACCESS')+ expect(res.body.expiresAt).toBe(future.toISOString())+ })+ })})