From a5dde37f8f7ebc2b9e5f5b405f07835ebb87c44e Mon Sep 17 00:00:00 2001 From: Dimitri Herzog Date: Thu, 7 Sep 2023 22:09:01 +0200 Subject: [PATCH 1/2] fix: don't convert regex from punycode to ASCII (#1126) --- lists/parsers/hosts.go | 15 +++++++++++---- lists/parsers/hosts_test.go | 8 +++++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lists/parsers/hosts.go b/lists/parsers/hosts.go index 67ef77f9a..1cba0a3f0 100644 --- a/lists/parsers/hosts.go +++ b/lists/parsers/hosts.go @@ -201,15 +201,18 @@ func (e HostsFileEntry) forEachHost(callback func(string) error) error { } func normalizeHostsListEntry(host string) (string, error) { + var err 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 !isRegex(host) { + host, err = idnaProfile.ToASCII(host) + if err != nil { + return "", fmt.Errorf("%w: %s", err, host) + } } if err := validateHostsListEntry(host); err != nil { @@ -231,12 +234,16 @@ func validateDomainName(host string) error { return fmt.Errorf("invalid domain name: %s", host) } +func isRegex(host string) bool { + return strings.HasPrefix(host, "/") && strings.HasSuffix(host, "/") +} + func validateHostsListEntry(host string) error { if net.ParseIP(host) != nil { return nil } - if strings.HasPrefix(host, "/") && strings.HasSuffix(host, "/") { + if isRegex(host) { _, err := regexp.Compile(host) return err diff --git a/lists/parsers/hosts_test.go b/lists/parsers/hosts_test.go index 0d6e51141..b6a365912 100644 --- a/lists/parsers/hosts_test.go +++ b/lists/parsers/hosts_test.go @@ -34,6 +34,7 @@ var _ = Describe("Hosts", func() { "127.0.0.1 domain.tld # comment", "::1 localhost alias", `/domain\.(tld|local)/`, + `/^(.*\.)?2023\.xn--aptslabs-6fd\.net$/`, ) }) @@ -58,11 +59,16 @@ var _ = Describe("Hosts", func() { Expect(iteratorToList(it.ForEach)).Should(Equal([]string{`/domain\.(tld|local)/`})) Expect(sut.Position()).Should(Equal("line 6")) + it, err = sut.Next(context.Background()) + Expect(err).Should(Succeed()) + Expect(iteratorToList(it.ForEach)).Should(Equal([]string{`/^(.*\.)?2023\.xn--aptslabs-6fd\.net$/`})) + Expect(sut.Position()).Should(Equal("line 7")) + _, 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 7")) + Expect(sut.Position()).Should(Equal("line 8")) }) }) From eb1b86bc810ef0165c388b99d18d5a2a07fb0cfa Mon Sep 17 00:00:00 2001 From: Dimitri Herzog Date: Fri, 8 Sep 2023 07:24:37 +0000 Subject: [PATCH 2/2] chore(test): add additional tests --- lists/parsers/hosts_test.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lists/parsers/hosts_test.go b/lists/parsers/hosts_test.go index b6a365912..3f10433e5 100644 --- a/lists/parsers/hosts_test.go +++ b/lists/parsers/hosts_test.go @@ -35,6 +35,7 @@ var _ = Describe("Hosts", func() { "::1 localhost alias", `/domain\.(tld|local)/`, `/^(.*\.)?2023\.xn--aptslabs-6fd\.net$/`, + `müller.com`, ) }) @@ -64,11 +65,16 @@ var _ = Describe("Hosts", func() { Expect(iteratorToList(it.ForEach)).Should(Equal([]string{`/^(.*\.)?2023\.xn--aptslabs-6fd\.net$/`})) Expect(sut.Position()).Should(Equal("line 7")) + it, err = sut.Next(context.Background()) + Expect(err).Should(Succeed()) + Expect(iteratorToList(it.ForEach)).Should(Equal([]string{`xn--mller-kva.com`})) + 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 8")) + Expect(sut.Position()).Should(Equal("line 9")) }) }) @@ -77,6 +83,7 @@ var _ = Describe("Hosts", func() { lines := []string{ "invalidIP localhost", "!notadomain!", + "xn---mllerk1va.com", `/invalid regex ??/`, }