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
feat(security): enhance SSRF protection in RemoteAPIChat
wizardchenfd5278d62d02
diff --git a/internal/models/chat/remote_api.go b/internal/models/chat/remote_api.goindex 2364206a3..2a7316073 100644--- a/internal/models/chat/remote_api.go+++ b/internal/models/chat/remote_api.go@@ -6,7 +6,6 @@ import ("encoding/json""fmt""io"- "net""net/http""strings""time"@@ -20,9 +19,10 @@ import (// rawHTTPClient is a shared HTTP client for raw HTTP LLM calls with connection-level timeouts.// No overall Timeout is set so streaming calls are controlled by context cancellation only.+// Uses SSRFSafeDialContext to prevent DNS rebinding attacks at the connection layer.var rawHTTPClient = &http.Client{Transport: &http.Transport{- DialContext: (&net.Dialer{Timeout: 15 * time.Second}).DialContext,+ DialContext: secutils.SSRFSafeDialContext,TLSHandshakeTimeout: 10 * time.Second,IdleConnTimeout: 90 * time.Second,MaxIdleConnsPerHost: 5,@@ -50,6 +50,12 @@ type RemoteAPIChat struct {// NewRemoteAPIChat 创建远程 API 聊天实例func NewRemoteAPIChat(chatConfig *ChatConfig) (*RemoteAPIChat, error) {+ if chatConfig.BaseURL != "" {+ if err := secutils.ValidateURLForSSRF(chatConfig.BaseURL); err != nil {+ return nil, fmt.Errorf("baseURL SSRF check failed: %w", err)+ }+ }+apiKey := chatConfig.APIKeyconfig := openai.DefaultConfig(apiKey)if baseURL := chatConfig.BaseURL; baseURL != "" {@@ -295,6 +301,9 @@ func (c *RemoteAPIChat) chatWithRawHTTP(ctx context.Context, endpoint string, cuif endpoint == "" {endpoint = c.baseURL + "/chat/completions"}+ if err := secutils.ValidateURLForSSRF(endpoint); err != nil {+ return nil, fmt.Errorf("endpoint SSRF check failed: %w", err)+ }httpReq, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewBuffer(jsonData))if err != nil {return nil, fmt.Errorf("create request: %w", err)@@ -438,6 +447,9 @@ func (c *RemoteAPIChat) chatStreamWithRawHTTP(ctx context.Context, endpoint striif endpoint == "" {endpoint = c.baseURL + "/chat/completions"}+ if err := secutils.ValidateURLForSSRF(endpoint); err != nil {+ return nil, fmt.Errorf("endpoint SSRF check failed: %w", err)+ }httpReq, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewBuffer(jsonData))if err != nil {return nil, fmt.Errorf("create request: %w", err)diff --git a/internal/utils/security.go b/internal/utils/security.goindex 0054d9c06..4cb6d9ea3 100644--- a/internal/utils/security.go+++ b/internal/utils/security.go@@ -746,7 +746,7 @@ func NewSSRFSafeHTTPClient(config SSRFSafeHTTPClientConfig) *http.Client {DisableKeepAlives: config.DisableKeepAlives,DisableCompression: config.DisableCompression,// Dial with SSRF protection - validates resolved IPs before connecting- DialContext: ssrfSafeDialContext,+ DialContext: SSRFSafeDialContext,}return &http.Client{@@ -769,16 +769,26 @@ func NewSSRFSafeHTTPClient(config SSRFSafeHTTPClientConfig) *http.Client {}}-// ssrfSafeDialContext is a custom dial function that validates the resolved IP addresses+// 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.-func ssrfSafeDialContext(ctx context.Context, network, addr string) (net.Conn, error) {+func SSRFSafeDialContext(ctx context.Context, network, addr string) (net.Conn, error) {// Parse host and porthost, _, err := net.SplitHostPort(addr)if err != nil {return nil, fmt.Errorf("invalid address %s: %w", addr, err)}+ // Whitelisted hosts bypass all dial-time SSRF checks, consistent with+ // ValidateURLForSSRF which skips IsSSRFSafeURL for whitelisted hosts.+ if IsSSRFWhitelisted(host) {+ dialer := &net.Dialer{+ Timeout: 30 * time.Second,+ KeepAlive: 30 * time.Second,+ }+ return dialer.DialContext(ctx, network, addr)+ }+// Check if the host is a restricted hostnamehostLower := strings.ToLower(host)for _, restricted := range restrictedHostnames {