Skip to content

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

Fix: honor ALLOW_ANY_HOST (#21949)

Wang Qiba80c3de0e50
diff --git a/docker/.env b/docker/.env
index 992a3d5144..2ff65a4490 100644
--- a/docker/.env
+++ b/docker/.env
@@ -310,11 +310,11 @@ SVR_MCP_PORT=9382
# /ragflow/logs volume).
ADMIN_DEFAULT_PASSWORD=admin
-# Development-only: set to 1 to allow private database and connector hosts
-# (MySQL, PostgreSQL, IMAP, test_db_connection, ExeSQL), including
-# host.docker.internal, and HTTP request (Invoke) target and proxy URLs.
+# Development-only: set to 1 to allow private hosts in shared URL and host
+# guards (model requests, agent tools, connectors, databases, MCP, and Invoke),
+# including host.docker.internal and localhost.
# Dials stay pinned to the resolved address; HTTP/HTTPS validation still applies.
-# Other Agent URL checks stay in force. Do not enable in production.
+# Do not enable in production.
ALLOW_ANY_HOST=0
# The RAGFlow Docker image to download. v0.22+ doesn't include embedding models.
diff --git a/internal/agent/component/agent_test.go b/internal/agent/component/agent_test.go
index e9ca18c2d9..0346b94add 100644
--- a/internal/agent/component/agent_test.go
+++ b/internal/agent/component/agent_test.go
@@ -1719,8 +1719,7 @@ func (f *fakeAgentRetrievalService) Search(ctx context.Context, db *gorm.DB, req
}
func TestAgent_AnthropicReActToolCallLoop(t *testing.T) {
- common.AllowAnyHostForTest = true
- defer func() { common.AllowAnyHostForTest = false }()
+ t.Setenv(common.EnvAllowAnyHost, "true")
fakeRetrieval := &fakeAgentRetrievalService{
chunks: []agenttool.RetrievalChunk{
diff --git a/internal/agent/component/invoke_test.go b/internal/agent/component/invoke_test.go
index 4d222ee692..c5d18d4c0d 100644
--- a/internal/agent/component/invoke_test.go
+++ b/internal/agent/component/invoke_test.go
@@ -27,6 +27,7 @@ import (
"net/url"
"ragflow/internal/common"
"reflect"
+ "strconv"
"strings"
"sync/atomic"
"testing"
@@ -36,14 +37,10 @@ import (
"go.uber.org/zap/zaptest/observer"
)
-// Reset the environment opt-in so tests select private-host access explicitly
-// through either ALLOW_ANY_HOST or the in-memory test hook.
+// Set the environment opt-in so tests select private-host access explicitly.
func setupAllowAnyHost(t *testing.T, enabled bool) {
t.Helper()
- t.Setenv(common.EnvAllowAnyHost, "")
- prev := common.AllowAnyHostForTest
- common.AllowAnyHostForTest = enabled
- t.Cleanup(func() { common.AllowAnyHostForTest = prev })
+ t.Setenv(common.EnvAllowAnyHost, strconv.FormatBool(enabled))
}
// TestInvoke_GET exercises the happy path: a GET request to a stub
@@ -328,7 +325,7 @@ func TestInvoke_AllowAnyHostRejectsInvalidURLs(t *testing.T) {
}
// TestInvoke_SSRFGuard_BlocksLoopback mirrors PR #15426: when
-// AllowAnyHostForTest is unset (production shape), the Invoke
+// ALLOW_ANY_HOST is unset, the Invoke
// component must reject loopback / link-local / RFC1918 URLs
// BEFORE any HTTP request is made. The test sets up an
// httptest server on 127.0.0.1, points Invoke at it, and
diff --git a/internal/agent/tool/ssrf.go b/internal/agent/tool/ssrf.go
index 12c03aaadc..f53bca22b1 100644
--- a/internal/agent/tool/ssrf.go
+++ b/internal/agent/tool/ssrf.go
@@ -47,9 +47,9 @@ var credentialQueryParams = map[string]struct{}{
}
// validateURLForSSRF parses rawURL and rejects any target whose host
-// resolves (via DNS) to a non-public IP. The check is repeated against
-// every returned A/AAAA record because a name that resolves to a mix
-// of public and private IPs is also dangerous (DNS-rebinding /
+// resolves (via DNS) to a non-public IP unless ALLOW_ANY_HOST is enabled.
+// The check covers every returned A/AAAA record because a name that resolves
+// to a mix of public and private IPs is also dangerous (DNS-rebinding /
// multi-A-record pinning).
func validateURLForSSRF(rawURL string) error {
_, _, err := ResolveAndValidate(rawURL)
@@ -57,7 +57,8 @@ func validateURLForSSRF(rawURL string) error {
}
// ResolveAndValidate parses rawURL, performs the SSRF blocklist checks,
-// and returns the first non-public IP that the host resolves to. The
+// and returns the first validated IP that the host resolves to. ALLOW_ANY_HOST
+// permits non-public addresses while preserving scheme and DNS checks. The
// returned IP is safe to dial directly (bypassing a fresh DNS lookup)
// which defeats DNS-rebinding attacks: an attacker cannot swap a
// public record for a private one between this lookup and the connect,
@@ -153,17 +154,16 @@ func isNonGlobalIP(ip net.IP) bool {
return err != nil
}
-// allowAnyHost reports the in-memory test override. ValidateDBHost also
-// honors ALLOW_ANY_HOST; ResolveAndValidate does not.
+// allowAnyHost reads the shared environment opt-in for private addresses.
func allowAnyHost() bool {
- return common.AllowAnyHostForTest
+ return common.AllowConfiguredPrivateHost()
}
// ValidateDBHost parses host (literal IP or DNS name), verifies it
-// resolves only to public IPs, and returns the validated address as a
-// string. The returned value is safe to pass to a SQL driver instead
-// of the original hostname so the connection is pinned at the
-// transport layer — closing the DNS-rebinding window between
+// resolves only to public IPs unless ALLOW_ANY_HOST is enabled, and returns
+// the validated address as a string. The returned value is safe to pass to a
+// SQL driver instead of the original hostname so the connection is pinned
+// at the transport layer — closing the DNS-rebinding window between
// validation and the actual TCP connect. Mirrors the SSRF guard
// applied to `test_db_connection` (PR #15609, Python
// agent/tools/exesql.py).
@@ -179,15 +179,9 @@ func ValidateDBHost(host string) (string, error) {
return "", fmt.Errorf("%w: empty host", ErrSSRFBlocked)
}
- // Operator opt-in. ResolveAndValidate does not use this path, so
- // user-controlled URLs stay strict.
if pinned, enabled, err := common.PinConfiguredPrivateHost(host); enabled {
return pinned, err
}
- // In-memory test hook. Still resolve so the SQL dial stays pinned.
- if allowAnyHost() {
- return common.ResolveHostPin(host)
- }
// Short-circuit the well-known host aliases DNS lookups may also
// catch but defending against the literal name is cheap and saves
diff --git a/internal/agent/tool/ssrf_test.go b/internal/agent/tool/ssrf_test.go
index 2f279181b6..0dbbfa5c65 100644
--- a/internal/agent/tool/ssrf_test.go
+++ b/internal/agent/tool/ssrf_test.go
@@ -202,22 +202,19 @@ func TestResolveAndValidate(t *testing.T) {
})
}
-// TestResolveAndValidateProductionEnvCannotDisableURLGuard checks that
-// ALLOW_ANY_HOST does not relax user-controlled URL checks, while a
-// database host dial still accepts the resolved address.
-func TestResolveAndValidateProductionEnvCannotDisableURLGuard(t *testing.T) {
- t.Setenv(common.EnvAllowAnyHost, "true")
-
- if _, _, err := ResolveAndValidate("http://127.0.0.1/"); err == nil {
- t.Fatal("ResolveAndValidate(loopback) = nil, want SSRF rejection")
- }
-
- dbHost, err := ValidateDBHost("127.0.0.1")
- if err != nil {
- t.Fatalf("ValidateDBHost(loopback) = %v, want nil when ALLOW_ANY_HOST is set", err)
- }
- if dbHost != "127.0.0.1" {
- t.Fatalf("ValidateDBHost(loopback) = %q, want 127.0.0.1", dbHost)
+// TestResolveAndValidateRejectsPrivateHostsByDefault checks that false or
+// unrecognized ALLOW_ANY_HOST values keep URL and database guards enabled.
+func TestResolveAndValidateRejectsPrivateHostsByDefault(t *testing.T) {
+ for _, value := range []string{"", "0", "false", "no", "off", "invalid"} {
+ t.Run(value, func(t *testing.T) {
+ t.Setenv(common.EnvAllowAnyHost, value)
+ if _, _, err := ResolveAndValidate("http://127.0.0.1/"); err == nil {
+ t.Fatal("ResolveAndValidate(loopback) = nil, want SSRF rejection")
+ }
+ if _, err := ValidateDBHost("127.0.0.1"); err == nil {
+ t.Fatal("ValidateDBHost(loopback) = nil, want SSRF rejection")
+ }
+ })
}
}
@@ -241,18 +238,26 @@ func TestValidateDBHostAllowAnyHostPinsPrivateResolution(t *testing.T) {
if got != "192.168.65.254" {
t.Fatalf("ValidateDBHost(host.docker.internal) = %q, want 192.168.65.254", got)
}
+ host, ip, err := ResolveAndValidate("http://host.docker.internal/")
+ if err != nil || host != "host.docker.internal" || !ip.Equal(net.ParseIP("192.168.65.254")) {
+ t.Fatalf("ResolveAndValidate(host.docker.internal) = %q, %v, %v", host, ip, err)
+ }
if _, err = ValidateDBHost("missing.internal"); err == nil {
t.Fatal("unresolvable host must still fail when ALLOW_ANY_HOST is set")
}
+ for _, rawURL := range []string{"http://missing.internal/", "ftp://127.0.0.1/", "http:///path"} {
+ if _, _, err := ResolveAndValidate(rawURL); err == nil {
+ t.Fatalf("ResolveAndValidate(%q) must still fail when ALLOW_ANY_HOST is set", rawURL)
+ }
+ }
}
-func TestResolveAndValidateTestHookAllowsLocalhost(t *testing.T) {
- common.AllowAnyHostForTest = true
- t.Cleanup(func() { common.AllowAnyHostForTest = false })
+func TestResolveAndValidateAllowAnyHostAllowsLocalhost(t *testing.T) {
+ t.Setenv(common.EnvAllowAnyHost, "true")
host, ip, err := ResolveAndValidate("http://127.0.0.1/")
if err != nil {
- t.Fatalf("ResolveAndValidate(loopback) = %v, want nil with test hook", err)
+ t.Fatalf("ResolveAndValidate(loopback) = %v, want nil when ALLOW_ANY_HOST is set", err)
}
if host != "127.0.0.1" || !ip.Equal(net.ParseIP("127.0.0.1")) {
t.Fatalf("ResolveAndValidate(loopback) = (%q, %v), want (127.0.0.1, 127.0.0.1)", host, ip)
@@ -260,7 +265,7 @@ func TestResolveAndValidateTestHookAllowsLocalhost(t *testing.T) {
dbHost, err := ValidateDBHost("127.0.0.1")
if err != nil {
- t.Fatalf("ValidateDBHost(loopback) = %v, want nil with test hook", err)
+ t.Fatalf("ValidateDBHost(loopback) = %v, want nil when ALLOW_ANY_HOST is set", err)
}
if dbHost != "127.0.0.1" {
t.Fatalf("ValidateDBHost(loopback) = %q, want 127.0.0.1", dbHost)
diff --git a/internal/common/allow_private_host.go b/internal/common/allow_private_host.go
index d683db2bb7..8476c0c7f1 100644
--- a/internal/common/allow_private_host.go
+++ b/internal/common/allow_private_host.go
@@ -28,11 +28,10 @@ import (
// AllowConfiguredPrivateHost reports whether ALLOW_ANY_HOST is set to a
// truthy value (1, true, yes, on), matching the Python host guard.
//
-// The flag is the development opt-in documented in docker/.env. It applies
-// to host-based database and connector dials (MySQL, PostgreSQL, IMAP,
-// test_db_connection, ExeSQL), including host.docker.internal, and configured
-// MCP servers, and Invoke target and proxy URLs. Other URL guards do not
-// call this. DNS pinning remains enabled. Default is off.
+// The flag applies to shared URL and host guards, including database and
+// connector dials, configured MCP servers, agent tools, and Invoke target
+// and proxy URLs. Scheme validation, DNS resolution, and connection pinning
+// remain enabled. Default is off.
func AllowConfiguredPrivateHost() bool {
switch strings.ToLower(strings.TrimSpace(GetEnv(EnvAllowAnyHost))) {
case "1", "true", "yes", "on":
diff --git a/internal/common/allow_private_host_test.go b/internal/common/allow_private_host_test.go
index e7c40f54ae..bfb502c7a1 100644
--- a/internal/common/allow_private_host_test.go
+++ b/internal/common/allow_private_host_test.go
@@ -33,6 +33,7 @@ func TestAllowConfiguredPrivateHost(t *testing.T) {
{value: "false", want: false},
{value: "no", want: false},
{value: "off", want: false},
+ {value: "invalid", want: false},
{value: "1", want: true},
{value: "true", want: true},
{value: "TRUE", want: true},
@@ -45,6 +46,17 @@ func TestAllowConfiguredPrivateHost(t *testing.T) {
if got := AllowConfiguredPrivateHost(); got != tc.want {
t.Fatalf("AllowConfiguredPrivateHost(%q) = %v, want %v", tc.value, got, tc.want)
}
+ if _, err := AssertHostSafe("127.0.0.1"); (err == nil) != tc.want {
+ t.Fatalf("AssertHostSafe(loopback) = %v, want allowed=%v", err, tc.want)
+ }
+ _, ip, err := AssertURLSafe("http://127.0.0.1/")
+ if (err == nil) != tc.want || (err == nil && ip != "127.0.0.1") {
+ t.Fatalf("AssertURLSafe(loopback) = %q, %v, want allowed=%v", ip, err, tc.want)
+ }
+ _, ip, err = AssertURLSafeWithContext(t.Context(), "http://127.0.0.1/")
+ if (err == nil) != tc.want || (err == nil && ip != "127.0.0.1") {
+ t.Fatalf("AssertURLSafeWithContext(loopback) = %q, %v, want allowed=%v", ip, err, tc.want)
+ }
})
}
}
diff --git a/internal/common/http.go b/internal/common/http.go
index c4a1b5a16d..5baf3d286d 100644
--- a/internal/common/http.go
+++ b/internal/common/http.go
@@ -451,10 +451,10 @@ func (t *schemeSafeTransport) RoundTrip(req *http.Request) (*http.Response, erro
}
// strictSSRFTransport wraps an http.RoundTripper so every outgoing request is
-// validated by the strict SSRF guard (scheme + host + globally routable IP).
-// This is the default for cloud-hosted model drivers and closes the
-// go/request-forgery data flow: the user-controllable BaseURL cannot be made to
-// point at private hosts, loopback, link-local, or cloud metadata endpoints.
+// validated by the SSRF guard (scheme + host + globally routable IP unless
+// ALLOW_ANY_HOST is enabled).
+// This is the default for cloud-hosted model drivers and blocks private hosts,
+// loopback, link-local, and cloud metadata endpoints unless explicitly enabled.
type strictSSRFTransport struct{ base http.RoundTripper }
func (t *strictSSRFTransport) RoundTrip(req *http.Request) (*http.Response, error) {
@@ -469,25 +469,10 @@ var AllowedURLSchemes = []string{"http", "https"}
// LookupHost is the indirection used to resolve hostnames. Tests override it.
var LookupHost = net.LookupHost
-// AllowAnyHostForTest is a test-only override that skips the
-// public-IP routability check in AssertURLSafe and AssertHostSafe.
-// Scheme, host, and DNS resolution checks are unchanged: hostnames
-// still resolve, unresolvable hostnames still fail, and callers
-// still pin connections to the returned resolved address. Production
-// code MUST leave this at its zero value (false). Tests that need to
-// talk to a local httptest server flip it on and reset it in
-// t.Cleanup.
-//
-// Host-based database and connector dials, configured MCP servers, and Invoke
-// URLs have a separate ALLOW_ANY_HOST opt-in via AllowConfiguredPrivateHost.
-// AssertURLSafe and AssertHostSafe stay limited to the in-memory test hook.
-var AllowAnyHostForTest = false
-
-// allowAnyHost reads the test-only override. Kept as a private
-// helper so the call sites don't all have to know about the
-// exported variable name.
+// allowAnyHost reads the shared environment opt-in. Scheme, host, and DNS
+// validation still apply when private addresses are permitted.
func allowAnyHost() bool {
- return AllowAnyHostForTest
+ return AllowConfiguredPrivateHost()
}
// AssertURLSchemeSafe is a lenient SSRF guard for drivers that may legitimately
@@ -570,12 +555,12 @@ func assertURLSafe(rawURL string, lookup func(string) ([]string, error)) (hostna
}
// AssertHostSafe validates a bare host (a hostname or a literal IP, with no
-// scheme or port) and returns the first resolved public IP. It is the
+// scheme or port) and returns the first validated IP. It is the
// host-type counterpart of AssertURLSafe: every resolved address must be
-// globally routable (private, loopback, link-local, metadata, multicast and
-// reserved ranges are rejected). Callers dial the returned IP directly so DNS
-// cannot rebind the connection to an internal address between validation and
-// the TCP connect.
+// globally routable unless ALLOW_ANY_HOST is enabled (private, loopback,
+// link-local, metadata, multicast and reserved ranges are otherwise rejected).
+// Callers dial the returned IP directly so DNS cannot rebind the connection
+// to an internal address between validation and the TCP connect.
//
// Used by host-based data sources (IMAP/MySQL/PostgreSQL) and by the ExeSQL /
// test_db_connection host guards, mirroring common/ssrf_guard.py:
diff --git a/internal/dao/ingestion_parser_config_runtime_test.go b/internal/dao/ingestion_parser_config_runtime_test.go
index e8a35704de..91fea3ca05 100644
--- a/internal/dao/ingestion_parser_config_runtime_test.go
+++ b/internal/dao/ingestion_parser_config_runtime_test.go
@@ -143,9 +143,7 @@ func TestMigratedIngestionConfigReachesRuntime(t *testing.T) {
// process-wide SSRF bypass, so this test and its subtests must not run in parallel.
func TestMigratedMinerUControlsReachRequest(t *testing.T) {
t.Setenv(common.EnvMineruBackend, "pipeline")
- previous := common.AllowAnyHostForTest
- common.AllowAnyHostForTest = true
- t.Cleanup(func() { common.AllowAnyHostForTest = previous })
+ t.Setenv(common.EnvAllowAnyHost, "true")
var response bytes.Buffer
archive := zip.NewWriter(&response)
diff --git a/internal/entity/models/model_test_helpers_test.go b/internal/entity/models/model_test_helpers_test.go
index 6096841caa..edecc3ab38 100644
--- a/internal/entity/models/model_test_helpers_test.go
+++ b/internal/entity/models/model_test_helpers_test.go
@@ -14,9 +14,7 @@ import (
// its own dedicated coverage in internal/utility/ssrf_test.go.
func withSSRFBypass(t *testing.T) {
t.Helper()
- prev := common.AllowAnyHostForTest
- common.AllowAnyHostForTest = true
- t.Cleanup(func() { common.AllowAnyHostForTest = prev })
+ t.Setenv(common.EnvAllowAnyHost, "true")
}
func requireNoSuchMethod(t *testing.T, name string, err error) {
diff --git a/internal/entity/models/mws_test.go b/internal/entity/models/mws_test.go
index 65cdc33a75..cbe4d67fce 100644
--- a/internal/entity/models/mws_test.go
+++ b/internal/entity/models/mws_test.go
@@ -114,12 +114,10 @@ func TestNormalizeMWSProjectURL(t *testing.T) {
func TestMWSHTTPClientRejectsPrivateDNSResult(t *testing.T) {
previousLookup := common.LookupHost
- previousAllowAny := common.AllowAnyHostForTest
common.LookupHost = func(string) ([]string, error) { return []string{"127.0.0.1"}, nil }
- common.AllowAnyHostForTest = false
+ t.Setenv(common.EnvAllowAnyHost, "")
t.Cleanup(func() {
common.LookupHost = previousLookup
- common.AllowAnyHostForTest = previousAllowAny
})
request, err := http.NewRequest(http.MethodGet, "https://gpt.mwsapis.ru/projects/demo/openai/v1/models", nil)
diff --git a/internal/handler/agent_model_inputs_test.go b/internal/handler/agent_model_inputs_test.go
index e64f74f924..8d34e9008d 100644
--- a/internal/handler/agent_model_inputs_test.go
+++ b/internal/handler/agent_model_inputs_test.go
@@ -37,9 +37,7 @@ func setupModelInputDebug(t *testing.T, endpoint string) (*AgentHandler, *entity
if err := testsupport.PrepareEnterprisePermissionFixture(db, "u-1", "u-1"); err != nil {
t.Fatalf("prepare agent model input permission fixture: %v", err)
}
- previousHostSetting := common.AllowAnyHostForTest
- common.AllowAnyHostForTest = true
- t.Cleanup(func() { common.AllowAnyHostForTest = previousHostSetting })
+ t.Setenv(common.EnvAllowAnyHost, "true")
if err := models.InitProviderManager("../../conf/models"); err != nil {
t.Fatalf("InitProviderManager: %v", err)
}
diff --git a/internal/handler/chat_audio_test.go b/internal/handler/chat_audio_test.go
index 13034e16ab..511fd5e32b 100644
--- a/internal/handler/chat_audio_test.go
+++ b/internal/handler/chat_audio_test.go
@@ -29,9 +29,7 @@ func TestChatAudioSpeechFailureOmitsProviderError(t *testing.T) {
}))
defer server.Close()
- prevAllow := common.AllowAnyHostForTest
- common.AllowAnyHostForTest = true
- t.Cleanup(func() { common.AllowAnyHostForTest = prevAllow })
+ t.Setenv(common.EnvAllowAnyHost, "true")
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{TranslateError: true})
if err != nil {
diff --git a/internal/ingestion/component/chunker/paddleocr_test.go b/internal/ingestion/component/chunker/paddleocr_test.go
index 9b57caa5cf..ef998f0b42 100644
--- a/internal/ingestion/component/chunker/paddleocr_test.go
+++ b/internal/ingestion/component/chunker/paddleocr_test.go
@@ -20,9 +20,7 @@ import (
)
func TestPaddleOCRHighlightsThroughPipeline(t *testing.T) {
- previous := common.AllowAnyHostForTest
- common.AllowAnyHostForTest = true
- t.Cleanup(func() { common.AllowAnyHostForTest = previous })
+ t.Setenv(common.EnvAllowAnyHost, "true")
tokenizer.SetEngineType("infinity")
t.Cleanup(func() { tokenizer.SetEngineType("") })
body, err := os.ReadFile("../../../entity/models/testdata/paddleocr_layout.json")
diff --git a/internal/ingestion/component/image_ocr_test.go b/internal/ingestion/component/image_ocr_test.go
index 5d051d3ecf..8728690276 100644
--- a/internal/ingestion/component/image_ocr_test.go
+++ b/internal/ingestion/component/image_ocr_test.go
@@ -390,7 +390,7 @@ func TestImageOCRDoesNotUseGlobalEndpoint(t *testing.T) {
// OCR requests even when the model was explicitly selected.
func TestImageOCRKeepsCloudSSRFGuard(t *testing.T) {
db, requests := imageOCRFixture(t, "PaddleOCR", false)
- common.AllowAnyHostForTest = false
+ t.Setenv(common.EnvAllowAnyHost, "")
_, err := extractSelectedImageOCR(t.Context(), db, "tenant-1", "model-b", picturePNG(t))
if err == nil || !strings.Contains(err.Error(), "non-public address") || len(requests.list()) != 0 {
t.Fatalf("cloud transport policy changed: err=%v requests=%v", err, requests.list())
diff --git a/internal/ingestion/component/pdf_parser_model_test.go b/internal/ingestion/component/pdf_parser_model_test.go
index 65d0f5508f..b0a79da546 100644
--- a/internal/ingestion/component/pdf_parser_model_test.go
+++ b/internal/ingestion/component/pdf_parser_model_test.go
@@ -483,7 +483,7 @@ func TestTenantPDFParserEnvironmentIsolation(t *testing.T) {
// bypass the repository's production transport policy.
func TestTenantPDFParserKeepsSSRFGuard(t *testing.T) {
db, ref, requests := tenantPDFParserFixture(t, "SoMark")
- common.AllowAnyHostForTest = false
+ t.Setenv(common.EnvAllowAnyHost, "")
_, handled, err := maybeDispatchTenantPDFParser(t.Context(), db, "sample.pdf", []byte("pdf"),
"tenant-1", schema.ParserSetup{"parse_method": ref})
if !handled || err == nil || !strings.Contains(err.Error(), "non-public address") || len(requests) != 0 {
diff --git a/internal/ingestion/component/ssrf_test_helper_test.go b/internal/ingestion/component/ssrf_test_helper_test.go
index d714d61152..2b54db1f65 100644
--- a/internal/ingestion/component/ssrf_test_helper_test.go
+++ b/internal/ingestion/component/ssrf_test_helper_test.go
@@ -13,7 +13,5 @@ import (
// coverage in internal/utility/ssrf_test.go.
func withSSRFBypass(t *testing.T) {
t.Helper()
- prev := common.AllowAnyHostForTest
- common.AllowAnyHostForTest = true
- t.Cleanup(func() { common.AllowAnyHostForTest = prev })
+ t.Setenv(common.EnvAllowAnyHost, "true")
}
diff --git a/internal/parser/parser/ssrf_test_helper_test.go b/internal/parser/parser/ssrf_test_helper_test.go
index eea94a5306..5c1a2a8ee4 100644
--- a/internal/parser/parser/ssrf_test_helper_test.go
+++ b/internal/parser/parser/ssrf_test_helper_test.go
@@ -12,7 +12,5 @@ import (
// internal/utility/ssrf_test.go.
func withSSRFBypass(t *testing.T) {
t.Helper()
- prev := common.AllowAnyHostForTest
- common.AllowAnyHostForTest = true
- t.Cleanup(func() { common.AllowAnyHostForTest = prev })
+ t.Setenv(common.EnvAllowAnyHost, "true")
}
diff --git a/internal/service/agent_dbcheck.go b/internal/service/agent_dbcheck.go
index ef7d35e5a4..db10b694c5 100644
--- a/internal/service/agent_dbcheck.go
+++ b/internal/service/agent_dbcheck.go
@@ -47,33 +47,13 @@ type TestDBConnectionRequest struct {
Password string `json:"password"`
}
-// AllowAnyHostForTest is a test-only override that disables the SSRF guard
-// in AssertHostIsSafe. Production code must leave it false. Operators allow
-// private database hosts with ALLOW_ANY_HOST; that flag does not relax URL
-// or Invoke checks.
-var AllowAnyHostForTest = false
-
-// allowAnyHost reports the in-memory test override. AssertHostIsSafe checks
-// ALLOW_ANY_HOST separately so URL and Invoke guards stay strict.
-func allowAnyHost() bool {
- return AllowAnyHostForTest
-}
-
-// AssertHostIsSafe returns the first resolved public IP for host, or an
-// error when the host resolves to any non-public address. The check
-// mirrors the SSRF guard in the Python implementation so external
-// service calls cannot pivot to internal network ranges.
+// AssertHostIsSafe resolves host and returns the first validated IP.
+// Non-public addresses are rejected unless ALLOW_ANY_HOST is enabled.
func AssertHostIsSafe(host string) (string, error) {
host = strings.TrimSpace(host)
if host == "" {
return "", errors.New("host must not be empty")
}
- if allowAnyHost() {
- zap.L().Warn("SSRF guard bypass enabled via AllowAnyHostForTest; allowing host without validation",
- zap.String("host", host),
- )
- return host, nil
- }
if pinned, enabled, err := common.PinConfiguredPrivateHost(host); enabled {
return pinned, err
}
diff --git a/internal/service/agent_tool_history_test.go b/internal/service/agent_tool_history_test.go
index cc374aea23..5c4f8c463d 100644
--- a/internal/service/agent_tool_history_test.go
+++ b/internal/service/agent_tool_history_test.go
@@ -59,9 +59,7 @@ func TestRunAgent_ToolHistoryPersists(t *testing.T) {
previousInvoker := component.GetDefaultChatInvokerForTest()
component.SetDefaultChatInvoker(nil)
t.Cleanup(func() { component.SetDefaultChatInvoker(previousInvoker) })
- previousAllow := common.AllowAnyHostForTest
- common.AllowAnyHostForTest = true
- t.Cleanup(func() { common.AllowAnyHostForTest = previousAllow })
+ t.Setenv(common.EnvAllowAnyHost, "true")
if err := models.InitProviderManager("../../conf/models"); err != nil {
t.Fatal(err)
}
diff --git a/internal/service/model_service_test.go b/internal/service/model_service_test.go
index 923d82c4f1..4b90c378fb 100644
--- a/internal/service/model_service_test.go
+++ b/internal/service/model_service_test.go
@@ -314,9 +314,7 @@ func TestVerifyProviderModelNamesUnverifiableTypes(t *testing.T) {
// a real driver. The stand-in used above accepts any request — including one the
// endpoint would refuse — so it cannot catch verification asking for nothing.
func TestVerifyProviderModelAsksSystemOneDecisionEndpoint(t *testing.T) {
- prev := common.AllowAnyHostForTest
- common.AllowAnyHostForTest = true
- t.Cleanup(func() { common.AllowAnyHostForTest = prev })
+ t.Setenv(common.EnvAllowAnyHost, "true")
asked := false
var request struct {
diff --git a/internal/syncer/connector/bitbucket_test.go b/internal/syncer/connector/bitbucket_test.go
index 90adc3c96b..6aa9f5b77c 100644
--- a/internal/syncer/connector/bitbucket_test.go
+++ b/internal/syncer/connector/bitbucket_test.go
@@ -479,18 +479,16 @@ func TestBitbucketConnectorValidateMissingConfig(t *testing.T) {
}
func TestBitbucketGetJSONRetriesOnTransientStatuses(t *testing.T) {
- originalAllowAny := common.AllowAnyHostForTest
originalTries := bitbucketRetryTries
originalDelay := bitbucketRetryBaseDelay
originalBackoff := bitbucketRetryBackoff
originalMaxDelay := bitbucketRetryMaxDelay
- common.AllowAnyHostForTest = true
+ t.Setenv(common.EnvAllowAnyHost, "true")
bitbucketRetryTries = 3
bitbucketRetryBaseDelay = time.Millisecond
bitbucketRetryBackoff = 1
bitbucketRetryMaxDelay = 10 * time.Millisecond
t.Cleanup(func() {
- common.AllowAnyHostForTest = originalAllowAny
bitbucketRetryTries = originalTries
bitbucketRetryBaseDelay = originalDelay
bitbucketRetryBackoff = originalBackoff
diff --git a/internal/syncer/connector/ssrf.go b/internal/syncer/connector/ssrf.go
index 1c79ddeb7b..7385ad0f76 100644
--- a/internal/syncer/connector/ssrf.go
+++ b/internal/syncer/connector/ssrf.go
@@ -199,9 +199,7 @@ func assertConnectorSchemeHTTPS(parsed *url.URL) error {
// validateConnectorURL is the config-time SSRF check used by connector
// Validate/New paths.
func validateConnectorURL(rawURL string) error {
- // localhost is never a legitimate connector base URL, even under the test
- // hook (which only relaxes loopback IPs such as httptest's 127.0.0.1).
- if parsed, err := url.Parse(strings.TrimSpace(rawURL)); err == nil {
+ 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())}
diff --git a/internal/syncer/connector/ssrf_test.go b/internal/syncer/connector/ssrf_test.go
index 3c95d05aab..56a86629b4 100644
--- a/internal/syncer/connector/ssrf_test.go
+++ b/internal/syncer/connector/ssrf_test.go
@@ -525,7 +525,7 @@ func TestAssertConnectorHostSafeLoopbackHookAllowsOnlyLoopback(t *testing.T) {
// TestAssertConnectorHostSafeHonorsAllowAnyHost checks that a private
// connector host is pinned when ALLOW_ANY_HOST is set, and that URL
-// connector checks stay strict.
+// connector checks honor the same opt-in.
func TestAssertConnectorHostSafeHonorsAllowAnyHost(t *testing.T) {
t.Setenv(common.EnvAllowAnyHost, "1")
connectorAllowLoopbackForTest = false
@@ -533,7 +533,10 @@ func TestAssertConnectorHostSafeHonorsAllowAnyHost(t *testing.T) {
orig := common.LookupHost
common.LookupHost = func(host string) ([]string, error) {
- if host == "host.docker.internal" {
+ if ip := net.ParseIP(host); ip != nil {
+ return []string{ip.String()}, nil
+ }
+ if host == "host.docker.internal" || host == "localhost" {
return []string{"192.168.65.254"}, nil
}
return nil, &net.DNSError{Err: "no such host", Name: host, IsNotFound: true}
@@ -555,8 +558,15 @@ func TestAssertConnectorHostSafeHonorsAllowAnyHost(t *testing.T) {
if _, err = assertConnectorHostSafe("missing.internal"); err == nil {
t.Fatal("unresolvable host must still fail when ALLOW_ANY_HOST is set")
}
- if err = validateConnectorURL("http://10.0.0.5/x"); err == nil {
- t.Fatal("ALLOW_ANY_HOST must not relax URL connector checks")
+ for _, rawURL := range []string{"http://10.0.0.5/x", "http://host.docker.internal/x", "http://localhost/x"} {
+ if err = validateConnectorURL(rawURL); err != nil {
+ t.Fatalf("validateConnectorURL(%q) = %v, want nil when ALLOW_ANY_HOST is set", rawURL, err)
+ }
+ }
+ for _, rawURL := range []string{"ftp://10.0.0.5/x", "http:///path", "http://missing.internal/x"} {
+ if err = validateConnectorURL(rawURL); err == nil {
+ t.Fatalf("validateConnectorURL(%q) must still fail when ALLOW_ANY_HOST is set", rawURL)
+ }
}
}
diff --git a/internal/syncer/connector/teams_test.go b/internal/syncer/connector/teams_test.go
index d9f4adecef..ab3a0a33c2 100644
--- a/internal/syncer/connector/teams_test.go
+++ b/internal/syncer/connector/teams_test.go
@@ -113,9 +113,7 @@ func TestTeamsConnectorValidateQueriesTeams(t *testing.T) {
}
func TestTeamsGetJSONReadsBodyBeforeCancel(t *testing.T) {
- previousAllowAnyHost := common.AllowAnyHostForTest
- common.AllowAnyHostForTest = true
- t.Cleanup(func() { common.AllowAnyHostForTest = previousAllowAnyHost })
+ t.Setenv(common.EnvAllowAnyHost, "true")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
diff --git a/internal/utility/upload_test.go b/internal/utility/upload_test.go
index 98f6f5157a..810e06d962 100644
--- a/internal/utility/upload_test.go
+++ b/internal/utility/upload_test.go
@@ -129,9 +129,7 @@ func TestFetchRemoteFileSafelyCancelsDNS(t *testing.T) {
}}
t.Cleanup(func() { net.DefaultResolver = previousResolver })
defer close(release)
- previousAllow := common.AllowAnyHostForTest
- common.AllowAnyHostForTest = true
- t.Cleanup(func() { common.AllowAnyHostForTest = previousAllow })
+ t.Setenv(common.EnvAllowAnyHost, "true")
target := "http://blocked-dns.invalid/image.png"
if redirect {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {