mirror of https://github.com/0xERR0R/blocky.git
* fix: don't convert regex from punycode to ASCII (#1126) * chore(test): add additional tests
This commit is contained in:
parent
51bb54b595
commit
245bb613df
|
@ -201,15 +201,18 @@ func (e HostsFileEntry) forEachHost(callback func(string) error) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func normalizeHostsListEntry(host string) (string, 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.
|
// 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"
|
// 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
|
// since the goal of the list is to determine whether the domain should be used or not, we leave
|
||||||
// that decision to it.
|
// that decision to it.
|
||||||
idnaProfile := idna.Punycode
|
idnaProfile := idna.Punycode
|
||||||
|
|
||||||
host, err := idnaProfile.ToASCII(host)
|
if !isRegex(host) {
|
||||||
if err != nil {
|
host, err = idnaProfile.ToASCII(host)
|
||||||
return "", fmt.Errorf("%w: %s", err, host)
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("%w: %s", err, host)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := validateHostsListEntry(host); err != nil {
|
if err := validateHostsListEntry(host); err != nil {
|
||||||
|
@ -231,12 +234,16 @@ func validateDomainName(host string) error {
|
||||||
return fmt.Errorf("invalid domain name: %s", host)
|
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 {
|
func validateHostsListEntry(host string) error {
|
||||||
if net.ParseIP(host) != nil {
|
if net.ParseIP(host) != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(host, "/") && strings.HasSuffix(host, "/") {
|
if isRegex(host) {
|
||||||
_, err := regexp.Compile(host)
|
_, err := regexp.Compile(host)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -34,6 +34,8 @@ var _ = Describe("Hosts", func() {
|
||||||
"127.0.0.1 domain.tld # comment",
|
"127.0.0.1 domain.tld # comment",
|
||||||
"::1 localhost alias",
|
"::1 localhost alias",
|
||||||
`/domain\.(tld|local)/`,
|
`/domain\.(tld|local)/`,
|
||||||
|
`/^(.*\.)?2023\.xn--aptslabs-6fd\.net$/`,
|
||||||
|
`müller.com`,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -58,11 +60,21 @@ var _ = Describe("Hosts", func() {
|
||||||
Expect(iteratorToList(it.ForEach)).Should(Equal([]string{`/domain\.(tld|local)/`}))
|
Expect(iteratorToList(it.ForEach)).Should(Equal([]string{`/domain\.(tld|local)/`}))
|
||||||
Expect(sut.Position()).Should(Equal("line 6"))
|
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"))
|
||||||
|
|
||||||
|
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())
|
_, err = sut.Next(context.Background())
|
||||||
Expect(err).ShouldNot(Succeed())
|
Expect(err).ShouldNot(Succeed())
|
||||||
Expect(err).Should(MatchError(io.EOF))
|
Expect(err).Should(MatchError(io.EOF))
|
||||||
Expect(IsNonResumableErr(err)).Should(BeTrue())
|
Expect(IsNonResumableErr(err)).Should(BeTrue())
|
||||||
Expect(sut.Position()).Should(Equal("line 7"))
|
Expect(sut.Position()).Should(Equal("line 9"))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -71,6 +83,7 @@ var _ = Describe("Hosts", func() {
|
||||||
lines := []string{
|
lines := []string{
|
||||||
"invalidIP localhost",
|
"invalidIP localhost",
|
||||||
"!notadomain!",
|
"!notadomain!",
|
||||||
|
"xn---mllerk1va.com",
|
||||||
`/invalid regex ??/`,
|
`/invalid regex ??/`,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue