From d505f2320a2a366dad19ef9482edf5da7f725274 Mon Sep 17 00:00:00 2001 From: Otto Yiu Date: Sun, 14 Oct 2018 14:46:21 -0700 Subject: [PATCH] fix domain filter match logic to not match similar domain names --- provider/domain_filter.go | 11 ++++++++++- provider/domain_filter_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/provider/domain_filter.go b/provider/domain_filter.go index 6da6759b7..4bfdef90f 100644 --- a/provider/domain_filter.go +++ b/provider/domain_filter.go @@ -46,8 +46,17 @@ func (df DomainFilter) Match(domain string) bool { } for _, filter := range df.filters { + strippedDomain := strings.TrimSuffix(domain, ".") - if strings.HasSuffix(strings.TrimSuffix(domain, "."), filter) { + if filter == "" { + return true + } else if strings.HasPrefix(filter, ".") && strings.HasSuffix(strippedDomain, filter) { + return true + } else if strings.Count(strippedDomain, ".") == strings.Count(filter, ".") { + if strippedDomain == filter { + return true + } + } else if strings.HasSuffix(strippedDomain, "."+filter) { return true } } diff --git a/provider/domain_filter_test.go b/provider/domain_filter_test.go index cb11ebe2f..12a562664 100644 --- a/provider/domain_filter_test.go +++ b/provider/domain_filter_test.go @@ -99,6 +99,36 @@ var domainFilterTests = []domainFilterTest{ []string{"foo.bar.sub.example.org"}, true, }, + { + []string{"example.org"}, + []string{"anexample.org", "test.anexample.org"}, + false, + }, + { + []string{".example.org"}, + []string{"anexample.org", "test.anexample.org"}, + false, + }, + { + []string{".example.org"}, + []string{"example.org"}, + false, + }, + { + []string{".example.org"}, + []string{"test.example.org"}, + true, + }, + { + []string{"anexample.org"}, + []string{"example.org", "test.example.org"}, + false, + }, + { + []string{".org"}, + []string{"example.org", "test.example.org", "foo.test.example.org"}, + true, + }, } func TestDomainFilterMatch(t *testing.T) {