fix(web): all web_search backends use SafeHTTPClient (N4)
This commit is contained in:
1 parent
d7777f7c89
commit
7d85074509
3 files changed
+42
-4
No files matched your search
@@ -14,6 +14,11 @@ import (
|
||||
|
||||
const searchTimeout = 15 * time.Second
|
||||
|
||||
// searchClient is the shared SSRF-safe HTTP client for all web_search
|
||||
// backends. It pins the validated IP and re-validates redirects, bringing
|
||||
// web_search to parity with web_fetch. (N4)
|
||||
var searchClient = SafeHTTPClient(searchTimeout)
|
||||
|
||||
// WebSearchTool searches the web and returns results via a configurable
|
||||
// backend. The default backend (when Backend is nil) scrapes DuckDuckGo's
|
||||
// HTML interface — no API key required, but fragile to upstream HTML
|
||||
@@ -131,7 +136,7 @@ func duckDuckGoSearch(ctx context.Context, query string, maxResults int) ([]sear
|
||||
}
|
||||
req.Header.Set("User-Agent", "harness/1.0 (Go agent framework)")
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
resp, err := searchClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -17,6 +17,10 @@ import (
|
||||
// are constructed with their config (API key, base URL, etc.) at
|
||||
// registration time and remain immutable thereafter. This keeps the tool
|
||||
// dispatch path zero-allocation in the hot loop.
|
||||
//
|
||||
// Implementations that make outbound HTTP calls MUST use the package
|
||||
// searchClient (SafeHTTPClient) so a configured/typo'd internal base URL
|
||||
// cannot be used for SSRF. (N4)
|
||||
type WebSearchBackend interface {
|
||||
Name() string
|
||||
Search(ctx context.Context, query string, maxResults int) ([]searchResult, error)
|
||||
@@ -58,7 +62,7 @@ func (b *braveBackend) Search(ctx context.Context, q string, n int) ([]searchRes
|
||||
}
|
||||
req.Header.Set("Accept", "application/json")
|
||||
req.Header.Set("X-Subscription-Token", b.apiKey)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
resp, err := searchClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -119,7 +123,7 @@ func (b *tavilyBackend) Search(ctx context.Context, q string, n int) ([]searchRe
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
resp, err := searchClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -169,7 +173,7 @@ func (b *searxngBackend) Search(ctx context.Context, q string, n int) ([]searchR
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
resp, err := searchClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func testCtx(t *testing.T) context.Context {
|
||||
t.Helper()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
t.Cleanup(cancel)
|
||||
return ctx
|
||||
}
|
||||
|
||||
func TestSearchClient_IsSafe(t *testing.T) {
|
||||
require.NotNil(t, searchClient)
|
||||
require.NotNil(t, searchClient.Transport)
|
||||
require.NotNil(t, searchClient.CheckRedirect)
|
||||
require.Equal(t, searchTimeout, searchClient.Timeout)
|
||||
}
|
||||
|
||||
func TestSearxng_BlocksPrivateBaseURL(t *testing.T) {
|
||||
b := newSearxngBackend("http://127.0.0.1:8080")
|
||||
_, err := b.Search(testCtx(t), "hello", 3)
|
||||
require.Error(t, err)
|
||||
}
|
||||
Reference in new issue
Block a user