Files
harness/tools/web/client_test.go
sausheong 60e6077408 fix(browser,stagehand): contain Chrome egress with an SSRF-validating proxy
Chrome's --host-resolver-rules only glob-matches host strings, so the CIDR
entries (127.0.0.0/8, 10.0.0.0/8, 172.16.0.0/12, 169.254.0.0/16,
192.168.0.0/16) never matched: pages could fetch private IP literals and
names resolving to private IPs, and the live SSRF test hung dialing
169.254.169.254 until its deadline.

Replace the rules with web.EgressProxy, a loopback HTTP proxy (CONNECT and
absolute-URI HTTP) that resolves each destination, refuses it if any
address is private, and dials the validated IP directly. Chrome is pointed
at it with --proxy-server and <-loopback>, and WebRTC is limited to proxied
UDP. Stagehand's local mode allows only Chrome's own debugging port through,
which its extension needs.

SafeHTTPClient and the proxy now share one safe dialer, which races the
validated addresses happy-eyeballs style so dual-stack hosts with broken
IPv6 don't stall. isPrivateIP also covers multicast, reserved, benchmark,
Teredo and NAT64/6to4/IPv4-compatible addresses embedding a private IPv4.
2026-10-03 23:15:50 +08:00

75 lines
2.4 KiB
Go

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)
}