publicsuffix: don't treat ip addresses as domain names

While IP addresses are not domain names and probably
shouldn't be passed to these functions at all, it seems
wrong to have it handle IPv4 and IPv6 differently.

Fixes golang/go#32979

Change-Id: Id321a08b552c11d990c3966636b64793f762143f
Reviewed-on: https://go-review.googlesource.com/c/net/+/715100
Reviewed-by: Michael Pratt <mpratt@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
Sean Liao
2025-10-26 22:34:45 +00:00
parent d1f64cc670
commit 5ac9daca08
2 changed files with 15 additions and 0 deletions

View File

@@ -51,6 +51,7 @@ package publicsuffix // import "golang.org/x/net/publicsuffix"
import (
"fmt"
"net/http/cookiejar"
"net/netip"
"strings"
)
@@ -84,6 +85,10 @@ func (list) String() string {
// domains like "foo.appspot.com" can be found at
// https://wiki.mozilla.org/Public_Suffix_List/Use_Cases
func PublicSuffix(domain string) (publicSuffix string, icann bool) {
if _, err := netip.ParseAddr(domain); err == nil {
return domain, false
}
lo, hi := uint32(0), uint32(numTLD)
s, suffix, icannNode, wildcard := domain, len(domain), false, false
loop:

View File

@@ -5,6 +5,7 @@
package publicsuffix
import (
"net/netip"
"sort"
"strings"
"testing"
@@ -85,6 +86,11 @@ var publicSuffixTestCases = []struct {
// Empty string.
{"", "", false},
// IP addresses don't have a domain hierarchy
{"192.0.2.0", "192.0.2.0", false},
{"::ffff:192.0.2.0", "::ffff:192.0.2.0", false},
{"2001:db8::", "2001:db8::", false},
// The .ao rules are:
// ao
// ed.ao
@@ -332,6 +338,10 @@ type slowPublicSuffixRule struct {
// This function returns the public suffix, not the registrable domain, and so
// it stops after step 6.
func slowPublicSuffix(domain string) (string, bool) {
if _, err := netip.ParseAddr(domain); err == nil {
return domain, false
}
match := func(rulePart, domainPart string) bool {
switch rulePart[0] {
case '*':