Skip to content

Commit

Permalink
fix: host verification was wrong regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
pdazcom committed Jul 24, 2024
1 parent 375ae9e commit 476ca91
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion botdetector.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (m *BotMiddleware) verifyBot(ip string, userAgent string) bool {
return false
}

botDomainPattern, _ := regexp.Compile(`(\.|^)(yandex\.(ru|net|com)|google(usercontent|bot)?\.com)$`)
botDomainPattern, _ := regexp.Compile(`(\.|^)(yandex\.(ru|net|com)|google(usercontent|bot)?\.com)\.$`)

for _, hostname := range hostnames {
match := botDomainPattern.MatchString(hostname)
Expand Down
16 changes: 12 additions & 4 deletions botdetector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,27 @@ type MockDNSResolver struct{}

func (r *MockDNSResolver) LookupAddr(ip string) ([]string, error) {
if ip == "66.249.66.1" {
return []string{"crawl-66-249-66-1.googlebot.com"}, nil
return []string{"crawl-66-249-66-1.googlebot.com."}, nil
}
if ip == "77.88.55.66" {
return []string{"spider-77-88-55-66.yandex.com"}, nil
return []string{"spider-77-88-55-66.yandex.com."}, nil
}
if ip == "5.255.253.36" {
return []string{"5-255-253-36.spider.yandex.com."}, nil
}
return nil, errors.New("unknown host")
}

func (r *MockDNSResolver) LookupIP(hostname string) ([]net.IP, error) {
if hostname == "crawl-66-249-66-1.googlebot.com" {
if hostname == "crawl-66-249-66-1.googlebot.com." {
return []net.IP{net.ParseIP("66.249.66.1")}, nil
}
if hostname == "spider-77-88-55-66.yandex.com" {
if hostname == "spider-77-88-55-66.yandex.com." {
return []net.IP{net.ParseIP("77.88.55.66")}, nil
}
if hostname == "5-255-253-36.spider.yandex.com." {
return []net.IP{net.ParseIP("5.255.253.36")}, nil
}
return nil, errors.New("unknown host")
}

Expand All @@ -38,6 +44,7 @@ func TestIsSearchBot(t *testing.T) {
{"Googlebot", true},
{"Mozilla/5.0", false},
{"YandexBot/3.0; +http://yandex.com/bots", true},
{"Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0", true},
}

middleware := &BotMiddleware{}
Expand All @@ -60,6 +67,7 @@ func TestVerifyBot(t *testing.T) {
{"77.88.55.66", "YandexBot", true},
{"77.88.55.67", "YandexBot", false},
{"192.168.1.1", "Mozilla/5.0", false},
{"5.255.253.36", "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0", true},
}

middleware := &BotMiddleware{
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ experimental:
plugins:
botdetector:
modulename = "github.com/pdazcom/botdetector"
version = "v0.0.3"
version = "v0.0.4"

entryPoints:
http:
Expand Down

0 comments on commit 476ca91

Please sign in to comment.