Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): stop ssrf-guarding the remote rail internal proxy hop (R1-B1)
Diego Zacarias071360d5344d
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.jsonindex 77595a5ff..412f0840f 100644--- a/mcp-host/package-lock.json+++ b/mcp-host/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-mcp-host",- "version": "0.1.371",+ "version": "0.1.372","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-mcp-host",- "version": "0.1.371",+ "version": "0.1.372","license": "SEE LICENSE IN LICENSE","dependencies": {"@anthropic-ai/sdk": "^0.32.0",diff --git a/mcp-host/package.json b/mcp-host/package.jsonindex 1e5649454..c6d454df5 100644--- a/mcp-host/package.json+++ b/mcp-host/package.json@@ -1,6 +1,6 @@{"name": "clerum-mcp-host",- "version": "0.1.371",+ "version": "0.1.372","description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude","main": "dist/main.js","engines": {diff --git a/mcp-host/src/mcp/__tests__/clientRemoteInternalHop.test.ts b/mcp-host/src/mcp/__tests__/clientRemoteInternalHop.test.tsnew file mode 100644index 000000000..5d6ee1701--- /dev/null+++ b/mcp-host/src/mcp/__tests__/clientRemoteInternalHop.test.ts@@ -0,0 +1,128 @@+/**+ * R1-B1 (T3) — a REMOTE MCP server is reached through the HCC nginx egress+ * proxy, whose `transport.url` is an in-cluster Service (http by design). The+ * external destination (`spec.remote.baseUrl`) is SSRF-validated by+ * control-api/HCC and is NEVER projected to mcp-host, so mcp-host must NOT+ * SSRF-guard/pin `transport.url` — that internal hop is trusted exactly like the+ * MCP_PROXY_URL rail and the in-cluster DNS fallback.+ *+ * Against the unfixed head, `validateRemoteTarget` rejects the http internal hop+ * with `SsrfBlockedError('...must be https (got http:)')` before any transport is+ * built, so the whole remote rail is dead: `built.streamable` stays empty and+ * `connect()` rejects. These tests assert the OBSERVABLE result (T4): connect+ * resolves, the client is connected, and the transport was built to the internal+ * hop URL.+ *+ * `core/net/ssrf` is the REAL mold (NOT mocked) so on the unfixed head the guard+ * genuinely throws. The SDK transports are mocked to capture the URL built.+ *+ * The internal-hop fixture is the production shape emitted by the producer chain+ * (control-api remoteMcp.ts → HCC k8sClient.ts → mcpAuthorization.ts → the+ * mcp-host decoder contextMapperClient.ts): `remote:true` with `transport.url`+ * pointing at the HCC egress-proxy Service `http://<name>.<ns>.svc.cluster.local:3000/mcp`.+ */+import { beforeEach, describe, expect, it, vi } from 'vitest'+import type { McpServerInfo } from '../../types'+import { McpClient, staticTokenProvider } from '../client'++const built: { streamable: string[]; sse: string[] } = { streamable: [], sse: [] }++vi.mock('@modelcontextprotocol/sdk/client/index.js', () => ({+ Client: class MockClient {+ connect = vi.fn(async () => undefined)+ close = vi.fn().mockResolvedValue(undefined)+ listTools = vi.fn(async () => ({ tools: [] }))+ request = vi.fn(async () => ({ tools: [] }))+ callTool = vi.fn(async () => ({ content: [] }))+ },+}))++vi.mock('@modelcontextprotocol/sdk/client/streamableHttp.js', () => ({+ StreamableHTTPClientTransport: class MockStreamable {+ close = vi.fn().mockResolvedValue(undefined)+ constructor(url: URL) {+ built.streamable.push(url.toString())+ }+ },+}))++vi.mock('@modelcontextprotocol/sdk/client/sse.js', () => ({+ SSEClientTransport: class MockSSE {+ close = vi.fn().mockResolvedValue(undefined)+ constructor(url: URL) {+ built.sse.push(url.toString())+ }+ },+}))++// The HCC egress-proxy Service the remote CR is projected onto. This is the exact+// shape the decoder emits (contextMapperClient.ts): an in-cluster Service URL,+// http by design — the external baseUrl is validated upstream and never lands here.+const NS = 'clerum-host-xyz'+const INTERNAL_HOP = `http://remote-gh.${NS}.svc.cluster.local:3000/mcp`++function remoteServer(authKind: 'oauth-user' | 'static'): McpServerInfo {+ return {+ name: 'remote-gh',+ transport: { type: 'streamableHttp', port: 3000, url: INTERNAL_HOP },+ authKind,+ remote: true,+ enabled: true,+ status: { deployed: true, ready: true },+ }+}++function localServer(): McpServerInfo {+ return {+ name: 'local-x',+ transport: {+ type: 'streamableHttp',+ url: 'http://local-x.mcp-server.svc.cluster.local:3000/mcp',+ },+ enabled: true,+ status: { deployed: true, ready: true },+ }+}++beforeEach(() => {+ built.streamable = []+ built.sse = []+})++// ─── The internal hop is trusted, not SSRF-guarded (R1-B1) ─────────────────────++describe('remote server connects through the http HCC egress-proxy hop (R1-B1)', () => {+ it('connects a remote oauth server to its internal hop URL, building the transport', async () => {+ const client = new McpClient(remoteServer('oauth-user'), staticTokenProvider('t'))+ await expect(client.connect()).resolves.toBeUndefined()+ expect(client.isConnected).toBe(true)+ expect(built.streamable).toEqual([INTERNAL_HOP])+ })++ it('connects a remote static server to its internal hop URL (not oauth-only)', async () => {+ const client = new McpClient(remoteServer('static'), staticTokenProvider('t'))+ await expect(client.connect()).resolves.toBeUndefined()+ expect(client.isConnected).toBe(true)+ expect(built.streamable).toEqual([INTERNAL_HOP])+ })+})++// ─── The MCP_PROXY_URL bypass is preserved (remote != proxy rail) ──────────────++describe('remote server bypasses the MCP_PROXY_URL rail (R1-B1)', () => {+ const PROXY = 'http://mcp-proxy.svc:8080'++ it('a remote server with a proxy set still targets its internal hop, not the proxy rail', async () => {+ const client = new McpClient(remoteServer('oauth-user'), staticTokenProvider('t'), PROXY)+ await expect(client.connect()).resolves.toBeUndefined()+ // The remote egress is its own HCC hop, never collapsed into the proxy rail.+ expect(built.streamable).toEqual([INTERNAL_HOP])+ expect(built.streamable[0]).not.toContain('mcp-proxy')+ })++ it('a LOCAL server with the same proxy still routes through the proxy rail (control)', async () => {+ const client = new McpClient(localServer(), staticTokenProvider('t'), PROXY)+ await client.connect()+ expect(built.streamable).toEqual([`${PROXY}/servers/local-x/mcp`])+ })+})diff --git a/mcp-host/src/mcp/__tests__/clientRemoteTarget.test.ts b/mcp-host/src/mcp/__tests__/clientRemoteTarget.test.tsindex a4567874a..42af0d321 100644--- a/mcp-host/src/mcp/__tests__/clientRemoteTarget.test.ts+++ b/mcp-host/src/mcp/__tests__/clientRemoteTarget.test.ts@@ -1,17 +1,13 @@/**- * Mini-spec 19 §D-6 / C4.1 — remote target handling in McpClient:- * (d) the arbitrary remote `transport.url` is SSRF-guarded (https + public IP)- * with the shared `core/net/ssrf` mold, BEFORE any transport is built.+ * Mini-spec 19 §D-2 / C4.1 — remote target handling in McpClient:* (e) with MCP_PROXY_URL active, a remote server BYPASSES the in-cluster proxy* rail and targets its published `transport.url` (its egress goes through* the HCC's own proxy, D-2); a local server still uses the proxy rail.** The SDK transports are mocked to capture the target URL that was actually- * built. `core/net/ssrf` is the REAL mold, exercised with IP-literal hosts so no- * DNS is touched: a literal is classified directly by `resolvePinnedPublicIp`.+ * built.*/import { beforeEach, describe, expect, it, vi } from 'vitest'-import { SsrfBlockedError } from '../../core/net/ssrf'import type { McpServerInfo } from '../../types'import { McpClient, staticTokenProvider } from '../client'@@ -73,38 +69,6 @@ beforeEach(() => {built.sse = []})-// ─── (d) SSRF guard on the remote target ───────────────────────────────────────--describe('remote target is SSRF-guarded before any transport is built (d)', () => {- it('rejects a target that resolves to a private/loopback IP, building no transport', async () => {- const client = new McpClient(remoteServer('https://127.0.0.1/mcp'), staticTokenProvider('t'))- await expect(client.connect()).rejects.toBeInstanceOf(SsrfBlockedError)- expect(built.streamable).toEqual([])- expect(built.sse).toEqual([])- })-- it('rejects the cloud metadata IP (169.254.169.254)', async () => {- const client = new McpClient(- remoteServer('https://169.254.169.254/mcp'),- staticTokenProvider('t')- )- await expect(client.connect()).rejects.toBeInstanceOf(SsrfBlockedError)- expect(built.streamable).toEqual([])- })-- it('rejects a non-https remote target', async () => {- const client = new McpClient(remoteServer('http://8.8.8.8/mcp'), staticTokenProvider('t'))- await expect(client.connect()).rejects.toBeInstanceOf(SsrfBlockedError)- expect(built.streamable).toEqual([])- })-- it('allows a public https target and builds the transport to it', async () => {- const client = new McpClient(remoteServer('https://8.8.8.8/mcp'), staticTokenProvider('t'))- await expect(client.connect()).resolves.toBeUndefined()- expect(built.streamable).toEqual(['https://8.8.8.8/mcp'])- })-})-// ─── (e) MCP_PROXY_URL bypass for a remote server ──────────────────────────────describe('remote server bypasses the MCP_PROXY_URL rail (e)', () => {diff --git a/mcp-host/src/mcp/__tests__/clientRemoteTargetPin.test.ts b/mcp-host/src/mcp/__tests__/clientRemoteTargetPin.test.tsdeleted file mode 100644index 222ad58b5..000000000--- a/mcp-host/src/mcp/__tests__/clientRemoteTargetPin.test.ts+++ /dev/null@@ -1,130 +0,0 @@-/**- * R1-H1 (T3) — DNS-rebinding TOCTOU on the remote MCP target.- *- * The guard validates the target resolves to a public IP, but the connection- * must also be PINNED to that exact IP, or the SDK's fetch re-resolves the- * hostname at connect time and a TTL≈0 record can rebind it to a- * private/metadata address (delivering the per-user Bearer + an SSRF read- * primitive to an internal host).- *- * This asserts the observable wiring (T4): the IP returned by- * `resolvePinnedPublicIp` is USED to build a pinned `fetch` that is handed to the- * SDK transport (`opts.fetch`). Against the anchor `7bfc2e9a9` — which validates- * the IP but DISCARDS it and injects no fetch — `opts.fetch` is undefined, so the- * assertions fail. `core/net/ssrf` is mocked so `pinnedFetch` is observable and- * no DNS is touched; the pin primitive itself is proven against a real server in- * core/net/__tests__/ssrf.test.ts.- */-import { beforeEach, describe, expect, it, vi } from 'vitest'-import { pinnedFetch, resolvePinnedPublicIp } from '../../core/net/ssrf'-import type { McpServerInfo } from '../../types'-import { McpClient, staticTokenProvider } from '../client'--const captured: { streamableOpts?: { fetch?: unknown }; sseOpts?: { fetch?: unknown } } = {}--vi.mock('@modelcontextprotocol/sdk/client/index.js', () => ({- Client: class MockClient {- connect = vi.fn(async () => undefined)- close = vi.fn().mockResolvedValue(undefined)- listTools = vi.fn(async () => ({ tools: [] }))- request = vi.fn(async () => ({ tools: [] }))- callTool = vi.fn(async () => ({ content: [] }))- },-}))--vi.mock('@modelcontextprotocol/sdk/client/streamableHttp.js', () => ({- StreamableHTTPClientTransport: class MockStreamable {- close = vi.fn().mockResolvedValue(undefined)- constructor(_url: URL, opts?: { fetch?: unknown }) {- captured.streamableOpts = opts- }- },-}))--vi.mock('@modelcontextprotocol/sdk/client/sse.js', () => ({- SSEClientTransport: class MockSSE {- close = vi.fn().mockResolvedValue(undefined)- constructor(_url: URL, opts?: { fetch?: unknown }) {- captured.sseOpts = opts- }- },-}))--// Mock the SSRF mold so `pinnedFetch` is observable and no DNS/socket is touched.-// resolvePinnedPublicIp returns a FIXED public IP (the "validation" result);-// pinnedFetch tags the fetch it builds with the IP it was pinned to.-vi.mock('../../core/net/ssrf', () => ({- SsrfBlockedError: class SsrfBlockedError extends Error {},- resolvePinnedPublicIp: vi.fn(async () => '203.0.113.10'),- pinnedFetch: vi.fn((ip: string) =>- Object.assign(- vi.fn(async () => new Response('{}')),- { __pinnedIp: ip }- )- ),-}))--function remoteServer(url = 'https://rebind.example.com/mcp'): McpServerInfo {- return {- name: 'remote-x',- transport: { type: 'streamableHttp', url },- authKind: 'oauth-user',- remote: true,- enabled: true,- status: { deployed: true, ready: true },- }-}--function localServer(): McpServerInfo {- return {- name: 'local-x',- transport: {- type: 'streamableHttp',- url: 'http://local-x.mcp-server.svc.cluster.local:3000/mcp',- },- enabled: true,- status: { deployed: true, ready: true },- }-}--beforeEach(() => {- captured.streamableOpts = undefined- captured.sseOpts = undefined- vi.mocked(resolvePinnedPublicIp).mockClear()- vi.mocked(pinnedFetch).mockClear()-})--describe('remote target connection is pinned to the validated IP (R1-H1, T3)', () => {- it('injects a fetch pinned to the resolvePinnedPublicIp IP into the SDK transport', async () => {- const client = new McpClient(remoteServer(), staticTokenProvider('t'))- await client.connect()-- // The validated IP is actually USED to build the connection's fetch (against- // the anchor it is resolved then discarded, so pinnedFetch is never called).- expect(pinnedFetch).toHaveBeenCalledWith('203.0.113.10')- // ...and that pinned fetch is handed to the SDK transport, so the SDK cannot- // re-resolve the hostname at connect time. Anchor: opts.fetch is undefined.- const injected = captured.streamableOpts?.fetch as { __pinnedIp?: string } | undefined- expect(injected).toBeDefined()- expect(injected!.__pinnedIp).toBe('203.0.113.10')- })-- it('does NOT pin a local (in-cluster) server — no fetch override, no resolve', async () => {- const client = new McpClient(localServer(), staticTokenProvider('t'))- await client.connect()- expect(resolvePinnedPublicIp).not.toHaveBeenCalled()- expect(pinnedFetch).not.toHaveBeenCalled()- expect(captured.streamableOpts?.fetch).toBeUndefined()- })-- it('does NOT pin the MCP_PROXY_URL rail for a local server (internal egress)', async () => {- const client = new McpClient(- localServer(),- staticTokenProvider('t'),- 'http://mcp-proxy.svc:8080'- )- await client.connect()- expect(pinnedFetch).not.toHaveBeenCalled()- expect(captured.streamableOpts?.fetch).toBeUndefined()- })-})diff --git a/mcp-host/src/mcp/client.ts b/mcp-host/src/mcp/client.tsindex 5c19a97df..d4a76d0ba 100644--- a/mcp-host/src/mcp/client.ts+++ b/mcp-host/src/mcp/client.ts@@ -6,7 +6,6 @@ import { Client } from '@modelcontextprotocol/sdk/client/index.js'import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js'import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'import { ListToolsResultSchema } from '@modelcontextprotocol/sdk/types.js'-import { SsrfBlockedError, pinnedFetch, resolvePinnedPublicIp } from '../core/net/ssrf'import { McpServerInfo, McpTool } from '../types'import {type McpToolCallOptions,@@ -168,10 +167,15 @@ export class McpClient {}/**- * A REMOTE server (mini-spec 19 §D-6) is an off-cluster, spec-compliant- * upstream reached over its published `transport.url`. Two behaviors fork on- * it: the target is SSRF-guarded (validateRemoteTarget) and the in-cluster- * MCP_PROXY_URL rail is bypassed (see resolveUrl / usesProxyRail).+ * A REMOTE server (mini-spec 19 §D-2) is an off-cluster upstream reached+ * THROUGH the HCC's own nginx egress proxy. Its `transport.url` is that+ * proxy's in-cluster Service (http by design); the external destination lives+ * in the CR's `spec.remote.baseUrl`, which control-api/HCC SSRF-validate and+ * never project here. So this internal hop is trusted exactly like the+ * MCP_PROXY_URL rail and the in-cluster DNS fallback — it is NOT SSRF-guarded+ * or pinned. The only behavior that forks on `remote` is the MCP_PROXY_URL+ * bypass (see usesProxyRail): a remote server egresses through the HCC proxy,+ * not `${proxyUrl}/servers/…`.*/private isRemoteServer(): boolean {return this.serverConfig.remote === true@@ -203,30 +207,7 @@ export class McpClient {return transport.url || `http://${this.serverConfig.name}.mcp-server.svc.cluster.local:3000/mcp`}- /**- * SSRF-guard a REMOTE server's target before any transport I/O and return the- * single public IP to PIN the connection to. The target (`transport.url`) is- * arbitrary operator/discovery input, so it is validated with the shared mold- * (`core/net/ssrf`): https-only, and the host must resolve exclusively to public- * addresses (private/loopback/link-local/metadata and unresolvable hosts throw- * `SsrfBlockedError`). The returned IP is fed to `pinnedFetch` in- * `createTransport` so the SDK cannot re-resolve the hostname at connect time- * (DNS-rebinding TOCTOU). The in-cluster DNS fallback and the MCP_PROXY_URL rail- * are internal and deliberately NOT guarded here.- */- private async validateRemoteTarget(options: McpToolCallOptions): Promise<string> {- const url = new URL(this.resolveUrl())- if (url.protocol !== 'https:') {- throw new SsrfBlockedError(`remote MCP server URL must be https (got ${url.protocol})`)- }- // Throws SsrfBlockedError on a private/reserved/metadata or unresolvable host;- // returns the exact IP the connection is then pinned to.- const pinnedIp = await resolvePinnedPublicIp(url)- ensureNotAborted(options.signal)- return pinnedIp- }-- private createTransport(pinnedRemoteIp?: string): SupportedMcpTransport {+ private createTransport(): SupportedMcpTransport {const { transport } = this.serverConfigconst headers: Record<string, string> = {}@@ -251,32 +232,12 @@ export class McpClient {const targetUrl = this.resolveUrl()- // Pin the SOCKET of a remote connection to the IP validated in- // validateRemoteTarget so the SDK's fetch cannot re-resolve the hostname to a- // private/metadata address at connect time (DNS-rebinding TOCTOU). Host header- // + TLS SNI still track the hostname. Only remote targets are pinned; the- // MCP_PROXY_URL rail and the in-cluster DNS fallback are internal and keep the- // default fetch. Fail closed if a remote transport is somehow built without a- // pinned IP — never connect a remote target unpinned.- let pinnedFetchImpl:- | ((input: string | URL, init?: RequestInit) => Promise<Response>)- | undefined- if (this.isRemoteServer()) {- if (!pinnedRemoteIp) {- throw new SsrfBlockedError(- `remote MCP server ${this.name} reached transport build without a pinned IP`- )- }- pinnedFetchImpl = pinnedFetch(pinnedRemoteIp)- }-if (this.usesProxyRail() || transport.type === 'streamableHttp') {console.log(`[MCP:${this.name}] Using Streamable HTTP transport`)return new StreamableHTTPClientTransport(new URL(targetUrl), {requestInit: {headers,},- ...(pinnedFetchImpl ? { fetch: pinnedFetchImpl } : {}),})}@@ -286,7 +247,6 @@ export class McpClient {requestInit: {headers,},- ...(pinnedFetchImpl ? { fetch: pinnedFetchImpl } : {}),})}@@ -313,16 +273,8 @@ export class McpClient {} else {this.currentAuthToken = undefined}- // SSRF-guard a remote target BEFORE any transport is built (throws on a- // private/metadata/unresolvable host or a non-https URL) and capture the IP to- // pin the connection to. Local servers (proxy rail or in-cluster DNS) are- // internal and skip this.- let pinnedRemoteIp: string | undefined- if (this.isRemoteServer()) {- pinnedRemoteIp = await this.validateRemoteTarget(options)- }const connectionEpoch = ++this.connectionEpoch- const nextTransport = this.createTransport(pinnedRemoteIp)+ const nextTransport = this.createTransport()const nextClient = new Client({name: 'clerum-mcp-host',diff --git a/mcp-host/src/types.ts b/mcp-host/src/types.tsindex e4f4950e1..1dcaf2f4b 100644--- a/mcp-host/src/types.ts+++ b/mcp-host/src/types.ts@@ -230,9 +230,11 @@ export interface McpServerInfo {* policy side of the decoder's forbidden-metadata guard). Absent → local* (in-cluster/own-image), the historical default. A remote oauth server is* spec-compliant and 401s already at `initialize`, so mcp-host must (a) populate- * its catalog authenticated (never token-less), (b) SSRF-guard the arbitrary- * `transport.url`, and (c) bypass the in-cluster MCP_PROXY_URL rail — its egress- * goes through the HCC's own auth-passthrough proxy.+ * its catalog authenticated (never token-less), (b) connect to `transport.url`+ * as the in-cluster HCC egress-proxy hop (http) — NOT as an external target to+ * SSRF-guard: the external `baseUrl` is validated by control-api/HCC and never+ * projected here — and (c) bypass the in-cluster MCP_PROXY_URL rail, because its+ * egress goes through the HCC's own auth-passthrough proxy.*/remote?: boolean/**