RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
refactor(agent): reject non-global destinations (#20461)
Sbaaoui Idriss3c0d02f56411
diff --git a/internal/agent/tool/exesql_ssrf_test.go b/internal/agent/tool/exesql_ssrf_test.goindex bfd8d8693c..0af7bcc9e2 100644--- a/internal/agent/tool/exesql_ssrf_test.go+++ b/internal/agent/tool/exesql_ssrf_test.go@@ -74,6 +74,12 @@ func TestExeSQL_SSRF_RejectsLoopbackLinkLocalAndRFC1918(t *testing.T) {{"rfc1918_192", "192.168.1.1"},{"rfc1918_172", "172.16.0.1"},{"unspecified", "0.0.0.0"},+ {"this_network", "0.1.2.3"},+ {"cgnat", "100.64.0.1"},+ {"benchmark", "198.19.0.1"},+ {"mapped_cgnat", "::ffff:100.64.0.1"},+ {"six_to_four_loopback", "2002:7f00:101::1"},+ {"nat64_loopback", "64:ff9b::7f00:1"},}for _, c := range cases {@@ -81,7 +87,7 @@ func TestExeSQL_SSRF_RejectsLoopbackLinkLocalAndRFC1918(t *testing.T) {t.Run(c.name, func(t *testing.T) {t.Parallel()- rec := &recordingDialer{}+ rec := &recordingDialer{failFast: true}e := NewExeSQLTool(exesqlConnParams{DBType: "mysql", Host: c.host, Port: 3306,Database: "d", Username: "u", Password: "p",@@ -128,6 +134,27 @@ func TestExeSQL_SSRF_RejectsEmptyHost(t *testing.T) {}}+func TestExeSQL_SSRF_RejectsMixedDNSBeforeDial(t *testing.T) {+ origLookup := common.LookupHost+ common.LookupHost = func(host string) ([]string, error) {+ if host == "mixed.example" {+ return []string{"1.1.1.1", "198.18.0.1"}, nil+ }+ return origLookup(host)+ }+ t.Cleanup(func() { common.LookupHost = origLookup })++ rec := &recordingDialer{failFast: true}+ e := NewExeSQLTool(exesqlConnParams{+ DBType: "mysql", Host: "mixed.example", Port: 3306,+ Database: "d", Username: "u", Password: "p", MaxRecords: 10,+ }).WithExeSQLDialer(rec.dial)+ _, err := e.InvokableRun(t.Context(), `{"sql":"SELECT 1"}`)+ if !errors.Is(err, ErrSSRFBlocked) || len(rec.dialed) != 0 {+ t.Fatalf("mixed DNS = (%v, %d dials), want SSRF blocked before dial", err, len(rec.dialed))+ }+}+// TestExeSQL_SSRF_PinsToValidatedIP ensures that when a DNS name// resolves to a public IP, InvokableRun dials the validated IP, not// the original hostname — closing the TOCTOU window for DNSdiff --git a/internal/agent/tool/ssrf.go b/internal/agent/tool/ssrf.goindex f465107d7e..ca996249dd 100644--- a/internal/agent/tool/ssrf.go+++ b/internal/agent/tool/ssrf.go@@ -114,7 +114,7 @@ func ResolveAndValidate(rawURL string) (originalHost string, pinnedIP net.IP, er// If the host is a literal IP, no DNS lookup is needed.if ip := net.ParseIP(host); ip != nil {- if isPrivateOrLoopback(ip) {+ if isNonGlobalIP(ip) {return "", nil, fmt.Errorf("%w: literal %s", ErrSSRFBlocked, host)}return host, ip, nil@@ -130,7 +130,7 @@ func ResolveAndValidate(rawURL string) (originalHost string, pinnedIP net.IP, erif ip == nil {return "", nil, fmt.Errorf("ssrf: could not parse resolved address %q for %s", addr, host)}- if isPrivateOrLoopback(ip) {+ if isNonGlobalIP(ip) {return "", nil, fmt.Errorf("%w: %s -> %s", ErrSSRFBlocked, host, ip)}if firstSafe == nil {@@ -143,20 +143,14 @@ func ResolveAndValidate(rawURL string) (originalHost string, pinnedIP net.IP, erreturn host, firstSafe, nil}-// isPrivateOrLoopback reports whether ip is in any of the ranges we-// refuse to fetch from. It is deliberately conservative — link-local-// (169.254.0.0/16, fe80::/10) is rejected because that is where cloud-// metadata services live; multicast and the unspecified address are-// also rejected.-func isPrivateOrLoopback(ip net.IP) bool {+// isNonGlobalIP applies the same special-use address policy as the shared+// host guard without repeating its range list or performing another DNS lookup.+func isNonGlobalIP(ip net.IP) bool {if ip == nil {return true}- if ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() ||- ip.IsPrivate() || ip.IsMulticast() || ip.IsUnspecified() {- return true- }- return false+ _, err := common.AssertHostSafe(ip.String())+ return err != nil}func allowAnyHost() bool {@@ -210,7 +204,7 @@ func ValidateDBHost(host string) (string, error) {// Literal IP — no DNS lookup needed.if ip := net.ParseIP(host); ip != nil {- if isPrivateOrLoopback(ip) {+ if isNonGlobalIP(ip) {return "", fmt.Errorf("%w: literal %s", ErrSSRFBlocked, host)}return ip.String(), nil@@ -232,7 +226,7 @@ func ValidateDBHost(host string) (string, error) {if ip == nil {return "", fmt.Errorf("ssrf: could not parse resolved address %q for %s", addr, host)}- if isPrivateOrLoopback(ip) {+ if isNonGlobalIP(ip) {return "", fmt.Errorf("%w: %s -> %s", ErrSSRFBlocked, host, ip)}if firstSafe == "" {diff --git a/internal/agent/tool/ssrf_test.go b/internal/agent/tool/ssrf_test.goindex 79778bea3e..b0f6eeca52 100644--- a/internal/agent/tool/ssrf_test.go+++ b/internal/agent/tool/ssrf_test.go@@ -17,12 +17,43 @@package toolimport (+ "errors""net""ragflow/internal/common""strings""testing")+func TestResolveAndValidateRejectsNonGlobalAddresses(t *testing.T) {+ origLookup := common.LookupHost+ common.LookupHost = func(host string) ([]string, error) {+ if mixed := map[string]string{+ "mixed-zero.example": "0.1.2.3",+ "mixed-cgnat.example": "100.64.0.1",+ "mixed-benchmark.example": "198.19.0.1",+ }[host]; mixed != "" {+ return []string{"1.1.1.1", mixed}, nil+ }+ return origLookup(host)+ }+ t.Cleanup(func() { common.LookupHost = origLookup })++ for _, rawURL := range []string{+ "http://0.1.2.3/", "http://100.64.0.1/", "http://198.19.0.1/",+ "http://[::ffff:100.64.0.1]/", "http://[2002:7f00:101::1]/",+ "http://[64:ff9b::7f00:1]/",+ "http://mixed-zero.example/", "http://mixed-cgnat.example/",+ "http://mixed-benchmark.example/",+ } {+ t.Run(rawURL, func(t *testing.T) {+ host, ip, err := ResolveAndValidate(rawURL)+ if !errors.Is(err, ErrSSRFBlocked) || host != "" || ip != nil {+ t.Fatalf("ResolveAndValidate(%q) = (%q, %v, %v), want blocked with no pinned IP", rawURL, host, ip, err)+ }+ })+ }+}+func TestValidateURLForSSRF(t *testing.T) {originalLookupHost := common.LookupHostcommon.LookupHost = func(host string) ([]string, error) {