fix(security): harden web_fetch and datasource connector SSRF paths

Use SSRF-safe HTTP clients in chat pipeline web_fetch and Feishu/Yuque/Notion
connectors, validating tenant-supplied base_url values before outbound requests.
This commit is contained in:
wizardchen
2026-07-02 12:21:39 +08:00
committed by lyingbug
parent 1d322c525a
commit 8325dac738
13 changed files with 179 additions and 66 deletions
@@ -26,16 +26,19 @@ type notionClient struct {
}
// newClient creates a new Notion API client.
func newClient(token, baseURL string) *notionClient {
func newClient(token, baseURL string) (*notionClient, error) {
if baseURL == "" {
baseURL = DefaultBaseURL
}
if err := datasource.ValidateConnectorBaseURL(baseURL); err != nil {
return nil, err
}
return &notionClient{
token: token,
httpClient: &http.Client{Timeout: 30 * time.Second},
limiter: rate.NewLimiter(rate.Limit(3), 3), // 3 req/s with burst of 3 (matches Notion's actual behavior)
httpClient: datasource.NewConnectorHTTPClient(30 * time.Second),
limiter: rate.NewLimiter(rate.Limit(3), 3),
baseURL: baseURL,
}
}, nil
}
const maxRetries = 3
@@ -7,8 +7,22 @@ import (
"net/http/httptest"
"testing"
"time"
secutils "github.com/Tencent/WeKnora/internal/utils"
)
func mustTestClient(t *testing.T, token, baseURL string) *notionClient {
t.Helper()
t.Setenv("SSRF_WHITELIST", "127.0.0.1,localhost")
secutils.ResetSSRFWhitelistForTest()
client, err := newClient(token, baseURL)
if err != nil {
t.Fatalf("newClient: %v", err)
}
return client
}
// fakeNotion builds an httptest.Server that emulates relevant Notion API endpoints.
func fakeNotion() (*httptest.Server, *Config) {
mux := http.NewServeMux()
@@ -233,7 +247,7 @@ func TestClientPing(t *testing.T) {
ts, cfg := fakeNotion()
defer ts.Close()
client := newClient(cfg.APIKey, ts.URL)
client := mustTestClient(t, cfg.APIKey, ts.URL)
if err := client.Ping(context.Background()); err != nil {
t.Fatalf("Ping() error: %v", err)
}
@@ -243,7 +257,7 @@ func TestClientPing_InvalidToken(t *testing.T) {
ts, _ := fakeNotion()
defer ts.Close()
client := newClient("wrong-token", ts.URL)
client := mustTestClient(t, "wrong-token", ts.URL)
err := client.Ping(context.Background())
if err == nil {
t.Fatal("expected error for invalid token")
@@ -254,7 +268,7 @@ func TestClientSearchPages(t *testing.T) {
ts, cfg := fakeNotion()
defer ts.Close()
client := newClient(cfg.APIKey, ts.URL)
client := mustTestClient(t, cfg.APIKey, ts.URL)
pages, err := client.SearchPages(context.Background())
if err != nil {
t.Fatalf("SearchPages() error: %v", err)
@@ -277,7 +291,7 @@ func TestClientGetBlockChildrenAll(t *testing.T) {
ts, cfg := fakeNotion()
defer ts.Close()
client := newClient(cfg.APIKey, ts.URL)
client := mustTestClient(t, cfg.APIKey, ts.URL)
blocks, err := client.GetBlockChildrenAll(context.Background(), "page-1")
if err != nil {
t.Fatalf("GetBlockChildrenAll() error: %v", err)
@@ -301,7 +315,7 @@ func TestClientGetPage(t *testing.T) {
ts, cfg := fakeNotion()
defer ts.Close()
client := newClient(cfg.APIKey, ts.URL)
client := mustTestClient(t, cfg.APIKey, ts.URL)
page, err := client.GetPage(context.Background(), "page-1")
if err != nil {
t.Fatalf("GetPage() error: %v", err)
@@ -318,7 +332,7 @@ func TestClientQueryDatabaseAll(t *testing.T) {
ts, cfg := fakeNotion()
defer ts.Close()
client := newClient(cfg.APIKey, ts.URL)
client := mustTestClient(t, cfg.APIKey, ts.URL)
records, err := client.QueryDatabaseAll(context.Background(), "db-1")
if err != nil {
t.Fatalf("QueryDatabaseAll() error: %v", err)
@@ -40,7 +40,10 @@ func (c *Connector) Validate(ctx context.Context, config *types.DataSourceConfig
return err
}
client := newClient(notionCfg.APIKey, extractBaseURL(config))
client, err := newClient(notionCfg.APIKey, extractBaseURL(config))
if err != nil {
return err
}
return client.Ping(ctx)
}
@@ -71,7 +74,10 @@ func (c *Connector) ListResources(
return nil, err
}
client := newClient(notionCfg.APIKey, extractBaseURL(config))
client, err := newClient(notionCfg.APIKey, extractBaseURL(config))
if err != nil {
return nil, err
}
pages, err := client.SearchPages(ctx)
if err != nil {
return nil, fmt.Errorf("search notion pages: %w", err)
@@ -131,7 +137,10 @@ func (c *Connector) FetchAll(ctx context.Context, config *types.DataSourceConfig
return nil, err
}
client := newClient(notionCfg.APIKey, extractBaseURL(config))
client, err := newClient(notionCfg.APIKey, extractBaseURL(config))
if err != nil {
return nil, err
}
visited := c.excludedSetFromListResources(ctx, config, resourceIDs)
var allItems []types.FetchedItem
@@ -167,7 +176,10 @@ func (c *Connector) FetchIncremental(ctx context.Context, config *types.DataSour
return nil, nil, fmt.Errorf("no resource IDs configured")
}
client := newClient(notionCfg.APIKey, extractBaseURL(config))
client, err := newClient(notionCfg.APIKey, extractBaseURL(config))
if err != nil {
return nil, nil, err
}
// Parse previous cursor
var prevCursor notionCursor