Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(control-api): reject a remote oauth install when the AS lacks RFC 9207 (R3F-H1)
Diego Zacarias08477359fe96
diff --git a/control-api/package-lock.json b/control-api/package-lock.jsonindex cf9d31a5d..47ecec061 100644--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-api",- "version": "0.1.662",+ "version": "0.1.663","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.662",+ "version": "0.1.663","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 91727b853..c1b2ad12b 100644--- a/control-api/package.json+++ b/control-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-api",- "version": "0.1.662",+ "version": "0.1.663","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/admin/remoteMcp.ts b/control-api/src/routes/admin/remoteMcp.tsindex ca834b7e1..e8ced5ea6 100644--- a/control-api/src/routes/admin/remoteMcp.ts+++ b/control-api/src/routes/admin/remoteMcp.ts@@ -487,6 +487,29 @@ export function createAdminRemoteMcpRouter()}+ // RFC 9207 issuer binding is MANDATORY on the remote lane. Every remote mode+ // (dcr/cimd/pre-registered) lands on the SHARED remote callback+ // (`/oauth-callback/remote`), whose mix-up defence fails closed when no issuer is+ // pinned (`issuer_binding_required`, R3-H2). `discovery.issForCallback` is set iff+ // the AS advertised `authorization_response_iss_parameter_supported`. Reject at+ // install — BEFORE any AS registration (DCR saga step 0) or K8s/store write — so+ // the operator learns now instead of the user hitting a 400 at consent against a+ // server that can never complete OAuth (the regression this closes, R3F-H1).+ // Ordered AFTER the bearerInBody/mode-match/transport admission checks so their+ // more specific rejections keep precedence; still before any write.+ if (!discovery.issForCallback) {+ log.warn(+ { event: 'remote_oauth_issuer_binding_unsupported', serverName: body.serverName },+ 'remote install rejected: authorization server does not advertise RFC 9207 (authorization_response_iss_parameter_supported)'+ )+ res.status(422).json({+ error: 'issuer_binding_required',+ message:+ 'the authorization server does not advertise RFC 9207 (authorization_response_iss_parameter_supported); a remote OAuth install requires it',+ })+ return+ }+// Client mode: pre-registered/CIMD are fixed by the mode; DCR derives it from// the AS auth methods (public iff `none`, else confidential) — NOT the body.const clientMode: 'public' | 'confidential' =diff --git a/control-api/test/oauth.callback.remote.test.ts b/control-api/test/oauth.callback.remote.test.tsindex 5ccae0d2d..85f21b36b 100644--- a/control-api/test/oauth.callback.remote.test.ts+++ b/control-api/test/oauth.callback.remote.test.ts@@ -36,13 +36,18 @@ const ENCRYPTION_KEY = deriveOAuthEncryptionKey()const MCP_NS = 'mcp-server'const VALIDATED_IP = '93.184.216.34'-const TOKEN_ENDPOINT = 'https://mcp.notion.com/token'+const TOKEN_ENDPOINT = 'https://mcp.sentry.dev/oauth/token'// Shape `buildRemoteOAuthSpec` emits for a CIMD-public remote server (kept inline-// so this file runs against the parent SHA — see file docstring).+// so this file runs against the parent SHA — see file docstring). Modeled on SENTRY+// (test/fixtures/remoteOAuthDiscovery.ts): its REAL AS metadata advertises+// `authorization_response_iss_parameter_supported`, so `issForCallback` is a shape+// the discovery producer actually emits. Notion does NOT advertise RFC 9207 (see+// oauth.discovery.test.ts) — a Notion CR with issForCallback is impossible, and the+// remote install now rejects such servers (R3F-H1), so this fixture uses Sentry.function remoteServerCr() {return {- metadata: { name: 'notion-remote', namespace: MCP_NS },+ metadata: { name: 'sentry-remote', namespace: MCP_NS },spec: {contextRef: 'ctx-A',auth: { type: 'oauth' },@@ -50,13 +55,13 @@ function remoteServerCr() {source: 'remote',id: 'https://control.example.com/api/v1/.well-known/evenfire-mcp-client',clientMode: 'public',- authorizationEndpoint: 'https://mcp.notion.com/authorize',+ authorizationEndpoint: 'https://mcp.sentry.dev/oauth/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',+ issuer: 'https://mcp.sentry.dev',+ // Pinned RFC 9207 issuer: Sentry's real AS advertises it, and the shared+ // remote callback fails closed without it (R3-H2).+ issForCallback: 'https://mcp.sentry.dev',+ resource: 'https://mcp.sentry.dev/mcp',grantScope: 'user',scopes: ['read'],bearerInBody: false,@@ -90,7 +95,7 @@ function recordingTransport(responseJson: string, status = 200) {const transport = async (input: PinnedTransportInput): Promise<PinnedRawResponse> => {let connectedIP: string | undefinedinput.lookup(- 'mcp.notion.com',+ 'mcp.sentry.dev',{ all: true } as never,((err, addrs) => {if (!err && Array.isArray(addrs)) connectedIP = addrs[0]?.address@@ -107,7 +112,7 @@ function remoteState() {// stable `remote` (CIMD/DCR register one fixed redirect_uri).return signOAuthState(STATE_SECRET, {subjectKind: 'mcp',- mcpServerName: 'notion-remote',+ mcpServerName: 'sentry-remote',userId: 'user-9',oauthClientId: REMOTE_CLIENT_ID,grantKind: 'user',@@ -123,7 +128,7 @@ function remoteInput(): CallbackInput {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',+ iss: 'https://mcp.sentry.dev',}}diff --git a/control-api/test/routes.adminRemoteMcp.test.ts b/control-api/test/routes.adminRemoteMcp.test.tsindex 98ec188ea..f8d788995 100644--- a/control-api/test/routes.adminRemoteMcp.test.ts+++ b/control-api/test/routes.adminRemoteMcp.test.ts@@ -104,13 +104,34 @@ function gatewayWithContext(contextName = 'ctx-a'): MockGateway {return gw}-// Real DiscoveryResult, derived from the real Notion probe fixtures (T1).+// Real DiscoveryResult, derived from the real Notion probe fixtures (T1). Notion's+// real AS metadata does NOT advertise RFC 9207 (see oauth.discovery.test.ts), so this+// carries NO `issForCallback` — the fixture the install must now REJECT (R3F-H1).let notionResult: DiscoveryResult+// A DiscoveryResult that DOES advertise RFC 9207, derived from the real producer (T1)+// by documented substitution (like `bodyBearerResult`): the real Notion pilot with+// `authorization_response_iss_parameter_supported:true` added to the AS metadata, then+// run through the real discovery client so `issForCallback` is producer-derived, not+// hand-authored. Every remote install SUCCESS path needs an RFC-9207 AS now that the+// install guard is fail-closed; all other endpoints stay Notion's, so success-path+// assertions are unchanged except for the added `issForCallback`.+let rfc9207Result: DiscoveryResult// A DiscoveryResult whose resource requires the token in the BODY, derived from the// real producer (T1) by documented substitution of the Notion PRM's// `bearer_methods_supported` to `["body"]` — the only field changed.let bodyBearerResult: DiscoveryResult+/**+ * Documented T1 substitution: add `authorization_response_iss_parameter_supported`+ * to a pilot's AS metadata (parsed-object level, key-order agnostic) so the real+ * discovery producer derives `issForCallback`. Never hand-authors the DiscoveryResult.+ */+function withRfc9207<T extends { as: { json: string } }>(pilot: T): T {+ const as = JSON.parse(pilot.as.json)+ as.authorization_response_iss_parameter_supported = true+ return { ...pilot, as: { ...pilot.as, json: JSON.stringify(as) } }+}+beforeAll(async () => {const actual = await vi.importActual<typeof import('../src/oauth/discovery.js')>('../src/oauth/discovery.js'@@ -122,6 +143,14 @@ beforeAll(async () => {if (!outcome.ok) throw new Error(`fixture discovery failed: ${outcome.error.kind}`)notionResult = outcome.result+ const rfc9207Outcome = await actual.discoverRemoteOAuth(PILOTS.notion.mcpUrl, {+ transport: makeDiscoveryTransport(withRfc9207(PILOTS.notion)),+ resolveDns: async () => ['93.184.216.34'],+ })+ if (!rfc9207Outcome.ok)+ throw new Error(`rfc9207 fixture discovery failed: ${rfc9207Outcome.error.kind}`)+ rfc9207Result = rfc9207Outcome.result+const vercelOutcome = await actual.discoverRemoteOAuth(VERCEL_PILOT.mcpUrl, {transport: makeDiscoveryTransport(VERCEL_PILOT),resolveDns: async () => ['93.184.216.34'],@@ -257,7 +286,7 @@ describe('POST /admin/mcp-servers/remote (install saga)', () => {const CIMD_SELF_CLIENT_ID = 'https://control.example.com/api/v1/.well-known/evenfire-mcp-client'it('CIMD (public): creates the CR with the pinned spec.oauth shape + attaches to Context', async () => {- mockDiscovery(notionResult)+ mockDiscovery(rfc9207Result)const gw = gatewayWithContext('ctx-a')const res = await request(makeApp(gw)).post('/admin/mcp-servers/remote').send({serverName: 'notion-remote',@@ -289,6 +318,8 @@ describe('POST /admin/mcp-servers/remote (install saga)', () => {tokenEndpoint: 'https://mcp.notion.com/token',registrationEndpoint: 'https://mcp.notion.com/register',issuer: 'https://mcp.notion.com',+ // RFC 9207 advertised (rfc9207Result) ⇒ pinned for the shared remote callback.+ issForCallback: 'https://mcp.notion.com',resource: 'https://mcp.notion.com',grantScope: 'user',scopes: ['default'],@@ -303,6 +334,41 @@ describe('POST /admin/mcp-servers/remote (install saga)', () => {expect(ctx.spec.mcpServers).toContain('notion-remote')})+ // R3F-H1 (regression of R3-H2): the shared remote callback fails closed without a+ // pinned issuer, so an install against an AS that does NOT advertise RFC 9207 would+ // create a server that can never complete OAuth (user hits 400 at consent). The+ // install must reject up front. `notionResult` is the REAL Notion discovery (no+ // `authorization_response_iss_parameter_supported`), derived from the real producer —+ // NOT hand-authored. Fails at parent a835d7130 (install proceeds, 201 + CR).+ it('rejects a remote install against an AS without RFC 9207 → 422, no CR, no Secret', async () => {+ // Self-check the T1 fixture: real Notion genuinely lacks the issuer pin.+ expect(notionResult.issForCallback).toBeUndefined()+ mockDiscovery(notionResult)+ const gw = gatewayWithContext('ctx-a')+ const createResourceSpy = vi.spyOn(gw, 'createResource')+ const createSecretSpy = vi.spyOn(gw, 'createSecret')++ const res = await request(makeApp(gw)).post('/admin/mcp-servers/remote').send({+ serverName: 'no-rfc9207-remote',+ contextRef: 'ctx-a',+ baseUrl: 'https://mcp.notion.com/mcp',+ mode: 'pre-registered',+ clientId: 'client-abc',+ clientSecret: 'shhh-secret',+ })++ // Observable outcome (T4): rejected before any write.+ expect(res.status).toBe(422)+ expect(res.body.error).toBe('issuer_binding_required')+ expect(createResourceSpy).not.toHaveBeenCalled()+ expect(createSecretSpy).not.toHaveBeenCalled()+ await expect(gw.getResource('mcpservers', 'no-rfc9207-remote', NS)).rejects.toThrow()+ const ctx = (await gw.getResource('contexts', 'ctx-a', NS)) as {+ spec: { mcpServers?: string[] }+ }+ expect(ctx.spec.mcpServers ?? []).not.toContain('no-rfc9207-remote')+ })+it('body-bearer resource: rejects at admission and creates no CR (the runtime cannot honor bearerInBody)', async () => {// Self-check the T1 fixture: the real producer must have derived the body-bearer// quirk, or this test would pass vacuously against any server.@@ -327,7 +393,7 @@ describe('POST /admin/mcp-servers/remote (install saga)', () => {})it('pre-registered confidential: creates the client Secret + references it in spec.oauth', async () => {- mockDiscovery(notionResult)+ mockDiscovery(rfc9207Result)const gw = gatewayWithContext('ctx-a')const res = await request(makeApp(gw)).post('/admin/mcp-servers/remote').send({serverName: 'slack-remote',@@ -411,7 +477,7 @@ describe('POST /admin/mcp-servers/remote (install saga)', () => {})it('rolls back the Secret when the CR create fails', async () => {- mockDiscovery(notionResult)+ mockDiscovery(rfc9207Result)const gw = gatewayWithContext('ctx-a')const k8sErr = Object.assign(new Error('already exists'), { code: 409 })vi.spyOn(gw, 'createResource').mockRejectedValueOnce(k8sErr)@@ -429,7 +495,7 @@ describe('POST /admin/mcp-servers/remote (install saga)', () => {})it('rolls back the CR AND Secret when the Context attach fails', async () => {- mockDiscovery(notionResult)+ mockDiscovery(rfc9207Result)// Context does NOT exist → getResource('contexts', …) throws K8sNotFoundError.const gw = new MockGateway(NS)const res = await request(makeApp(gw)).post('/admin/mcp-servers/remote').send({@@ -447,7 +513,7 @@ describe('POST /admin/mcp-servers/remote (install saga)', () => {})it('fenced Secret rollback: a homonym recreated with a NEW uid survives the CR-create rollback (P1)', async () => {- mockDiscovery(notionResult)+ mockDiscovery(rfc9207Result)const gw = gatewayWithContext('ctx-a')const secretName = 'race-remote-oauth-client'@@ -487,7 +553,7 @@ describe('POST /admin/mcp-servers/remote (install saga)', () => {})it('fenced CR rollback: a homonym recreated with a NEW uid survives the Context-attach rollback (P1)', async () => {- mockDiscovery(notionResult)+ mockDiscovery(rfc9207Result)const gw = gatewayWithContext('ctx-a')const serverName = 'race2-remote'@@ -526,7 +592,7 @@ describe('POST /admin/mcp-servers/remote (install saga)', () => {})it('forces the namespace server-side (caller-supplied namespace is ignored)', async () => {- mockDiscovery(notionResult)+ mockDiscovery(rfc9207Result)const gw = gatewayWithContext('ctx-a')const res = await request(makeApp(gw)).post('/admin/mcp-servers/remote').send({serverName: 'ns-remote',@@ -603,7 +669,7 @@ describe('POST /admin/mcp-servers/remote (install saga)', () => {// An inconclusive probe (5xx) must NOT block: the install proceeds to 201.it('install proceeds to 201 when the transport probe is inconclusive (5xx)', async () => {- mockDiscovery(notionResult)+ mockDiscovery(rfc9207Result)const gw = gatewayWithContext('ctx-a')const res = await request(makeApp(gw, inconclusiveProbe)).post('/admin/mcp-servers/remote')@@ -631,22 +697,39 @@ describe('POST /admin/mcp-servers/remote — DCR install saga (C2)', () => {// probe with CIMD support removed (→ DCR) and, for confidential, `none` dropped.let dcrPublicResult: DiscoveryResultlet dcrConfidentialResult: DiscoveryResult+ // Real DCR DiscoveryResult WITHOUT RFC 9207 (pristine `dcrPilot`, no substitution):+ // the fixture the install must reject before any AS registration (R3F-H1).+ let dcrNoIssResult: DiscoveryResultbeforeAll(async () => {const actual = await vi.importActual<typeof import('../src/oauth/discovery.js')>('../src/oauth/discovery.js')+ const noIssPilot = dcrPilot('public')+ const noIssOutcome = await actual.discoverRemoteOAuth(noIssPilot.mcpUrl, {+ transport: makeDiscoveryTransport(noIssPilot),+ resolveDns: PUBLIC_IP,+ })+ if (!noIssOutcome.ok)+ throw new Error(`dcr no-iss fixture discovery failed: ${noIssOutcome.error.kind}`)+ expect(noIssOutcome.result.issForCallback).toBeUndefined()+ dcrNoIssResult = noIssOutcome.result+for (const [mode, assign] of [['public', (r: DiscoveryResult) => (dcrPublicResult = r)],['confidential', (r: DiscoveryResult) => (dcrConfidentialResult = r)],] as const) {- const pilot = dcrPilot(mode)+ // RFC 9207 is mandatory for a remote install (R3F-H1); a DCR AS that omits it+ // is rejected before registration. Add the advertisement via the same+ // producer-derived substitution so the DCR success paths exercise the saga.+ const pilot = withRfc9207(dcrPilot(mode))const outcome = await actual.discoverRemoteOAuth(pilot.mcpUrl, {transport: makeDiscoveryTransport(pilot),resolveDns: PUBLIC_IP,})if (!outcome.ok) throw new Error(`dcr fixture discovery failed: ${outcome.error.kind}`)expect(outcome.result.registrationMode).toBe('dcr')+ expect(outcome.result.issForCallback).toBeTruthy()assign(outcome.result)}})@@ -677,6 +760,37 @@ describe('POST /admin/mcp-servers/remote — DCR install saga (C2)', () => {return app}+ // R3F-H1: a DCR AS without RFC 9207 must be rejected BEFORE the DCR registration+ // POST (saga step 0), so no throwaway client is minted at the AS, no dynamic_clients+ // row is written, and no CR is created. Fails at parent a835d7130 (install registers+ // + persists + creates the CR). `dcrNoIssResult` is producer-derived (no iss), T1.+ it('rejects a DCR install against an AS without RFC 9207 → 422, no DCR registration, no row, no CR', async () => {+ vi.mocked(discoverRemoteOAuth).mockResolvedValue({ ok: true, result: dcrNoIssResult })+ const { db, rows } = makeInMemoryDynamicClientsDb()+ const { transport, calls } = makeDcrTransport({+ responseJson: JSON.stringify(DCR_PUBLIC_REGISTRATION_RESPONSE),+ })+ const gw = gatewayWithContext('ctx-a')+ const res = await request(+ makeAppWithDeps(gw, { db, dcr: { transport, resolveDns: PUBLIC_IP } })+ )+ .post('/admin/mcp-servers/remote')+ .send({+ serverName: 'no-rfc9207-dcr',+ contextRef: 'ctx-a',+ baseUrl: 'https://mcp.notion.com/mcp',+ mode: 'dcr',+ })++ // Observable outcome (T4): rejected before any AS registration or write.+ expect(res.status).toBe(422)+ expect(res.body.error).toBe('issuer_binding_required')+ // No DCR POST to the AS, no persisted dynamic_clients row, no CR.+ expect(calls).toHaveLength(0)+ expect(rows.size).toBe(0)+ await expect(gw.getResource('mcpservers', 'no-rfc9207-dcr', NS)).rejects.toThrow()+ })+// T3(a) — fails at parent 9e4677652: DCR install returns 400 dcr_not_available there.it('public DCR → 201, persists a dynamic_clients row, sets spec.oauth.id, no Secret refs', async () => {vi.mocked(discoverRemoteOAuth).mockResolvedValue({ ok: true, result: dcrPublicResult })