package mcphttp import ( "crypto/tls" "fmt" "net" "net/http" "os" "strings" "syscall" "time" ) // Remote MCP endpoints are configured by the operator, and persistence tools // that change them need operator approval. This guard is not a privilege // boundary: it is the same defence-in-depth default that notify/ uses for // webhook delivery, so a configured URL cannot silently reach loopback, // link-local or the cloud metadata endpoint. A local MCP server is a legitimate // setup, hence the explicit opt-out rather than a hardcoded allowance. The // opt-out covers loopback only: metadata and link-local addresses stay refused. const AllowLocalTargetsEnv = "ARTEX_MCP_ALLOW_LOCAL" func allowLocalTargets() bool { v := strings.TrimSpace(os.Getenv(AllowLocalTargetsEnv)) return v == "1" || strings.EqualFold(v, "true") } // isBlockedDialIP mirrors notify's isBlockedDialIP. RFC1918 ranges are // intentionally allowed: LAN MCP servers are a normal deployment, while // loopback, link-local (including 169.254.169.254), unspecified and multicast // are the sensitive addresses the default should refuse. // Cloud metadata services outside link-local: Alibaba Cloud (100.100.100.200), // the Azure WireServer (168.63.129.16) and the AWS IMDS IPv6 endpoint // (fd00:ec2::254, a unique-local address). var ( metadataIPs = []net.IP{ net.IPv4(100, 100, 100, 200), net.IPv4(168, 63, 129, 16), net.ParseIP("fd00:ec2::254"), } _, nat64Prefix, _ = net.ParseCIDR("64:ff9b::/96") // RFC 8215 local-use NAT64 prefix. The operator chooses where the IPv4 // address sits inside it, so the whole prefix is refused. _, nat64LocalPrefix, _ = net.ParseCIDR("64:ff9b:1::/48") ) func isBlockedDialIP(ip net.IP) bool { if ip == nil { return true } // IPv4-mapped IPv6 must be converted back or it bypasses the IPv4 checks. if v4 := ip.To4(); v4 != nil { ip = v4 } else if len(ip) == net.IPv6len && nat64Prefix != nil && nat64Prefix.Contains(ip) { // Check the embedded IPv4 address in RFC 6052 well-known NAT64 prefix. return isBlockedDialIP(net.IPv4(ip[12], ip[13], ip[14], ip[15])) } if nat64LocalPrefix != nil && nat64LocalPrefix.Contains(ip) { return true } for _, m := range metadataIPs { if ip.Equal(m) { return true } } return ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() || ip.IsInterfaceLocalMulticast() || ip.IsUnspecified() || ip.IsMulticast() } // dialAllowed applies the guard to one dialled IP. ARTEX_MCP_ALLOW_LOCAL lets // a local MCP server through: it exempts loopback (127.0.0.0/8, ::1 and their // IPv4-mapped forms) and nothing else. func dialAllowed(ip net.IP, allowLocal bool) bool { if allowLocal && ip != nil && ip.IsLoopback() { return true } return !isBlockedDialIP(ip) } // blockInternalDial is a Dialer.Control hook: it sees the address actually // dialled, so it also covers DNS rebinding and same-origin redirects. func blockInternalDial(_, address string, _ syscall.RawConn) error { host, _, err := net.SplitHostPort(address) if err != nil { return err } ip := net.ParseIP(host) if ip == nil { return fmt.Errorf("cannot parse mcp target address %q", host) } if !dialAllowed(ip, allowLocalTargets()) { return fmt.Errorf("mcp: refusing to dial loopback/link-local address %s (set %s=1 for a local MCP server)", ip, AllowLocalTargetsEnv) } return nil } // mcpTransport is shared by the Streamable HTTP and legacy SSE clients. The // proxy is cleared deliberately: with a proxy the dial hook would see the // proxy's address, not the MCP endpoint, and could not enforce the guard. func mcpTransport(insecure bool) *http.Transport { t, ok := http.DefaultTransport.(*http.Transport) if !ok { t = &http.Transport{} } else { t = t.Clone() } t.Proxy = nil t.DialContext = (&net.Dialer{Timeout: 10 * time.Second, Control: blockInternalDial}).DialContext if insecure { tc := &tls.Config{} if t.TLSClientConfig != nil { tc = t.TLSClientConfig.Clone() } tc.InsecureSkipVerify = true //nolint:gosec // explicit opt-out for self-signed MCP servers t.TLSClientConfig = tc } return t }