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.
This commit is contained in:
sausheong committed 2026-10-03 23:15:50 +08:00
1 parent 49616eec19
commit 60e6077408
10 files changed
+871 -101

No files matched your search

+35
View File
@@ -21,6 +21,32 @@ var privateNetworks = []string{
"fc00::/7", // IPv6 unique local
"fe80::/10", // IPv6 link-local
"::/128", // IPv6 unspecified
"198.18.0.0/15", // benchmarking (RFC 2544)
"224.0.0.0/4", // IPv4 multicast
"240.0.0.0/4", // reserved, incl. 255.255.255.255 broadcast
"ff00::/8", // IPv6 multicast
"2001::/32", // Teredo (embeds an obfuscated IPv4)
"64:ff9b:1::/48", // local-use NAT64 (RFC 8215)
}
// embeddedV4Nets are IPv6 prefixes that carry an IPv4 address translators may
// route to: the embedded address is re-checked so e.g. 64:ff9b::7f00:1 (NAT64
// for 127.0.0.1) is blocked while a public embedded address stays allowed.
var embeddedV4Nets = []struct {
net *net.IPNet
offset int // byte offset of the embedded IPv4 address
}{
{mustCIDR("64:ff9b::/96"), 12}, // NAT64 well-known prefix
{mustCIDR("2002::/16"), 2}, // 6to4
{mustCIDR("::/96"), 12}, // IPv4-compatible (deprecated)
}
func mustCIDR(s string) *net.IPNet {
_, n, err := net.ParseCIDR(s)
if err != nil {
panic(err)
}
return n
}
var parsedPrivateNets []*net.IPNet
@@ -41,6 +67,15 @@ func isPrivateIP(ip net.IP) bool {
return true
}
}
if ip.To4() == nil {
if ip16 := ip.To16(); ip16 != nil {
for _, e := range embeddedV4Nets {
if e.net.Contains(ip16) {
return isPrivateIP(net.IP(ip16[e.offset : e.offset+4]))
}
}
}
}
return false
}