Details
Suggested fix
Canonicalize the resolved address to its embedded IPv4 before the range checks, so checkIP classifies the address the request will actually reach rather than the routable IPv6 wrapper. Minimal patch:
--- a/pkg/request/ssrf.go
+++ b/pkg/request/ssrf.go
@@ func checkIPWithAllowlist ... return checkIP(ip) }
+
+// effectiveIP unwraps IPv4-in-IPv6 transition forms to the IPv4 address the
+// packet ultimately reaches, so checkIP classifies the real target rather than
+// the (often globally-routable) IPv6 wrapper. Covers IPv4-mapped
+// (::ffff:a.b.c.d), NAT64 well-known prefix (64:ff9b::/96, RFC 6052),
+// 6to4 (2002::/16, RFC 3056) and IPv4-compatible (::a.b.c.d).
+func effectiveIP(ip net.IP) net.IP {
+ if v4 := ip.To4(); v4 != nil {
+ return v4
+ }
+ v6 := ip.To16()
+ if v6 == nil {
+ return ip
+ }
+ if v6[0] == 0x00 && v6[1] == 0x64 && v6[2] == 0xff && v6[3] == 0x9b &&
+ v6[4] == 0 && v6[5] == 0 && v6[6] == 0 && v6[7] == 0 &&
+ v6[8] == 0 && v6[9] == 0 && v6[10] == 0 && v6[11] == 0 {
+ return net.IPv4(v6[12], v6[13], v6[14], v6[15]).To4()
+ }
+ if v6[0] == 0x20 && v6[1] == 0x02 {
+ return net.IPv4(v6[2], v6[3], v6[4], v6[5]).To4()
+ }
+ allZeroTop := true
+ for i := 0; i < 12; i++ {
+ if v6[i] != 0 {
+ allZeroTop = false
+ break
+ }
+ }
+ if allZeroTop {
+ last := uint32(v6[12])<<24 | uint32(v6[13])<<16 | uint32(v6[14])<<8 | uint32(v6[15])
+ if last > 1 {
+ return net.IPv4(v6[12], v6[13], v6[14], v6[15]).To4()
+ }
+ }
+ return ip
+}
+
func checkIP(ip net.IP) error {
+ ip = effectiveIP(ip)
if ip == nil {
return fmt.Errorf("invalid IP: %w", ErrUnsafeURL)
}
As defense in depth, consider rejecting all non-IPv4-mapped IPv4-embedding IPv6 forms outright unless the deployment intentionally uses NAT64.
Credit
tonghuaroot ([email protected]).