Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): fail closed on a malformed broker expiresAt (R4-L2)
Diego Zacarias82148d869a9d
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.jsonindex 9c8f272d3..b0ac82003 100644--- a/mcp-host/package-lock.json+++ b/mcp-host/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-mcp-host",- "version": "0.1.294",+ "version": "0.1.295","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-mcp-host",- "version": "0.1.294",+ "version": "0.1.295","license": "SEE LICENSE IN LICENSE","dependencies": {"@anthropic-ai/sdk": "^0.32.0",diff --git a/mcp-host/package.json b/mcp-host/package.jsonindex a45cd59f5..77e36b8e6 100644--- a/mcp-host/package.json+++ b/mcp-host/package.json@@ -1,6 +1,6 @@{"name": "clerum-mcp-host",- "version": "0.1.294",+ "version": "0.1.295","description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude","main": "dist/main.js","engines": {diff --git a/mcp-host/src/mcp/__tests__/brokerTokenProvider.test.ts b/mcp-host/src/mcp/__tests__/brokerTokenProvider.test.tsindex 5927c9214..ff6185785 100644--- a/mcp-host/src/mcp/__tests__/brokerTokenProvider.test.ts+++ b/mcp-host/src/mcp/__tests__/brokerTokenProvider.test.ts@@ -168,6 +168,41 @@ describe('createBrokerTokenProvider — fail-closed contract', () => {await expect(p.resolve()).rejects.toThrow(/malformed/)})+ it('200 with a present-but-malformed expiresAt string → throws (fail-closed) and caches nothing (R4-L2)', async () => {+ // A non-expiring token is expiresAt:null/absent. A PRESENT string that+ // Date.parse cannot read is a defect, not a non-expiring token: collapsing it+ // to null would reuse it under the NULL_EXPIRY_MAX_AGE_MS cap. It must fail+ // closed (throw), and leave no cache — a second resolve() must re-consult the+ // broker, never silently serve a stored token.+ const f = stubFetch(() => jsonResponse(200, { token: 'tok', expiresAt: 'not-a-date' }))+ const p = createBrokerTokenProvider({ name: 'gh' }, { userId: 'alice' }, deps(f))+ await expect(p.resolve()).rejects.toThrow(/expiresAt/)+ // No stale cache: the retry re-POSTs (still malformed → still throws), never a+ // silent token from a cached entry.+ await expect(p.resolve()).rejects.toThrow(/expiresAt/)+ expect(f).toHaveBeenCalledTimes(2)+ })++ it('200 with a present non-string expiresAt (number) → throws (fail-closed)', async () => {+ // Any present-but-non-string expiresAt is malformed too — only absent/null is+ // the legitimate non-expiring shape.+ const f = stubFetch(() => jsonResponse(200, { token: 'tok', expiresAt: 1_700_000_000_000 }))+ const p = createBrokerTokenProvider({ name: 'gh' }, { userId: 'alice' }, deps(f))+ await expect(p.resolve()).rejects.toThrow(/expiresAt/)+ })++ it('200 with expiresAt:null → non-expiring, returns the token (the malformed frontier)', async () => {+ // Fixes the boundary against the malformed case above: explicit null is the+ // valid non-expiring contract (control-api emits it for Notion/ClickUp) and+ // still returns a cacheable token.+ const f = stubFetch(() => jsonResponse(200, { token: 'tok', expiresAt: null }))+ const p = createBrokerTokenProvider({ name: 'gh' }, { userId: 'alice' }, deps(f))+ expect(await p.resolve()).toBe('tok')+ // Cached as non-expiring — a second resolve serves from cache (no re-POST).+ expect(await p.resolve()).toBe('tok')+ expect(f).toHaveBeenCalledTimes(1)+ })+it('missing gateway URL → undefined, no fetch', async () => {const f = stubFetch(() => jsonResponse(200, { token: 't', expiresAt: null }))const p = createBrokerTokenProvider(diff --git a/mcp-host/src/mcp/brokerTokenProvider.ts b/mcp-host/src/mcp/brokerTokenProvider.tsindex 5339a5655..f9a03f72c 100644--- a/mcp-host/src/mcp/brokerTokenProvider.ts+++ b/mcp-host/src/mcp/brokerTokenProvider.ts@@ -152,10 +152,34 @@ export function createBrokerTokenProvider(if (typeof data.token !== 'string' || data.token.length === 0) {throw new Error(`mcp-oauth broker returned malformed 200 body for ${server.name}`)}- const parsed = typeof data.expiresAt === 'string' ? Date.parse(data.expiresAt) : NaN+ // Distinguish a legitimately non-expiring token from a malformed one, so a+ // corrupt expiry never collapses to null (= non-expiring, reusable under the+ // NULL_EXPIRY_MAX_AGE_MS cap). control-api emits `expiresAt:null` (or omits+ // it) for upstreams whose token never expires — Notion/ClickUp always — so+ // absent/explicit-null is a valid non-expiring contract. But an expiresAt+ // that is PRESENT and non-parseable (a string Date.parse can't read, or any+ // non-string type) is a defect: fail closed rather than treat it as+ // non-expiring (R4-L2, composes with R1-M1). server.name is non-PII; never+ // include the token in the error.+ let expiresAtMs: number | null+ if (data.expiresAt === undefined || data.expiresAt === null) {+ expiresAtMs = null+ } else if (typeof data.expiresAt === 'string') {+ const parsed = Date.parse(data.expiresAt)+ if (Number.isNaN(parsed)) {+ throw new Error(+ `mcp-oauth broker returned 200 with an unparseable expiresAt for ${server.name}`+ )+ }+ expiresAtMs = parsed+ } else {+ throw new Error(+ `mcp-oauth broker returned 200 with an invalid expiresAt type for ${server.name}`+ )+ }cached = {token: data.token,- expiresAtMs: Number.isNaN(parsed) ? null : parsed,+ expiresAtMs,cachedAtMs: now(),}return data.token