Skip to content

Commit

Permalink
fix: make domain validation in list parser more lenient
Browse files Browse the repository at this point in the history
  • Loading branch information
ThinkChaos committed Apr 14, 2023
1 parent 8253075 commit ea95d36
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
1 change: 0 additions & 1 deletion go.mod
Expand Up @@ -79,7 +79,6 @@ require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a // indirect
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Expand Up @@ -62,8 +62,6 @@ github.com/alicebob/miniredis/v2 v2.30.1/go.mod h1:b25qWj4fCEsBeAAR2mlb0ufImGC6u
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef h1:2JGTg6JapxP9/R33ZaagQtAM4EkkSYnIAlOG5EI8gkM=
github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef/go.mod h1:JS7hed4L1fj0hXcyEejnW57/7LCetXggd+vwrRnYeII=
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d h1:Byv0BzEl3/e6D5CLfI0j/7hiIEtvGVFPCZ7Ei2oq8iQ=
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/avast/retry-go/v4 v4.3.3 h1:G56Bp6mU0b5HE1SkaoVjscZjlQb0oy4mezwY/cGH19w=
github.com/avast/retry-go/v4 v4.3.3/go.mod h1:rg6XFaiuFYII0Xu3RDbZQkxCofFwruZKW8oEF1jpWiU=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down
13 changes: 10 additions & 3 deletions lists/parsers/hosts.go
Expand Up @@ -10,14 +10,21 @@ import (
"regexp"
"strings"

"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
const (
maxDomainNameLength = 255 // https://www.rfc-editor.org/rfc/rfc1034#section-3.1

var domainNameRegex = regexp.MustCompile(govalidator.DNSName)
dnsLabelPattern = `[a-zA-Z0-9_-]{1,63}`
)

// Validate a domain name, but with extra flexibility:
// - no restriction on the start or end of labels
//
// https://www.rfc-editor.org/rfc/rfc1034#section-3.5
var domainNameRegex = regexp.MustCompile(`^` + dnsLabelPattern + `(\.` + dnsLabelPattern + `)*[\._]?$`)

// Hosts parses `r` as a series of `HostsIterator`.
// It supports both the hosts file and host list formats.
Expand Down
16 changes: 15 additions & 1 deletion lists/parsers/hosts_test.go
Expand Up @@ -307,6 +307,10 @@ var _ = Describe("HostList", func() {

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

// invalid domain names we want to support
"-start-with-a-hyphen.com",
"end-with-a-hyphen-.com",
)
})

Expand Down Expand Up @@ -341,11 +345,21 @@ var _ = Describe("HostList", func() {
Expect(entry.String()).Should(Equal("domain_underscore.tld"))
Expect(sut.Position()).Should(Equal("line 8"))

entry, err = sut.Next(context.Background())
Expect(err).Should(Succeed())
Expect(entry.String()).Should(Equal("-start-with-a-hyphen.com"))
Expect(sut.Position()).Should(Equal("line 9"))

entry, err = sut.Next(context.Background())
Expect(err).Should(Succeed())
Expect(entry.String()).Should(Equal("end-with-a-hyphen-.com"))
Expect(sut.Position()).Should(Equal("line 10"))

_, 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 9"))
Expect(sut.Position()).Should(Equal("line 11"))
})
})

Expand Down

0 comments on commit ea95d36

Please sign in to comment.