Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
refactor: 出站拨号超时默认 5s 并改为 env 可配 (WEKNORA_OUTBOUND_DIAL_TIMEOUT) (#4122)
tayadwxd6269994b336
diff --git a/internal/config/config.go b/internal/config/config.goindex 541bc42a1..16d293e8a 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.goindex efb64534b..fb94fb9a9 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 errordiff --git a/internal/utils/security_dial_timeout_test.go b/internal/utils/security_dial_timeout_test.gonew file mode 100644index 000000000..8c4d2ffd2--- /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)+ }+}