Skip to content

Commit 5ac9dac

Browse files
committed
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>
1 parent d1f64cc commit 5ac9dac

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

publicsuffix/list.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ package publicsuffix // import "golang.org/x/net/publicsuffix"
5151
import (
5252
"fmt"
5353
"net/http/cookiejar"
54+
"net/netip"
5455
"strings"
5556
)
5657

@@ -84,6 +85,10 @@ func (list) String() string {
8485
// domains like "foo.appspot.com" can be found at
8586
// https://wiki.mozilla.org/Public_Suffix_List/Use_Cases
8687
func PublicSuffix(domain string) (publicSuffix string, icann bool) {
88+
if _, err := netip.ParseAddr(domain); err == nil {
89+
return domain, false
90+
}
91+
8792
lo, hi := uint32(0), uint32(numTLD)
8893
s, suffix, icannNode, wildcard := domain, len(domain), false, false
8994
loop:

publicsuffix/list_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package publicsuffix
66

77
import (
8+
"net/netip"
89
"sort"
910
"strings"
1011
"testing"
@@ -85,6 +86,11 @@ var publicSuffixTestCases = []struct {
8586
// Empty string.
8687
{"", "", false},
8788

89+
// IP addresses don't have a domain hierarchy
90+
{"192.0.2.0", "192.0.2.0", false},
91+
{"::ffff:192.0.2.0", "::ffff:192.0.2.0", false},
92+
{"2001:db8::", "2001:db8::", false},
93+
8894
// The .ao rules are:
8995
// ao
9096
// ed.ao
@@ -332,6 +338,10 @@ type slowPublicSuffixRule struct {
332338
// This function returns the public suffix, not the registrable domain, and so
333339
// it stops after step 6.
334340
func slowPublicSuffix(domain string) (string, bool) {
341+
if _, err := netip.ParseAddr(domain); err == nil {
342+
return domain, false
343+
}
344+
335345
match := func(rulePart, domainPart string) bool {
336346
switch rulePart[0] {
337347
case '*':

0 commit comments

Comments
 (0)