Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(control-api): scope the pre-registered client_id conflict to its issuer
Jose Gabriel Ramirez Encinas2ec41dc5e369
diff --git a/control-api/package-lock.json b/control-api/package-lock.jsonindex 5174523c5..1b0663181 100644--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-api",- "version": "0.1.703",+ "version": "0.1.704","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.703",+ "version": "0.1.704","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 7a971fdfa..6f1d09e83 100644--- a/control-api/package.json+++ b/control-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-api",- "version": "0.1.703",+ "version": "0.1.704","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/dynamicClientStore.ts b/control-api/src/oauth/dynamicClientStore.tsindex cae4f6b9f..0dbac6109 100644--- a/control-api/src/oauth/dynamicClientStore.ts+++ b/control-api/src/oauth/dynamicClientStore.ts@@ -247,20 +247,21 @@ export async function getDynamicClient(}/**- * Whether any dynamic client in this server namespace carries `clientId`, pending,- * bound or legacy alike: every row stands for a client that may still be live at its- * AS (teardown deletes the row). Reads no encrypted column.+ * Whether any dynamic client of `issuer` in this server namespace carries `clientId`,+ * pending, bound or legacy alike: every row stands for a client that may still be live+ * at its AS (teardown deletes the row). A client_id is only unique within its AS, so a+ * row of another issuer is another client. Reads no encrypted column.*/export async function isDynamicClientIdRegistered(db: DbClient,- input: { serverNamespace: string; clientId: string; ownerKind?: 'mcpserver' }+ input: { serverNamespace: string; issuer: string; clientId: string; ownerKind?: 'mcpserver' }): Promise<boolean> {const result = await db.query(`SELECT 1FROM dynamic_clients- WHERE owner_kind = $1 AND server_namespace = $2 AND client_id = $3+ WHERE owner_kind = $1 AND server_namespace = $2 AND client_id = $3 AND issuer = $4LIMIT 1`,- [resolveOwnerKind(input), input.serverNamespace, input.clientId]+ [resolveOwnerKind(input), input.serverNamespace, input.clientId, input.issuer])return result.rows.length > 0}diff --git a/control-api/src/routes/admin/remoteMcp.ts b/control-api/src/routes/admin/remoteMcp.tsindex eaef8d8d8..51ba1f08e 100644--- a/control-api/src/routes/admin/remoteMcp.ts+++ b/control-api/src/routes/admin/remoteMcp.ts@@ -387,24 +387,36 @@ export type PreRegisteredClientIdConflict = 'cimd_client' | 'remote_server' | 'd* per-server client must belong to exactly one server: its one registered redirect URI* is what binds its codes to that server, and a client shared with another server (or* one of our DCR clients, or the platform CIMD identity, which lists the shared- * callback) would let the AS deliver a code for one server to another. Check-then-create- * without a fence: an admin-only action on a confidential client, where a lost race- * still leaves each server with its own redirect URI.+ * callback) would let the AS deliver a code for one server to another. A client_id is+ * only unique within its AS, so servers and DCR clients of another issuer never+ * conflict: that AS holds a different client, with its own redirect URIs. Issuers are+ * compared exactly, as discovery pins them (RFC 8414: the metadata `issuer` must equal+ * the URL it was fetched from). Check-then-create without a fence: an admin-only action+ * on a confidential client, where a lost race still leaves each server with its own+ * redirect URI.*/async function preRegisteredClientIdConflict(gateway: K8sGateway,db: DbClient,namespace: string,+ issuer: string,clientId: string): Promise<PreRegisteredClientIdConflict | null> {if (isCimdClientId(clientId)) return 'cimd_client'const servers = (await gateway.listResource('mcpservers', namespace)) as Array<{- spec?: { oauth?: { source?: unknown; id?: unknown } }+ spec?: { oauth?: { source?: unknown; id?: unknown; issuer?: unknown } }}>- if (servers.some(s => s?.spec?.oauth?.source === 'remote' && s.spec.oauth.id === clientId)) {+ if (+ servers.some(+ s =>+ s?.spec?.oauth?.source === 'remote' &&+ s.spec.oauth.id === clientId &&+ s.spec.oauth.issuer === issuer+ )+ ) {return 'remote_server'}- if (await isDynamicClientIdRegistered(db, { serverNamespace: namespace, clientId })) {+ if (await isDynamicClientIdRegistered(db, { serverNamespace: namespace, issuer, clientId })) {return 'dynamic_client'}return null@@ -807,6 +819,7 @@ export function createAdminRemoteMcpRouter(gateway,db,config.mcpServersNamespace,+ discovery.issuer,body.clientId as string)if (conflict !== null) {diff --git a/control-api/test/fixtures/remoteOAuthDiscovery.ts b/control-api/test/fixtures/remoteOAuthDiscovery.tsindex fd25aafc6..5662bad53 100644--- a/control-api/test/fixtures/remoteOAuthDiscovery.ts+++ b/control-api/test/fixtures/remoteOAuthDiscovery.ts@@ -644,13 +644,14 @@ export function makeInMemoryDynamicClientsDb(): {return { rows: [], rowCount: 0 }}if (text.includes('FROM dynamic_clients') && text.includes('client_id = $3')) {- // Existence by client_id within a namespace (pre-registered uniqueness).- const [owner_kind, server_namespace, client_id] = values+ // Existence by (issuer, client_id) within a namespace (pre-registered uniqueness).+ const [owner_kind, server_namespace, client_id, issuer] = valuesconst hit = [...rows.values()].some(row =>row.owner_kind === owner_kind &&row.server_namespace === server_namespace &&- row.client_id === client_id+ row.client_id === client_id &&+ row.issuer === issuer)return hit ? { rows: [{ '?column?': 1 }], rowCount: 1 } : { rows: [], rowCount: 0 }}diff --git a/control-api/test/oauth.dynamicClientStore.installIdentity.realPostgres.integration.test.ts b/control-api/test/oauth.dynamicClientStore.installIdentity.realPostgres.integration.test.tsindex d84212db5..6038d7923 100644--- a/control-api/test/oauth.dynamicClientStore.installIdentity.realPostgres.integration.test.ts+++ b/control-api/test/oauth.dynamicClientStore.installIdentity.realPostgres.integration.test.ts@@ -219,15 +219,31 @@ describeRealPostgres('dynamicClientStore install identity (real Postgres)', () =)await upsertDynamicClient(db, KEY, baseCreds(legacy, { clientId: `cid-l-${tag}` }))+ const issuer = 'https://as.example.com'for (const clientId of [`cid-p-${tag}`, `cid-b-${tag}`, `cid-l-${tag}`]) {- expect(await isDynamicClientIdRegistered(db, { serverNamespace: NS, clientId })).toBe(true)+ expect(await isDynamicClientIdRegistered(db, { serverNamespace: NS, issuer, clientId })).toBe(+ true+ )}expect(- await isDynamicClientIdRegistered(db, { serverNamespace: NS, clientId: `cid-none-${tag}` })+ await isDynamicClientIdRegistered(db, {+ serverNamespace: NS,+ issuer,+ clientId: `cid-none-${tag}`,+ })).toBe(false)expect(await isDynamicClientIdRegistered(db, {serverNamespace: `${NS}-other`,+ issuer,+ clientId: `cid-b-${tag}`,+ })+ ).toBe(false)+ // A client_id is only unique within its AS: the same id at another issuer is not ours.+ expect(+ await isDynamicClientIdRegistered(db, {+ serverNamespace: NS,+ issuer: 'https://other-as.example.org',clientId: `cid-b-${tag}`,})).toBe(false)diff --git a/control-api/test/routes.adminRemoteMcp.test.ts b/control-api/test/routes.adminRemoteMcp.test.tsindex 03a8e51f4..c95386eb3 100644--- a/control-api/test/routes.adminRemoteMcp.test.ts+++ b/control-api/test/routes.adminRemoteMcp.test.ts@@ -2324,6 +2324,54 @@ describe('POST /admin/mcp-servers/remote — per-server callback (AS without RFCawait expect(gw.getResource('mcpservers', 'cimd-pre', NS)).rejects.toThrow()})+ // A client_id is only unique within its AS: the same string at another provider is+ // another client, registered there with its own redirect URI.+ function atOtherProvider(result: DiscoveryResult): DiscoveryResult {+ return JSON.parse(+ JSON.stringify(result).split('https://mcp.notion.com').join('https://auth.other-provider.io')+ ) as DiscoveryResult+ }++ it('I17 the same client_id at another provider (remote server) → 201', async () => {+ const gw = gatewayWithContext('ctx-a')+ const { db } = makeInMemoryDynamicClientsDb()+ const other = atOtherProvider(notionResult)+ expect(other.issuer).toBe('https://auth.other-provider.io')+ expect((await installPreRegistered(gw, { db }, 'first-server', 'shared-cid')).status).toBe(201)+ const res = await installPreRegistered(gw, { db }, 'second-server', 'shared-cid', other)+ expect(res.status).toBe(201)+ })++ it('I17 the same client_id as a DCR client at another provider → 201', async () => {+ const gw = gatewayWithContext('ctx-a')+ const { db, rows } = makeInMemoryDynamicClientsDb()+ const { transport } = makeDcrTransport({+ responseJson: JSON.stringify(DCR_CONFIDENTIAL_REGISTRATION_RESPONSE),+ redirectUris: 'requested',+ })+ mockDiscovery(dcrConfNoIssResult)+ const dcr = await request(app(gw, { db, dcr: { transport, resolveDns: PUBLIC_IP } }))+ .post('/admin/mcp-servers/remote')+ .send({+ serverName: 'dcr-server',+ contextRef: 'ctx-a',+ baseUrl: 'https://mcp.notion.com/mcp',+ mode: 'dcr',+ })+ expect(dcr.status).toBe(201)+ const dcrClientId = [...rows.values()][0].client_id as string+ await gw.deleteResource('mcpservers', 'dcr-server', NS)++ const res = await installPreRegistered(+ gw,+ { db },+ 'pre-server',+ dcrClientId,+ atOtherProvider(notionResult)+ )+ expect(res.status).toBe(201)+ })+it('I17 does not constrain a shared (RFC 9207) pre-registered install → 201', async () => {const gw = gatewayWithContext('ctx-a')const { db } = makeInMemoryDynamicClientsDb()