Skip to content

Commit

Permalink
fix(gateway): fix subdomain matching
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Beaumont <[email protected]>
  • Loading branch information
michaelbeaumont committed Jan 22, 2024
1 parent bfdac84 commit cc5ea0a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 54 deletions.
29 changes: 0 additions & 29 deletions pkg/plugins/runtime/gateway/hostname_match_test.go

This file was deleted.

33 changes: 33 additions & 0 deletions pkg/plugins/runtime/gateway/match/hostname_match_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package match_test

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/kumahq/kuma/pkg/plugins/runtime/gateway/match"
)

var _ = Describe("Hostname matching", func() {
DescribeTable("should match",
func(hostname string, candidate string) {
Expect(match.Hostnames(hostname, candidate)).To(BeTrue())
},
Entry("equal exact", "foo.example.com", "foo.example.com"),
Entry("wild matches exact", "*.example.com", "foo.example.com"),
Entry("exact matches wild", "foo.example.com", "*.example.com"),
Entry("wild exact", "*.example.com", "*.example.com"),
Entry("larger wild matches smaller wild", "*.com", "*.examples.com"),
Entry("smaller wild matches larger wild", "*.examples.com", "*.com"),
Entry("larger exact matches smaller wild", "foo.example.com", "*.com"),
Entry("smaller wild matches larger exact", "foo.examples.com", "*.com"),
)
DescribeTable("should not match",
func(hostname string, candidate string) {
Expect(match.Hostnames(hostname, candidate)).To(BeFalse())
},
Entry("exact unequal", "foo.example.com", "bar.example.com"),
Entry("first wild with rest unequal", "*.example.com", "foo.examples.com"),
Entry("second wild with rest unequal", "foo.example.com", "*.examples.com"),
Entry("both wild with rest unequal", "*.example.com", "*.examples.com"),
)
})
47 changes: 22 additions & 25 deletions pkg/plugins/runtime/gateway/match/hostnames.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
package match

import "strings"
import (
"strings"
)

type hostname struct {
Host string
Domain string
Host string
DomainParts []string
}

func (h *hostname) wildcard() bool {
return h.Host == "*"
}

func (h *hostname) matches(name string) bool {
if name == "*" {
return true
}
n := makeHostname(name)
return h.contains(n) || n.contains(*h)
}

func (h *hostname) contains(n hostname) bool {
if len(h.DomainParts) > len(n.DomainParts) {
return false
}

if h.wildcard() || n.wildcard() {
return h.Domain == n.Domain
for i := 1; i <= len(h.DomainParts); i++ {
hInd := len(h.DomainParts) - i
nInd := len(n.DomainParts) - i
if n.DomainParts[nInd] != h.DomainParts[hInd] {
return false
}
}

return h.Host == n.Host && h.Domain == n.Domain
return h.wildcard() || h.Host == n.Host
}

func makeHostname(name string) hostname {
parts := strings.SplitN(name, ".", 2)
return hostname{Host: parts[0], Domain: parts[1]}
parts := strings.Split(name, ".")
return hostname{Host: parts[0], DomainParts: parts[1:]}
}

func Contains(target string, test string) bool {
if target == "*" {
return true
}
if test == "*" {
return false
}
targetHost := makeHostname(target)
testHost := makeHostname(test)
// TODO domain has to be less
if (targetHost.wildcard() || targetHost.Host == testHost.Host) && targetHost.Domain == testHost.Domain {
return true
}
return false
return targetHost.contains(testHost)
}

// Hostnames returns true if target is a host or domain name match for
Expand All @@ -54,9 +54,6 @@ func Contains(target string, test string) bool {
// 1. They are exactly equal, OR
// 2. One of them is a domain wildcard and the domain part matches.
func Hostnames(target string, matches ...string) bool {
if target == "*" {
return true
}
targetHost := makeHostname(target)

for _, m := range matches {
Expand Down

0 comments on commit cc5ea0a

Please sign in to comment.