From d6269994b33688c2b85488c24014d95db1948d52 Mon Sep 17 00:00:00 2001 From: tayadwx <42323260+tayadwx@users.noreply.github.com> Date: Sat, 10 Oct 2026 14:50:20 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=87=BA=E7=AB=99=E6=8B=A8?= =?UTF-8?q?=E5=8F=B7=E8=B6=85=E6=97=B6=E9=BB=98=E8=AE=A4=205s=20=E5=B9=B6?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=20env=20=E5=8F=AF=E9=85=8D=20(WEKNORA=5FOUTB?= =?UTF-8?q?OUND=5FDIAL=5FTIMEOUT)=20(#4122)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: make outbound SSRF-safe dial timeout configurable (default 5s) Previously SSRFSafeDialContext hardcoded a 30s dial timeout for every outbound (SSRF-validated) endpoint (VLM/LLM/embedding/ASR/connectors). A black-holed or unreachable endpoint therefore burned ~30s per attempt before failing, which (a) delayed the circuit breaker trip and (b) made the UI show a misleading "thinking" state instead of surfacing the bad-URL error quickly. - Move the dial timeout to WEKNORA_OUTBOUND_DIAL_TIMEOUT, default 5s. - Apply it in config.LoadConfig via utils.SetOutboundDialTimeout; the default is also set in an init() so it holds even if LoadConfig is skipped. Concurrent reads are safe (atomic nanoseconds). - KeepAlive stays 30s; the sandbox/cloud GuardedDialContext (10s) and Ollama dialer (10s) are intentionally left untouched. * test(utils): serialize outbound dial timeout tests --------- Co-authored-by: tayadwx Co-authored-by: wizardchen --- internal/config/config.go | 17 ++++++++++ internal/utils/security.go | 33 ++++++++++++++++++-- internal/utils/security_dial_timeout_test.go | 32 +++++++++++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 internal/utils/security_dial_timeout_test.go diff --git a/internal/config/config.go b/internal/config/config.go index 541bc42a..16d293e8 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -10,6 +10,7 @@ import ( "time" "github.com/Tencent/WeKnora/internal/types" + "github.com/Tencent/WeKnora/internal/utils" "github.com/go-viper/mapstructure/v2" "github.com/spf13/viper" "gopkg.in/yaml.v3" @@ -590,6 +591,7 @@ func LoadConfig() (*Config, error) { applyOIDCEnvOverrides(&cfg) applyAgentEnvOverrides(&cfg) applyKnowledgeBaseEnvOverrides(&cfg) + applyNetworkEnvOverrides() applyAuthAndTenantDefaults(&cfg) applyAuditDefaults(&cfg) @@ -783,6 +785,21 @@ func applyKnowledgeBaseEnvOverrides(cfg *Config) { } } +// applyNetworkEnvOverrides configures global network timeouts that are not +// scoped to a single config section. The outbound (SSRF-safe) dial timeout +// governs how quickly an unreachable model/connector endpoint fails; it +// defaults to 5s and can be raised via WEKNORA_OUTBOUND_DIAL_TIMEOUT for +// slow-to-connect serverless endpoints. +func applyNetworkEnvOverrides() { + dialTimeout := 5 * time.Second + if value := strings.TrimSpace(os.Getenv("WEKNORA_OUTBOUND_DIAL_TIMEOUT")); value != "" { + if d, err := time.ParseDuration(value); err == nil && d > 0 { + dialTimeout = d + } + } + utils.SetOutboundDialTimeout(dialTimeout) +} + func applyAgentEnvOverrides(cfg *Config) { if cfg.Agent == nil { cfg.Agent = &AgentConfig{} diff --git a/internal/utils/security.go b/internal/utils/security.go index efb64534..fb94fb9a 100644 --- a/internal/utils/security.go +++ b/internal/utils/security.go @@ -827,6 +827,35 @@ func SSRFSafeGRPCDialer(ctx context.Context, addr string) (net.Conn, error) { return SSRFSafeDialContext(ctx, "tcp", addr) } +// outboundDialTimeoutNanos caps how long SSRFSafeDialContext may spend +// establishing a TCP connection to an outbound (SSRF-validated) endpoint before +// giving up. A black-holed or otherwise unreachable endpoint should fail fast — +// if a dial takes longer than this, the remote service is effectively +// unavailable and the caller (e.g. the VLM/LLM client) can surface the error +// immediately instead of letting the UI show a misleading "thinking" state. +// Defaults to 5s and is tuned via WEKNORA_OUTBOUND_DIAL_TIMEOUT in +// config.LoadConfig. Stored as nanoseconds so it can be swapped atomically. +var outboundDialTimeoutNanos atomic.Int64 + +func init() { + outboundDialTimeoutNanos.Store(int64(5 * time.Second)) +} + +// SetOutboundDialTimeout overrides the per-connection dial timeout used by +// SSRFSafeDialContext. It is called once at startup from config.LoadConfig and +// is safe to call concurrently. Non-positive values are ignored. +func SetOutboundDialTimeout(d time.Duration) { + if d <= 0 { + return + } + outboundDialTimeoutNanos.Store(int64(d)) +} + +// OutboundDialTimeout returns the current per-connection dial timeout. +func OutboundDialTimeout() time.Duration { + return time.Duration(outboundDialTimeoutNanos.Load()) +} + // SSRFSafeDialContext is a custom dial function that validates the resolved IP addresses // before establishing a connection. This provides an additional layer of SSRF protection // against DNS rebinding attacks during the connection phase. @@ -843,7 +872,7 @@ func SSRFSafeDialContext(ctx context.Context, network, addr string) (net.Conn, e // hosts. Admins must ensure whitelisted domains are under their control. if IsSystemProxy(addr) || IsSSRFWhitelisted(host) { dialer := &net.Dialer{ - Timeout: 30 * time.Second, + Timeout: OutboundDialTimeout(), KeepAlive: 30 * time.Second, } return dialer.DialContext(ctx, network, addr) @@ -892,7 +921,7 @@ func SSRFSafeDialContext(ctx context.Context, network, addr string) (net.Conn, e // If we get here, all IPs are safe. Pin the connection to the validated DNS // answers; TLS still uses the request hostname for SNI/certificate checks. dialer := &net.Dialer{ - Timeout: 30 * time.Second, + Timeout: OutboundDialTimeout(), KeepAlive: 30 * time.Second, } var lastErr error diff --git a/internal/utils/security_dial_timeout_test.go b/internal/utils/security_dial_timeout_test.go new file mode 100644 index 00000000..8c4d2ffd --- /dev/null +++ b/internal/utils/security_dial_timeout_test.go @@ -0,0 +1,32 @@ +package utils + +import ( + "testing" + "time" +) + +// TestOutboundDialTimeoutDefault verifies the package default is 5s. +// Keep these tests serial: they read and mutate the same process-wide timeout. +func TestOutboundDialTimeoutDefault(t *testing.T) { + if got := OutboundDialTimeout(); got != 5*time.Second { + t.Fatalf("default dial timeout = %v, want %v", got, 5*time.Second) + } +} + +// TestSetOutboundDialTimeout verifies the setter is reflected by the getter and +// that non-positive values are ignored. +func TestSetOutboundDialTimeout(t *testing.T) { + original := OutboundDialTimeout() + t.Cleanup(func() { SetOutboundDialTimeout(original) }) + + SetOutboundDialTimeout(2 * time.Second) + if got := OutboundDialTimeout(); got != 2*time.Second { + t.Fatalf("after set 2s: got %v, want %v", got, 2*time.Second) + } + + // Non-positive must be ignored, leaving the previous value intact. + SetOutboundDialTimeout(0) + if got := OutboundDialTimeout(); got != 2*time.Second { + t.Fatalf("after set 0: got %v, want %v (value should be unchanged)", got, 2*time.Second) + } +}