From d7777f7c89aa53eddef50e4b9b7579a03adf75ba Mon Sep 17 00:00:00 2001 From: sausheong Date: Sat, 13 Jun 2026 11:20:12 +0800 Subject: [PATCH] =?UTF-8?q?fix(web):=20web=5Ffetch=20uses=20SafeHTTPClient?= =?UTF-8?q?=20=E2=80=94=20pins=20validated=20IP=20(S2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/web/webfetch.go | 14 +------------- tools/web/webfetch_test.go | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 13 deletions(-) create mode 100644 tools/web/webfetch_test.go diff --git a/tools/web/webfetch.go b/tools/web/webfetch.go index b5fbde1..71f70b6 100644 --- a/tools/web/webfetch.go +++ b/tools/web/webfetch.go @@ -85,19 +85,7 @@ func (t *WebFetchTool) Execute(ctx context.Context, input json.RawMessage) (tool req.Header.Set(k, v) } - client := &http.Client{ - Timeout: fetchTimeout, - CheckRedirect: func(req *http.Request, via []*http.Request) error { - if len(via) >= 10 { - return fmt.Errorf("too many redirects (max 10)") - } - // Re-validate each redirect destination against SSRF blocklist - if err := ValidateURLNotInternal(req.URL.String()); err != nil { - return fmt.Errorf("redirect blocked: %w", err) - } - return nil - }, - } + client := SafeHTTPClient(fetchTimeout) resp, err := client.Do(req) if err != nil { diff --git a/tools/web/webfetch_test.go b/tools/web/webfetch_test.go new file mode 100644 index 0000000..9eb7b91 --- /dev/null +++ b/tools/web/webfetch_test.go @@ -0,0 +1,16 @@ +package web + +import ( + "context" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestWebFetch_BlocksPrivateHost(t *testing.T) { + tool := &WebFetchTool{} + res, err := tool.Execute(context.Background(), + []byte(`{"url":"http://169.254.169.254/latest/meta-data/"}`)) + require.NoError(t, err) + require.NotEmpty(t, res.Error, "private metadata IP must be rejected") +}