refactor: 出站拨号超时默认 5s 并改为 env 可配 (WEKNORA_OUTBOUND_DIAL_TIMEOUT) (#4122)
* 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 <tayadwx@users.noreply.github.com> Co-authored-by: wizardchen <wizardchen@tencent.com>main
parent
8125ed250f
commit
d6269994b3
|
|
@ -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{}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue