diff --git a/README.md b/README.md index 7a71a5d..6338c40 100644 --- a/README.md +++ b/README.md @@ -694,7 +694,10 @@ Key settings in more detail: and generator pages. Default: empty - `blocked_networks` — list of CIDR ranges to refuse for SSRF protection, added to the always-enforced built-in ranges (loopback, private, link-local, CGNAT, - benchmark, NAT64, and the like); an invalid CIDR aborts startup + benchmark, NAT64, and the like); an invalid CIDR aborts startup. An IPv6 + address in the IPv4-mapped form (`::ffff:a.b.c.d`) is matched only as the IPv4 + address it holds, so an IPv6 range such as `::/0` refuses neither it nor a + plain IPv4 address - `trusted_proxies` — list of CIDR ranges of the reverse proxies in front of pixa. `X-Forwarded-For` is believed only when the direct peer falls inside one of these ranges; the logged and login-recorded client address is then the diff --git a/TODO.md b/TODO.md index 5296f5e..596940e 100644 --- a/TODO.md +++ b/TODO.md @@ -32,17 +32,16 @@ P2: security: per-origin rate limiting - 2026-10-16 P1 security bug: the check that keeps pixa from fetching internal addresses refuses the local-use NAT64 range (`64:ff9b:1::/48`, RFC 8215) as it - refuses `64:ff9b::/96`, and refuses an IPv6 address that holds an IPv4 - address, in the IPv4-compatible form (`::/96`) or the mapped form - (`::ffff:a.b.c.d`), when `blocked_networks` contains either the address as - written or the IPv4 address it holds (closes #299). A source host such as + refuses `64:ff9b::/96`, and refuses an IPv6 address in the IPv4-compatible + form (`::/96`) when `blocked_networks` contains either the address as written + or the IPv4 address it holds (closes #299). A source host such as `[64:ff9b:1::a00:1]` was fetched, which on a network that runs NAT64 on that - range reaches the IPv4 address it holds, here `10.0.0.1`; with + range reaches the IPv4 address it holds, here `10.0.0.1`; and with `203.0.113.0/24` in `blocked_networks`, `[::ffff:203.0.113.5]` was refused but - `[::203.0.113.5]` was fetched; and with `::ffff:0:0/96` listed, - `[::ffff:203.0.113.5]` was fetched. All now answer 403. Go keeps a plain IPv4 - address in the mapped form, so an IPv6 entry covering `::ffff:0:0/96`, such as - `::/0`, now refuses every IPv4 address too. + `[::203.0.113.5]` was fetched. Both now answer 403. The mapped form + (`::ffff:a.b.c.d`) is still matched only as the IPv4 address it holds: Go + keeps a plain IPv4 address in that form, and an IPv6 entry such as `::/0` must + not refuse every IPv4 address. - 2026-10-16 P1 security bug: the check that keeps pixa from fetching internal addresses judges an IPv6 address in the IPv4-compatible form (`::/96`), such as `::127.0.0.1` or `::10.0.0.1`, by the IPv4 address in its last four bytes diff --git a/internal/httpfetcher/httpfetcher.go b/internal/httpfetcher/httpfetcher.go index 0ebb436..f4c705e 100644 --- a/internal/httpfetcher/httpfetcher.go +++ b/internal/httpfetcher/httpfetcher.go @@ -599,39 +599,32 @@ func isPrivateIP(ip net.IP) bool { } // isBlockedIP reports whether ip is refused, either by the built-in -// internal-range check or by one of the operator-supplied prefixes. An IPv6 -// address that holds an IPv4 address is refused when a prefix contains the -// address as written or the IPv4 address it holds. +// internal-range check or by one of the operator-supplied prefixes. An +// IPv4-compatible address (in ::/96) is refused when a prefix contains the +// address as written or the IPv4 address it holds. An IPv4-mapped address (in +// ::ffff:0:0/96) is matched only as the IPv4 address it holds: Go keeps a +// plain IPv4 address in that form too, and an IPv6 prefix such as ::/0 must +// not refuse every IPv4 address. func isBlockedIP(ip net.IP, blocked []netip.Prefix) bool { if isPrivateIP(ip) { return true } - asIPv6, asIPv4 := ipv6AndIPv4Forms(ip) - if !asIPv6.IsValid() { + addr, ok := netip.AddrFromSlice(ip) + if !ok { return true } - return slices.ContainsFunc(blocked, func(prefix netip.Prefix) bool { - return prefix.Contains(asIPv6) || prefix.Contains(asIPv4) - }) -} - -// ipv6AndIPv4Forms returns ip as an IPv6 address and, when it holds an IPv4 -// address in the IPv4-mapped form (::ffff:0:0/96) or the IPv4-compatible form -// (::/96), that IPv4 address. Otherwise the IPv4 form is the zero netip.Addr, -// which no prefix contains. Go keeps a plain IPv4 address in the mapped form, -// so the IPv6 form of 203.0.113.5 is ::ffff:203.0.113.5. An ip that is not 4 -// or 16 bytes long gives two zero addresses. -func ipv6AndIPv4Forms(ip net.IP) (netip.Addr, netip.Addr) { - asIPv6, ok := netip.AddrFromSlice(ip.To16()) - if !ok { - return netip.Addr{}, netip.Addr{} - } + addr = addr.Unmap() + // ip read as IPv4 where it is or holds an IPv4 address, otherwise the + // zero netip.Addr, which no prefix contains. It differs from addr only + // for an IPv4-compatible address. asIPv4, _ := netip.AddrFromSlice(ipv4CompatibleToIPv4(ip).To4()) - return asIPv6, asIPv4 + return slices.ContainsFunc(blocked, func(prefix netip.Prefix) bool { + return prefix.Contains(addr) || prefix.Contains(asIPv4) + }) } // ipv4CompatibleToIPv4 returns the IPv4 address that an IPv4-compatible IPv6