Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

fix: block SSRF via IPv4 embedded in 6to4 and NAT64 IPv6 addresses

Luigi Agostia6bd5fafc52e
diff --git a/internal/netguard/netguard.go b/internal/netguard/netguard.go
index 2b0446e2..62bfb35a 100644
--- a/internal/netguard/netguard.go
+++ b/internal/netguard/netguard.go
@@ -24,6 +24,29 @@ var blockedPrefixes = []netip.Prefix{
netip.MustParsePrefix("198.18.0.0/15"),
}
+var nat64WellKnownPrefix = netip.MustParsePrefix("64:ff9b::/96")
+
+// embeddedIPv4 extracts the IPv4 address carried inside an IPv6 transition
+// address (6to4 2002::/16, NAT64 64:ff9b::/96). These wrappers are globally
+// scoped, so IsPrivate/IsLoopback/IsLinkLocalUnicast all report false while the
+// traffic still reaches the embedded IPv4 — 64:ff9b::a9fe:a9fe is the cloud
+// metadata service. The embedded address is decoded rather than the prefixes
+// being banned outright so transition addresses wrapping a public IPv4 keep
+// working.
+func embeddedIPv4(addr netip.Addr) (netip.Addr, bool) {
+ if !addr.Is6() {
+ return netip.Addr{}, false
+ }
+ b := addr.As16()
+ if b[0] == 0x20 && b[1] == 0x02 {
+ return netip.AddrFrom4([4]byte{b[2], b[3], b[4], b[5]}), true
+ }
+ if nat64WellKnownPrefix.Contains(addr) {
+ return netip.AddrFrom4([4]byte{b[12], b[13], b[14], b[15]}), true
+ }
+ return netip.Addr{}, false
+}
+
func NormalizeHost(host string) string {
return strings.TrimSuffix(strings.ToLower(strings.TrimSpace(host)), ".")
}
@@ -71,6 +94,9 @@ func ValidatePublicIP(ip net.IP) error {
return ErrPrivateInternalIP
}
}
+ if v4, ok := embeddedIPv4(addr); ok {
+ return ValidatePublicIP(net.IP(v4.AsSlice()))
+ }
return nil
}
diff --git a/internal/netguard/transition_test.go b/internal/netguard/transition_test.go
new file mode 100644
index 00000000..56be787e
--- /dev/null
+++ b/internal/netguard/transition_test.go
@@ -0,0 +1,51 @@
+package netguard
+
+import (
+ "errors"
+ "net"
+ "testing"
+)
+
+// IPv6 transition prefixes wrap an IPv4 address inside a globally-scoped IPv6
+// address, so IsPrivate/IsLoopback/IsLinkLocalUnicast all report false. The
+// embedded IPv4 is what actually gets reached, so that is what must be judged.
+func TestValidatePublicIPBlocksIPv4EmbeddedInTransitionPrefixes(t *testing.T) {
+ blocked := []struct {
+ addr string
+ why string
+ }{
+ {"64:ff9b::a9fe:a9fe", "NAT64 -> 169.254.169.254 cloud metadata"},
+ {"64:ff9b::7f00:1", "NAT64 -> 127.0.0.1"},
+ {"64:ff9b::a00:1", "NAT64 -> 10.0.0.1"},
+ {"64:ff9b::c0a8:1", "NAT64 -> 192.168.0.1"},
+ {"2002:a9fe:a9fe::", "6to4 -> 169.254.169.254 cloud metadata"},
+ {"2002:7f00:1::", "6to4 -> 127.0.0.1"},
+ {"2002:a00:1::", "6to4 -> 10.0.0.1"},
+ }
+ for _, tc := range blocked {
+ ip := net.ParseIP(tc.addr)
+ if ip == nil {
+ t.Fatalf("test address %q does not parse", tc.addr)
+ }
+ if err := ValidatePublicIP(ip); !errors.Is(err, ErrPrivateInternalIP) {
+ t.Errorf("ValidatePublicIP(%s) = %v, want ErrPrivateInternalIP (%s)", tc.addr, err, tc.why)
+ }
+ }
+}
+
+// The embedded address is decoded rather than the whole prefix being banned, so
+// transition addresses wrapping a genuinely public IPv4 stay reachable.
+func TestValidatePublicIPAllowsPublicIPv4InTransitionPrefixes(t *testing.T) {
+ for _, addr := range []string{
+ "64:ff9b::808:808", // NAT64 -> 8.8.8.8
+ "2002:808:808::", // 6to4 -> 8.8.8.8
+ } {
+ ip := net.ParseIP(addr)
+ if ip == nil {
+ t.Fatalf("test address %q does not parse", addr)
+ }
+ if err := ValidatePublicIP(ip); err != nil {
+ t.Errorf("ValidatePublicIP(%s) = %v, want nil", addr, err)
+ }
+ }
+}