From 80ba2fca9a9095efb1f998c0694b9f08a714f511 Mon Sep 17 00:00:00 2001 From: li Date: Sat, 28 Nov 2026 14:41:57 +0800 Subject: [PATCH] fix(websearch): authenticate Tavily with a Bearer header Tavily's /search endpoint documents a single credential scheme, `Authorization: Bearer `, and its official Python/JS SDKs send only that header. The provider still put the key in an `api_key` body field and sent no Authorization header, so web search emulation through Tavily relied on an undocumented contract. Brave and Exa in the same package already send their keys as headers. Send the key as a Bearer token and drop it from the request body. The old unit test only marshalled the request struct and asserted the body `api_key`, so it locked in the stale contract without exercising Search(); replace it with a test that drives Search() through a recording transport and checks the outgoing headers and body. Refs #7201 --- backend/internal/pkg/websearch/tavily.go | 4 +- backend/internal/pkg/websearch/tavily_test.go | 51 +++++++++++++------ 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/backend/internal/pkg/websearch/tavily.go b/backend/internal/pkg/websearch/tavily.go index ac4928a6f8..1adf96abb9 100644 --- a/backend/internal/pkg/websearch/tavily.go +++ b/backend/internal/pkg/websearch/tavily.go @@ -39,7 +39,6 @@ func (t *TavilyProvider) Search(ctx context.Context, req SearchRequest) (*Search } payload := tavilyRequest{ - APIKey: t.apiKey, Query: req.Query, MaxResults: maxResults, SearchDepth: tavilySearchDepthBasic, @@ -55,6 +54,8 @@ func (t *TavilyProvider) Search(ctx context.Context, req SearchRequest) (*Search return nil, fmt.Errorf("tavily: build request: %w", err) } httpReq.Header.Set("Content-Type", "application/json") + // Tavily authenticates with a Bearer token; the documented request body has no api_key field. + httpReq.Header.Set("Authorization", "Bearer "+t.apiKey) resp, err := t.httpClient.Do(httpReq) if err != nil { @@ -89,7 +90,6 @@ func (t *TavilyProvider) Search(ctx context.Context, req SearchRequest) (*Search } type tavilyRequest struct { - APIKey string `json:"api_key"` Query string `json:"query"` MaxResults int `json:"max_results"` SearchDepth string `json:"search_depth"` diff --git a/backend/internal/pkg/websearch/tavily_test.go b/backend/internal/pkg/websearch/tavily_test.go index e1b6819a6b..99406d166b 100644 --- a/backend/internal/pkg/websearch/tavily_test.go +++ b/backend/internal/pkg/websearch/tavily_test.go @@ -1,7 +1,9 @@ package websearch import ( + "context" "encoding/json" + "net/http" "testing" "github.com/stretchr/testify/require" @@ -12,23 +14,40 @@ func TestTavilyProvider_Name(t *testing.T) { require.Equal(t, "tavily", p.Name()) } -func TestTavilyProvider_Search_RequestConstruction(t *testing.T) { - // Verify tavilyRequest struct fields map correctly - req := tavilyRequest{ - APIKey: "test-key", - Query: "golang", - MaxResults: 3, - SearchDepth: tavilySearchDepthBasic, - } - data, err := json.Marshal(req) - require.NoError(t, err) +func TestTavilyProvider_Search_SendsBearerAuth(t *testing.T) { + var seen http.Request + var seenBody []byte + resp := `{"results":[{"url":"https://go.dev","title":"Go","content":"Go programming language","score":0.95}]}` + p := NewTavilyProvider("tvly-test-key", exaTestClient(http.StatusOK, resp, &seen, &seenBody)) - var parsed map[string]any - require.NoError(t, json.Unmarshal(data, &parsed)) - require.Equal(t, "test-key", parsed["api_key"]) - require.Equal(t, "golang", parsed["query"]) - require.Equal(t, float64(3), parsed["max_results"]) - require.Equal(t, "basic", parsed["search_depth"]) + out, err := p.Search(context.Background(), SearchRequest{Query: "golang", MaxResults: 3}) + + require.NoError(t, err) + require.Equal(t, http.MethodPost, seen.Method) + require.Equal(t, tavilySearchEndpoint, seen.URL.String()) + require.Equal(t, "Bearer tvly-test-key", seen.Header.Get("Authorization")) + require.Equal(t, "application/json", seen.Header.Get("Content-Type")) + var body map[string]any + require.NoError(t, json.Unmarshal(seenBody, &body)) + require.Equal(t, map[string]any{ + "query": "golang", + "max_results": float64(3), + "search_depth": "basic", + }, body, "the API key must travel in the Authorization header, not the request body") + + require.Equal(t, []SearchResult{ + {URL: "https://go.dev", Title: "Go", Snippet: "Go programming language"}, + }, out.Results) +} + +func TestTavilyProvider_Search_ReportsUpstreamStatus(t *testing.T) { + var seen http.Request + var seenBody []byte + p := NewTavilyProvider("bad-key", exaTestClient(http.StatusUnauthorized, `{"detail":{"error":"Unauthorized"}}`, &seen, &seenBody)) + + _, err := p.Search(context.Background(), SearchRequest{Query: "golang"}) + + require.ErrorContains(t, err, "tavily: status 401") } func TestTavilyProvider_Search_ResponseParsing(t *testing.T) {