Skip to content

Commit 6cb2819

Browse files
authored
fix(mcp): return 400 from test-connection when the server hostname does not exist (#10474)
* fix(mcp): return 400 from test-connection when the server hostname does not exist A hostname the resolver proves does not exist (ENOTFOUND) is the caller's input, so test-connection now answers 400 with the same message. Transient lookup failures (EAI_AGAIN, timeouts) keep returning 502. The classification reuses isHostNotFound, matching the credential validators' site_not_found split. * test(mcp): cover test-connection status for missing and transient DNS failures
1 parent f5575bd commit 6cb2819

4 files changed

Lines changed: 54 additions & 6 deletions

File tree

‎apps/sim/app/api/mcp/servers/test-connection/route.test.ts‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ vi.mock('@/lib/mcp/client', () => ({
4545
},
4646
}))
4747

48-
vi.mock('@/lib/mcp/domain-check', () => ({
48+
vi.mock('@/lib/mcp/domain-check', async (importOriginal) => ({
4949
MCP_EGRESS_PROFILE: 'selfHostedService',
5050
OAUTH_EGRESS_PROFILE: 'contentFetch',
51-
McpDnsResolutionError: class extends Error {},
51+
McpDnsResolutionError: (await importOriginal<typeof import('@/lib/mcp/domain-check')>())
52+
.McpDnsResolutionError,
5253
McpDomainNotAllowedError: class extends Error {},
5354
McpSsrfError: MockMcpSsrfError,
5455
validateMcpDomain: vi.fn(),
@@ -80,6 +81,7 @@ vi.mock('@/lib/mcp/resolve-config', () => ({
8081
resolveMcpConfigEnvVars: mockResolveMcpConfigEnvVars,
8182
}))
8283

84+
import { McpDnsResolutionError } from '@/lib/mcp/domain-check'
8385
import { POST } from '@/app/api/mcp/servers/test-connection/route'
8486

8587
const { mockDetectMcpAuthType } = mcpOauthMockFns
@@ -114,6 +116,30 @@ describe('MCP server test-connection route', () => {
114116
mockDisconnect.mockResolvedValue(undefined)
115117
})
116118

119+
it.each([
120+
['before', true, 400],
121+
['before', false, 502],
122+
['after', true, 400],
123+
['after', false, 502],
124+
])(
125+
'answers a DNS failure %s env resolution with hostNotFound=%s as %i',
126+
async (phase, hostNotFound, status) => {
127+
if (phase === 'after') mockValidateMcpServerSsrf.mockResolvedValueOnce('203.0.113.10')
128+
mockValidateMcpServerSsrf.mockRejectedValueOnce(
129+
new McpDnsResolutionError('example.invalid', hostNotFound)
130+
)
131+
132+
const response = await POST(createTestRequest())
133+
134+
expect(response.status).toBe(status)
135+
expect(await response.json()).toEqual({
136+
success: false,
137+
error: 'MCP server URL hostname "example.invalid" could not be resolved',
138+
})
139+
expect(mockValidateMcpServerSsrf).toHaveBeenCalledTimes(phase === 'after' ? 2 : 1)
140+
}
141+
)
142+
117143
it('tests configured bearer headers before treating OAuth discovery as mandatory', async () => {
118144
const response = await POST(createTestRequest({ Authorization: 'Bearer static-api-token' }))
119145
const body = await response.json()

‎apps/sim/app/api/mcp/servers/test-connection/route.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export const POST = withRouteHandler(
130130
await validateMcpServerSsrf(body.url)
131131
} catch (e) {
132132
if (e instanceof McpDnsResolutionError) {
133-
return createMcpErrorResponse(e, e.message, 502)
133+
return createMcpErrorResponse(e, e.message, e.hostNotFound ? 400 : 502)
134134
}
135135
if (e instanceof McpSsrfError) {
136136
return createMcpErrorResponse(e, e.message, 403)
@@ -177,7 +177,7 @@ export const POST = withRouteHandler(
177177
resolvedIP = await validateMcpServerSsrf(testConfig.url)
178178
} catch (e) {
179179
if (e instanceof McpDnsResolutionError) {
180-
return createMcpErrorResponse(e, e.message, 502)
180+
return createMcpErrorResponse(e, e.message, e.hostNotFound ? 400 : 502)
181181
}
182182
if (e instanceof McpSsrfError) {
183183
return createMcpErrorResponse(e, e.message, 403)

‎apps/sim/lib/mcp/domain-check.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ vi.mock('@/executor/utils/reference-validation', () => ({
1616
import {
1717
isMcpDomainAllowed,
1818
MCP_EGRESS_PROFILE,
19+
McpDnsResolutionError,
1920
McpDomainNotAllowedError,
2021
McpSsrfError,
2122
OAUTH_EGRESS_PROFILE,
@@ -148,6 +149,18 @@ describe('validateMcpServerSsrf', () => {
148149
)
149150
})
150151

152+
it.each([
153+
['ENOTFOUND', true],
154+
['EAI_AGAIN', false],
155+
])('classifies a %s lookup failure as hostNotFound=%s', async (code, hostNotFound) => {
156+
mockDnsLookup.mockRejectedValue(
157+
Object.assign(new Error(`getaddrinfo ${code} example.invalid`), { code })
158+
)
159+
const validation = validateMcpServerSsrf('https://example.invalid/mcp')
160+
await expect(validation).rejects.toBeInstanceOf(McpDnsResolutionError)
161+
await expect(validation).rejects.toMatchObject({ hostNotFound })
162+
})
163+
151164
it('throws McpSsrfError for cloud metadata IP literal', async () => {
152165
await expect(validateMcpServerSsrf('http://169.254.169.254/latest/meta-data/')).rejects.toThrow(
153166
McpSsrfError

‎apps/sim/lib/mcp/domain-check.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createLogger } from '@sim/logger'
22
import { getAllowedMcpDomainsFromEnv } from '@/lib/core/config/env-flags'
33
import type { EgressProfile } from '@/lib/core/security/egress/profiles'
44
import { validateUrlWithDNS } from '@/lib/core/security/input-validation.server'
5+
import { isHostNotFound } from '@/lib/credentials/token-service-accounts/errors'
56
import { createEnvVarPattern } from '@/executor/utils/reference-validation'
67

78
const logger = createLogger('McpDomainCheck')
@@ -38,8 +39,16 @@ export class McpSsrfError extends Error {
3839
}
3940
}
4041

42+
/**
43+
* The MCP server hostname did not resolve. `hostNotFound` is true only when the
44+
* resolver proved the name doesn't exist (`ENOTFOUND`), which is the caller's input;
45+
* a transient lookup failure (`EAI_AGAIN`, timeout) leaves it false.
46+
*/
4147
export class McpDnsResolutionError extends Error {
42-
constructor(hostname: string) {
48+
constructor(
49+
hostname: string,
50+
readonly hostNotFound = false
51+
) {
4352
super(`MCP server URL hostname "${hostname}" could not be resolved`)
4453
this.name = 'McpDnsResolutionError'
4554
}
@@ -154,7 +163,7 @@ export async function validateMcpServerSsrf(
154163
// Fall back to the raw URL in the message.
155164
}
156165
logger.warn('DNS lookup failed for MCP server URL', { hostname })
157-
throw new McpDnsResolutionError(hostname)
166+
throw new McpDnsResolutionError(hostname, isHostNotFound(validation.cause))
158167
}
159168
logger.warn('MCP server URL refused by egress policy', { error })
160169
throw new McpSsrfError(error)

0 commit comments

Comments
 (0)