diff --git a/tools/web/client.go b/tools/web/client.go new file mode 100644 index 0000000..4e134a5 --- /dev/null +++ b/tools/web/client.go @@ -0,0 +1,56 @@ +package web + +import ( + "context" + "fmt" + "net" + "net/http" + "time" +) + +// SafeHTTPClient returns an *http.Client whose transport resolves and validates +// the destination host, then dials the exact validated IP — closing the +// TOCTOU/DNS-rebinding window where a stock http.Client would re-resolve the +// hostname independently of ValidateURLNotInternal. Redirects are re-validated. +func SafeHTTPClient(timeout time.Duration) *http.Client { + dialer := &net.Dialer{Timeout: timeout} + transport := &http.Transport{ + Proxy: http.ProxyFromEnvironment, + DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { + host, port, err := net.SplitHostPort(addr) + if err != nil { + return nil, err + } + ips, err := net.DefaultResolver.LookupIP(ctx, "ip", host) + if err != nil { + return nil, fmt.Errorf("cannot resolve %q — blocking to prevent SSRF", host) + } + var dialIP net.IP + for _, ip := range ips { + if isPrivateIP(ip) { + return nil, fmt.Errorf("access to internal address %s (%s) is blocked", host, ip) + } + if dialIP == nil { + dialIP = ip + } + } + if dialIP == nil { + return nil, fmt.Errorf("no usable address for %q", host) + } + return dialer.DialContext(ctx, network, net.JoinHostPort(dialIP.String(), port)) + }, + } + return &http.Client{ + Timeout: timeout, + Transport: transport, + CheckRedirect: func(req *http.Request, via []*http.Request) error { + if len(via) >= 10 { + return fmt.Errorf("too many redirects (max 10)") + } + if err := ValidateURLNotInternal(req.URL.String()); err != nil { + return fmt.Errorf("redirect blocked: %w", err) + } + return nil + }, + } +} diff --git a/tools/web/client_test.go b/tools/web/client_test.go new file mode 100644 index 0000000..ec8e263 --- /dev/null +++ b/tools/web/client_test.go @@ -0,0 +1,43 @@ +package web + +import ( + "net" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +func TestSafeHTTPClient_BlocksPrivateAtDial(t *testing.T) { + client := SafeHTTPClient(5 * time.Second) + // 127.0.0.1 is itself private, so dialing it directly must be refused. + _, err := client.Get("http://127.0.0.1:9/") + require.Error(t, err) + require.Contains(t, err.Error(), "internal address") +} + +func TestSafeHTTPClient_AllowsPublic(t *testing.T) { + client := SafeHTTPClient(5 * time.Second) + tr, ok := client.Transport.(*http.Transport) + require.True(t, ok) + require.NotNil(t, tr.DialContext) + require.NotNil(t, client.CheckRedirect) + require.Equal(t, 5*time.Second, client.Timeout) +} + +func TestSafeHTTPClient_RedirectToPrivateBlocked(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, "http://169.254.169.254/latest/meta-data/", http.StatusFound) + })) + defer srv.Close() + client := SafeHTTPClient(5 * time.Second) + // The httptest server binds loopback (private), so the FIRST dial is already blocked. + _, err := client.Get(srv.URL) + require.Error(t, err) +} + +var _ = isPrivateIP(net.IPv4(127, 0, 0, 1)) +var _ = strings.TrimSpace