From 3c5276a9acd4765838544c11f43f04406c9dffb0 Mon Sep 17 00:00:00 2001 From: afjcjsbx Date: Wed, 18 Mar 2026 20:20:27 +0100 Subject: [PATCH] fix lint --- pkg/tools/web.go | 15 ++++++++++++--- pkg/tools/web_test.go | 4 ++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/pkg/tools/web.go b/pkg/tools/web.go index 56da0bb3e..42cf79578 100644 --- a/pkg/tools/web.go +++ b/pkg/tools/web.go @@ -926,12 +926,16 @@ func (t *WebFetchTool) Execute(ctx context.Context, args map[string]any) *ToolRe return nil, nil, fmt.Errorf("request failed: %w", doErr) } resp.Body = http.MaxBytesReader(nil, resp.Body, t.fetchLimitBytes) - defer resp.Body.Close() + b, readErr := io.ReadAll(resp.Body) return resp, b, readErr } resp, body, err := doFetch(userAgent) + if resp != nil && resp.Body != nil { + defer resp.Body.Close() + } + if err != nil { var maxBytesErr *http.MaxBytesError if errors.As(err, &maxBytesErr) { @@ -943,11 +947,16 @@ func (t *WebFetchTool) Execute(ctx context.Context, args map[string]any) *ToolRe // Cloudflare (and similar WAFs) signal bot challenges with 403 + cf-mitigated: challenge. // Retry once with an honest User-Agent that identifies picoclaw, which some // operators explicitly allow-list for AI assistants. - if resp.StatusCode == http.StatusForbidden && resp.Header.Get("cf-mitigated") == "challenge" { + if resp.StatusCode == http.StatusForbidden && resp.Header.Get("Cf-Mitigated") == "challenge" { logger.DebugCF("tool", "Cloudflare challenge detected, retrying with honest User-Agent", map[string]any{"url": urlStr}) honestUA := fmt.Sprintf(userAgentHonest, config.Version) - if resp2, body2, err2 := doFetch(honestUA); err2 == nil { + resp2, body2, err2 := doFetch(honestUA) + if resp2 != nil && resp2.Body != nil { + defer resp2.Body.Close() + } + + if err2 == nil { resp, body = resp2, body2 } else { var maxBytesErr *http.MaxBytesError diff --git a/pkg/tools/web_test.go b/pkg/tools/web_test.go index 057efed74..98c763193 100644 --- a/pkg/tools/web_test.go +++ b/pkg/tools/web_test.go @@ -1084,7 +1084,7 @@ func TestWebFetchTool_CloudflareChallenge_RetryWithHonestUA(t *testing.T) { if requestCount == 1 { // First request: simulate Cloudflare challenge - w.Header().Set("cf-mitigated", "challenge") + w.Header().Set("Cf-Mitigated", "challenge") w.Header().Set("Content-Type", "text/html") w.WriteHeader(http.StatusForbidden) w.Write([]byte("Cloudflare challenge")) @@ -1158,7 +1158,7 @@ func TestWebFetchTool_CloudflareChallenge_RetryFailsToo(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Always return CF challenge regardless of UA - w.Header().Set("cf-mitigated", "challenge") + w.Header().Set("Cf-Mitigated", "challenge") w.Header().Set("Content-Type", "text/html") w.WriteHeader(http.StatusForbidden) w.Write([]byte("still blocked"))