Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

20.2 KB
Raw Blame
//
// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package connector

import (
	"bytes"
	"context"
	"crypto/tls"
	"encoding/base64"
	"errors"
	"fmt"
	"io"
	"net"
	"net/http"
	"net/url"
	"ragflow/internal/common"
	"strings"
	"time"
)

// connectorAssertURLSafe is the SSRF guard shared by all connectors. It is an
// indirection over common.AssertURLSafe so unit tests can substitute a stub
// without touching the shared utility guard (same pattern as sitemap.go and
// azure_blob.go).
var connectorAssertURLSafe = common.AssertURLSafe

// connectorAssertHostSafe is the host-type SSRF guard shared by the host-based
// connectors (IMAP/MySQL/PostgreSQL). It is an indirection over
// utility.AssertHostSafe so unit tests can substitute a stub without touching
// the shared utility guard (same pattern as connectorAssertURLSafe).
var connectorAssertHostSafe = common.AssertHostSafe

// assertConnectorHostSafe validates a bare host (hostname or literal IP, no
// scheme or port) with the shared strict SSRF guard and returns the first
// validated public IP so callers can pin the dial, preventing DNS rebinding
// between validation and the connection.
//
// When ALLOW_ANY_HOST is set, the public-address check is skipped for this
// host dial (MySQL, PostgreSQL, IMAP). The hostname is still resolved and
// the dial stays pinned to that address. URL connectors do not use this path.
//
// When connectorAllowLoopbackForTest is set, loopback-only hosts are allowed
// (unit tests run against local listeners); anything else falls through to the
// strict guard, so a mixed private address is still rejected.
func assertConnectorHostSafe(host string) (net.IP, error) {
	host = strings.TrimSpace(host)
	if connectorAllowLoopbackForTest {
		if ip := net.ParseIP(host); ip != nil {
			if ip.IsLoopback() {
				return ip, nil
			}
		} else {
			lower := strings.ToLower(host)
			if lower == "localhost" || strings.HasSuffix(lower, ".localhost") {
				if _, ip, ok := loopbackTestAllow(lower); ok {
					return ip, nil
				}
			}
		}
		// Not allowed by the test hook — fall through to the strict guard.
	}
	if pinned, enabled, err := common.PinConfiguredPrivateHost(host); enabled {
		if err != nil {
			return nil, err
		}
		ip := net.ParseIP(pinned)
		if ip == nil {
			return nil, fmt.Errorf("Could not parse validated address %q for host %q", pinned, host)
		}
		return ip, nil
	}
	ipStr, err := connectorAssertHostSafe(host)
	if err != nil {
		return nil, err
	}
	ip := net.ParseIP(ipStr)
	if ip == nil {
		return nil, fmt.Errorf("Could not parse validated address %q for host %q", ipStr, host)
	}
	return ip, nil
}

// connectorAllowLoopbackForTest lets unit tests exercise the real HTTP path
// against httptest servers bound to loopback. Production code keeps it false so
// loopback/private endpoints stay blocked.
var connectorAllowLoopbackForTest bool

// assertConnectorURLSafe validates rawURL with the shared strict SSRF guard and
// returns the hostname plus the first validated IP so callers can pin DNS for
// the actual dial, preventing DNS rebinding between validation and the
// connection.
//
// When connectorAllowLoopbackForTest is set, URLs whose hostname resolves only
// to loopback addresses are allowed; anything else falls through to the strict
// guard, so a mixed private address is still rejected.
func assertConnectorURLSafe(rawURL string) (string, net.IP, error) {
	if connectorAllowLoopbackForTest {
		parsed, err := url.Parse(strings.TrimSpace(rawURL))
		if err != nil || parsed.Hostname() == "" {
			return "", nil, fmt.Errorf("URL is missing a host.")
		}
		if scheme := strings.ToLower(parsed.Scheme); scheme == "http" || scheme == "https" {
			if host, ip, ok := loopbackTestAllow(parsed.Hostname()); ok {
				return host, ip, nil
			}
		}
		// Not allowed by the test hook — fall through to the strict guard.
	}
	hostname, ipStr, err := connectorAssertURLSafe(rawURL)
	if err != nil {
		return "", nil, err
	}
	ip := net.ParseIP(ipStr)
	if ip == nil {
		return "", nil, fmt.Errorf("Could not parse validated address %q for hostname %q", ipStr, hostname)
	}
	return hostname, ip, nil
}

// loopbackTestAllow reports whether hostname is a loopback-only target that the
// test hook may allow. Literal loopback IPs are allowed directly; localhost
// names must resolve (via the shared resolver seam) to loopback addresses only.
func loopbackTestAllow(hostname string) (string, net.IP, bool) {
	if ip := net.ParseIP(hostname); ip != nil {
		if ip.IsLoopback() {
			return hostname, ip, true
		}
		return "", nil, false
	}
	lower := strings.ToLower(hostname)
	if lower != "localhost" && !strings.HasSuffix(lower, ".localhost") {
		return "", nil, false
	}
	addrs, err := common.LookupHost(hostname)
	if err != nil {
		return "", nil, false
	}
	allLoopback := true
	var first net.IP
	for _, addr := range addrs {
		ip := net.ParseIP(addr)
		if ip == nil || !ip.IsLoopback() {
			allLoopback = false
			break
		}
		if first == nil {
			first = ip
		}
	}
	if allLoopback && first != nil {
		return hostname, first, true
	}
	return "", nil, false
}

// assertConnectorURLSafeHTTPS validates rawURL with the strict SSRF guard and
// additionally requires HTTPS. Plain-HTTP loopback targets are still permitted
// under the test hook (httptest servers are plain HTTP); anything else must use
// TLS so credentials in request headers are never sent in the clear.
func assertConnectorURLSafeHTTPS(rawURL string) (string, net.IP, error) {
	if parsed, err := url.Parse(strings.TrimSpace(rawURL)); err == nil {
		if err := assertConnectorSchemeHTTPS(parsed); err != nil {
			return "", nil, err
		}
	}
	return assertConnectorURLSafe(rawURL)
}

// assertConnectorSchemeHTTPS is the scheme half of assertConnectorURLSafeHTTPS
// for callers that already hold a parsed URL and whose host is validated
// elsewhere (the S3 transport pins every dial).
func assertConnectorSchemeHTTPS(parsed *url.URL) error {
	scheme := strings.ToLower(parsed.Scheme)
	if scheme == "https" {
		return nil
	}
	if connectorAllowLoopbackForTest {
		if _, _, ok := loopbackTestAllow(parsed.Hostname()); ok {
			return nil
		}
	}
	return fmt.Errorf("URL must use HTTPS (got %q)", scheme)
}

// validateConnectorURL is the config-time SSRF check used by connector
// Validate/New paths.
func validateConnectorURL(rawURL string) error {
	if parsed, err := url.Parse(strings.TrimSpace(rawURL)); err == nil && !common.AllowConfiguredPrivateHost() {
		host := strings.ToLower(parsed.Hostname())
		if host == "localhost" || strings.HasSuffix(host, ".localhost") {
			return &ConnectorValidationError{Message: fmt.Sprintf("URL hostname %q is not allowed (localhost is blocked).", parsed.Hostname())}
		}
	}
	if _, _, err := assertConnectorURLSafe(rawURL); err != nil {
		return &ConnectorValidationError{Message: err.Error()}
	}
	return nil
}

// basicAuthHeader returns the value for an HTTP Basic Authorization header.
func basicAuthHeader(username, password string) string {
	return base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
}

// ---------------------------------------------------------------------------
// Shared request path
// ---------------------------------------------------------------------------

// connectorRequestHop is the per-hop request state carried by connectorRequest.
type connectorRequestHop struct {
	Method  string
	URL     string
	Body    []byte
	Headers map[string]string
}

// connectorRequestOptions configures connectorRequest. Zero values use safe
// defaults: Validate falls back to assertConnectorURLSafe and MaxRedirects to 10.
type connectorRequestOptions struct {
	Method  string
	RawURL  string
	Body    []byte
	Headers map[string]string
	// SensitiveHeaders are additional credential header names to remove when
	// a redirect crosses to a different origin. This covers auth schemes with
	// tenant-configured header names.
	SensitiveHeaders []string
	Timeout          time.Duration
	MaxRedirects     int
	// Base, when non-nil, provides the starting transport so custom TLS/CA
	// settings are preserved; otherwise a default pinned transport is used.
	Base *http.Client
	// Validate checks a hop URL for SSRF and returns the hostname plus the IP
	// to pin for that hop.
	Validate func(rawURL string) (string, net.IP, error)
	// Prepare customizes each hop's request before it is sent (basic auth,
	// query parameters, ...).
	Prepare func(req *http.Request, hop connectorRequestHop) error
	// CheckRedirect, when non-nil, is invoked before following to the next hop
	// (after the next hop's SSRF validation) and may enforce a same-origin
	// policy or re-apply credentials.
	CheckRedirect func(req *http.Request, via []*http.Request) error
	// RetryStatus, when non-nil, decides whether a non-redirect response should
	// be retried on the same hop URL; it returns the wait duration and whether
	// to retry. Used for REST API 429 handling.
	RetryStatus func(resp *http.Response, attempt int) (time.Duration, bool)
	// NextHop adapts the hop state (method/body/headers/URL) for the hop after
	// a redirect; it runs after connectorRequest's default GET downgrade and
	// cross-origin credential stripping.
	NextHop func(nextURL string, status int, hop connectorRequestHop) connectorRequestHop
}

// connectorUnsafeErr returns the underlying error when err was produced by the
// SSRF guard (connectorUnsafeURLError), so callers can surface the original
// message; otherwise it returns err unchanged.
func connectorUnsafeErr(err error) error {
	var unsafe *connectorUnsafeURLError
	if errors.As(err, &unsafe) {
		return unsafe.Err
	}
	return err
}

// connectorUnsafeURLError marks a hop URL rejected by the SSRF guard so
// callers can tell validation failures apart from transport errors.
type connectorUnsafeURLError struct{ Err error }

func (e *connectorUnsafeURLError) Error() string { return e.Err.Error() }
func (e *connectorUnsafeURLError) Unwrap() error { return e.Err }

// connectorRequest is the single HTTP send path shared by every connector. It
// follows redirects manually so that each hop is SSRF-validated and DNS-pinned
// before a connection is made, closing the TOCTOU window that an automatic
// redirect policy would leave open (the redirect target would otherwise be
// validated once and resolved again at dial time).
//
// Per-hop behavior:
//   - Validate checks the hop URL and returns the hostname + IP to pin.
//   - The hop is sent through a transport that dials exactly the validated IP,
//     so a DNS change after validation cannot rebind the connection.
//   - Sensitive headers (Authorization etc.) are dropped when a redirect
//     crosses to a different origin, mirroring http.Client's default.
//   - 301/302/303 downgrade the next hop to GET with an empty body.
//
// Connector-specific behavior is expressed through the Prepare, CheckRedirect,
// RetryStatus, and NextHop hooks.
func connectorRequest(ctx context.Context, opts connectorRequestOptions) (*http.Response, error) {
	if opts.Validate == nil {
		opts.Validate = assertConnectorURLSafe
	}
	if opts.MaxRedirects <= 0 {
		opts.MaxRedirects = 10
	}
	hop := connectorRequestHop{Method: opts.Method, URL: opts.RawURL, Body: opts.Body, Headers: opts.Headers}
	previousOrigin := connectorOrigin(hop.URL)
	var via []*http.Request
	for redirects := 0; ; redirects++ {
		hostname, pinIP, err := opts.Validate(hop.URL)
		if err != nil {
			return nil, &connectorUnsafeURLError{Err: err}
		}
		var reader io.Reader
		if hop.Body != nil {
			reader = bytes.NewReader(hop.Body)
		}
		req, err := http.NewRequestWithContext(ctx, hop.Method, hop.URL, reader)
		if err != nil {
			return nil, err
		}
		for k, v := range hop.Headers {
			req.Header.Set(k, v)
		}
		if opts.Prepare != nil {
			if err := opts.Prepare(req, hop); err != nil {
				return nil, err
			}
		}
		if redirects > 0 && opts.CheckRedirect != nil {
			if err := opts.CheckRedirect(req, via); err != nil {
				return nil, err
			}
		}
		proxyURL, err := connectorProxyURL(opts.Base, req)
		if err != nil {
			return nil, fmt.Errorf("resolve connector proxy: %w", err)
		}
		originalURL := *req.URL
		if req.Host == "" {
			req.Host = req.URL.Host
		}
		requestURI := req.URL.RequestURI()
		pinnedHost := connectorPinnedHost(req.URL, pinIP)
		req.URL.Host = pinnedHost
		if proxyURL != nil && req.URL.Scheme == "http" && (proxyURL.Scheme == "http" || proxyURL.Scheme == "https") {
			// net/http normally builds an HTTP proxy's absolute request target
			// from Request.Host. Use an opaque absolute URI so the proxy connects
			// to the validated IP while the Host header retains the origin name.
			req.URL.Opaque = "//" + pinnedHost + requestURI
		}
		transport := newConnectorPinnedTransport(opts.Base, hostname, pinIP, opts.Timeout, proxyURL)
		client := &http.Client{
			Transport: transport,
			Timeout:   opts.Timeout,
			// Redirects are followed manually by this loop; each hop gets its
			// own validated, pinned transport.
			CheckRedirect: func(*http.Request, []*http.Request) error {
				return http.ErrUseLastResponse
			},
		}
		var resp *http.Response
		for attempt := 1; ; attempt++ {
			resp, err = client.Do(req)
			if err != nil {
				req.URL = &originalURL
				transport.CloseIdleConnections()
				return nil, err
			}
			if !connectorIsRedirect(resp.StatusCode) && opts.RetryStatus != nil {
				if wait, retry := opts.RetryStatus(resp, attempt); retry {
					resp.Body.Close()
					select {
					case <-ctx.Done():
						transport.CloseIdleConnections()
						return nil, ctx.Err()
					case <-time.After(wait):
					}
					continue
				}
			}
			break
		}
		req.URL = &originalURL
		if resp.Request != nil {
			resp.Request.URL = &originalURL
		}
		if !connectorIsRedirect(resp.StatusCode) {
			resp.Body = &connectorCloseIdleBody{body: resp.Body, transport: transport}
			return resp, nil
		}
		location := resp.Header.Get("Location")
		resp.Body.Close()
		transport.CloseIdleConnections()
		if location == "" {
			return nil, fmt.Errorf("redirect with empty Location header")
		}
		if redirects >= opts.MaxRedirects {
			return nil, fmt.Errorf("request stopped after %d redirects", opts.MaxRedirects)
		}
		nextURL, err := connectorResolveURL(hop.URL, location)
		if err != nil {
			return nil, err
		}
		nextOrigin := connectorOrigin(nextURL)
		if nextOrigin != "" && nextOrigin != previousOrigin {
			hop.Headers = connectorStripAuthHeaders(hop.Headers, opts.SensitiveHeaders...)
			// 307/308 preserve the request method and body; never forward a
			// request body (which may carry credentials, e.g. a Seafile or
			// Moodle token exchange) to a different origin.
			if len(hop.Body) > 0 && (resp.StatusCode == http.StatusTemporaryRedirect || resp.StatusCode == http.StatusPermanentRedirect) {
				return nil, fmt.Errorf("redirect to a different origin is not allowed for a request with a body")
			}
		}
		previousOrigin = nextOrigin
		if resp.StatusCode == http.StatusMovedPermanently || resp.StatusCode == http.StatusFound || resp.StatusCode == http.StatusSeeOther {
			hop.Method = http.MethodGet
			hop.Body = nil
		}
		if opts.NextHop != nil {
			hop = opts.NextHop(nextURL, resp.StatusCode, hop)
		} else {
			hop.URL = nextURL
		}
		via = append(via, req)
	}
}

// connectorProxyURL resolves the configured proxy before the request target is pinned.
func connectorProxyURL(base *http.Client, req *http.Request) (*url.URL, error) {
	proxy := http.ProxyFromEnvironment
	if base != nil && base.Transport != nil {
		transport, ok := base.Transport.(*http.Transport)
		if !ok {
			return nil, nil
		}
		proxy = transport.Proxy
	}
	if proxy == nil {
		return nil, nil
	}
	return proxy(req)
}

// connectorPinnedHost returns the validated IP combined with the origin port.
func connectorPinnedHost(reqURL *url.URL, pinIP net.IP) string {
	port := reqURL.Port()
	if port == "" {
		if reqURL.Scheme == "https" {
			port = "443"
		} else {
			port = "80"
		}
	}
	return net.JoinHostPort(pinIP.String(), port)
}

// newConnectorPinnedTransport builds a transport that pins direct connections
// to pinIP or dials the selected proxy while keeping pinIP as the proxy target.
// When base provides an *http.Transport (custom TLS/CA), it is cloned and
// reused; otherwise a default transport is used.
func newConnectorPinnedTransport(base *http.Client, hostname string, pinIP net.IP, timeout time.Duration, proxyURL *url.URL) *http.Transport {
	dialer := &net.Dialer{Timeout: timeout, KeepAlive: 30 * time.Second}
	dial := func(ctx context.Context, network, addr string) (net.Conn, error) {
		if proxyURL != nil {
			return dialer.DialContext(ctx, network, addr)
		}
		_, port, err := net.SplitHostPort(addr)
		if err != nil {
			port = "443"
		}
		return dialer.DialContext(ctx, network, net.JoinHostPort(pinIP.String(), port))
	}
	proxy := func(*http.Request) (*url.URL, error) { return proxyURL, nil }
	if base != nil {
		if t, ok := base.Transport.(*http.Transport); ok {
			clone := t.Clone()
			clone.DialContext = dial
			clone.Proxy = proxy
			if clone.TLSClientConfig == nil {
				clone.TLSClientConfig = &tls.Config{ServerName: hostname, MinVersion: tls.VersionTLS12}
			} else {
				clone.TLSClientConfig = clone.TLSClientConfig.Clone()
				if clone.TLSClientConfig.ServerName == "" {
					clone.TLSClientConfig.ServerName = hostname
				}
			}
			return clone
		}
	}
	return &http.Transport{
		Proxy:                 proxy,
		DialContext:           dial,
		TLSHandshakeTimeout:   timeout,
		ResponseHeaderTimeout: timeout,
		ExpectContinueTimeout: 1 * time.Second,
		ForceAttemptHTTP2:     false,
		TLSClientConfig:       &tls.Config{ServerName: hostname, MinVersion: tls.VersionTLS12},
	}
}

// connectorCloseIdleBody closes the underlying response body and then releases
// the per-hop pinned transport's idle connections, so per-hop transports do not
// leak keep-alive sockets during long syncs.
type connectorCloseIdleBody struct {
	body      io.ReadCloser
	transport *http.Transport
}

func (b *connectorCloseIdleBody) Read(p []byte) (int, error) { return b.body.Read(p) }
func (b *connectorCloseIdleBody) Close() error {
	err := b.body.Close()
	b.transport.CloseIdleConnections()
	return err
}

// connectorAuthSensitiveHeaders lists headers that must not be forwarded to a
// different origin on redirect.
var connectorAuthSensitiveHeaders = map[string]struct{}{
	"authorization":       {},
	"proxy-authorization": {},
	"apikey":              {},
	"api-key":             {},
	"x-api-key":           {},
	"x-auth-token":        {},
	"private-token":       {}, // GitLab credential header
}

func connectorStripAuthHeaders(headers map[string]string, additional ...string) map[string]string {
	out := make(map[string]string, len(headers))
	sensitiveHeaders := make(map[string]struct{}, len(connectorAuthSensitiveHeaders)+len(additional))
	for name := range connectorAuthSensitiveHeaders {
		sensitiveHeaders[name] = struct{}{}
	}
	for _, name := range additional {
		sensitiveHeaders[strings.ToLower(strings.TrimSpace(name))] = struct{}{}
	}
	for k, v := range headers {
		if _, sensitive := sensitiveHeaders[strings.ToLower(k)]; sensitive {
			continue
		}
		out[k] = v
	}
	return out
}

func connectorOrigin(rawURL string) string {
	parsed, err := url.Parse(rawURL)
	if err != nil || parsed.Scheme == "" || parsed.Hostname() == "" {
		return ""
	}

	scheme := strings.ToLower(parsed.Scheme)
	host := strings.TrimSuffix(strings.ToLower(parsed.Hostname()), ".")
	port := parsed.Port()
	if port == "" {
		switch scheme {
		case "http":
			port = "80"
		case "https":
			port = "443"
		default:
			return ""
		}
	}

	return scheme + "://" + net.JoinHostPort(host, port)
}

func connectorResolveURL(base, location string) (string, error) {
	baseParsed, err := url.Parse(base)
	if err != nil {
		return "", err
	}
	ref, err := url.Parse(location)
	if err != nil {
		return "", err
	}
	return baseParsed.ResolveReference(ref).String(), nil
}

func connectorIsRedirect(status int) bool {
	switch status {
	case http.StatusMovedPermanently, http.StatusFound, http.StatusSeeOther,
		http.StatusTemporaryRedirect, http.StatusPermanentRedirect:
		return true
	}
	return false
}