Refuse more non-public address forms

Add the benchmarking, reserved, NAT64 and 6to4 ranges, and refuse numeric host names such as 2130706433 or 127.1 when a proxy resolves the name.

(cherry picked from commit a802a59627acdc6400ac2ecf19c8e5cbcc2868e8)
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Kevin Ravensberg
2026-11-13 20:33:44 +00:00
committed by Michilis
parent 5ff6e841e1
commit 5360a9d4a1
2 changed files with 39 additions and 5 deletions
+28 -5
View File
@@ -7,18 +7,33 @@ import (
"fmt"
"net"
"net/netip"
"strconv"
"strings"
"syscall"
"time"
)
// 100.64.0.0/10 is carrier-grade NAT space, also used by overlay networks.
var cgnat = netip.MustParsePrefix("100.64.0.0/10")
// Ranges that are not public but pass netip's own checks.
var reserved = []netip.Prefix{
netip.MustParsePrefix("100.64.0.0/10"), // carrier-grade NAT, overlay networks
netip.MustParsePrefix("198.18.0.0/15"), // benchmarking
netip.MustParsePrefix("240.0.0.0/4"), // reserved
netip.MustParsePrefix("64:ff9b::/96"), // NAT64, embeds an IPv4 address
netip.MustParsePrefix("2002::/16"), // 6to4, embeds an IPv4 address
}
// IsPublic reports whether ip is an address on the public internet.
func IsPublic(ip netip.Addr) bool {
ip = ip.Unmap()
return ip.IsGlobalUnicast() && !ip.IsPrivate() && !cgnat.Contains(ip)
if !ip.IsGlobalUnicast() || ip.IsPrivate() {
return false
}
for _, prefix := range reserved {
if prefix.Contains(ip) {
return false
}
}
return true
}
// The check runs at connect time on the resolved address, so it also covers
@@ -54,8 +69,16 @@ func CheckHost(addr string) error {
if host == "localhost" || strings.HasSuffix(host, ".localhost") {
return fmt.Errorf("connection to %s refused", host)
}
if ip, err := netip.ParseAddr(host); err == nil && !IsPublic(ip) {
return fmt.Errorf("connection to non-public address %s refused", ip)
if ip, err := netip.ParseAddr(host); err == nil {
if !IsPublic(ip) {
return fmt.Errorf("connection to non-public address %s refused", ip)
}
return nil
}
// Resolvers also accept IPv4 addresses written as 2130706433, 127.1 or
// 0x7f.1. No real hostname ends in a number.
if _, err := strconv.ParseUint(host[strings.LastIndex(host, ".")+1:], 0, 64); err == nil {
return fmt.Errorf("connection to numeric host %s refused", host)
}
return nil
}
+11
View File
@@ -25,6 +25,10 @@ func TestIsPublic(t *testing.T) {
"::ffff:192.168.1.1": false,
"224.0.0.1": false,
"255.255.255.255": false,
"198.18.0.1": false,
"240.0.0.1": false,
"64:ff9b::7f00:1": false,
"2002:7f00:1::1": false,
} {
if got := IsPublic(netip.MustParseAddr(addr)); got != want {
t.Errorf("IsPublic(%s) = %v, want %v", addr, got, want)
@@ -63,6 +67,13 @@ func TestCheckHost(t *testing.T) {
"[::1]:6060": false,
"192.168.0.10:5000": false,
"0.0.0.0:80": false,
"2130706433:80": false,
"127.1:80": false,
"0x7f.1:80": false,
"0x7f000001:80": false,
"017700000001:80": false,
"example.com1:80": true,
"1.example.com:80": true,
} {
if err := CheckHost(addr); (err == nil) != ok {
t.Errorf("CheckHost(%s) = %v, want ok=%v", addr, err, ok)