Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(control-api): fail closed on the shared remote oauth callback with no pinned issuer (R3-H2)
Diego Zacariasb010a813cd0f
diff --git a/control-api/package-lock.json b/control-api/package-lock.jsonindex ef0764a9f..d50228298 100644--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-api",- "version": "0.1.660",+ "version": "0.1.661","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.660",+ "version": "0.1.661","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.jsonindex e2574651f..579090ab4 100644--- a/control-api/package.json+++ b/control-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-api",- "version": "0.1.660",+ "version": "0.1.661","description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets","main": "dist/main.js","engines": {diff --git a/control-api/src/oauth/__tests__/callbackRemoteIss.test.ts b/control-api/src/oauth/__tests__/callbackRemoteIss.test.tsindex e6fe861bd..6231c9ab1 100644--- a/control-api/src/oauth/__tests__/callbackRemoteIss.test.ts+++ b/control-api/src/oauth/__tests__/callbackRemoteIss.test.ts@@ -17,12 +17,15 @@ import { signOAuthState } from '../state.js'** The remote AS issuer is discovered and pinned on the McpServer CR* (`spec.oauth.issForCallback`), surfaced as `subject.decl.remote.issForCallback`.- * The callback must read the authorization-response `iss` (`input.iss`) and, when- * an issuer was advertised, require an exact match BEFORE the single-use auth-code- * is exchanged — the AS mix-up defence. When no issuer was advertised the check is- * skipped (fail-open by absence of producer data). These tests pin the observable- * outcome (T4): a mismatch/absent `iss` short-circuits to `issuer_mismatch` with no- * exchange and no persist; a match (or a not-advertised issuer) exchanges normally.+ * The callback must read the authorization-response `iss` (`input.iss`) and require+ * an exact match BEFORE the single-use auth-code is exchanged — the AS mix-up+ * defence. The remote callback is SHARED across every remote subject, so it FAILS+ * CLOSED (R3-H2): a remote subject with no pinned issuer is rejected outright+ * (`issuer_binding_required`) rather than skipping the check, because a malicious+ * installed AS that omitted `iss` at discovery could otherwise relay the code from+ * an honest AS. These tests pin the observable outcome (T4): a mismatched, absent,+ * or unpinned issuer short-circuits BEFORE exchange with no exchange and no+ * persist; only a pinned issuer matched by `input.iss` exchanges normally.*/const STATE_SECRET = 'test-state-secret-0123456789abcdef' // ≥ 32 chars@@ -182,9 +185,12 @@ describe('handleOAuthCallback — remote lane RFC 9207 iss validation (H-1)', ()expect(String(query.mock.calls[0][0])).toContain('INSERT INTO oauth_grants')})- it('skips the check when no issuer was advertised: exchange proceeds', async () => {+ // R3-H2: FAIL CLOSED. A remote subject that pinned no issuer at install can never+ // satisfy the mix-up defence, so the shared remote callback must reject it rather+ // than skip the check (the pre-fix behaviour was `ok` + exchange + persist).+ it('rejects a remote subject with no pinned issuer WITHOUT exchange or persist', async () => {const pinnedTransport = okPinnedTransport()- // Subject WITHOUT issForCallback (AS did not advertise `iss`) + absent input.iss.+ // Subject WITHOUT issForCallback (AS did not advertise `iss` at discovery).const deps = buildDeps({subject: remoteSubject(),pinnedTransport,@@ -192,10 +198,26 @@ describe('handleOAuthCallback — remote lane RFC 9207 iss validation (H-1)', ()const result = await handleOAuthCallback(input({ iss: undefined }), deps)- expect(result.kind).toBe('ok')- expect(pinnedTransport).toHaveBeenCalledTimes(1)- const query = deps.db.query as ReturnType<typeof vi.fn>- expect(query).toHaveBeenCalledTimes(1)- expect(String(query.mock.calls[0][0])).toContain('INSERT INTO oauth_grants')+ expect(result.kind).toBe('issuer_binding_required')+ expect(pinnedTransport).not.toHaveBeenCalled()+ expect(deps.db.query as ReturnType<typeof vi.fn>).not.toHaveBeenCalled()+ })++ // R3-H2 (relay case): the malicious server omitted `iss` at install, then relays+ // the user through an HONEST AS that DOES emit `iss`. A present, well-formed+ // callback `iss` must NOT rescue an unpinned remote subject — the skip was+ // governed by the malicious server's own (missing) metadata. Fail closed.+ it('rejects an unpinned remote subject even when a relayed iss is present', async () => {+ const pinnedTransport = okPinnedTransport()+ const deps = buildDeps({+ subject: remoteSubject(),+ pinnedTransport,+ })++ const result = await handleOAuthCallback(input({ iss: ADVERTISED_ISS }), deps)++ expect(result.kind).toBe('issuer_binding_required')+ expect(pinnedTransport).not.toHaveBeenCalled()+ expect(deps.db.query as ReturnType<typeof vi.fn>).not.toHaveBeenCalled()})})diff --git a/control-api/src/oauth/callback.ts b/control-api/src/oauth/callback.tsindex 2f079b3c0..0d2bf0c72 100644--- a/control-api/src/oauth/callback.ts+++ b/control-api/src/oauth/callback.ts@@ -308,6 +308,13 @@ export type CallbackResult =* exchanged. No value fields — the issuer strings are NEVER echoed back.*/| { kind: 'issuer_mismatch' }+ /**+ * RFC 9207 mix-up defence, absence case: a remote subject reached the SHARED+ * remote callback but no issuer was pinned at install (`issForCallback` absent),+ * so the callback cannot attribute the code to an authorization server. Fail+ * closed BEFORE the single-use code is exchanged. No value fields.+ */+ | { kind: 'issuer_binding_required' }| { kind: 'unknown_oauth_client' }| { kind: 'recipe_not_found' }/** mcp subject: the McpServer named in the signed state does not exist / is not OAuth. */@@ -499,12 +506,24 @@ async function handleMcpOAuthCallback(// the honest issuer at discovery (`issForCallback`) and require the callback's// `iss` to match it here — BEFORE the single-use code is ever exchanged.//- // Skip when `issForCallback` is absent/empty: the AS did not advertise `iss`- // in its discovery document, so there is nothing to compare against. This is- // fail-open by ABSENCE OF PRODUCER DATA, not a lax choice. The baked lane has- // no `decl.remote`, so `expectedIss` is always absent there and it never runs.- const expectedIss = subject.decl.remote?.issForCallback- if (typeof expectedIss === 'string' && expectedIss.length > 0) {+ // FAIL CLOSED for every remote subject. The stable remote callback+ // (`/oauth-callback/remote`) is SHARED across all remote subjects (CIMD/DCR/+ // pre-registered register ONE fixed redirect_uri), so whether the check runs+ // must NOT be governed by the resolved AS's own advertised metadata: a+ // malicious installed AS whose discovery omitted `iss` (⇒ `issForCallback`+ // absent) could relay the user to an honest AS sharing this redirect/state/PKCE+ // and have the honest code attributed to itself. So a remote subject with no+ // pinned issuer can never satisfy the defence — reject rather than bind a code+ // we cannot attribute. The baked/generic lanes have no `decl.remote`; they are+ // unaffected (this is a remote-lane defence only).+ if (subject.decl.remote) {+ const expectedIss = subject.decl.remote.issForCallback+ if (typeof expectedIss !== 'string' || expectedIss.length === 0) {+ // No issuer pinned at install → the mix-up defence is unsatisfiable for this+ // server. (An install-time guard would surface this to the operator earlier+ // instead of the user at consent — tracked as a follow-up.)+ return { kind: 'issuer_binding_required' }+ }// Fail closed on any deviation, INCLUDING an absent/empty callback `iss`.if (input.iss !== expectedIss) {return { kind: 'issuer_mismatch' }diff --git a/control-api/src/routes/external/oauthCallback.ts b/control-api/src/routes/external/oauthCallback.tsindex 0d9fd6cf6..0b7e68898 100644--- a/control-api/src/routes/external/oauthCallback.ts+++ b/control-api/src/routes/external/oauthCallback.ts@@ -150,6 +150,11 @@ export function createOAuthCallbackRouter(gateway: K8sGateway): Router {// RFC 9207 mix-up defence — no issuer echo. 400, consistent with the// sibling invalid_state mapping.return res.status(400).json({ error: 'issuer_mismatch' })+ case 'issuer_binding_required':+ // RFC 9207 mix-up defence, absence case: the remote server pinned no+ // issuer at install, so the shared remote callback cannot attribute the+ // code. Fail closed — same 400 class, distinct error for operator triage.+ return res.status(400).json({ error: 'issuer_binding_required' })case 'unknown_oauth_client':return res.status(400).json({ error: 'unknown_oauth_client' })case 'recipe_not_found':diff --git a/control-api/test/oauth.callback.remote.test.ts b/control-api/test/oauth.callback.remote.test.tsindex 58a3bdf0c..5ccae0d2d 100644--- a/control-api/test/oauth.callback.remote.test.ts+++ b/control-api/test/oauth.callback.remote.test.ts@@ -53,6 +53,9 @@ function remoteServerCr() {authorizationEndpoint: 'https://mcp.notion.com/authorize',tokenEndpoint: TOKEN_ENDPOINT,issuer: 'https://mcp.notion.com',+ // Pinned RFC 9207 issuer: a compliant remote AS advertises `iss`, and the+ // shared remote callback now fails closed without it (R3-H2).+ issForCallback: 'https://mcp.notion.com',resource: 'https://mcp.notion.com',grantScope: 'user',scopes: ['read'],@@ -118,6 +121,9 @@ function remoteInput(): CallbackInput {code: 'AUTH_CODE',state: remoteState(),redirectUri: 'https://control.example.com/api/v1/oauth-callback/remote',+ // RFC 9207 issuer echoed by the authorization response; matches the pinned+ // `issForCallback` so the mix-up defence passes and the exchange proceeds.+ iss: 'https://mcp.notion.com',}}