Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions iana/iana.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@ package iana

import (
"fmt"
"time"

"github.com/weppos/publicsuffix-go/publicsuffix"
zlintutil "github.com/zmap/zlint/v3/util"
)

// ExtractSuffix returns the public suffix of the domain using only the "ICANN"
// section of the Public Suffix List database.
// If the domain does not end in a suffix that belongs to an IANA-assigned
// domain, ExtractSuffix returns an error.
// It confirms with zlint's TLD list.
func ExtractSuffix(name string) (string, error) {
if name == "" {
return "", fmt.Errorf("Blank name argument passed to ExtractSuffix")
}

if !zlintutil.HasValidTLD(name, time.Now()) {
return "", fmt.Errorf("%s has an unknown TLD", name)
}

rule := publicsuffix.DefaultList.Find(name, &publicsuffix.FindOptions{IgnorePrivate: true, DefaultRule: nil})
if rule == nil {
return "", fmt.Errorf("Domain %s has no IANA TLD", name)
Expand Down
2 changes: 1 addition & 1 deletion iana/iana_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestExtractSuffix_Valid(t *testing.T) {
for _, tc := range testCases {
got, err := ExtractSuffix(tc.domain)
if err != nil {
t.Errorf("%q: returned error", tc.domain)
t.Errorf("%q: returned error: %v", tc.domain, err)
continue
}
if got != tc.want {
Expand Down