mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-16 13:00:55 +00:00
🙅 fix: Deny HTTP Access to System MCP OAuth Flows (#17408)
Co-authored-by: Lia <lia@librechat.ai>
This commit is contained in:
parent
532214c022
commit
5a472bfef2
5 changed files with 101 additions and 10 deletions
|
|
@ -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');
|
||||
|
||||
|
|
|
|||
|
|
@ -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 = () => ({
|
||||
|
|
|
|||
47
packages/api/src/mcp/oauth/access.spec.ts
Normal file
47
packages/api/src/mcp/oauth/access.spec.ts
Normal file
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
13
packages/api/src/mcp/oauth/access.ts
Normal file
13
packages/api/src/mcp/oauth/access.ts
Normal file
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -9,3 +9,4 @@ export * from './events';
|
|||
export * from './resume';
|
||||
export * from './cleanup';
|
||||
export * from './target';
|
||||
export * from './access';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue