package web import ( "context" "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) } func TestSafeHTTPClient_PublicUnreachableReturnsDialError(t *testing.T) { // 192.0.2.1 is RFC5737 TEST-NET-1 (public, unrouteable). It is NOT in the // private blocklist, so validation passes and we should get a DIAL error // (timeout/unreachable), not a "blocked" validation error — proving the // path dials rather than rejecting. client := SafeHTTPClient(2 * time.Second) _, err := client.Get("http://192.0.2.1/") require.Error(t, err) require.NotContains(t, err.Error(), "internal address") } var _ = isPrivateIP(net.IPv4(127, 0, 0, 1)) var _ = strings.TrimSpace func TestDialValidated_FallsBackPastDeadAddress(t *testing.T) { ln, err := net.Listen("tcp4", "127.0.0.1:0") require.NoError(t, err) defer ln.Close() go func() { if c, err := ln.Accept(); err == nil { _ = c.Close() } }() _, port, _ := net.SplitHostPort(ln.Addr().String()) // First address is a black hole (TEST-NET-1); the second is live. ips := []net.IP{net.ParseIP("192.0.2.1"), net.ParseIP("127.0.0.1")} start := time.Now() c, err := dialValidated(context.Background(), &net.Dialer{Timeout: 30 * time.Second}, "tcp", ips, port) require.NoError(t, err) _ = c.Close() require.Less(t, time.Since(start), 5*time.Second) }