UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

131 lines (130 loc) • 4.8 kB
import { g as ssrfPolicyFromHttpBaseUrlAllowedOrigin } from "./ssrf-DHBNJw0c.mjs"; import { a as loadUndiciRuntimeDeps, o as globalUndiciStreamTimeoutMs } from "./undici-runtime-ByR6iuTV.mjs"; import { d as wrapGuardedBodyStream } from "./fetch-guard-BLMw2n18.mjs"; import { n as fetchWithProviderHttpGuard } from "./provider-transport-dispatcher-pool-DIU5EVl7.mjs"; import fs from "node:fs"; //#region src/agents/mcp-http-fetch.ts /** * MCP HTTP fetch wrappers. * Adds SSRF protection, scoped TLS/client-cert dispatchers, response cleanup, * and same-origin header handling around the MCP SDK fetch contract. */ const fetchWithUndiciGuard = async (e, t) => await loadUndiciRuntimeDeps().fetch(e instanceof Request ? e.url : e, t); const MCP_HTTP_MAX_REDIRECTS = 20; function resolveFetchRequest(e, t) { if (e instanceof Request) { const n = new Request(e, t); const r = n.body ?? void 0; return { url: n.url, signal: n.signal, init: { method: n.method, headers: n.headers, body: r, redirect: n.redirect, ...r ? { duplex: "half" } : {} } }; } const { signal: n, ...r } = t ?? {}; return { url: e instanceof URL ? e.toString() : e, signal: n ?? void 0, init: t ? r : void 0 }; } function buildMcpHttpFetchWithRedirectPolicy(e, t) { const n = e.sslVerify === false || Boolean(e.clientCert || e.clientKey); const r = e.resourceUrl ? new URL(e.resourceUrl).origin : void 0; const i = e.resourceUrl ? ssrfPolicyFromHttpBaseUrlAllowedOrigin(e.resourceUrl) : void 0; let a; const resolveCustomDispatcherPolicy = (t) => { if (!n || !r || t.origin !== r) return; a ??= { ...e.sslVerify === false ? { rejectUnauthorized: false } : {}, ...e.clientCert ? { cert: fs.readFileSync(e.clientCert, "utf-8") } : {}, ...e.clientKey ? { key: fs.readFileSync(e.clientKey, "utf-8") } : {} }; return { mode: "direct", connect: a }; }; return async (r, a) => { const o = resolveFetchRequest(r, a); const s = { url: o.url, init: o.init, fetchImpl: fetchWithUndiciGuard, maxRedirects: MCP_HTTP_MAX_REDIRECTS, ...t === "reject" ? { rejectCrossOriginUnsafeRedirectReplay: true } : { allowCrossOriginUnsafeRedirectReplay: true }, auditContext: "mcp-http", useEnvProxyForEligibleUrls: true, beforeRequest: e.beforeRequest, streamTimeoutMs: e.requestTimeoutMs === void 0 ? void 0 : Math.max(e.requestTimeoutMs, globalUndiciStreamTimeoutMs ?? 3e5), ...o.signal ? { signal: o.signal } : {}, ...e.timeoutMs !== void 0 ? { timeoutMs: e.timeoutMs } : {}, ...i ? { policy: i } : {}, ...n ? { resolveDispatcherPolicy: resolveCustomDispatcherPolicy } : {} }; const { response: c, release: l, refreshTimeout: u } = await fetchWithProviderHttpGuard(s); if (!c.body) l(); return new Response(c.body ? wrapGuardedBodyStream({ body: c.body, cleanup: l, refreshTimeout: u }) : null, { status: c.status, statusText: c.statusText, headers: c.headers }); }; } /** Builds an MCP resource fetch with optional TLS/client-cert dispatcher support. */ function buildMcpHttpFetch(e) { return buildMcpHttpFetchWithRedirectPolicy(e, "replay"); } /** Builds an OAuth fetch with scoped resource headers and fail-closed redirect replay. */ function buildMcpOAuthHttpFetch(e) { const { headers: t, ...n } = e; return withSameOriginMcpHttpHeaders({ fetchFn: buildMcpHttpFetchWithRedirectPolicy(n, "reject"), headers: withoutMcpAuthorizationHeader(t), resourceUrl: e.resourceUrl }); } /** Removes Authorization from MCP headers before forwarding to non-authorized paths. */ function withoutMcpAuthorizationHeader(e) { if (!e) return; const t = Object.entries(e).filter(([e]) => e.toLowerCase() !== "authorization"); return t.length > 0 ? Object.fromEntries(t) : void 0; } /** Wraps MCP fetch so configured headers are applied only to the resource origin. */ function withSameOriginMcpHttpHeaders(e) { if (!e.headers || Object.keys(e.headers).length === 0) return e.fetchFn; const t = new URL(e.resourceUrl).origin; return (n, r) => { if (new URL(n).origin !== t) return e.fetchFn(n, r); const i = new Headers(e.headers); for (const [e, t] of new Headers(r?.headers)) i.set(e, t); return e.fetchFn(n, { ...r, headers: i }); }; } /** OAuth discovery and token responses are short-lived, so the deadline covers their bodies. */ function buildMcpOAuthAuthorizationFetch(e, t) { return buildMcpOAuthHttpFetch({ sslVerify: e.sslVerify, clientCert: e.clientCert, clientKey: e.clientKey, resourceUrl: e.url, timeoutMs: e.requestTimeoutMs, beforeRequest: t, headers: e.headers }); } //#endregion export { withoutMcpAuthorizationHeader as a, withSameOriginMcpHttpHeaders as i, buildMcpOAuthAuthorizationFetch as n, buildMcpOAuthHttpFetch as r, buildMcpHttpFetch as t };