From e5b1ade40d8afd5fdfbc20b4684f576d43be747f Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 29 Oct 2026 18:37:56 -0400 Subject: [PATCH] feat: Add OAUTH_DISABLE_CIMD to turn off client ID metadata documents (#14410) --- server/env.ts | 10 ++++++++++ server/routes/discovery/mcp.ts | 3 ++- server/routes/oauth/oauth.test.ts | 17 +++++++++++++++++ server/utils/oauth/ClientMetadataHelper.test.ts | 16 ++++++++++++++++ server/utils/oauth/ClientMetadataHelper.ts | 7 +++++-- 5 files changed, 50 insertions(+), 3 deletions(-) diff --git a/server/env.ts b/server/env.ts index 60ba6df0a2..3b0028605c 100644 --- a/server/env.ts +++ b/server/env.ts @@ -834,6 +834,16 @@ export class Environment { environment.OAUTH_DISABLE_DCR ?? "false" ); + /** + * Whether to disable OAuth Client ID Metadata Documents (CIMD). When set to + * true, clients must use Dynamic Client Registration instead. Use this when + * the server cannot fetch metadata documents from client hosts. + */ + @IsBoolean() + public OAUTH_DISABLE_CIMD = this.toBoolean( + environment.OAUTH_DISABLE_CIMD ?? "false" + ); + /** * Enable unsafe-inline in script-src CSP directive */ diff --git a/server/routes/discovery/mcp.ts b/server/routes/discovery/mcp.ts index 85f84c4966..bc97c91a8f 100644 --- a/server/routes/discovery/mcp.ts +++ b/server/routes/discovery/mcp.ts @@ -24,7 +24,8 @@ router.get("/.well-known/oauth-authorization-server", async (ctx) => { mcpEnabled && { registration_endpoint: `${origin}/oauth/register`, }), - client_id_metadata_document_supported: mcpEnabled, + client_id_metadata_document_supported: + mcpEnabled && !env.OAUTH_DISABLE_CIMD, response_types_supported: OAuthHelper.responseTypes, response_modes_supported: ["query"], grant_types_supported: OAuthHelper.grantTypes, diff --git a/server/routes/oauth/oauth.test.ts b/server/routes/oauth/oauth.test.ts index 5be4d1f3a1..93ae7a8131 100644 --- a/server/routes/oauth/oauth.test.ts +++ b/server/routes/oauth/oauth.test.ts @@ -613,6 +613,23 @@ describe("GET /.well-known/oauth-authorization-server", () => { expect(body.client_id_metadata_document_supported).toBe(false); }); + it("should not advertise metadata documents when they are disabled", async () => { + const team = await buildTeam({ subdomain: buildSubdomain() }); + env.OAUTH_DISABLE_CIMD = true; + + try { + const res = await server.get("/.well-known/oauth-authorization-server", { + headers: { host: `${team.subdomain}.outline.dev` }, + }); + + const body = await res.json(); + expect(body.registration_endpoint).toContain("/oauth/register"); + expect(body.client_id_metadata_document_supported).toBe(false); + } finally { + env.OAUTH_DISABLE_CIMD = false; + } + }); + it("should not serve OAuth metadata at /mcp suffix path", async () => { const res = await server.get("/.well-known/oauth-authorization-server/mcp"); diff --git a/server/utils/oauth/ClientMetadataHelper.test.ts b/server/utils/oauth/ClientMetadataHelper.test.ts index b1c41e99ad..3fd8faf677 100644 --- a/server/utils/oauth/ClientMetadataHelper.test.ts +++ b/server/utils/oauth/ClientMetadataHelper.test.ts @@ -2,6 +2,7 @@ import { randomUUID } from "node:crypto"; import { http, HttpResponse } from "msw"; import { Minute, Day } from "@shared/utils/time"; import { TeamPreference } from "@shared/types"; +import env from "@server/env"; import type { Team } from "@server/models"; import { OAuthClient } from "@server/models"; import UploadOAuthClientLogoTask from "@server/queues/tasks/UploadOAuthClientLogoTask"; @@ -326,6 +327,21 @@ describe("ClientMetadataHelper", () => { expect(mock.requests()).toEqual(0); }); + it("should not fetch when metadata documents are disabled", async () => { + const clientId = buildClientId(); + const mock = mockDocument(clientId, buildDocument(clientId)); + env.OAUTH_DISABLE_CIMD = true; + + try { + expect( + await ClientMetadataHelper.findOrFetchByClientId(clientId, team) + ).toBeNull(); + expect(mock.requests()).toEqual(0); + } finally { + env.OAUTH_DISABLE_CIMD = false; + } + }); + it("should find a generated client without fetching", async () => { const client = await buildOAuthClient(); const found = await ClientMetadataHelper.findOrFetchByClientId( diff --git a/server/utils/oauth/ClientMetadataHelper.ts b/server/utils/oauth/ClientMetadataHelper.ts index 183844faf4..45d70bc6d8 100644 --- a/server/utils/oauth/ClientMetadataHelper.ts +++ b/server/utils/oauth/ClientMetadataHelper.ts @@ -107,8 +107,8 @@ export class ClientMetadataHelper { * * @param clientId The public client identifier. * @param team The team of the user that requests the client. - * @returns The OAuth client, or null if it is not found or the client_id is - * not a valid metadata document URL. + * @returns The OAuth client, or null if it is not found, the client_id is + * not a valid metadata document URL, or metadata documents are disabled. * @throws AuthorizationError if the client is a metadata document and the * team has turned MCP off. * @throws InvalidClientMetadataError if the metadata document cannot be @@ -121,6 +121,9 @@ export class ClientMetadataHelper { if (!OAuthClient.isMetadataDocumentClientId(clientId)) { return OAuthClient.findByClientId(clientId); } + if (env.OAUTH_DISABLE_CIMD) { + return null; + } // Metadata documents only serve MCP, so do not fetch one for a team that // has turned it off.