Skip to content

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

refactor(mcp): reject unvalidated redirect destinations (#20447)

Sbaaoui Idriss8cfda8930658
diff --git a/internal/utility/mcp_client_test.go b/internal/utility/mcp_client_test.go
index 06aeabe20d..d24f5d3571 100644
--- a/internal/utility/mcp_client_test.go
+++ b/internal/utility/mcp_client_test.go
@@ -43,6 +43,39 @@ func allowLoopbackForTests(t *testing.T) func() {
return func() { common.LookupHost = orig }
}
+func TestFetchToolsDoesNotFollowRedirectToUnvalidatedHost(t *testing.T) {
+ var targetRequests atomic.Int32
+ target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ targetRequests.Add(1)
+ w.WriteHeader(http.StatusOK)
+ }))
+ defer target.Close()
+
+ source := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ http.Redirect(w, r, target.URL, http.StatusFound)
+ }))
+ defer source.Close()
+
+ const hostname = "public.example"
+ original := common.AssertURLSafe
+ common.AssertURLSafe = func(string) (string, string, error) {
+ return hostname, "127.0.0.1", nil
+ }
+ defer func() { common.AssertURLSafe = original }()
+
+ _, err := FetchTools(t.Context(), FetchOptions{
+ URL: "http://" + hostname + ":" + strings.TrimPrefix(source.URL, "http://127.0.0.1:"),
+ ServerType: TransportStreamableHTTP,
+ Timeout: time.Second,
+ })
+ if err == nil {
+ t.Fatal("FetchTools unexpectedly followed redirect")
+ }
+ if got := targetRequests.Load(); got != 0 {
+ t.Fatalf("unvalidated target received %d requests, want 0", got)
+ }
+}
+
func TestStreamableSessionCleanupBudget(t *testing.T) {
for _, tc := range []struct {
name string
diff --git a/internal/utility/ssrf.go b/internal/utility/ssrf.go
index 6fa1a6455c..bca40959eb 100644
--- a/internal/utility/ssrf.go
+++ b/internal/utility/ssrf.go
@@ -18,16 +18,24 @@ package utility
import (
"context"
+ "fmt"
"net"
"net/http"
+ "strings"
"time"
+
+ "golang.org/x/net/idna"
)
-// PinnedHTTPClient returns an HTTP client whose Transport rewrites every
-// outbound dial for hostname:port to resolvedIP:port, closing the TOCTOU
-// window between AssertURLSafe and the actual TCP connection. Pins are
-// scoped to this client only.
+// PinnedHTTPClient returns an HTTP client whose Transport dials resolvedIP
+// for hostname and rejects other hosts (including redirect targets), closing
+// the TOCTOU window between AssertURLSafe and the TCP connection.
var PinnedHTTPClient = func(hostname, resolvedIP string, timeout time.Duration) *http.Client {
+ asciiHostname := hostname
+ var idnaErr error
+ if net.ParseIP(hostname) == nil {
+ asciiHostname, idnaErr = idna.Lookup.ToASCII(hostname)
+ }
dialer := &net.Dialer{
Timeout: timeout,
KeepAlive: 30 * time.Second,
@@ -39,10 +47,10 @@ var PinnedHTTPClient = func(hostname, resolvedIP string, timeout time.Duration)
Proxy: nil,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
host, port, splitErr := net.SplitHostPort(addr)
- if splitErr == nil && host == hostname && resolvedIP != "" {
- return dialer.DialContext(ctx, network, net.JoinHostPort(resolvedIP, port))
+ if splitErr != nil || idnaErr != nil || !strings.EqualFold(host, asciiHostname) || resolvedIP == "" {
+ return nil, fmt.Errorf("unvalidated HTTP destination %q", addr)
}
- return dialer.DialContext(ctx, network, addr)
+ return dialer.DialContext(ctx, network, net.JoinHostPort(resolvedIP, port))
},
TLSHandshakeTimeout: timeout,
ResponseHeaderTimeout: timeout,
diff --git a/internal/utility/ssrf_test.go b/internal/utility/ssrf_test.go
index 655dc464e9..ab2281f7bb 100644
--- a/internal/utility/ssrf_test.go
+++ b/internal/utility/ssrf_test.go
@@ -17,11 +17,97 @@
package utility
import (
+ "fmt"
+ "net/http"
+ "net/http/httptest"
"ragflow/internal/common"
"strings"
+ "sync/atomic"
"testing"
+ "time"
)
+func TestPinnedHTTPClientRedirects(t *testing.T) {
+ var forbiddenRequests atomic.Int32
+ forbidden := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ forbiddenRequests.Add(1)
+ w.WriteHeader(http.StatusOK)
+ }))
+ defer forbidden.Close()
+
+ public := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ switch r.URL.Path {
+ case "/external":
+ http.Redirect(w, r, forbidden.URL, http.StatusFound)
+ case "/same-host":
+ http.Redirect(w, r, "/ok", http.StatusFound)
+ default:
+ w.WriteHeader(http.StatusNoContent)
+ }
+ }))
+ defer public.Close()
+
+ const hostname = "public.example"
+ client := PinnedHTTPClient(hostname, "127.0.0.1", time.Second)
+ baseURL := fmt.Sprintf("http://%s:%s", hostname, strings.TrimPrefix(public.URL, "http://127.0.0.1:"))
+
+ resp, err := client.Get(baseURL + "/external")
+ if err == nil {
+ resp.Body.Close()
+ t.Fatal("redirect to unvalidated host unexpectedly succeeded")
+ }
+ if forbiddenRequests.Load() != 0 {
+ t.Fatalf("forbidden target received %d requests, want 0", forbiddenRequests.Load())
+ }
+
+ resp, err = client.Get(baseURL + "/same-host")
+ if err != nil {
+ t.Fatalf("same-host redirect: %v", err)
+ }
+ defer resp.Body.Close()
+ if resp.StatusCode != http.StatusNoContent {
+ t.Fatalf("same-host redirect status=%d, want %d", resp.StatusCode, http.StatusNoContent)
+ }
+}
+
+func TestPinnedHTTPClientUnicodeHostname(t *testing.T) {
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusNoContent)
+ }))
+ defer server.Close()
+
+ const hostname = "bücher.example"
+ client := PinnedHTTPClient(hostname, "127.0.0.1", time.Second)
+ target := strings.Replace(server.URL, "127.0.0.1", hostname, 1)
+ resp, err := client.Get(target)
+ if err != nil {
+ t.Fatalf("pinned Unicode hostname: %v", err)
+ }
+ defer resp.Body.Close()
+ if resp.StatusCode != http.StatusNoContent {
+ t.Fatalf("status=%d, want %d", resp.StatusCode, http.StatusNoContent)
+ }
+}
+
+func TestPinnedHTTPClientIPv6Literal(t *testing.T) {
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusNoContent)
+ }))
+ defer server.Close()
+
+ const hostname = "2001:4860:4860::8888"
+ client := PinnedHTTPClient(hostname, "127.0.0.1", time.Second)
+ target := fmt.Sprintf("http://[%s]:%s", hostname, strings.TrimPrefix(server.URL, "http://127.0.0.1:"))
+ resp, err := client.Get(target)
+ if err != nil {
+ t.Fatalf("pinned IPv6 literal: %v", err)
+ }
+ defer resp.Body.Close()
+ if resp.StatusCode != http.StatusNoContent {
+ t.Fatalf("status=%d, want %d", resp.StatusCode, http.StatusNoContent)
+ }
+}
+
func TestAssertURLSafe(t *testing.T) {
orig := common.LookupHost
defer func() { common.LookupHost = orig }()