Skip to content

Commit 773a94c

Browse files
authored
fix(web_search): validate missing API key/URL directly in Search methods (sipeed#2517)
1 parent bf6d4fd commit 773a94c

2 files changed

Lines changed: 37 additions & 8 deletions

File tree

pkg/tools/web.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ func (p *BraveSearchProvider) Search(
218218
count int,
219219
rangeCode string,
220220
) (string, error) {
221+
if p.keyPool == nil || len(p.keyPool.keys) == 0 {
222+
return "", errors.New("no API key provided")
223+
}
224+
221225
searchURL := fmt.Sprintf("https://api.search.brave.com/res/v1/web/search?q=%s&count=%d",
222226
url.QueryEscape(query), count)
223227
if freshness := mapBraveFreshness(rangeCode); freshness != "" {
@@ -317,6 +321,10 @@ func (p *TavilySearchProvider) Search(
317321
count int,
318322
rangeCode string,
319323
) (string, error) {
324+
if p.keyPool == nil || len(p.keyPool.keys) == 0 {
325+
return "", errors.New("no API key provided")
326+
}
327+
320328
searchURL := p.baseURL
321329
if searchURL == "" {
322330
searchURL = "https://api.tavily.com/search"
@@ -532,6 +540,10 @@ func (p *PerplexitySearchProvider) Search(
532540
count int,
533541
rangeCode string,
534542
) (string, error) {
543+
if p.keyPool == nil || len(p.keyPool.keys) == 0 {
544+
return "", errors.New("no API key provided")
545+
}
546+
535547
searchURL := "https://api.perplexity.ai/chat/completions"
536548

537549
var lastErr error
@@ -645,6 +657,10 @@ func (p *SearXNGSearchProvider) Search(
645657
count int,
646658
rangeCode string,
647659
) (string, error) {
660+
if p.baseURL == "" {
661+
return "", errors.New("no SearXNG URL provided")
662+
}
663+
648664
searchURL := fmt.Sprintf("%s/search?q=%s&format=json&categories=general",
649665
strings.TrimSuffix(p.baseURL, "/"),
650666
url.QueryEscape(query))
@@ -719,6 +735,10 @@ func (p *GLMSearchProvider) Search(
719735
count int,
720736
rangeCode string,
721737
) (string, error) {
738+
if p.apiKey == "" {
739+
return "", errors.New("no API key provided")
740+
}
741+
722742
searchURL := p.baseURL
723743
if searchURL == "" {
724744
searchURL = "https://open.bigmodel.cn/api/paas/v4/web_search"
@@ -808,6 +828,10 @@ func (p *BaiduSearchProvider) Search(
808828
count int,
809829
rangeCode string,
810830
) (string, error) {
831+
if p.apiKey == "" {
832+
return "", errors.New("no API key provided")
833+
}
834+
811835
searchURL := p.baseURL
812836
if searchURL == "" {
813837
searchURL = "https://qianfan.baidubce.com/v2/ai_search/web_search"
@@ -921,7 +945,7 @@ func NewWebSearchTool(opts WebSearchToolOptions) (*WebSearchTool, error) {
921945
var provider SearchProvider
922946
maxResults := 10
923947
// Priority: Perplexity > Brave > SearXNG > Tavily > DuckDuckGo > Baidu Search > GLM Search
924-
if opts.PerplexityEnabled && len(opts.PerplexityAPIKeys) > 0 {
948+
if opts.PerplexityEnabled {
925949
client, err := utils.CreateHTTPClient(opts.Proxy, perplexityTimeout)
926950
if err != nil {
927951
return nil, fmt.Errorf("failed to create HTTP client for Perplexity: %w", err)
@@ -934,7 +958,7 @@ func NewWebSearchTool(opts WebSearchToolOptions) (*WebSearchTool, error) {
934958
if opts.PerplexityMaxResults > 0 {
935959
maxResults = min(opts.PerplexityMaxResults, 10)
936960
}
937-
} else if opts.BraveEnabled && len(opts.BraveAPIKeys) > 0 {
961+
} else if opts.BraveEnabled {
938962
client, err := utils.CreateHTTPClient(opts.Proxy, searchTimeout)
939963
if err != nil {
940964
return nil, fmt.Errorf("failed to create HTTP client for Brave: %w", err)
@@ -943,12 +967,12 @@ func NewWebSearchTool(opts WebSearchToolOptions) (*WebSearchTool, error) {
943967
if opts.BraveMaxResults > 0 {
944968
maxResults = min(opts.BraveMaxResults, 10)
945969
}
946-
} else if opts.SearXNGEnabled && opts.SearXNGBaseURL != "" {
970+
} else if opts.SearXNGEnabled {
947971
provider = &SearXNGSearchProvider{baseURL: opts.SearXNGBaseURL}
948972
if opts.SearXNGMaxResults > 0 {
949973
maxResults = min(opts.SearXNGMaxResults, 10)
950974
}
951-
} else if opts.TavilyEnabled && len(opts.TavilyAPIKeys) > 0 {
975+
} else if opts.TavilyEnabled {
952976
client, err := utils.CreateHTTPClient(opts.Proxy, searchTimeout)
953977
if err != nil {
954978
return nil, fmt.Errorf("failed to create HTTP client for Tavily: %w", err)
@@ -971,7 +995,7 @@ func NewWebSearchTool(opts WebSearchToolOptions) (*WebSearchTool, error) {
971995
if opts.DuckDuckGoMaxResults > 0 {
972996
maxResults = min(opts.DuckDuckGoMaxResults, 10)
973997
}
974-
} else if opts.BaiduSearchEnabled && opts.BaiduSearchAPIKey != "" {
998+
} else if opts.BaiduSearchEnabled {
975999
client, err := utils.CreateHTTPClient(opts.Proxy, perplexityTimeout)
9761000
if err != nil {
9771001
return nil, fmt.Errorf("failed to create HTTP client for Baidu Search: %w", err)
@@ -985,7 +1009,7 @@ func NewWebSearchTool(opts WebSearchToolOptions) (*WebSearchTool, error) {
9851009
if opts.BaiduSearchMaxResults > 0 {
9861010
maxResults = min(opts.BaiduSearchMaxResults, 10)
9871011
}
988-
} else if opts.GLMSearchEnabled && opts.GLMSearchAPIKey != "" {
1012+
} else if opts.GLMSearchEnabled {
9891013
client, err := utils.CreateHTTPClient(opts.Proxy, searchTimeout)
9901014
if err != nil {
9911015
return nil, fmt.Errorf("failed to create HTTP client for GLM Search: %w", err)

pkg/tools/web_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,13 @@ func TestWebTool_WebSearch_NoApiKey(t *testing.T) {
391391
if err != nil {
392392
t.Fatalf("Unexpected error: %v", err)
393393
}
394-
if tool != nil {
395-
t.Errorf("Expected nil tool when Brave API key is empty")
394+
if tool == nil {
395+
t.Fatalf("Expected tool to be created")
396+
}
397+
ctx := context.Background()
398+
result := tool.Execute(ctx, map[string]any{"query": "test"})
399+
if !result.IsError {
400+
t.Errorf("Expected error when API key is missing")
396401
}
397402

398403
// Also nil when nothing is enabled

0 commit comments

Comments
 (0)