diff --git a/apps/api/src/app/endpoints/oauth/oauth-metadata.controller.spec.ts b/apps/api/src/app/endpoints/oauth/oauth-metadata.controller.spec.ts index 6cc078fbf9..853b3ef3d1 100644 --- a/apps/api/src/app/endpoints/oauth/oauth-metadata.controller.spec.ts +++ b/apps/api/src/app/endpoints/oauth/oauth-metadata.controller.spec.ts @@ -44,6 +44,16 @@ describe('OAuthMetadataController', () => { }).toThrow(NotFoundException); }); + it('throws NotFoundException when ROOT_URL is not valid as the issuer', () => { + mockConfig.ENABLE_FEATURE_MCP = true; + mockConfig.ENABLE_FEATURE_MCP_OAUTH = true; + mockConfig.ROOT_URL = 'http://0.0.0.0:3333'; + + expect(() => { + return controller.getProtectedResourceMetadata(); + }).toThrow(NotFoundException); + }); + it('returns RFC 9728 protected resource metadata when feature flags are enabled', () => { mockConfig.ENABLE_FEATURE_MCP = true; mockConfig.ENABLE_FEATURE_MCP_OAUTH = true; @@ -92,6 +102,16 @@ describe('OAuthMetadataController', () => { }).toThrow(NotFoundException); }); + it('throws NotFoundException when ROOT_URL is not valid as the issuer', () => { + mockConfig.ENABLE_FEATURE_MCP = true; + mockConfig.ENABLE_FEATURE_MCP_OAUTH = true; + mockConfig.ROOT_URL = 'http://0.0.0.0:3333'; + + expect(() => { + return controller.getAuthorizationServerMetadata(); + }).toThrow(NotFoundException); + }); + it('returns RFC 8414 authorization server metadata when feature flags are enabled', () => { mockConfig.ENABLE_FEATURE_MCP = true; mockConfig.ENABLE_FEATURE_MCP_OAUTH = true; diff --git a/apps/api/src/helper/mcp-oauth.helper.spec.ts b/apps/api/src/helper/mcp-oauth.helper.spec.ts index 7e7b57c123..32d8f78d1c 100644 --- a/apps/api/src/helper/mcp-oauth.helper.spec.ts +++ b/apps/api/src/helper/mcp-oauth.helper.spec.ts @@ -5,7 +5,8 @@ import { getMcpOAuthBaseUrl, getMcpProtectedResourceMetadataUrl, getMcpResourceUrl, - isMcpOAuthEnabled + isMcpOAuthEnabled, + isValidMcpOAuthIssuerUrl } from './mcp-oauth.helper'; describe('mcp-oauth.helper', () => { @@ -94,5 +95,37 @@ describe('mcp-oauth.helper', () => { expect(isMcpOAuthEnabled(configurationService)).toBe(false); }); + + it('returns false when ROOT_URL is not valid as the issuer', () => { + mockConfig.ENABLE_FEATURE_MCP = true; + mockConfig.ENABLE_FEATURE_MCP_OAUTH = true; + mockConfig.ROOT_URL = 'http://192.168.1.10:3333'; + + expect(isMcpOAuthEnabled(configurationService)).toBe(false); + }); + }); + + describe('isValidMcpOAuthIssuerUrl', () => { + it.each([ + 'http://127.0.0.1:3333', + 'http://localhost:3333/', + 'https://192.168.1.10:3333', + 'https://ghostfol.io' + ])('returns true for %s', (rootUrl) => { + mockConfig.ROOT_URL = rootUrl; + + expect(isValidMcpOAuthIssuerUrl(configurationService)).toBe(true); + }); + + it.each([ + 'http://0.0.0.0:3333', + 'http://192.168.1.10:3333', + 'http://[::1]:3333', + 'http://ghostfolio.local' + ])('returns false for %s', (rootUrl) => { + mockConfig.ROOT_URL = rootUrl; + + expect(isValidMcpOAuthIssuerUrl(configurationService)).toBe(false); + }); }); }); diff --git a/apps/api/src/helper/mcp-oauth.helper.ts b/apps/api/src/helper/mcp-oauth.helper.ts index 18bbbd22c2..b331bdc420 100644 --- a/apps/api/src/helper/mcp-oauth.helper.ts +++ b/apps/api/src/helper/mcp-oauth.helper.ts @@ -57,11 +57,32 @@ export function getMcpResourceUrl(configurationService: ConfigurationService) { /** * Tells if the authorization with OAuth 2.1 is enabled. It depends on the - * server of the Model Context Protocol (MCP). + * server of the Model Context Protocol (MCP) and on a valid issuer URL. */ export function isMcpOAuthEnabled(configurationService: ConfigurationService) { return ( configurationService.get('ENABLE_FEATURE_MCP') && - configurationService.get('ENABLE_FEATURE_MCP_OAUTH') + configurationService.get('ENABLE_FEATURE_MCP_OAUTH') && + isValidMcpOAuthIssuerUrl(configurationService) + ); +} + +/** + * Tells if the root URL is valid as the issuer of the authorization server: + * OAuth 2.1 requires HTTPS, except for localhost. This is the same rule as in + * buildOAuthProtectedResourceMetadata() of @modelcontextprotocol/server, which + * throws an error for any other issuer. + */ +export function isValidMcpOAuthIssuerUrl( + configurationService: ConfigurationService +) { + const { hostname, protocol } = new URL( + getMcpOAuthBaseUrl(configurationService) + ); + + return ( + protocol === 'https:' || + hostname === '127.0.0.1' || + hostname === 'localhost' ); } diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index c3acd4361e..16121c5adc 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -1,3 +1,4 @@ +import { isValidMcpOAuthIssuerUrl } from '@ghostfolio/api/helper/mcp-oauth.helper'; import { getHelmetOptions } from '@ghostfolio/api/helper/security-headers.helper'; import { languageRedirectMiddleware } from '@ghostfolio/api/middlewares/language-redirect.middleware'; import { createMcpAuthorizationMiddleware } from '@ghostfolio/api/middlewares/mcp-authorization.middleware'; @@ -156,6 +157,15 @@ async function bootstrap() { ); } + if ( + configurationService.get('ENABLE_FEATURE_MCP_OAUTH') && + !isValidMcpOAuthIssuerUrl(configurationService) + ) { + logger.warn( + 'The authorization with OAuth 2.1 of the Model Context Protocol (MCP) is enabled, but ROOT_URL does not use HTTPS. The authorization stays disabled. Set ROOT_URL to the public HTTPS URL of the instance.' + ); + } + // The middleware has to be registered before the transport mounts its // route, so that an unauthorized request gets the status 401 of the // specification instead of an error of the protocol diff --git a/apps/api/src/middlewares/mcp-authorization.middleware.ts b/apps/api/src/middlewares/mcp-authorization.middleware.ts index 569c63a50c..27053fa562 100644 --- a/apps/api/src/middlewares/mcp-authorization.middleware.ts +++ b/apps/api/src/middlewares/mcp-authorization.middleware.ts @@ -21,13 +21,10 @@ import { getReasonPhrase, StatusCodes } from 'http-status-codes'; * cannot come from a guard: a guard runs while the transport dispatches the * message and can no longer set the status of the response. * - * If MCP OAuth is disabled, the challenge names no resource metadata, because - * Ghostfolio has no authorization server in that mode. A client which reads - * the challenge asks the user for the identifier of the access instead of - * starting a flow which would fail. - * - * When ENABLE_FEATURE_MCP_OAUTH is enabled, the challenge includes the RFC 9728 - * resource_metadata parameter pointing to the protected resource metadata document. + * The challenge names the protected resource metadata (RFC 9728) only if the + * authorization with OAuth 2.1 is enabled. Otherwise, a client which reads the + * challenge asks the user for the identifier of the access instead of starting + * a flow which would fail. * * With the subscription, the user who granted the access has the same limits * as with the other clients: an inactive user and a user who exceeds the daily