diff --git a/api/server/routes/__tests__/mcp.spec.js b/api/server/routes/__tests__/mcp.spec.js index 4ba01927af..96404b776c 100644 --- a/api/server/routes/__tests__/mcp.spec.js +++ b/api/server/routes/__tests__/mcp.spec.js @@ -2534,6 +2534,44 @@ describe('MCP Routes', () => { }); }); + describe.each(['tokens', 'status'])('GET /oauth/%s/:flowId authorization', (endpoint) => { + it.each([ + ['USER', undefined, 'system:test-server'], + ['ADMIN', undefined, 'system:test-server'], + ['USER', 'tenant-a', 'tenant:tenant-a:system:test-server'], + ['ADMIN', 'tenant-a', 'tenant:tenant-a:system:test-server'], + ])( + 'denies %s access in tenant %s to %s before reading flow state', + async (role, tenantId, flowId) => { + const { getTenantId } = require('@librechat/data-schemas'); + const { getFlowStateManager } = require('~/config'); + const { getLogStores } = require('~/cache'); + const mockFlowManager = { + getFlowState: jest.fn().mockResolvedValue({ + status: 'COMPLETED', + result: { + access_token: 'system-access-token', + refresh_token: 'system-refresh-token', + }, + }), + }; + currentUser = { id: 'test-user-id', role }; + getTenantId.mockReturnValue(tenantId); + getFlowStateManager.mockReturnValue(mockFlowManager); + + const response = await request(app).get( + `/api/mcp/oauth/${endpoint}/${encodeURIComponent(flowId)}`, + ); + + expect(response.status).toBe(403); + expect(response.body).toEqual({ error: 'Access denied' }); + expect(getLogStores).not.toHaveBeenCalled(); + expect(getFlowStateManager).not.toHaveBeenCalled(); + expect(mockFlowManager.getFlowState).not.toHaveBeenCalled(); + }, + ); + }); + describe('GET /oauth/tokens/:flowId', () => { const { getLogStores } = require('~/cache'); diff --git a/api/server/routes/mcp.js b/api/server/routes/mcp.js index eed00a4baa..f9c45b6a57 100644 --- a/api/server/routes/mcp.js +++ b/api/server/routes/mcp.js @@ -35,6 +35,7 @@ const { prepareMCPAuthorizationMutation, persistMCPAuthorizationTransaction, resolveMCPOAuthTarget, + canAccessMCPOAuthFlow, } = require('@librechat/api'); const { createMCPServerController, @@ -97,16 +98,7 @@ const OAUTH_CSRF_COOKIE_PATH = '/api/mcp'; const getOAuthFlowId = (userId, serverName) => MCPOAuthHandler.generateFlowId(userId, serverName, getTenantId()); -const canAccessOAuthFlow = (flowId, userId) => { - const parsed = MCPOAuthHandler.parseFlowId(flowId); - if (!parsed) { - return false; - } - if (parsed.tenantId && parsed.tenantId !== getTenantId()) { - return false; - } - return parsed.userId === userId || parsed.userId === 'system'; -}; +const canAccessOAuthFlow = (flowId, userId) => canAccessMCPOAuthFlow(flowId, userId, getTenantId()); /** What `resolveMCPOAuthTarget` reads: the principal's resolved servers and its config. */ const getOAuthTargetDeps = () => ({ diff --git a/packages/api/src/mcp/oauth/access.spec.ts b/packages/api/src/mcp/oauth/access.spec.ts new file mode 100644 index 0000000000..ad52ea6467 --- /dev/null +++ b/packages/api/src/mcp/oauth/access.spec.ts @@ -0,0 +1,47 @@ +import { canAccessMCPOAuthFlow } from './access'; + +const userId = 'user-123'; + +describe('canAccessMCPOAuthFlow', () => { + it.each([ + ['user-123:server', undefined], + ['user-123:server:with:colons', undefined], + ['user-123:server', 'tenant-a'], + ['tenant:tenant-a:user-123:server', 'tenant-a'], + ['tenant:tenant%2Fa:user-123:server', 'tenant/a'], + ])('allows the owner of %s in tenant %s', (flowId, tenantId) => { + expect(canAccessMCPOAuthFlow(flowId, userId, tenantId)).toBe(true); + }); + + it.each([ + ['system:server', undefined], + ['tenant:tenant-a:system:server', 'tenant-a'], + ['tenant:tenant-a:system:server', 'tenant-b'], + ['tenant:tenant-a:system:server', undefined], + ])('denies system flow %s in tenant %s', (flowId, tenantId) => { + expect(canAccessMCPOAuthFlow(flowId, userId, tenantId)).toBe(false); + expect(canAccessMCPOAuthFlow(flowId, 'system', tenantId)).toBe(false); + }); + + it.each([ + ['other-user:server', undefined], + ['tenant:tenant-a:other-user:server', 'tenant-a'], + ['tenant:tenant-a:user-123:server', 'tenant-b'], + ['tenant:tenant-a:user-123:server', undefined], + ])('denies another principal access to %s in tenant %s', (flowId, tenantId) => { + expect(canAccessMCPOAuthFlow(flowId, userId, tenantId)).toBe(false); + }); + + it.each([ + '', + 'user-123', + ':server', + 'user-123:', + 'tenant::user-123:server', + 'tenant:tenant-a::server', + 'tenant:tenant-a:user-123:', + 'tenant:%ZZ:user-123:server', + ])('denies malformed flow ID %s', (flowId) => { + expect(canAccessMCPOAuthFlow(flowId, userId)).toBe(false); + }); +}); diff --git a/packages/api/src/mcp/oauth/access.ts b/packages/api/src/mcp/oauth/access.ts new file mode 100644 index 0000000000..f204eda18f --- /dev/null +++ b/packages/api/src/mcp/oauth/access.ts @@ -0,0 +1,13 @@ +import { MCPOAuthHandler } from './handler'; + +/** HTTP flow readers must never expose internal system credentials. */ +export function canAccessMCPOAuthFlow(flowId: string, userId: string, tenantId?: string): boolean { + const parsed = MCPOAuthHandler.parseFlowId(flowId); + if (!parsed || parsed.userId === 'system') { + return false; + } + if (parsed.tenantId && parsed.tenantId !== tenantId) { + return false; + } + return parsed.userId === userId; +} diff --git a/packages/api/src/mcp/oauth/index.ts b/packages/api/src/mcp/oauth/index.ts index 0b1257644f..1f3c07f2c2 100644 --- a/packages/api/src/mcp/oauth/index.ts +++ b/packages/api/src/mcp/oauth/index.ts @@ -9,3 +9,4 @@ export * from './events'; export * from './resume'; export * from './cleanup'; export * from './target'; +export * from './access';