Browse Source

Disable MCP OAuth for a root URL without HTTPS

pull/8401/head
Thomas Kaul 3 weeks ago
parent
commit
e7e31bb546
  1. 20
      apps/api/src/app/endpoints/oauth/oauth-metadata.controller.spec.ts
  2. 35
      apps/api/src/helper/mcp-oauth.helper.spec.ts
  3. 25
      apps/api/src/helper/mcp-oauth.helper.ts
  4. 10
      apps/api/src/main.ts
  5. 11
      apps/api/src/middlewares/mcp-authorization.middleware.ts

20
apps/api/src/app/endpoints/oauth/oauth-metadata.controller.spec.ts

@ -44,6 +44,16 @@ describe('OAuthMetadataController', () => {
}).toThrow(NotFoundException); }).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', () => { it('returns RFC 9728 protected resource metadata when feature flags are enabled', () => {
mockConfig.ENABLE_FEATURE_MCP = true; mockConfig.ENABLE_FEATURE_MCP = true;
mockConfig.ENABLE_FEATURE_MCP_OAUTH = true; mockConfig.ENABLE_FEATURE_MCP_OAUTH = true;
@ -92,6 +102,16 @@ describe('OAuthMetadataController', () => {
}).toThrow(NotFoundException); }).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', () => { it('returns RFC 8414 authorization server metadata when feature flags are enabled', () => {
mockConfig.ENABLE_FEATURE_MCP = true; mockConfig.ENABLE_FEATURE_MCP = true;
mockConfig.ENABLE_FEATURE_MCP_OAUTH = true; mockConfig.ENABLE_FEATURE_MCP_OAUTH = true;

35
apps/api/src/helper/mcp-oauth.helper.spec.ts

@ -5,7 +5,8 @@ import {
getMcpOAuthBaseUrl, getMcpOAuthBaseUrl,
getMcpProtectedResourceMetadataUrl, getMcpProtectedResourceMetadataUrl,
getMcpResourceUrl, getMcpResourceUrl,
isMcpOAuthEnabled isMcpOAuthEnabled,
isValidMcpOAuthIssuerUrl
} from './mcp-oauth.helper'; } from './mcp-oauth.helper';
describe('mcp-oauth.helper', () => { describe('mcp-oauth.helper', () => {
@ -94,5 +95,37 @@ describe('mcp-oauth.helper', () => {
expect(isMcpOAuthEnabled(configurationService)).toBe(false); 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);
});
}); });
}); });

25
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 * 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) { export function isMcpOAuthEnabled(configurationService: ConfigurationService) {
return ( return (
configurationService.get('ENABLE_FEATURE_MCP') && 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'
); );
} }

10
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 { getHelmetOptions } from '@ghostfolio/api/helper/security-headers.helper';
import { languageRedirectMiddleware } from '@ghostfolio/api/middlewares/language-redirect.middleware'; import { languageRedirectMiddleware } from '@ghostfolio/api/middlewares/language-redirect.middleware';
import { createMcpAuthorizationMiddleware } from '@ghostfolio/api/middlewares/mcp-authorization.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 // The middleware has to be registered before the transport mounts its
// route, so that an unauthorized request gets the status 401 of the // route, so that an unauthorized request gets the status 401 of the
// specification instead of an error of the protocol // specification instead of an error of the protocol

11
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 * cannot come from a guard: a guard runs while the transport dispatches the
* message and can no longer set the status of the response. * message and can no longer set the status of the response.
* *
* If MCP OAuth is disabled, the challenge names no resource metadata, because * The challenge names the protected resource metadata (RFC 9728) only if the
* Ghostfolio has no authorization server in that mode. A client which reads * authorization with OAuth 2.1 is enabled. Otherwise, a client which reads the
* the challenge asks the user for the identifier of the access instead of * challenge asks the user for the identifier of the access instead of starting
* starting a flow which would fail. * 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.
* *
* With the subscription, the user who granted the access has the same limits * 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 * as with the other clients: an inactive user and a user who exceeds the daily

Loading…
Cancel
Save