Merge pull request #736 from ottoyiu/fix-domainfilter

Fix domain-filter matching logic to not match similar domain names
This commit is contained in:
k8s-ci-robot 2018-10-17 02:44:05 -07:00 committed by GitHub
commit 2668b9f4ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 1 deletions

View File

@ -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
}
}

View File

@ -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) {