Skip to content

Commit a51efb4

Browse files
authored
Support hostname:port to pass host matcher's check #19543 (#19543)
hostmatcher: split the hostname from the `hostname:port` string, use the correct hostname to do the match.
1 parent 8eb1cd9 commit a51efb4

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

modules/hostmatcher/hostmatcher.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,18 @@ func (hl *HostMatchList) checkIP(ip net.IP) bool {
125125

126126
// MatchHostName checks if the host matches an allow/deny(block) list
127127
func (hl *HostMatchList) MatchHostName(host string) bool {
128+
hostname, _, err := net.SplitHostPort(host)
129+
if err != nil {
130+
hostname = host
131+
}
132+
128133
if hl == nil {
129134
return false
130135
}
131-
if hl.checkPattern(host) {
136+
if hl.checkPattern(hostname) {
132137
return true
133138
}
134-
if ip := net.ParseIP(host); ip != nil {
139+
if ip := net.ParseIP(hostname); ip != nil {
135140
return hl.checkIP(ip)
136141
}
137142
return false

modules/hostmatcher/hostmatcher_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func TestHostOrIPMatchesList(t *testing.T) {
3838

3939
{"", net.ParseIP("10.0.1.1"), true},
4040
{"10.0.1.1", nil, true},
41+
{"10.0.1.1:8080", nil, true},
4142
{"", net.ParseIP("192.168.1.1"), true},
4243
{"192.168.1.1", nil, true},
4344
{"", net.ParseIP("fd00::1"), true},
@@ -48,6 +49,7 @@ func TestHostOrIPMatchesList(t *testing.T) {
4849

4950
{"mydomain.com", net.IPv4zero, false},
5051
{"sub.mydomain.com", net.IPv4zero, true},
52+
{"sub.mydomain.com:8080", net.IPv4zero, true},
5153

5254
{"", net.ParseIP("169.254.1.1"), true},
5355
{"169.254.1.1", nil, true},

0 commit comments

Comments
 (0)