fix(web): block 0.0.0.0/8, CGNAT, and reserved ranges in isPrivateIP (S2 hardening)

This commit is contained in:
sausheong committed 2026-06-13 11:19:03 +08:00
1 parent 083122a81f
commit 1e00916a82
2 files changed
+31

No files matched your search

+4
View File
@@ -14,9 +14,13 @@ var privateNetworks = []string{
"172.16.0.0/12", // RFC 1918 "172.16.0.0/12", // RFC 1918
"192.168.0.0/16", // RFC 1918 "192.168.0.0/16", // RFC 1918
"169.254.0.0/16", // link-local "169.254.0.0/16", // link-local
"0.0.0.0/8", // "this" network / unspecified; routes to loopback on Linux
"100.64.0.0/10", // CGNAT (RFC 6598)
"192.0.0.0/24", // IETF protocol assignments (RFC 6890)
"::1/128", // IPv6 loopback "::1/128", // IPv6 loopback
"fc00::/7", // IPv6 unique local "fc00::/7", // IPv6 unique local
"fe80::/10", // IPv6 link-local "fe80::/10", // IPv6 link-local
"::/128", // IPv6 unspecified
} }
var parsedPrivateNets []*net.IPNet var parsedPrivateNets []*net.IPNet
+27
View File
@@ -1,10 +1,12 @@
package web package web
import ( import (
"net"
"strings" "strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestValidateURLNotInternal_BlocksLoopback(t *testing.T) { func TestValidateURLNotInternal_BlocksLoopback(t *testing.T) {
@@ -45,3 +47,28 @@ func TestValidateURLNotInternal_RejectsBadScheme(t *testing.T) {
assert.Error(t, err, "non-http schemes must be rejected") assert.Error(t, err, "non-http schemes must be rejected")
_ = strings.TrimSpace(err.Error()) _ = strings.TrimSpace(err.Error())
} }
func TestIsPrivateIP_BlocksReservedRanges(t *testing.T) {
blocked := []string{
"0.0.0.0", // unspecified / routes to loopback on Linux
"0.0.0.1", // 0.0.0.0/8
"100.64.0.0", // CGNAT 100.64.0.0/10
"100.127.255.255", // CGNAT upper
"192.0.0.1", // 192.0.0.0/24 IETF protocol assignments
"::", // IPv6 unspecified ::/128
}
for _, s := range blocked {
ip := net.ParseIP(s)
require.NotNil(t, ip, "parse %s", s)
require.True(t, isPrivateIP(ip), "%s must be treated as private/blocked", s)
}
}
func TestIsPrivateIP_AllowsPublic(t *testing.T) {
pub := []string{"8.8.8.8", "1.1.1.1", "93.184.216.34", "2606:2800:220:1:248:1893:25c8:1946"}
for _, s := range pub {
ip := net.ParseIP(s)
require.NotNil(t, ip, "parse %s", s)
require.False(t, isPrivateIP(ip), "%s must be allowed (public)", s)
}
}