Skip to content

Commit dd8c9f2

Browse files
wxiaoguangfnetX
authored andcommitted
Support hostname:port to pass host matcher's check (go-gitea#19543) (go-gitea#19544)
Backport go-gitea#19543 hostmatcher: split the hostname from the hostname:port string, use the correct hostname to do the match.
1 parent 777d752 commit dd8c9f2

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
@@ -127,13 +127,18 @@ func (hl *HostMatchList) checkIP(ip net.IP) bool {
127127

128128
// MatchHostName checks if the host matches an allow/deny(block) list
129129
func (hl *HostMatchList) MatchHostName(host string) bool {
130+
hostname, _, err := net.SplitHostPort(host)
131+
if err != nil {
132+
hostname = host
133+
}
134+
130135
if hl == nil {
131136
return false
132137
}
133-
if hl.checkPattern(host) {
138+
if hl.checkPattern(hostname) {
134139
return true
135140
}
136-
if ip := net.ParseIP(host); ip != nil {
141+
if ip := net.ParseIP(hostname); ip != nil {
137142
return hl.checkIP(ip)
138143
}
139144
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)