Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#736 from ottoyiu/fix-domainfilter
Browse files Browse the repository at this point in the history
Fix domain-filter matching logic to not match similar domain names
  • Loading branch information
k8s-ci-robot authored Oct 17, 2018
2 parents f9e8434 + d505f23 commit 2668b9f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
11 changes: 10 additions & 1 deletion provider/domain_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
30 changes: 30 additions & 0 deletions provider/domain_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 2668b9f

Please sign in to comment.