use EffectiveTLDPlusOne

This commit is contained in:
Caleb Doxsey 2022-11-17 19:27:36 -07:00
parent 177c2fff5e
commit 19d0c5065c
2 changed files with 6 additions and 4 deletions

View file

@ -4,7 +4,8 @@ package webauthnutil
import (
"net"
"net/http"
"strings"
"golang.org/x/net/publicsuffix"
"github.com/pomerium/pomerium/pkg/grpc/databroker"
"github.com/pomerium/webauthn"
@ -24,8 +25,8 @@ func GetEffectiveDomain(r *http.Request) string {
if err != nil {
h = r.Host
}
if idx := strings.Index(h, "."); idx >= 0 {
h = h[idx+1:]
if tld, err := publicsuffix.EffectiveTLDPlusOne(h); err == nil {
return tld
}
return h
}

View file

@ -16,7 +16,8 @@ func TestGetEffectiveDomain(t *testing.T) {
}{
{"https://www.example.com/some/path", "example.com"},
{"https://www.example.com:8080/some/path", "example.com"},
{"https://www.subdomain.example.com/some/path", "subdomain.example.com"},
{"https://www.subdomain.example.com/some/path", "example.com"},
{"https://example.com/some/path", "example.com"},
} {
tc := tc
t.Run(tc.expect, func(t *testing.T) {