Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support IDNA in lists #963

Merged
merged 1 commit into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 22 additions & 1 deletion lists/parsers/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/asaskevich/govalidator"
"github.com/hashicorp/go-multierror"
"golang.org/x/net/idna"
)

const maxDomainNameLength = 255 // https://www.rfc-editor.org/rfc/rfc1034#section-3.1
Expand Down Expand Up @@ -92,7 +93,8 @@ func (e *HostListEntry) UnmarshalText(data []byte) error {

host := scanner.Text()

if err := validateHostsListEntry(host); err != nil {
host, err := normalizeHostsListEntry(host)
if err != nil {
return err
}

Expand Down Expand Up @@ -191,6 +193,25 @@ func (e HostsFileEntry) forEachHost(callback func(string) error) error {
return nil
}

func normalizeHostsListEntry(host string) (string, error) {
// Lookup is the profile preferred for DNS queries, we use Punycode here as it does less validation.
// That avoids rejecting domains in a list for reasons that amount to "that domain should not be used"
// since the goal of the list is to determine whether the domain should be used or not, we leave
// that decision to it.
idnaProfile := idna.Punycode

host, err := idnaProfile.ToASCII(host)
if err != nil {
return "", fmt.Errorf("%w: %s", err, host)
}

if err := validateHostsListEntry(host); err != nil {
return "", err
}

return host, nil
}

func validateDomainName(host string) error {
if len(host) > maxDomainNameLength {
return fmt.Errorf("domain name is too long: %s", host)
Expand Down
30 changes: 29 additions & 1 deletion lists/parsers/hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,14 @@ var _ = Describe("HostList", func() {
"# comment",
" ",
"domain.tld # comment",

// http://www.i18nguy.com/markup/idna-examples.html
"belgië.icom.museum",
"الأردن.icom.museum",
"한국.icom.museum",

// Domain name w/ rune not supported by `idna.Lookup`
"domain_underscore.tld",
)
})

Expand All @@ -317,11 +325,31 @@ var _ = Describe("HostList", func() {
Expect(entry.String()).Should(Equal("domain.tld"))
Expect(sut.Position()).Should(Equal("line 4"))

entry, err = sut.Next(context.Background())
Expect(err).Should(Succeed())
Expect(entry.String()).Should(Equal("xn--belgi-rsa.icom.museum"))
Expect(sut.Position()).Should(Equal("line 5"))

entry, err = sut.Next(context.Background())
Expect(err).Should(Succeed())
Expect(entry.String()).Should(Equal("xn--igbhzh7gpa.icom.museum"))
Expect(sut.Position()).Should(Equal("line 6"))

entry, err = sut.Next(context.Background())
Expect(err).Should(Succeed())
Expect(entry.String()).Should(Equal("xn--3e0b707e.icom.museum"))
Expect(sut.Position()).Should(Equal("line 7"))

entry, err = sut.Next(context.Background())
Expect(err).Should(Succeed())
Expect(entry.String()).Should(Equal("domain_underscore.tld"))
Expect(sut.Position()).Should(Equal("line 8"))

_, err = sut.Next(context.Background())
Expect(err).ShouldNot(Succeed())
Expect(err).Should(MatchError(io.EOF))
Expect(IsNonResumableErr(err)).Should(BeTrue())
Expect(sut.Position()).Should(Equal("line 5"))
Expect(sut.Position()).Should(Equal("line 9"))
})
})

Expand Down