Skip to content

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

fix: secure channel egress against SSRF (#21486)

buua43609540bd04ea5
diff --git a/go.mod b/go.mod
index 7a9b45766a..d1899b4389 100644
--- a/go.mod
+++ b/go.mod
@@ -50,7 +50,7 @@ require (
github.com/jackc/pgx/v5 v5.10.0
github.com/jloor/go-excel-reader v0.3.0
github.com/kaptinlin/jsonrepair v0.4.8
- github.com/larksuite/oapi-sdk-go/v3 v3.9.9
+ github.com/larksuite/oapi-sdk-go/v3 v3.12.2
github.com/ledongthuc/pdf v0.0.0-20250511090121-5959a4027728
github.com/lib/pq v1.10.9
github.com/minio/minio-go/v7 v7.0.99
@@ -58,7 +58,7 @@ require (
github.com/nats-io/nats-server/v2 v2.14.3
github.com/nats-io/nats.go v1.52.0
github.com/nikolalohinski/gonja v1.5.3
- github.com/open-dingtalk/dingtalk-stream-sdk-go v0.0.5
+ github.com/open-dingtalk/dingtalk-stream-sdk-go v0.9.1
github.com/peterh/liner v1.2.2
github.com/pkg/errors v0.9.1
github.com/pkoukk/tiktoken-go v0.1.8
diff --git a/go.sum b/go.sum
index 61ade56138..4fbb526022 100644
--- a/go.sum
+++ b/go.sum
@@ -409,6 +409,8 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/larksuite/oapi-sdk-go/v3 v3.9.9 h1:qzVK5U1AuT/n0Z4LqCN2ATU6MlYp2Iipa7q+fVK5Nh8=
github.com/larksuite/oapi-sdk-go/v3 v3.9.9/go.mod h1:ZEplY+kwuIrj/nqw5uSCINNATcH3KdxSN7y+UxYY5fI=
+github.com/larksuite/oapi-sdk-go/v3 v3.12.2 h1:YjIVkSXW3TZOL3HCdJdTKYYP3ZBIM9ql9+TVyb1XL2w=
+github.com/larksuite/oapi-sdk-go/v3 v3.12.2/go.mod h1:F1nwLfYBSKvD8mS/OFw90Gr+V4RIugPBR44m/ThA5jg=
github.com/ledongthuc/pdf v0.0.0-20250511090121-5959a4027728 h1:QwWKgMY28TAXaDl+ExRDqGQltzXqN/xypdKP86niVn8=
github.com/ledongthuc/pdf v0.0.0-20250511090121-5959a4027728/go.mod h1:1fEHWurg7pvf5SG6XNE5Q8UZmOwex51Mkx3SLhrW5B4=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
@@ -469,6 +471,8 @@ github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/open-dingtalk/dingtalk-stream-sdk-go v0.0.5 h1:CUAhJXmX6soVTbC6hEjR8J9BqfItW/osZpQ7LAdKtCs=
github.com/open-dingtalk/dingtalk-stream-sdk-go v0.0.5/go.mod h1:ln3IqPYYocZbYvl9TAOrG/cxGR9xcn4pnZRLdCTEGEU=
+github.com/open-dingtalk/dingtalk-stream-sdk-go v0.9.1 h1:Lb/Uzkiw2Ugt2Xf03J5wmv81PdkYOiWbI8CNBi1boC8=
+github.com/open-dingtalk/dingtalk-stream-sdk-go v0.9.1/go.mod h1:ln3IqPYYocZbYvl9TAOrG/cxGR9xcn4pnZRLdCTEGEU=
github.com/pelletier/go-toml/v2 v2.4.3 h1:GTRvJQutkOSftxIFD5xw9aepkYNuPWmVJpffdDPYVpY=
github.com/pelletier/go-toml/v2 v2.4.3/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
github.com/peterh/liner v1.2.2 h1:aJ4AOodmL+JxOZZEL2u9iJf8omNRpqHc/EbrK+3mAXw=
diff --git a/internal/channels/bootstrap_test.go b/internal/channels/bootstrap_test.go
index 773bdb763f..e323369ce2 100644
--- a/internal/channels/bootstrap_test.go
+++ b/internal/channels/bootstrap_test.go
@@ -18,6 +18,9 @@ package channels
import (
"context"
+ "net/http"
+ "net/http/httptest"
+ "net/url"
"os"
"path/filepath"
"testing"
@@ -121,6 +124,45 @@ func TestGatewayEnabledRejectsInvalidValue(t *testing.T) {
}
}
+func TestGatewayHealthURLUsesLoopbackForWildcardBind(t *testing.T) {
+ t.Setenv("WHATSAPP_GATEWAY_HOST", "0.0.0.0")
+ t.Setenv("WHATSAPP_GATEWAY_PORT", "3005")
+
+ if got, want := gatewayHealthURL(), "http://127.0.0.1:3005/health"; got != want {
+ t.Fatalf("gatewayHealthURL() = %q, want %q", got, want)
+ }
+}
+
+func TestGatewayHealthURLRejectsRemoteHost(t *testing.T) {
+ t.Setenv("WHATSAPP_GATEWAY_HOST", "gateway.example")
+ t.Setenv("WHATSAPP_GATEWAY_PORT", "3005")
+
+ if got := gatewayHealthURL(); got != "" {
+ t.Fatalf("gatewayHealthURL() = %q, want empty URL for a non-loopback host", got)
+ }
+}
+
+func TestGatewayReachableUsesSafeLoopbackProbe(t *testing.T) {
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Path != "/health" {
+ t.Errorf("health request path = %q, want /health", r.URL.Path)
+ }
+ w.WriteHeader(http.StatusOK)
+ }))
+ defer server.Close()
+
+ serverURL, err := url.Parse(server.URL)
+ if err != nil {
+ t.Fatalf("parse test server URL: %v", err)
+ }
+ t.Setenv("WHATSAPP_GATEWAY_HOST", "127.0.0.1")
+ t.Setenv("WHATSAPP_GATEWAY_PORT", serverURL.Port())
+
+ if !gatewayReachable(context.Background()) {
+ t.Fatal("gatewayReachable() = false for a local healthy gateway")
+ }
+}
+
func TestWhatsAppEnqueueDoesNotMarkDroppedMessageSeen(t *testing.T) {
ch := newWhatsAppChannel(whatsappAccount{AccountID: "account-1"})
worker := &whatsappChatWorker{queue: make(chan core.IncomingMessage, 1)}
diff --git a/internal/channels/dingtalk.go b/internal/channels/dingtalk.go
index 0968942ec0..7b72a27b75 100644
--- a/internal/channels/dingtalk.go
+++ b/internal/channels/dingtalk.go
@@ -32,8 +32,7 @@ import (
"time"
"github.com/open-dingtalk/dingtalk-stream-sdk-go/chatbot"
- "github.com/open-dingtalk/dingtalk-stream-sdk-go/clientV2"
- "github.com/open-dingtalk/dingtalk-stream-sdk-go/payload"
+ "github.com/open-dingtalk/dingtalk-stream-sdk-go/client"
"go.uber.org/zap"
"ragflow/internal/channels/core"
@@ -64,7 +63,7 @@ type dingTalkChannel struct {
cancel context.CancelFunc
handler core.MessageHandler
client *http.Client
- stream clientV2.OpenDingTalkClient
+ stream *client.StreamClient
sessionWebhooks map[string]string
processed map[string]time.Time
inflight map[string]time.Time
@@ -81,26 +80,31 @@ type dingTalkQueuedMessage struct {
}
// newDingTalkChannel creates a DingTalk stream channel backed by the official SDK.
-func newDingTalkChannel(account dingTalkAccount) *dingTalkChannel {
+func newDingTalkChannel(account dingTalkAccount) (*dingTalkChannel, error) {
if account.APIBaseURL == "" {
account.APIBaseURL = defaultDingTalkAPIBaseURL
}
if account.Timeout <= 0 {
account.Timeout = defaultDingTalkTimeout
}
+ proxyURL, err := dingTalkSafeProxyURL()
+ if err != nil {
+ return nil, err
+ }
ch := &dingTalkChannel{
account: account,
- client: &http.Client{Timeout: account.Timeout},
+ client: newSafeChannelHTTPClient(account.Timeout, channelURLPolicy{exactHosts: []string{"oapi.dingtalk.com"}}),
sessionWebhooks: map[string]string{},
processed: map[string]time.Time{},
inflight: map[string]time.Time{},
workers: map[string]*dingTalkWorker{},
}
- ch.stream = clientV2.NewBuilder().
- Credential(&clientV2.AuthClientCredential{ClientId: account.ClientID, ClientSecret: account.ClientSecret}).
- RegisterCallbackHandler(payload.BotMessageCallbackTopic, ch.handleBotCallback).
- Build()
- return ch
+ ch.stream = client.NewStreamClient(
+ client.WithAppCredential(client.NewAppCredentialConfig(account.ClientID, account.ClientSecret)),
+ client.WithProxy(proxyURL),
+ )
+ ch.stream.RegisterChatBotCallbackRouter(ch.handleBotCallback)
+ return ch, nil
}
// newDingTalkChannelFromConfig builds a DingTalk channel from chat_channel.config.credential.
@@ -124,7 +128,7 @@ func newDingTalkChannelFromConfig(accountID string, cfg map[string]any) (*dingTa
ClientSecret: clientSecret,
APIBaseURL: firstString(cfg, "api_base_url", "api_url"),
Timeout: timeout,
- }), nil
+ })
}
// ChannelID returns the platform identifier used by the chat-channel runtime.
@@ -235,25 +239,27 @@ func (c *dingTalkChannel) run(ctx context.Context) {
}
// handleBotCallback receives DingTalk bot messages from the official stream SDK.
-func (c *dingTalkChannel) handleBotCallback(data *chatbot.BotCallbackDataModel) (*chatbot.BotCallbackRespModel, error) {
+func (c *dingTalkChannel) handleBotCallback(ctx context.Context, data *chatbot.BotCallbackDataModel) ([]byte, error) {
c.mu.Lock()
- ctx := c.ctx
+ runCtx := c.ctx
c.mu.Unlock()
- if ctx == nil {
+ if runCtx != nil {
+ ctx = runCtx
+ } else if ctx == nil {
ctx = context.Background()
}
incoming, dedupKey, ok := c.normalizeBotMessage(data)
if !ok {
- return &chatbot.BotCallbackRespModel{}, nil
+ return []byte(`{"response":{}}`), nil
}
if dedupKey != "" && !c.beginMessage(dedupKey) {
common.Info("dingtalk: skipping duplicate message", zap.String("account_id", c.account.AccountID), zap.String("dedup_key", dedupKey))
- return &chatbot.BotCallbackRespModel{}, nil
+ return []byte(`{"response":{}}`), nil
}
if !c.enqueueIncoming(ctx, dingTalkQueuedMessage{incoming: incoming, dedupKey: dedupKey}) && dedupKey != "" {
c.cancelInflight(dedupKey)
}
- return &chatbot.BotCallbackRespModel{}, nil
+ return []byte(`{"response":{}}`), nil
}
// normalizeBotMessage converts DingTalk SDK bot callback data to the chat-channel message model.
@@ -462,8 +468,9 @@ func (c *dingTalkChannel) postWebhook(ctx context.Context, sessionWebhook string
}
// dingTalkExtractText extracts user text from DingTalk message payload variants.
-func dingTalkExtractText(data map[string]any) string {
- text := data["text"]
+func dingTalkExtractText(data any) string {
+ dataMap := dingTalkMap(data)
+ text := dataMap["text"]
if textMap := dingTalkMap(text); len(textMap) > 0 {
if content := dingTalkString(textMap["content"]); content != "" {
return content
@@ -473,7 +480,7 @@ func dingTalkExtractText(data map[string]any) string {
return content
}
- content := data["content"]
+ content := dataMap["content"]
if contentMap := dingTalkMap(content); len(contentMap) > 0 {
for _, key := range []string{"text", "content", "recognition"} {
if value := dingTalkString(contentMap[key]); strings.TrimSpace(value) != "" {
diff --git a/internal/channels/dingtalk_proxy.go b/internal/channels/dingtalk_proxy.go
new file mode 100644
index 0000000000..9ec5c6cad4
--- /dev/null
+++ b/internal/channels/dingtalk_proxy.go
@@ -0,0 +1,151 @@
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package channels
+
+import (
+ "crypto/rand"
+ "crypto/subtle"
+ "encoding/base64"
+ "encoding/hex"
+ "fmt"
+ "io"
+ "net"
+ "net/http"
+ "net/url"
+ "strings"
+ "sync"
+ "time"
+
+ "ragflow/internal/common"
+)
+
+var dingTalkEgressProxy struct {
+ sync.Once
+ url string
+ err error
+}
+
+type dingtalkProxyHandler struct {
+ authorization string
+}
+
+func dingTalkSafeProxyURL() (string, error) {
+ dingTalkEgressProxy.Do(func() {
+ secret := make([]byte, 32)
+ if _, dingTalkEgressProxy.err = rand.Read(secret); dingTalkEgressProxy.err != nil {
+ return
+ }
+ password := hex.EncodeToString(secret)
+ authorization := "Basic " + base64.StdEncoding.EncodeToString([]byte("ragflow:"+password))
+
+ listener, err := net.Listen("tcp", "127.0.0.1:0")
+ if err != nil {
+ dingTalkEgressProxy.err = fmt.Errorf("start DingTalk egress proxy: %w", err)
+ return
+ }
+ server := &http.Server{
+ Handler: &dingtalkProxyHandler{authorization: authorization},
+ ReadHeaderTimeout: 5 * time.Second,
+ }
+ go func() {
+ _ = server.Serve(listener)
+ }()
+
+ proxyURL := &url.URL{
+ Scheme: "http",
+ Host: listener.Addr().String(),
+ User: url.UserPassword("ragflow", password),
+ }
+ dingTalkEgressProxy.url = proxyURL.String()
+ })
+ return dingTalkEgressProxy.url, dingTalkEgressProxy.err
+}
+
+func (h *dingtalkProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ if r.Method != http.MethodConnect {
+ http.Error(w, "CONNECT required", http.StatusMethodNotAllowed)
+ return
+ }
+ if subtle.ConstantTimeCompare([]byte(r.Header.Get("Proxy-Authorization")), []byte(h.authorization)) != 1 {
+ http.Error(w, "proxy authentication required", http.StatusProxyAuthRequired)
+ return
+ }
+
+ _, resolvedIP, err := resolveDingTalkProxyDestination(r.Host)
+ if err != nil {
+ http.Error(w, "destination is not allowed", http.StatusForbidden)
+ return
+ }
+ upstream, err := (&net.Dialer{Timeout: 10 * time.Second, KeepAlive: 30 * time.Second}).DialContext(
+ r.Context(),
+ "tcp",
+ net.JoinHostPort(resolvedIP, "443"),
+ )
+ if err != nil {
+ http.Error(w, "destination is unavailable", http.StatusBadGateway)
+ return
+ }
+ defer upstream.Close()
+
+ hijacker, ok := w.(http.Hijacker)
+ if !ok {
+ http.Error(w, "proxy connection is unavailable", http.StatusInternalServerError)
+ return
+ }
+ client, buffered, err := hijacker.Hijack()
+ if err != nil {
+ return
+ }
+ defer client.Close()
+ if _, err = buffered.WriteString("HTTP/1.1 200 Connection Established\r\n\r\n"); err != nil {
+ return
+ }
+ if err = buffered.Flush(); err != nil {
+ return
+ }
+
+ copyDone := make(chan struct{}, 2)
+ go func() {
+ _, _ = io.Copy(upstream, buffered.Reader)
+ copyDone <- struct{}{}
+ }()
+ go func() {
+ _, _ = io.Copy(client, upstream)
+ copyDone <- struct{}{}
+ }()
+ <-copyDone
+}
+
+func resolveDingTalkProxyDestination(address string) (string, string, error) {
+ host, port, err := net.SplitHostPort(strings.TrimSpace(address))
+ if err != nil {
+ return "", "", fmt.Errorf("invalid DingTalk proxy destination")
+ }
+ host, err = normalizeChannelHost(host)
+ if err != nil {
+ return "", "", fmt.Errorf("invalid DingTalk proxy host")
+ }
+ if port != "443" {
+ return "", "", fmt.Errorf("DingTalk proxy destination must use port 443")
+ }
+ if host != "api.dingtalk.com" && host != "wss-open-connection.dingtalk.com" {
+ return "", "", fmt.Errorf("DingTalk proxy host %q is not trusted", host)
+ }
+ resolvedIP, err := common.AssertHostSafe(host)
+ if err != nil {
+ return "", "", fmt.Errorf("DingTalk proxy host %q is not safe: %w", host, err)
+ }
+ return host, resolvedIP, nil
+}
diff --git a/internal/channels/dingtalk_proxy_test.go b/internal/channels/dingtalk_proxy_test.go
new file mode 100644
index 0000000000..ffc74d5fc9
--- /dev/null
+++ b/internal/channels/dingtalk_proxy_test.go
@@ -0,0 +1,98 @@
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package channels
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "ragflow/internal/common"
+)
+
+func TestResolveDingTalkProxyDestination(t *testing.T) {
+ originalLookup := common.LookupHost
+ t.Cleanup(func() { common.LookupHost = originalLookup })
+ common.LookupHost = func(host string) ([]string, error) {
+ if host != "api.dingtalk.com" {
+ t.Fatalf("unexpected DNS lookup host %q", host)
+ }
+ return []string{"8.8.8.8"}, nil
+ }
+
+ host, ip, err := resolveDingTalkProxyDestination("api.dingtalk.com:443")
+ if err != nil {
+ t.Fatalf("resolveDingTalkProxyDestination() error = %v", err)
+ }
+ if host != "api.dingtalk.com" || ip != "8.8.8.8" {
+ t.Fatalf("resolved destination = %q, %q; want api.dingtalk.com, 8.8.8.8", host, ip)
+ }
+}
+
+func TestResolveDingTalkProxyDestinationRejectsUntrustedHostsAndPorts(t *testing.T) {
+ for _, address := range []string{
+ "attacker.example:443",
+ "api.dingtalk.com:80",
+ "api.dingtalk.com",
+ } {
+ t.Run(address, func(t *testing.T) {
+ if _, _, err := resolveDingTalkProxyDestination(address); err == nil {
+ t.Fatalf("resolveDingTalkProxyDestination(%q) unexpectedly succeeded", address)
+ }
+ })
+ }
+}
+
+func TestResolveDingTalkProxyDestinationRejectsPrivateDNS(t *testing.T) {
+ originalLookup := common.LookupHost
+ t.Cleanup(func() { common.LookupHost = originalLookup })
+ common.LookupHost = func(string) ([]string, error) { return []string{"169.254.169.254"}, nil }
+
+ if _, _, err := resolveDingTalkProxyDestination("api.dingtalk.com:443"); err == nil || !strings.Contains(err.Error(), "not safe") {
+ t.Fatalf("resolveDingTalkProxyDestination() error = %v, want unsafe-address rejection", err)
+ }
+}
+
+func TestDingTalkProxyRequiresAuthenticationAndCONNECT(t *testing.T) {
+ handler := &dingtalkProxyHandler{authorization: "Basic expected"}
+ for _, test := range []struct {
+ name string
+ method string
+ authorize string
+ wantStatus int
+ host string
+ }{
+ {name: "unauthenticated CONNECT", method: http.MethodConnect, wantStatus: http.StatusProxyAuthRequired},
+ {name: "authenticated non-CONNECT", method: http.MethodGet, authorize: "Basic expected", wantStatus: http.StatusMethodNotAllowed},
+ {name: "authenticated untrusted CONNECT", method: http.MethodConnect, authorize: "Basic expected", wantStatus: http.StatusForbidden, host: "attacker.example:443"},
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ req := httptest.NewRequest(test.method, "http://proxy/", nil)
+ req.Host = test.host
+ if req.Host == "" {
+ req.Host = "api.dingtalk.com:443"
+ }
+ if test.authorize != "" {
+ req.Header.Set("Proxy-Authorization", test.authorize)
+ }
+ resp := httptest.NewRecorder()
+ handler.ServeHTTP(resp, req)
+ if resp.Code != test.wantStatus {
+ t.Fatalf("proxy status = %d, want %d", resp.Code, test.wantStatus)
+ }
+ })
+ }
+}
diff --git a/internal/channels/dingtalk_test.go b/internal/channels/dingtalk_test.go
index 3906b9bf55..4dc9392950 100644
--- a/internal/channels/dingtalk_test.go
+++ b/internal/channels/dingtalk_test.go
@@ -97,7 +97,10 @@ func TestDingTalkBotDedupKey(t *testing.T) {
}
func TestDingTalkNormalizeBotMessage(t *testing.T) {
- ch := newDingTalkChannel(dingTalkAccount{AccountID: "account-1", ClientID: "client-1", ClientSecret: "secret-1"})
+ ch, err := newDingTalkChannel(dingTalkAccount{AccountID: "account-1", ClientID: "client-1", ClientSecret: "secret-1"})
+ if err != nil {
+ t.Fatal(err)
+ }
incoming, dedupKey, ok := ch.normalizeBotMessage(&chatbot.BotCallbackDataModel{
ConversationId: "chat-1",
@@ -131,11 +134,15 @@ func TestDingTalkSendUsesCachedSessionWebhook(t *testing.T) {
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
+ allowChannelURLForTest(t, server.URL)
- ch := newDingTalkChannel(dingTalkAccount{AccountID: "account-1", ClientID: "client-1", ClientSecret: "secret-1"})
+ ch, err := newDingTalkChannel(dingTalkAccount{AccountID: "account-1", ClientID: "client-1", ClientSecret: "secret-1"})
+ if err != nil {
+ t.Fatal(err)
+ }
ch.sessionWebhooks["chat-1"] = server.URL
- err := ch.Send(context.Background(), core.OutgoingMessage{ChatID: "chat-1", Text: "hello"})
+ err = ch.Send(context.Background(), core.OutgoingMessage{ChatID: "chat-1", Text: "hello"})
if err != nil {
t.Fatalf("Send returned error: %v", err)
}
@@ -145,7 +152,10 @@ func TestDingTalkSendUsesCachedSessionWebhook(t *testing.T) {
}
func TestDingTalkEnqueueFailureCancelsInflight(t *testing.T) {
- ch := newDingTalkChannel(dingTalkAccount{AccountID: "account-1", ClientID: "client-1", ClientSecret: "secret-1"})
+ ch, err := newDingTalkChannel(dingTalkAccount{AccountID: "account-1", ClientID: "client-1", ClientSecret: "secret-1"})
+ if err != nil {
+ t.Fatal(err)
+ }
worker := &dingTalkWorker{queue: make(chan dingTalkQueuedMessage, 1)}
worker.queue <- dingTalkQueuedMessage{incoming: core.IncomingMessage{MessageID: "queued"}}
ch.workers["chat-1"] = worker
diff --git a/internal/channels/discord.go b/internal/channels/discord.go
index 944cd3f306..5c113b6930 100644
--- a/internal/channels/discord.go
+++ b/internal/channels/discord.go
@@ -145,7 +145,7 @@ func newDiscordChannel(account discordAccount) *discordChannel {
}
return &discordChannel{
account: account,
- client: &http.Client{Timeout: account.Timeout},
+ client: newSafeChannelHTTPClient(account.Timeout, channelURLPolicy{exactHosts: []string{"discord.com"}}),
seen: map[string]time.Time{},
workers: map[string]*discordChatWorker{},
}
@@ -305,10 +305,10 @@ func (c *discordChannel) runGateway(ctx context.Context) error {
// set websocket connection
header := http.Header{}
- dialer := *websocket.DefaultDialer
- dialer.HandshakeTimeout = discordHandshakeTimeout
-
- conn, _, err := dialer.DialContext(ctx, wsURL, header)
+ conn, _, err := dialSafeChannelWebSocket(ctx, wsURL, header, discordHandshakeTimeout, channelURLPolicy{
+ exactHosts: []string{"gateway.discord.gg"},
+ hostSuffixes: []string{"discord.gg"},
+ })
if err != nil {
return err
}
diff --git a/internal/channels/discord_test.go b/internal/channels/discord_test.go
index 2a4e9f2c05..b26edf82d2 100644
--- a/internal/channels/discord_test.go
+++ b/internal/channels/discord_test.go
@@ -116,6 +116,7 @@ func TestDiscordRequestJSONReturnsRateLimitError(t *testing.T) {
_, _ = w.Write([]byte(`{"message":"rate limited","retry_after":0.001,"global":true}`))
}))
defer server.Close()
+ allowChannelURLForTest(t, server.URL)
ch := newDiscordChannel(discordAccount{
AccountID: "account-1",
@@ -152,6 +153,7 @@ func TestDiscordSendRetriesRateLimit(t *testing.T) {
_, _ = w.Write([]byte(`{"id":"message-2"}`))
}))
defer server.Close()
+ allowChannelURLForTest(t, server.URL)
ch := newDiscordChannel(discordAccount{
AccountID: "account-1",
diff --git a/internal/channels/egress.go b/internal/channels/egress.go
new file mode 100644
index 0000000000..75daff5f9b
--- /dev/null
+++ b/internal/channels/egress.go
@@ -0,0 +1,443 @@
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package channels
+
+import (
+ "context"
+ "fmt"
+ "net"
+ "net/http"
+ "net/url"
+ "os"
+ "strings"
+ "sync"
+ "time"
+
+ "github.com/gorilla/websocket"
+ "golang.org/x/net/idna"
+
+ "ragflow/internal/common"
+ "ragflow/internal/utility"
+)
+
+// RAGFLOW_CHANNEL_ALLOWED_HOSTS is a comma-separated list of operator-approved
+// exact hosts, optionally with ports. It does not accept wildcards.
+const channelAllowedHostsEnv = "RAGFLOW_CHANNEL_ALLOWED_HOSTS"
+
+type channelURLPolicy struct {
+ exactHosts []string
+ hostSuffixes []string
+ localBaseURL string
+ localPathPrefix string
+}
+
+type channelHTTPTransport struct {
+ policy channelURLPolicy
+ timeout time.Duration
+ mu sync.Mutex
+ transports map[string]http.RoundTripper
+}
+
+func newSafeChannelHTTPClient(timeout time.Duration, policy channelURLPolicy) *http.Client {
+ if timeout <= 0 {
+ timeout = 30 * time.Second
+ }
+ return &http.Client{
+ Timeout: timeout,
+ Transport: &channelHTTPTransport{policy: policy, timeout: timeout, transports: map[string]http.RoundTripper{}},
+ CheckRedirect: func(*http.Request, []*http.Request) error {
+ return http.ErrUseLastResponse
+ },
+ }
+}
+
+func newChannelSDKWebSocketDialer(base *websocket.Dialer) *websocket.Dialer {
+ if base == nil {
+ base = &websocket.Dialer{}
+ }
+ dialer := *base
+ dialer.NetDial = nil
+ dialer.NetDialTLSContext = nil
+ dialer.Proxy = func(req *http.Request) (*url.URL, error) {
+ if req == nil || req.URL == nil || req.URL.Scheme != "https" || req.URL.User != nil || req.URL.Fragment != "" {
+ return nil, fmt.Errorf("channel SDK WebSocket must use a valid TLS URL")
+ }
+ if err := validateChannelSDKWebSocketDestination(req.URL.Hostname(), channelURLPort(req.URL)); err != nil {
+ return nil, err
+ }
+ return nil, nil
+ }
+ dialer.NetDialContext = func(ctx context.Context, network, address string) (net.Conn, error) {
+ if network != "tcp" && network != "tcp4" && network != "tcp6" {
+ return nil, fmt.Errorf("unsupported channel SDK WebSocket network %q", network)
+ }
+ host, port, err := net.SplitHostPort(address)
+ if err != nil {
+ return nil, fmt.Errorf("invalid channel SDK WebSocket destination")
+ }
+ host, err = normalizeChannelHost(host)
+ if err != nil {
+ return nil, fmt.Errorf("invalid channel SDK WebSocket host")
+ }
+ if err := validateChannelSDKWebSocketDestination(host, port); err != nil {
+ return nil, err
+ }
+ resolvedIP, err := common.AssertHostSafe(host)
+ if err != nil {
+ return nil, fmt.Errorf("channel SDK WebSocket host %q is not safe: %w", host, err)
+ }
+ return (&net.Dialer{Timeout: dialer.HandshakeTimeout, KeepAlive: 30 * time.Second}).DialContext(
+ ctx,
+ network,
+ net.JoinHostPort(resolvedIP, port),
+ )
+ }
+ return &dialer
+}
+
+func validateChannelSDKWebSocketDestination(host, port string) error {
+ host, err := normalizeChannelHost(host)
+ if err != nil {
+ return fmt.Errorf("invalid channel SDK WebSocket host")
+ }
+ if port != "443" {
+ return fmt.Errorf("channel SDK WebSocket destination must use port 443")
+ }
+ switch host {
+ case "msg-frontier.feishu.cn", "msg-frontier.larksuite.com", "wss-open-connection.dingtalk.com":
+ return nil
+ default:
+ return fmt.Errorf("channel SDK WebSocket host %q is not trusted", host)
+ }
+}
+
+func (t *channelHTTPTransport) RoundTrip(req *http.Request) (*http.Response, error) {
+ if req == nil || req.URL == nil {
+ return nil, fmt.Errorf("channel request has no URL")
+ }
+ host, resolvedIP, _, err := resolveChannelDestination(req.URL, t.policy, false)
+ if err != nil {
+ return nil, err
+ }
+ transport := t.transport(host, resolvedIP)
+ return transport.RoundTrip(req)
+}
+
+func (t *channelHTTPTransport) transport(host, resolvedIP string) http.RoundTripper {
+ key := host + "\x00" + resolvedIP
+ t.mu.Lock()
+ defer t.mu.Unlock()
+ if transport := t.transports[key]; transport != nil {
+ return transport
+ }
+ client := utility.PinnedHTTPClient(host, resolvedIP, t.timeout)
+ t.transports[key] = client.Transport
+ return client.Transport
+}
+
+func dialSafeChannelWebSocket(ctx context.Context, rawURL string, header http.Header, timeout time.Duration, policy channelURLPolicy) (*websocket.Conn, *http.Response, error) {
+ if timeout <= 0 {
+ timeout = 30 * time.Second
+ }
+ parsed, err := url.Parse(strings.TrimSpace(rawURL))
+ if err != nil {
+ return nil, nil, fmt.Errorf("invalid channel WebSocket URL")
+ }
+ host, resolvedIP, port, err := resolveChannelDestination(parsed, policy, true)
+ if err != nil {
+ return nil, nil, err
+ }
+
+ dialer := *websocket.DefaultDialer
+ dialer.Proxy = nil
+ dialer.NetDial = nil
+ dialer.NetDialTLSContext = nil
+ dialer.HandshakeTimeout = timeout
+ dialer.NetDialContext = func(ctx context.Context, network, address string) (net.Conn, error) {
+ addressHost, addressPort, splitErr := net.SplitHostPort(address)
+ normalizedAddressHost, normalizeErr := normalizeChannelHost(addressHost)
+ if splitErr != nil || normalizeErr != nil || normalizedAddressHost != host || addressPort != port {
+ return nil, fmt.Errorf("unvalidated channel WebSocket destination")
+ }
+ return (&net.Dialer{Timeout: timeout, KeepAlive: 30 * time.Second}).DialContext(
+ ctx,
+ network,
+ net.JoinHostPort(resolvedIP, addressPort),
+ )
+ }
+ return dialer.DialContext(ctx, rawURL, header)
+}
+
+func resolveChannelDestination(parsed *url.URL, policy channelURLPolicy, websocketURL bool) (string, string, string, error) {
+ if parsed == nil || !parsed.IsAbs() || parsed.Hostname() == "" || parsed.User != nil || parsed.Fragment != "" {
+ return "", "", "", fmt.Errorf("invalid channel destination URL")
+ }
+
+ scheme := strings.ToLower(parsed.Scheme)
+ switch {
+ case websocketURL && scheme != "ws" && scheme != "wss":
+ return "", "", "", fmt.Errorf("channel WebSocket URL must use ws or wss")
+ case !websocketURL && scheme != "http" && scheme != "https":
+ return "", "", "", fmt.Errorf("channel HTTP URL must use http or https")
+ }
+
+ host, err := normalizeChannelHost(parsed.Hostname())
+ if err != nil {
+ return "", "", "", fmt.Errorf("invalid channel destination host")
+ }
+ port := channelURLPort(parsed)
+
+ if localHost, localIP, localPort, ok := resolveAllowedLocalChannelDestination(parsed, policy, websocketURL); ok {
+ return localHost, localIP, localPort, nil
+ }
+
+ operatorAllowed, operatorPortAllowed := operatorAllowsChannelDestination(parsed)
+ if !operatorPortAllowed && !isStandardChannelPort(parsed) {
+ return "", "", "", fmt.Errorf("nonstandard port is not allowed for channel destination %q", host)
+ }
+
+ if operatorAllowed {
+ resolvedIP, privateOnly, err := lookupChannelHost(parsed.Hostname())
+ if err != nil {
+ return "", "", "", err
+ }
+ if !isSecureChannelScheme(scheme) && !privateOnly {
+ return "", "", "", fmt.Errorf("unencrypted channel destination %q must resolve only to private addresses", host)
+ }
+ return host, resolvedIP, port, nil
+ }
+
+ if !isTrustedChannelHost(host, policy) {
+ return "", "", "", fmt.Errorf("channel destination host %q is not trusted; configure %s for an approved custom endpoint", host, channelAllowedHostsEnv)
+ }
+ if !isSecureChannelScheme(scheme) {
+ return "", "", "", fmt.Errorf("channel destination %q must use TLS", host)
+ }
+
+ checkURL := *parsed
+ if websocketURL {
+ if scheme == "ws" {
+ checkURL.Scheme = "http"
+ } else {
+ checkURL.Scheme = "https"
+ }
+ }
+ _, resolvedIP, err := common.AssertURLSafe(checkURL.String())
+ if err != nil {
+ return "", "", "", fmt.Errorf("channel destination %q is not safe: %w", host, err)
+ }
+ return host, resolvedIP, port, nil
+}
+
+func isTrustedChannelHost(host string, policy channelURLPolicy) bool {
+ for _, allowedHost := range policy.exactHosts {
+ normalized, err := normalizeChannelHost(allowedHost)
+ if err == nil && host == normalized {
+ return true
+ }
+ }
+ for _, suffix := range policy.hostSuffixes {
+ normalized, err := normalizeChannelHost(suffix)
+ if err == nil && (host == normalized || strings.HasSuffix(host, "."+normalized)) {
+ return true
+ }
+ }
+ return false
+}
+
+func operatorAllowsChannelDestination(parsed *url.URL) (bool, bool) {
+ rawHosts := strings.Split(os.Getenv(channelAllowedHostsEnv), ",")
+ requestHost, err := normalizeChannelHost(parsed.Hostname())
+ if err != nil {
+ return false, false
+ }
+ requestPort := channelURLPort(parsed)
+ for _, rawHost := range rawHosts {
+ rawHost = strings.TrimSpace(rawHost)
+ if rawHost == "" || strings.Contains(rawHost, "://") {
+ continue
+ }
+ allowed, err := url.Parse("//" + rawHost)
+ if err != nil || allowed.Hostname() == "" || allowed.User != nil || allowed.Path != "" || allowed.RawQuery != "" || allowed.Fragment != "" {
+ continue
+ }
+ allowedHost, err := normalizeChannelHost(allowed.Hostname())
+ if err != nil || requestHost != allowedHost {
+ continue
+ }
+ if allowed.Port() == "" {
+ if isStandardChannelPort(parsed) {
+ return true, true
+ }
+ continue
+ }
+ if allowed.Port() == requestPort {
+ return true, true
+ }
+ }
+ return false, false
+}
+
+func lookupChannelHost(hostname string) (string, bool, error) {
+ addresses, err := common.LookupHost(hostname)
+ if err != nil {
+ return "", false, fmt.Errorf("could not resolve channel destination host %q: %w", hostname, err)
+ }
+ if len(addresses) == 0 {
+ return "", false, fmt.Errorf("channel destination host %q resolved to no addresses", hostname)
+ }
+ resolvedIP := ""
+ privateOnly := true
+ for _, address := range addresses {
+ ip := net.ParseIP(address)
+ if ip == nil || ip.IsUnspecified() || ip.IsMulticast() {
+ return "", false, fmt.Errorf("channel destination host %q resolved to an invalid address", hostname)
+ }
+ if ipv4 := ip.To4(); ipv4 != nil {
+ ip = ipv4
+ }
+ if ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() {
+ return "", false, fmt.Errorf("channel destination host %q resolved to a link-local address", hostname)
+ }
+ if resolvedIP == "" {
+ resolvedIP = ip.String()
+ }
+ if !ip.IsPrivate() && !ip.IsLoopback() {
+ privateOnly = false
+ }
+ }
+ return resolvedIP, privateOnly, nil
+}
+
+func resolveAllowedLocalChannelDestination(parsed *url.URL, policy channelURLPolicy, websocketURL bool) (string, string, string, bool) {
+ if policy.localBaseURL == "" || policy.localPathPrefix == "" || !isSafeLocalChannelPath(parsed.EscapedPath(), policy.localPathPrefix) {
+ return "", "", "", false
+ }
+ base, err := url.Parse(policy.localBaseURL)
+ if err != nil || base.User != nil || base.Path != "" || base.RawQuery != "" || base.Fragment != "" {
+ return "", "", "", false
+ }
+ baseHost, err := normalizeChannelHost(base.Hostname())
+ if err != nil || !isLocalChannelHost(baseHost) {
+ return "", "", "", false
+ }
+ resolvedIP, err := lookupLoopbackChannelHost(base.Hostname())
+ if err != nil {
+ return "", "", "", false
+ }
+ expectedScheme := strings.ToLower(base.Scheme)
+ if websocketURL {
+ switch expectedScheme {
+ case "http":
+ expectedScheme = "ws"
+ case "https":
+ expectedScheme = "wss"
+ }
+ }
+ actualHost, err := normalizeChannelHost(parsed.Hostname())
+ if err != nil || !strings.EqualFold(parsed.Scheme, expectedScheme) || actualHost != baseHost || channelURLPort(parsed) != channelURLPort(base) {
+ return "", "", "", false
+ }
+ return actualHost, resolvedIP, channelURLPort(parsed), true
+}
+
+func isSafeLocalChannelPath(path, prefix string) bool {
+ if strings.HasSuffix(prefix, "/") {
+ if !strings.HasPrefix(path, prefix) {
+ return false
+ }
+ } else if path != prefix {
+ return false
+ }
+ if strings.ContainsAny(path, "\\") || strings.Contains(strings.ToLower(path), "%2f") || strings.Contains(strings.ToLower(path), "%5c") {
+ return false
+ }
+ decoded, err := url.PathUnescape(path)
+ if err != nil || strings.ContainsAny(decoded, "\\") {
+ return false
+ }
+ for _, segment := range strings.Split(decoded, "/") {
+ if segment == "." || segment == ".." {
+ return false
+ }
+ }
+ return true
+}
+
+func isLocalChannelHost(host string) bool {
+ if ip := net.ParseIP(host); ip != nil {
+ return ip.IsLoopback()
+ }
+ return strings.EqualFold(host, "localhost")
+}
+
+func lookupLoopbackChannelHost(hostname string) (string, error) {
+ addresses, err := common.LookupHost(hostname)
+ if err != nil {
+ return "", fmt.Errorf("could not resolve local channel destination %q: %w", hostname, err)
+ }
+ if len(addresses) == 0 {
+ return "", fmt.Errorf("local channel destination %q resolved to no addresses", hostname)
+ }
+ resolvedIP := ""
+ for _, address := range addresses {
+ ip := net.ParseIP(address)
+ if ip == nil || !ip.IsLoopback() {
+ return "", fmt.Errorf("local channel destination %q did not resolve only to loopback addresses", hostname)
+ }
+ if resolvedIP == "" {
+ resolvedIP = ip.String()
+ }
+ }
+ return resolvedIP, nil
+}
+
+func channelURLPort(parsed *url.URL) string {
+ if port := parsed.Port(); port != "" {
+ return port
+ }
+ switch strings.ToLower(parsed.Scheme) {
+ case "http", "ws":
+ return "80"
+ case "https", "wss":
+ return "443"
+ default:
+ return ""
+ }
+}
+
+func isStandardChannelPort(parsed *url.URL) bool {
+ port := channelURLPort(parsed)
+ switch strings.ToLower(parsed.Scheme) {
+ case "http", "ws":
+ return port == "80"
+ case "https", "wss":
+ return port == "443"
+ default:
+ return false
+ }
+}
+
+func isSecureChannelScheme(scheme string) bool {
+ return strings.EqualFold(scheme, "https") || strings.EqualFold(scheme, "wss")
+}
+
+func normalizeChannelHost(host string) (string, error) {
+ host = strings.TrimSuffix(strings.TrimSpace(strings.ToLower(host)), ".")
+ if ip := net.ParseIP(host); ip != nil {
+ return ip.String(), nil
+ }
+ return idna.Lookup.ToASCII(host)
+}
diff --git a/internal/channels/egress_test.go b/internal/channels/egress_test.go
new file mode 100644
index 0000000000..0ceb36128e
--- /dev/null
+++ b/internal/channels/egress_test.go
@@ -0,0 +1,289 @@
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package channels
+
+import (
+ "context"
+ "fmt"
+ "net/http"
+ "net/http/httptest"
+ "net/url"
+ "os"
+ "strings"
+ "sync/atomic"
+ "testing"
+ "time"
+
+ "github.com/gorilla/websocket"
+
+ "ragflow/internal/common"
+)
+
+func allowChannelURLForTest(t *testing.T, rawURL string) {
+ t.Helper()
+ parsed, err := url.Parse(rawURL)
+ if err != nil {
+ t.Fatalf("parse test endpoint: %v", err)
+ }
+ allowed := strings.TrimSpace(os.Getenv(channelAllowedHostsEnv))
+ if allowed != "" {
+ allowed += ","
+ }
+ t.Setenv(channelAllowedHostsEnv, allowed+parsed.Host)
+}
+
+func TestSafeChannelHTTPClientRejectsUntrustedHost(t *testing.T) {
+ client := newSafeChannelHTTPClient(time.Second, channelURLPolicy{})
+ _, err := client.Get("https://attacker.example/path")
+ if err == nil || !strings.Contains(err.Error(), "is not trusted") {
+ t.Fatalf("Get() error = %v, want an untrusted-host rejection", err)
+ }
+}
+
+func TestSafeChannelHTTPClientRejectsPrivateOfficialHost(t *testing.T) {
+ t.Setenv(channelAllowedHostsEnv, "")
+ client := newSafeChannelHTTPClient(time.Second, channelURLPolicy{exactHosts: []string{"127.0.0.1"}})
+ _, err := client.Get("https://127.0.0.1/")
+ if err == nil || !strings.Contains(err.Error(), "non-public address") {
+ t.Fatalf("Get() error = %v, want a private-address rejection", err)
+ }
+}
+
+func TestSafeChannelHTTPClientPinsOperatorAllowedHost(t *testing.T) {
+ requests := make(chan string, 1)
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ requests <- r.Host
+ w.WriteHeader(http.StatusOK)
+ }))
+ defer server.Close()
+
+ serverURL, err := url.Parse(server.URL)
+ if err != nil {
+ t.Fatalf("parse server URL: %v", err)
+ }
+ targetURL := fmt.Sprintf("http://channel-gateway.test:%s/ping", serverURL.Port())
+ allowChannelURLForTest(t, targetURL)
+
+ lookupHost := common.LookupHost
+ common.LookupHost = func(host string) ([]string, error) {
+ if host == "channel-gateway.test" {
+ return []string{"127.0.0.1"}, nil
+ }
+ return lookupHost(host)
+ }
+ t.Cleanup(func() { common.LookupHost = lookupHost })
+
+ client := newSafeChannelHTTPClient(time.Second, channelURLPolicy{})
+ resp, err := client.Get(targetURL)
+ if err != nil {
+ t.Fatalf("Get() error = %v", err)
+ }
+ _ = resp.Body.Close()
+ select {
+ case host := <-requests:
+ if host != "channel-gateway.test:"+serverURL.Port() {
+ t.Fatalf("request Host = %q", host)
+ }
+ case <-time.After(time.Second):
+ t.Fatal("server did not receive the pinned request")
+ }
+}
+
+func TestSafeChannelHTTPClientDoesNotFollowRedirects(t *testing.T) {
+ var redirected atomic.Bool
+ destination := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
+ redirected.Store(true)
+ }))
+ defer destination.Close()
+
+ source := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ http.Redirect(w, r, destination.URL, http.StatusFound)
+ }))
+ defer source.Close()
+ allowChannelURLForTest(t, source.URL)
+
+ client := newSafeChannelHTTPClient(time.Second, channelURLPolicy{})
+ resp, err := client.Get(source.URL)
+ if err != nil {
+ t.Fatalf("Get() error = %v", err)
+ }
+ _ = resp.Body.Close()
+ if resp.StatusCode != http.StatusFound {
+ t.Fatalf("response status = %d, want %d", resp.StatusCode, http.StatusFound)
+ }
+ if redirected.Load() {
+ t.Fatal("client followed a redirect")
+ }
+}
+
+func TestSafeChannelWebSocketRejectsUntrustedLocalHost(t *testing.T) {
+ t.Setenv(channelAllowedHostsEnv, "")
+ _, _, err := dialSafeChannelWebSocket(
+ context.Background(),
+ "ws://127.0.0.1/whatsapp/test/events/ws",
+ nil,
+ time.Second,
+ channelURLPolicy{},
+ )
+ if err == nil || !strings.Contains(err.Error(), "is not trusted") {
+ t.Fatalf("dialSafeChannelWebSocket() error = %v, want an untrusted-host rejection", err)
+ }
+}
+
+func TestChannelSDKWebSocketDialerOnlyAllowsTLSPlatformEndpoints(t *testing.T) {
+ dialer := newChannelSDKWebSocketDialer(&websocket.Dialer{})
+ for _, test := range []struct {
+ url string
+ wantFail bool
+ }{
+ {url: "wss://msg-frontier.feishu.cn/ws/v2"},
+ {url: "wss://msg-frontier.larksuite.com/ws/v2"},
+ {url: "wss://wss-open-connection.dingtalk.com/connect"},
+ {url: "ws://msg-frontier.feishu.cn:443/ws/v2", wantFail: true},
+ {url: "wss://attacker.example/connect", wantFail: true},
+ {url: "wss://wss-open-connection.dingtalk.com:8443/connect", wantFail: true},
+ } {
+ t.Run(test.url, func(t *testing.T) {
+ parsed, err := url.Parse(test.url)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if parsed.Scheme == "wss" {
+ parsed.Scheme = "https"
+ } else if parsed.Scheme == "ws" {
+ parsed.Scheme = "http"
+ }
+ _, err = dialer.Proxy(&http.Request{URL: parsed})
+ if (err != nil) != test.wantFail {
+ t.Fatalf("Proxy() error = %v, wantFail %t", err, test.wantFail)
+ }
+ })
+ }
+}
+
+func TestChannelSDKWebSocketDialerRejectsNonPublicResolution(t *testing.T) {
+ originalLookup := common.LookupHost
+ t.Cleanup(func() { common.LookupHost = originalLookup })
+ dialer := newChannelSDKWebSocketDialer(&websocket.Dialer{})
+
+ for _, address := range []string{"169.254.169.254", "::ffff:169.254.169.254", "fe80::1"} {
+ t.Run(address, func(t *testing.T) {
+ common.LookupHost = func(host string) ([]string, error) {
+ if host != "msg-frontier.feishu.cn" {
+ t.Fatalf("unexpected DNS lookup host %q", host)
+ }
+ return []string{address}, nil
+ }
+ _, err := dialer.NetDialContext(context.Background(), "tcp", "msg-frontier.feishu.cn:443")
+ if err == nil || !strings.Contains(err.Error(), "not safe") {
+ t.Fatalf("NetDialContext() error = %v, want unsafe-address rejection", err)
+ }
+ })
+ }
+}
+
+func TestLookupChannelHostRejectsLinkLocalAddresses(t *testing.T) {
+ originalLookup := common.LookupHost
+ t.Cleanup(func() { common.LookupHost = originalLookup })
+
+ for _, address := range []string{"169.254.169.254", "::ffff:169.254.169.254", "fe80::1"} {
+ t.Run(address, func(t *testing.T) {
+ common.LookupHost = func(string) ([]string, error) { return []string{address}, nil }
+ if _, _, err := lookupChannelHost("channel-gateway.test"); err == nil || !strings.Contains(err.Error(), "link-local") {
+ t.Fatalf("lookupChannelHost() error = %v, want link-local rejection", err)
+ }
+ })
+ }
+}
+
+func TestOperatorAllowedChannelRejectsLinkLocalForAllSchemes(t *testing.T) {
+ t.Setenv(channelAllowedHostsEnv, "channel-gateway.test")
+ originalLookup := common.LookupHost
+ t.Cleanup(func() { common.LookupHost = originalLookup })
+ common.LookupHost = func(string) ([]string, error) { return []string{"169.254.169.254"}, nil }
+
+ for _, scheme := range []string{"http", "https", "ws", "wss"} {
+ t.Run(scheme, func(t *testing.T) {
+ parsed, err := url.Parse(scheme + "://channel-gateway.test/path")
+ if err != nil {
+ t.Fatal(err)
+ }
+ _, _, _, err = resolveChannelDestination(parsed, channelURLPolicy{}, scheme == "ws" || scheme == "wss")
+ if err == nil || !strings.Contains(err.Error(), "link-local") {
+ t.Fatalf("resolveChannelDestination() error = %v, want link-local rejection", err)
+ }
+ })
+ }
+}
+
+func TestWhatsAppDefaultGatewayExceptionIsPathScoped(t *testing.T) {
+ t.Setenv(channelAllowedHostsEnv, "")
+ policy := channelURLPolicy{
+ localBaseURL: defaultGatewayBaseURL,
+ localPathPrefix: "/whatsapp/",
+ }
+ allowed, err := url.Parse("ws://127.0.0.1:3005/whatsapp/test/events/ws")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, _, _, err := resolveChannelDestination(allowed, policy, true); err != nil {
+ t.Fatalf("default WhatsApp gateway rejected: %v", err)
+ }
+
+ for _, path := range []string{"/admin", "/whatsapp/../admin", "/whatsapp/%2e%2e/admin", "/whatsapp/%2fadmin"} {
+ disallowed, err := url.Parse("ws://127.0.0.1:3005" + path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, _, _, err := resolveChannelDestination(disallowed, policy, true); err == nil {
+ t.Errorf("default WhatsApp exception accepted unsafe path %q", path)
+ }
+ }
+}
+
+func TestWhatsAppExplicitDefaultGatewayIsAllowed(t *testing.T) {
+ channel := newWhatsAppChannel(whatsappAccount{GatewayBaseURL: defaultGatewayBaseURL})
+ if !channel.allowDefaultGateway {
+ t.Fatal("explicit default gateway was not recognized as the built-in local gateway")
+ }
+}
+
+func TestSafeChannelWebSocketPinsOperatorAllowedHost(t *testing.T) {
+ upgrader := websocket.Upgrader{}
+ connected := make(chan struct{}, 1)
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ conn, err := upgrader.Upgrade(w, r, nil)
+ if err != nil {
+ t.Errorf("upgrade websocket: %v", err)
+ return
+ }
+ connected <- struct{}{}
+ _ = conn.Close()
+ }))
+ defer server.Close()
+
+ wsURL := "ws" + strings.TrimPrefix(server.URL, "http")
+ allowChannelURLForTest(t, wsURL)
+ conn, _, err := dialSafeChannelWebSocket(context.Background(), wsURL, nil, time.Second, channelURLPolicy{})
+ if err != nil {
+ t.Fatalf("dialSafeChannelWebSocket() error = %v", err)
+ }
+ _ = conn.Close()
+ select {
+ case <-connected:
+ case <-time.After(time.Second):
+ t.Fatal("WebSocket server did not receive the pinned connection")
+ }
+}
diff --git a/internal/channels/feishu.go b/internal/channels/feishu.go
index fe8693aea8..c521fa6d64 100644
--- a/internal/channels/feishu.go
+++ b/internal/channels/feishu.go
@@ -26,6 +26,7 @@ import (
"sync"
"time"
+ "github.com/gorilla/websocket"
lark "github.com/larksuite/oapi-sdk-go/v3"
larkcore "github.com/larksuite/oapi-sdk-go/v3/core"
"github.com/larksuite/oapi-sdk-go/v3/event/dispatcher"
@@ -84,17 +85,24 @@ func newFeishuChannel(account feishuAccount) *feishuChannel {
seen: map[string]time.Time{},
workers: map[string]*feishuWorker{},
}
+ transportPolicy := channelURLPolicy{exactHosts: []string{"open.feishu.cn", "open.larksuite.com"}}
+ safeClient := newSafeChannelHTTPClient(account.Timeout, transportPolicy)
ch.rest = lark.NewClient(
account.AppID,
account.AppSecret,
lark.WithOpenBaseUrl(feishuBaseURL(account.Domain)),
+ lark.WithHttpClient(safeClient),
lark.WithLogLevel(larkcore.LogLevelInfo),
lark.WithReqTimeout(account.Timeout),
)
+ wsDialer := *websocket.DefaultDialer
+ wsDialer.HandshakeTimeout = account.Timeout
ch.wsClient = larkws.NewClient(
account.AppID,
account.AppSecret,
larkws.WithDomain(feishuBaseURL(account.Domain)),
+ larkws.WithHttpClient(safeClient),
+ larkws.WithWebSocketDialer(newChannelSDKWebSocketDialer(&wsDialer)),
larkws.WithEventHandler(ch.eventDispatcher()),
larkws.WithLogLevel(larkcore.LogLevelInfo),
)
diff --git a/internal/channels/line.go b/internal/channels/line.go
index c89e04a2e4..87e45203a2 100644
--- a/internal/channels/line.go
+++ b/internal/channels/line.go
@@ -112,7 +112,7 @@ func newLineChannel(account lineAccount) *lineChannel {
return &lineChannel{
account: account,
ctx: context.Background(),
- client: &http.Client{Timeout: account.Timeout},
+ client: newSafeChannelHTTPClient(account.Timeout, channelURLPolicy{exactHosts: []string{"api.line.me"}}),
replyTokens: map[string]lineReplyToken{},
seen: map[string]time.Time{},
workers: map[string]*lineWorker{},
diff --git a/internal/channels/qqbot.go b/internal/channels/qqbot.go
index 1196142563..e0c60edfb8 100644
--- a/internal/channels/qqbot.go
+++ b/internal/channels/qqbot.go
@@ -58,7 +58,6 @@ type qqBotAccount struct {
type qqBotChannel struct {
account qqBotAccount
client *http.Client
- dialer *websocket.Dialer
mu sync.Mutex
writeMu sync.Mutex
@@ -89,8 +88,7 @@ func newQQBotChannel(account qqBotAccount) *qqBotChannel {
}
return &qqBotChannel{
account: account,
- client: &http.Client{Timeout: qqBotRequestTimeout},
- dialer: websocket.DefaultDialer,
+ client: newSafeChannelHTTPClient(qqBotRequestTimeout, channelURLPolicy{exactHosts: []string{"api.sgroup.qq.com", "bots.qq.com"}}),
}
}
@@ -220,7 +218,9 @@ func (c *qqBotChannel) run(ctx context.Context) {
}
func (c *qqBotChannel) runGatewaySession(ctx context.Context, gatewayURL string, token string) error {
- conn, _, err := c.dialer.DialContext(ctx, gatewayURL, nil)
+ conn, _, err := dialSafeChannelWebSocket(ctx, gatewayURL, nil, qqBotRequestTimeout, channelURLPolicy{
+ hostSuffixes: []string{"qq.com"},
+ })
if err != nil {
return err
}
diff --git a/internal/channels/qqbot_test.go b/internal/channels/qqbot_test.go
index d1e5e42be5..a403a8657b 100644
--- a/internal/channels/qqbot_test.go
+++ b/internal/channels/qqbot_test.go
@@ -126,6 +126,7 @@ func TestQQBotSend(t *testing.T) {
_ = json.NewEncoder(w).Encode(map[string]any{})
}))
defer server.Close()
+ allowChannelURLForTest(t, server.URL)
channel := newQQBotChannel(qqBotAccount{
AccountID: "account-1",
@@ -263,6 +264,7 @@ func TestQQBotGatewayIdentifiesAndDispatches(t *testing.T) {
return nil
})
gatewayURL := "ws" + strings.TrimPrefix(server.URL, "http")
+ allowChannelURLForTest(t, gatewayURL)
if err := channel.runGatewaySession(context.Background(), gatewayURL, "token-1"); err == nil || !strings.Contains(err.Error(), "reconnect") {
t.Fatalf("runGatewaySession() error = %v, want reconnect request", err)
}
@@ -321,6 +323,7 @@ func TestQQBotGatewayResumesExistingSession(t *testing.T) {
channel.sessionID = "session-1"
channel.setSequence(42)
gatewayURL := "ws" + strings.TrimPrefix(server.URL, "http")
+ allowChannelURLForTest(t, gatewayURL)
if err := channel.runGatewaySession(context.Background(), gatewayURL, "token-1"); err == nil {
t.Fatal("runGatewaySession() succeeded, want reconnect request")
}
diff --git a/internal/channels/telegram.go b/internal/channels/telegram.go
index 233c2afd92..c923ff15e2 100644
--- a/internal/channels/telegram.go
+++ b/internal/channels/telegram.go
@@ -114,7 +114,7 @@ func newTelegramChannel(account telegramAccount) *telegramChannel {
}
return &telegramChannel{
account: account,
- client: &http.Client{Timeout: telegramHTTPTimeout},
+ client: newSafeChannelHTTPClient(telegramHTTPTimeout, channelURLPolicy{exactHosts: []string{"api.telegram.org"}}),
}
}
diff --git a/internal/channels/telegram_test.go b/internal/channels/telegram_test.go
index f1b04ea18d..f1f67bcea8 100644
--- a/internal/channels/telegram_test.go
+++ b/internal/channels/telegram_test.go
@@ -170,6 +170,7 @@ func TestTelegramSendUsesReplyParameters(t *testing.T) {
_, _ = w.Write([]byte(`{"ok":true,"result":{"message_id":1}}`))
}))
defer server.Close()
+ allowChannelURLForTest(t, server.URL)
ch := newTelegramChannel(telegramAccount{
AccountID: "account-1",
@@ -222,6 +223,7 @@ func TestTelegramStartAndStopPollsUpdates(t *testing.T) {
}
}))
defer server.Close()
+ allowChannelURLForTest(t, server.URL)
ch := newTelegramChannel(telegramAccount{
AccountID: "account-1",
diff --git a/internal/channels/wecom.go b/internal/channels/wecom.go
index d241254b5b..a3a5e347af 100644
--- a/internal/channels/wecom.go
+++ b/internal/channels/wecom.go
@@ -126,7 +126,7 @@ func newWeComChannel(account wecomAccount) (*wecomChannel, error) {
channel := &wecomChannel{
account: account,
- client: &http.Client{Timeout: weComHTTPTimeout},
+ client: newSafeChannelHTTPClient(weComHTTPTimeout, channelURLPolicy{exactHosts: []string{"qyapi.weixin.qq.com"}}),
}
if account.ConnectionType == "webhook" {
crypto, err := newWeComCrypto(account.Token, account.AESKey, account.CorpID)
@@ -348,9 +348,9 @@ func (c *wecomChannel) runWebSocket(ctx context.Context) {
}
func (c *wecomChannel) runWebSocketOnce(ctx context.Context) error {
- dialer := *websocket.DefaultDialer
- dialer.HandshakeTimeout = weComWSHandshakeTimeout
- conn, _, err := dialer.DialContext(ctx, c.account.WSURL, nil)
+ conn, _, err := dialSafeChannelWebSocket(ctx, c.account.WSURL, nil, weComWSHandshakeTimeout, channelURLPolicy{
+ exactHosts: []string{"openws.work.weixin.qq.com"},
+ })
if err != nil {
return err
}
diff --git a/internal/channels/wecom_test.go b/internal/channels/wecom_test.go
index ddacb79305..a3f3aa569d 100644
--- a/internal/channels/wecom_test.go
+++ b/internal/channels/wecom_test.go
@@ -326,6 +326,7 @@ func TestWeComApplicationSendCachesAccessToken(t *testing.T) {
}
}))
defer server.Close()
+ allowChannelURLForTest(t, server.URL)
encodingAESKey, _ := testWeComAESKey()
channel, err := newWeComChannel(wecomAccount{
@@ -401,6 +402,7 @@ func TestWeComWebSocketSubscribesHandlesAndSends(t *testing.T) {
defer server.Close()
wsURL := "ws" + strings.TrimPrefix(server.URL, "http")
+ allowChannelURLForTest(t, wsURL)
channel, err := newWeComChannel(wecomAccount{
AccountID: "account-1",
ConnectionType: "websocket",
diff --git a/internal/channels/whatsapp.go b/internal/channels/whatsapp.go
index c501893317..3b421ac795 100644
--- a/internal/channels/whatsapp.go
+++ b/internal/channels/whatsapp.go
@@ -75,20 +75,21 @@ type whatsappRuntimeSnapshot struct {
type whatsappChannel struct {
account whatsappAccount
- mu sync.Mutex
- cancel context.CancelFunc
- handler core.MessageHandler
- client *http.Client
- status string
- lastErr string
- qrDataURL string
- qrUpdatedAt float64
- connectedAt float64
- lastSnapshotAt float64
- sessionID string
- eventCursor int64
- seen map[string]time.Time
- workers map[string]*whatsappChatWorker
+ mu sync.Mutex
+ cancel context.CancelFunc
+ handler core.MessageHandler
+ client *http.Client
+ allowDefaultGateway bool
+ status string
+ lastErr string
+ qrDataURL string
+ qrUpdatedAt float64
+ connectedAt float64
+ lastSnapshotAt float64
+ sessionID string
+ eventCursor int64
+ seen map[string]time.Time
+ workers map[string]*whatsappChatWorker
}
type whatsappChatWorker struct {
@@ -99,6 +100,8 @@ var liveWhatsAppChannels sync.Map
// newWhatsAppChannel creates a WhatsApp channel instance with default runtime settings filled in.
func newWhatsAppChannel(account whatsappAccount) *whatsappChannel {
+ account.GatewayBaseURL = strings.TrimSpace(account.GatewayBaseURL)
+ allowDefaultGateway := account.GatewayBaseURL == "" || strings.TrimRight(account.GatewayBaseURL, "/") == defaultGatewayBaseURL
if account.GatewayBaseURL == "" {
account.GatewayBaseURL = defaultGatewayBaseURL
}
@@ -108,12 +111,18 @@ func newWhatsAppChannel(account whatsappAccount) *whatsappChannel {
if account.Timeout <= 0 {
account.Timeout = defaultTimeout
}
+ policy := channelURLPolicy{}
+ if allowDefaultGateway {
+ policy.localBaseURL = defaultGatewayBaseURL
+ policy.localPathPrefix = "/whatsapp/"
+ }
return &whatsappChannel{
- account: account,
- client: &http.Client{Timeout: account.Timeout},
- status: "stopped",
- seen: map[string]time.Time{},
- workers: map[string]*whatsappChatWorker{},
+ account: account,
+ client: newSafeChannelHTTPClient(account.Timeout, policy),
+ allowDefaultGateway: allowDefaultGateway,
+ status: "stopped",
+ seen: map[string]time.Time{},
+ workers: map[string]*whatsappChatWorker{},
}
}
@@ -302,9 +311,12 @@ func (c *whatsappChannel) runEvents(ctx context.Context) error {
if auth := c.authHeader(); auth != "" {
header.Set("Authorization", auth)
}
- dialer := *websocket.DefaultDialer
- dialer.HandshakeTimeout = wsHandshakeTimeout
- conn, _, err := dialer.DialContext(ctx, wsURL, header)
+ policy := channelURLPolicy{}
+ if c.allowDefaultGateway {
+ policy.localBaseURL = defaultGatewayBaseURL
+ policy.localPathPrefix = "/whatsapp/"
+ }
+ conn, _, err := dialSafeChannelWebSocket(ctx, wsURL, header, wsHandshakeTimeout, policy)
if err != nil {
return err
}
diff --git a/internal/channels/whatsapp_gateway.go b/internal/channels/whatsapp_gateway.go
index 1466291173..5710f3a915 100644
--- a/internal/channels/whatsapp_gateway.go
+++ b/internal/channels/whatsapp_gateway.go
@@ -20,7 +20,9 @@ import (
"context"
"errors"
"fmt"
+ "net"
"net/http"
+ "net/url"
"os"
"os/exec"
"path/filepath"
@@ -212,11 +214,28 @@ func gatewayReachable(ctx context.Context) bool {
probeCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()
- req, err := http.NewRequestWithContext(probeCtx, http.MethodGet, gatewayHealthURL(), nil)
+ healthURL := gatewayHealthURL()
+ if healthURL == "" {
+ return false
+ }
+ parsedURL, err := url.Parse(healthURL)
+ if err != nil {
+ return false
+ }
+ baseURL := *parsedURL
+ baseURL.Path = ""
+ baseURL.RawPath = ""
+ baseURL.RawQuery = ""
+ baseURL.Fragment = ""
+ client := newSafeChannelHTTPClient(2*time.Second, channelURLPolicy{
+ localBaseURL: baseURL.String(),
+ localPathPrefix: "/health",
+ })
+ req, err := http.NewRequestWithContext(probeCtx, http.MethodGet, healthURL, nil)
if err != nil {
return false
}
- resp, err := http.DefaultClient.Do(req)
+ resp, err := client.Do(req)
if err != nil {
return false
}
@@ -248,9 +267,27 @@ func gatewayHealthURL() string {
if host == "" {
host = "127.0.0.1"
}
+ host = strings.Trim(host, "[]")
+ if ip := net.ParseIP(host); ip != nil {
+ if ip.IsUnspecified() {
+ if ip.To4() != nil {
+ host = "127.0.0.1"
+ } else {
+ host = "::1"
+ }
+ } else if !ip.IsLoopback() {
+ return ""
+ }
+ } else if !strings.EqualFold(host, "localhost") {
+ return ""
+ }
port := strings.TrimSpace(os.Getenv("WHATSAPP_GATEWAY_PORT"))
if port == "" {
port = "3005"
}
- return fmt.Sprintf("http://%s:%s/health", host, port)
+ portNumber, err := strconv.Atoi(port)
+ if err != nil || portNumber < 1 || portNumber > 65535 {
+ return ""
+ }
+ return "http://" + net.JoinHostPort(host, strconv.Itoa(portNumber)) + "/health"
}