Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): fail closed when the broker has no control-token seed (R2-L1)
Diego Zacarias803ed197881b
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.jsonindex ba135dd08..eb3509021 100644--- a/mcp-host/package-lock.json+++ b/mcp-host/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-mcp-host",- "version": "0.1.246",+ "version": "0.1.247","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-mcp-host",- "version": "0.1.246",+ "version": "0.1.247","license": "SEE LICENSE IN LICENSE","dependencies": {"@anthropic-ai/sdk": "^0.32.0",diff --git a/mcp-host/package.json b/mcp-host/package.jsonindex 0c8c0791f..6c0b6046d 100644--- a/mcp-host/package.json+++ b/mcp-host/package.json@@ -1,6 +1,6 @@{"name": "clerum-mcp-host",- "version": "0.1.246",+ "version": "0.1.247","description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude","main": "dist/main.js","engines": {diff --git a/mcp-host/src/main.ts b/mcp-host/src/main.tsindex 965e3edd1..278538935 100644--- a/mcp-host/src/main.ts+++ b/mcp-host/src/main.ts@@ -856,8 +856,17 @@ function createMcpTokenProviderFactory(): McpTokenProviderFactory {export function brokerTokenProviderDeps(): BrokerTokenProviderDeps {return {gatewayUrl: () => config.mcpHostGatewayUrl,- controlToken: () =>- loadPersistedWorkflowControlToken(workflowControlTokenFromConfig() ?? '') || undefined,+ controlToken: () => {+ // No seed at all => fail closed. Calling loadPersistedWorkflowControlToken+ // with '' reaches its "no fallback binding to compare against" branch,+ // where ANY persisted token with well-formed binding claims is accepted+ // without being cross-checked against the pod's mounted identity. The seed+ // is what makes that cross-check possible, so without it there is nothing+ // to validate the state file against and the broker must send nothing.+ const seed = workflowControlTokenFromConfig()?.trim()+ if (!seed) return undefined+ return loadPersistedWorkflowControlToken(seed) || undefined+ },// Reactive recovery on a control-api 401, same as every sibling consumer// (setupUsageReporting wires `refreshOnUnauthorized: () => refreshWithRecovery(auth)`// into UsageReporter / GovernedRunReporter / ApprovalPromptHistoryClient).diff --git a/mcp-host/src/mcp/__tests__/brokerControlTokenSource.test.ts b/mcp-host/src/mcp/__tests__/brokerControlTokenSource.test.tsindex 94193195b..5f55fc201 100644--- a/mcp-host/src/mcp/__tests__/brokerControlTokenSource.test.ts+++ b/mcp-host/src/mcp/__tests__/brokerControlTokenSource.test.ts@@ -18,6 +18,7 @@ import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitesimport * as fs from 'node:fs'import * as os from 'node:os'import * as path from 'node:path'+import { config } from '../../config'import { brokerTokenProviderDeps } from '../../main'import { persistRuntimeAuthTokens } from '../../workflow/mcpHostJwtState'import { createBrokerTokenProvider } from '../brokerTokenProvider'@@ -32,15 +33,29 @@ const boot = vi.hoisted(() => {recipeNamespace: 'mcp-host',recipeName: 'standalone',}- const jwt = (iat: number, exp: number, label: string): string =>+ const jwtFor = (+ bindingClaims: Record<string, unknown>,+ iat: number,+ exp: number,+ label: string+ ): string =>`${base64url({ alg: 'none', typ: 'JWT' })}.${base64url({- ...binding,+ ...bindingClaims,iat,exp,label,typ: 'service',scopes: ['oauth:user-token'],})}.sig`+ const jwt = (iat: number, exp: number, label: string): string => jwtFor(binding, iat, exp, label)+ // Same well-formed shape, bound to a DIFFERENT host: what an attacker-planted+ // or cross-tenant state file would carry.+ const foreignToken = jwtFor(+ { hostRefs: ['host-other'], recipeNamespace: 'mcp-host', recipeName: 'standalone' },+ nowSecs - 60,+ nowSecs + 540,+ 'foreign'+ )// The seed the pod booted with: already past its ~10 min TTL.const bootToken = jwt(nowSecs - 1200, nowSecs - 600, 'boot-seed')@@ -53,7 +68,7 @@ const boot = vi.hoisted(() => {process.env.MCP_HOST_GATEWAY_URL = 'http://gateway.test:8092'process.env.MCP_HOST_WORKFLOW_CONTROL_TOKEN = bootTokendelete process.env.MCP_HOST_WORKFLOW_CONTROL_TOKEN_FILE- return { jwt, nowSecs, bootToken, rotatedToken, staleRotatedToken }+ return { jwt, nowSecs, bootToken, rotatedToken, staleRotatedToken, foreignToken }})const tempDirs: string[] = []@@ -245,3 +260,59 @@ describe('mcp-oauth broker control token recovery on 401', () => {expect(typeof brokerTokenProviderDeps().refreshControlToken).toBe('function')})})++describe('mcp-oauth broker control token — no seed means fail closed', () => {+ // The seed (mounted Secret / env) is the pod's mounted identity: it is what+ // loadPersistedWorkflowControlToken cross-checks the state file against. These+ // tests drive the real config object main.ts reads, not a copy.+ let savedSeed: string | undefined+ let savedSeedFile: string | undefined++ beforeEach(() => {+ savedSeed = config.mcpHostWorkflowControlToken+ savedSeedFile = config.mcpHostWorkflowControlTokenFile+ config.mcpHostWorkflowControlToken = ''+ config.mcpHostWorkflowControlTokenFile = ''+ })++ afterEach(() => {+ config.mcpHostWorkflowControlToken = savedSeed+ config.mcpHostWorkflowControlTokenFile = savedSeedFile+ })++ it('never sends a persisted token that could not be cross-checked against the mounted identity', async () => {+ useStateDir()+ // Real producer writes a state file whose control token is well-formed but+ // bound to another host. With no seed there is nothing to compare it to.+ await persistRuntimeAuthTokens({+ accessToken: 'foreign-access',+ refreshToken: 'foreign-refresh',+ mcpHostControlToken: boot.foreignToken,+ })++ const fetchImpl = capturingFetch()+ const provider = createBrokerTokenProvider(+ { name: 'mcp-clickup' },+ { userId: 'alice' },+ { ...brokerTokenProviderDeps(), fetchImpl: fetchImpl as unknown as typeof fetch }+ )++ await expect(provider.resolve()).resolves.toBeUndefined()+ expect(fetchImpl).not.toHaveBeenCalled()+ })++ it('resolves undefined without calling the broker when there is no seed and no state file', async () => {+ const dir = useStateDir()+ fs.rmSync(dir, { recursive: true, force: true })++ const fetchImpl = capturingFetch()+ const provider = createBrokerTokenProvider(+ { name: 'mcp-clickup' },+ { userId: 'alice' },+ { ...brokerTokenProviderDeps(), fetchImpl: fetchImpl as unknown as typeof fetch }+ )++ await expect(provider.resolve()).resolves.toBeUndefined()+ expect(fetchImpl).not.toHaveBeenCalled()+ })+})