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:
@@ -12,6 +12,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/datasource"
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
)
|
||||
|
||||
@@ -55,7 +56,7 @@ func NewClient(config *Config) *Client {
|
||||
baseURL: config.GetBaseURL(),
|
||||
appID: config.AppID,
|
||||
appSecret: config.AppSecret,
|
||||
httpClient: &http.Client{Timeout: 30 * time.Second},
|
||||
httpClient: datasource.NewConnectorHTTPClient(30 * time.Second),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/datasource"
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
@@ -499,6 +500,10 @@ func parseFeishuConfig(config *types.DataSourceConfig) (*Config, error) {
|
||||
return nil, fmt.Errorf("feishu app_id and app_secret are required")
|
||||
}
|
||||
|
||||
if err := datasource.ValidateConnectorBaseURL(feishuConfig.GetBaseURL()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &feishuConfig, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -6,14 +6,22 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
secutils "github.com/Tencent/WeKnora/internal/utils"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
os.Setenv("SSRF_WHITELIST", "127.0.0.1,localhost")
|
||||
secutils.ResetSSRFWhitelistForTest()
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
// ──────────────────────────────────────────────────────────────────────
|
||||
// Fake Feishu API server
|
||||
// ──────────────────────────────────────────────────────────────────────
|
||||
@@ -374,13 +382,13 @@ func TestParseFeishuConfig(t *testing.T) {
|
||||
Credentials: map[string]interface{}{
|
||||
"app_id": "id1",
|
||||
"app_secret": "sec1",
|
||||
"base_url": "https://custom.example.com",
|
||||
"base_url": "https://open.feishu.cn",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if cfg.AppID != "id1" || cfg.AppSecret != "sec1" || cfg.BaseURL != "https://custom.example.com" {
|
||||
if cfg.AppID != "id1" || cfg.AppSecret != "sec1" || cfg.BaseURL != "https://open.feishu.cn" {
|
||||
t.Errorf("unexpected config: %+v", cfg)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -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 ¬ionClient{
|
||||
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
|
||||
|
||||
@@ -36,7 +36,7 @@ func newClient(cfg *Config) *client {
|
||||
return &client{
|
||||
baseURL: cfg.GetBaseURL(),
|
||||
token: cfg.APIToken,
|
||||
httpClient: &http.Client{Timeout: defaultTimeout},
|
||||
httpClient: datasource.NewConnectorHTTPClient(defaultTimeout),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,11 +4,19 @@ import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
secutils "github.com/Tencent/WeKnora/internal/utils"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
os.Setenv("SSRF_WHITELIST", "127.0.0.1,localhost")
|
||||
secutils.ResetSSRFWhitelistForTest()
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
func makeDSConfig(f *fakeYuque, resourceIDs []string) *types.DataSourceConfig {
|
||||
return &types.DataSourceConfig{
|
||||
Type: types.ConnectorTypeYuque,
|
||||
|
||||
@@ -77,6 +77,9 @@ func parseYuqueConfig(config *types.DataSourceConfig) (*Config, error) {
|
||||
if strings.TrimSpace(cfg.APIToken) == "" {
|
||||
return nil, fmt.Errorf("%w: api_token is required", datasource.ErrInvalidCredentials)
|
||||
}
|
||||
if err := datasource.ValidateConnectorBaseURL(cfg.GetBaseURL()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package datasource
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/utils"
|
||||
)
|
||||
|
||||
// ValidateConnectorBaseURL checks a connector API base URL against the SSRF policy.
|
||||
// Empty rawURL is allowed; callers apply their own default before issuing requests.
|
||||
func ValidateConnectorBaseURL(rawURL string) error {
|
||||
url := strings.TrimSpace(rawURL)
|
||||
if url == "" {
|
||||
return nil
|
||||
}
|
||||
if !strings.Contains(url, "://") {
|
||||
url = "https://" + url
|
||||
}
|
||||
if err := utils.ValidateURLForSSRF(url); err != nil {
|
||||
return fmt.Errorf("base_url SSRF validation failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewConnectorHTTPClient returns an HTTP client with redirect and dial-time SSRF guards.
|
||||
func NewConnectorHTTPClient(timeout time.Duration) *http.Client {
|
||||
cfg := utils.DefaultSSRFSafeHTTPClientConfig()
|
||||
cfg.Timeout = timeout
|
||||
return utils.NewSSRFSafeHTTPClient(cfg)
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package datasource
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
secutils "github.com/Tencent/WeKnora/internal/utils"
|
||||
)
|
||||
|
||||
func TestValidateConnectorBaseURLBlocksLoopback(t *testing.T) {
|
||||
secutils.ResetSSRFWhitelistForTest()
|
||||
t.Cleanup(secutils.ResetSSRFWhitelistForTest)
|
||||
|
||||
err := ValidateConnectorBaseURL("http://127.0.0.1:8000")
|
||||
if err == nil {
|
||||
t.Fatal("expected loopback base_url to be rejected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateConnectorBaseURLAllowsPublicHTTPS(t *testing.T) {
|
||||
secutils.ResetSSRFWhitelistForTest()
|
||||
t.Cleanup(secutils.ResetSSRFWhitelistForTest)
|
||||
|
||||
if err := ValidateConnectorBaseURL("https://open.feishu.cn"); err != nil {
|
||||
t.Fatalf("expected public base_url to pass: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user