🙅 fix: Deny HTTP Access to System MCP OAuth Flows (#17408)

Co-authored-by: Lia <lia@librechat.ai>
This commit is contained in:
lia-by-librechat[bot] 2026-10-24 09:01:02 +02:00 • committed by GitHub
parent 532214c022
commit 5a472bfef2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 101 additions and 10 deletions

View file

@ -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');

View file

@ -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 = () => ({