|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { createMockRequest, loggerMock } from '@sim/testing' |
| 5 | +import type { NextRequest } from 'next/server' |
| 6 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | + |
| 8 | +const { |
| 9 | + mockClientOptions, |
| 10 | + mockConnect, |
| 11 | + mockDetectMcpAuthType, |
| 12 | + mockDisconnect, |
| 13 | + mockListTools, |
| 14 | + mockResolveMcpConfigEnvVars, |
| 15 | + mockValidateMcpServerSsrf, |
| 16 | + MockMcpSsrfError, |
| 17 | +} = vi.hoisted(() => ({ |
| 18 | + mockClientOptions: vi.fn(), |
| 19 | + mockConnect: vi.fn(), |
| 20 | + mockDetectMcpAuthType: vi.fn(), |
| 21 | + mockDisconnect: vi.fn(), |
| 22 | + mockListTools: vi.fn(), |
| 23 | + mockResolveMcpConfigEnvVars: vi.fn(), |
| 24 | + mockValidateMcpServerSsrf: vi.fn(), |
| 25 | + MockMcpSsrfError: class extends Error {}, |
| 26 | +})) |
| 27 | + |
| 28 | +vi.mock('@/lib/core/utils/with-route-handler', () => ({ |
| 29 | + withRouteHandler: (handler: unknown) => handler, |
| 30 | +})) |
| 31 | + |
| 32 | +vi.mock('@/lib/mcp/client', () => ({ |
| 33 | + McpClient: class { |
| 34 | + constructor(options: unknown) { |
| 35 | + mockClientOptions(options) |
| 36 | + } |
| 37 | + |
| 38 | + static getVersionInfo() { |
| 39 | + return { preferred: '2025-06-18', supported: ['2025-06-18'] } |
| 40 | + } |
| 41 | + |
| 42 | + connect = mockConnect |
| 43 | + disconnect = mockDisconnect |
| 44 | + listTools = mockListTools |
| 45 | + |
| 46 | + getNegotiatedVersion() { |
| 47 | + return '2025-06-18' |
| 48 | + } |
| 49 | + }, |
| 50 | +})) |
| 51 | + |
| 52 | +vi.mock('@/lib/mcp/domain-check', () => ({ |
| 53 | + McpDnsResolutionError: class extends Error {}, |
| 54 | + McpDomainNotAllowedError: class extends Error {}, |
| 55 | + McpSsrfError: MockMcpSsrfError, |
| 56 | + validateMcpDomain: vi.fn(), |
| 57 | + validateMcpServerSsrf: mockValidateMcpServerSsrf, |
| 58 | +})) |
| 59 | + |
| 60 | +vi.mock('@/lib/mcp/middleware', () => ({ |
| 61 | + mcpBodyReadErrorResponse: vi.fn(() => null), |
| 62 | + readMcpJsonBodyWithLimit: (request: NextRequest) => request.json(), |
| 63 | + withMcpAuth: |
| 64 | + () => |
| 65 | + ( |
| 66 | + handler: ( |
| 67 | + request: NextRequest, |
| 68 | + context: { userId: string; workspaceId: string; requestId: string } |
| 69 | + ) => Promise<Response> |
| 70 | + ) => |
| 71 | + (request: NextRequest) => |
| 72 | + handler(request, { |
| 73 | + userId: 'user-1', |
| 74 | + workspaceId: 'workspace-1', |
| 75 | + requestId: 'request-1', |
| 76 | + }), |
| 77 | +})) |
| 78 | + |
| 79 | +vi.mock('@/lib/mcp/oauth', () => ({ |
| 80 | + detectMcpAuthType: mockDetectMcpAuthType, |
| 81 | +})) |
| 82 | + |
| 83 | +vi.mock('@/lib/mcp/resolve-config', () => ({ |
| 84 | + resolveMcpConfigEnvVars: mockResolveMcpConfigEnvVars, |
| 85 | +})) |
| 86 | + |
| 87 | +import { POST } from '@/app/api/mcp/servers/test-connection/route' |
| 88 | + |
| 89 | +const mockLogger = vi.mocked(loggerMock.createLogger).mock.results.at(-1)?.value |
| 90 | + |
| 91 | +function createTestRequest(headers: Record<string, string> = {}) { |
| 92 | + return createMockRequest( |
| 93 | + 'POST', |
| 94 | + { |
| 95 | + name: 'Dual Auth Server', |
| 96 | + transport: 'streamable-http', |
| 97 | + url: 'https://example.com/mcp', |
| 98 | + headers, |
| 99 | + timeout: 10000, |
| 100 | + }, |
| 101 | + {}, |
| 102 | + 'http://localhost/api/mcp/servers/test-connection' |
| 103 | + ) |
| 104 | +} |
| 105 | + |
| 106 | +describe('MCP server test-connection route', () => { |
| 107 | + beforeEach(() => { |
| 108 | + vi.clearAllMocks() |
| 109 | + mockDetectMcpAuthType.mockResolvedValue('oauth') |
| 110 | + mockValidateMcpServerSsrf.mockResolvedValue('203.0.113.10') |
| 111 | + mockResolveMcpConfigEnvVars.mockImplementation(async (config: unknown) => ({ |
| 112 | + config, |
| 113 | + missingVars: [], |
| 114 | + })) |
| 115 | + mockConnect.mockResolvedValue(undefined) |
| 116 | + mockListTools.mockResolvedValue([]) |
| 117 | + mockDisconnect.mockResolvedValue(undefined) |
| 118 | + }) |
| 119 | + |
| 120 | + it('tests configured bearer headers before treating OAuth discovery as mandatory', async () => { |
| 121 | + const response = await POST(createTestRequest({ Authorization: 'Bearer static-api-token' })) |
| 122 | + const body = await response.json() |
| 123 | + |
| 124 | + expect(response.status).toBe(200) |
| 125 | + expect(body.data).toEqual( |
| 126 | + expect.objectContaining({ success: true, authType: 'headers', toolCount: 0 }) |
| 127 | + ) |
| 128 | + expect(mockDetectMcpAuthType).not.toHaveBeenCalled() |
| 129 | + expect(mockClientOptions).toHaveBeenCalledWith( |
| 130 | + expect.objectContaining({ |
| 131 | + config: expect.objectContaining({ |
| 132 | + headers: { Authorization: 'Bearer static-api-token' }, |
| 133 | + }), |
| 134 | + }) |
| 135 | + ) |
| 136 | + expect(mockConnect).toHaveBeenCalledTimes(1) |
| 137 | + }) |
| 138 | + |
| 139 | + it('returns a header-auth failure when the configured token is rejected', async () => { |
| 140 | + mockConnect.mockRejectedValueOnce(new Error('HTTP 401: Unauthorized')) |
| 141 | + |
| 142 | + const response = await POST(createTestRequest({ Authorization: 'Bearer invalid-static-token' })) |
| 143 | + const body = await response.json() |
| 144 | + |
| 145 | + expect(response.status).toBe(400) |
| 146 | + expect(body.data).toEqual( |
| 147 | + expect.objectContaining({ |
| 148 | + success: false, |
| 149 | + authType: 'headers', |
| 150 | + error: 'HTTP 401: Unauthorized', |
| 151 | + }) |
| 152 | + ) |
| 153 | + expect(mockDetectMcpAuthType).not.toHaveBeenCalled() |
| 154 | + expect(mockConnect).toHaveBeenCalledTimes(1) |
| 155 | + expect(mockDisconnect).toHaveBeenCalledTimes(1) |
| 156 | + }) |
| 157 | + |
| 158 | + it('does not expose configured credentials echoed by an upstream error', async () => { |
| 159 | + const token = 'opaque-static-token' |
| 160 | + mockConnect.mockRejectedValueOnce(new Error(`Upstream rejected ${token}`)) |
| 161 | + |
| 162 | + const response = await POST(createTestRequest({ Authorization: `Bearer ${token}` })) |
| 163 | + const body = await response.json() |
| 164 | + |
| 165 | + expect(response.status).toBe(400) |
| 166 | + expect(body.data).toEqual( |
| 167 | + expect.objectContaining({ success: false, authType: 'headers', error: 'Connection failed' }) |
| 168 | + ) |
| 169 | + expect(mockLogger).toBeDefined() |
| 170 | + expect(JSON.stringify(mockLogger?.warn.mock.calls)).not.toContain(token) |
| 171 | + }) |
| 172 | + |
| 173 | + it('preserves OAuth discovery when no static headers are configured', async () => { |
| 174 | + const response = await POST(createTestRequest()) |
| 175 | + const body = await response.json() |
| 176 | + |
| 177 | + expect(response.status).toBe(200) |
| 178 | + expect(body.data).toEqual( |
| 179 | + expect.objectContaining({ success: false, authRequired: true, authType: 'oauth' }) |
| 180 | + ) |
| 181 | + expect(mockDetectMcpAuthType).toHaveBeenCalledWith('https://example.com/mcp', '203.0.113.10') |
| 182 | + expect(mockClientOptions).not.toHaveBeenCalled() |
| 183 | + }) |
| 184 | + |
| 185 | + it('preserves OAuth discovery when only supplemental headers are configured', async () => { |
| 186 | + const response = await POST(createTestRequest({ 'X-Sim-Via': 'workflow' })) |
| 187 | + const body = await response.json() |
| 188 | + |
| 189 | + expect(response.status).toBe(200) |
| 190 | + expect(body.data).toEqual( |
| 191 | + expect.objectContaining({ success: false, authRequired: true, authType: 'oauth' }) |
| 192 | + ) |
| 193 | + expect(mockDetectMcpAuthType).toHaveBeenCalledWith('https://example.com/mcp', '203.0.113.10') |
| 194 | + expect(mockClientOptions).not.toHaveBeenCalled() |
| 195 | + }) |
| 196 | + |
| 197 | + it('blocks an env-resolved private URL before forwarding configured credentials', async () => { |
| 198 | + const token = 'private-static-token' |
| 199 | + mockResolveMcpConfigEnvVars.mockResolvedValueOnce({ |
| 200 | + config: { |
| 201 | + id: 'test-request-1', |
| 202 | + name: 'Dual Auth Server', |
| 203 | + transport: 'streamable-http', |
| 204 | + url: 'http://127.0.0.1/mcp', |
| 205 | + headers: { Authorization: `Bearer ${token}` }, |
| 206 | + timeout: 10000, |
| 207 | + retries: 1, |
| 208 | + enabled: true, |
| 209 | + }, |
| 210 | + missingVars: [], |
| 211 | + }) |
| 212 | + mockValidateMcpServerSsrf.mockImplementation(async (url: string) => { |
| 213 | + if (url === 'http://127.0.0.1/mcp') { |
| 214 | + throw new MockMcpSsrfError('Private network targets are not allowed') |
| 215 | + } |
| 216 | + return '203.0.113.10' |
| 217 | + }) |
| 218 | + |
| 219 | + const response = await POST(createTestRequest({ Authorization: `Bearer ${token}` })) |
| 220 | + const responseText = await response.text() |
| 221 | + |
| 222 | + expect(response.status).toBe(403) |
| 223 | + expect(responseText).not.toContain(token) |
| 224 | + expect(mockValidateMcpServerSsrf).toHaveBeenNthCalledWith(2, 'http://127.0.0.1/mcp') |
| 225 | + expect(mockClientOptions).not.toHaveBeenCalled() |
| 226 | + expect(mockDetectMcpAuthType).not.toHaveBeenCalled() |
| 227 | + }) |
| 228 | +}) |
0 commit comments