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>
diff --git a/publicsuffix/list.go b/publicsuffix/list.go
index 047cb30..7ab8b3c 100644
--- a/publicsuffix/list.go
+++ b/publicsuffix/list.go
@@ -51,6 +51,7 @@
import (
"fmt"
"net/http/cookiejar"
+ "net/netip"
"strings"
)
@@ -84,6 +85,10 @@
// 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:
diff --git a/publicsuffix/list_test.go b/publicsuffix/list_test.go
index 7a1bb0f..7648fdb 100644
--- a/publicsuffix/list_test.go
+++ b/publicsuffix/list_test.go
@@ -5,6 +5,7 @@
package publicsuffix
import (
+ "net/netip"
"sort"
"strings"
"testing"
@@ -85,6 +86,11 @@
// 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 @@
// 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 '*':