aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2022-04-29 01:39:50 +0800
committerGitHub <noreply@github.com>2022-04-29 01:39:50 +0800
commita51efb4c2c315db0deb4120c8f2874390c2d7e40 (patch)
treeb0cf66109ae8aadbfbc0a084646f95c7ec99308b
parent8eb1cd9264af9493e0f57a4a4c0a1f764f7cefcf (diff)
downloadgitea-a51efb4c2c315db0deb4120c8f2874390c2d7e40.tar.gz
gitea-a51efb4c2c315db0deb4120c8f2874390c2d7e40.zip
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.
-rw-r--r--modules/hostmatcher/hostmatcher.go9
-rw-r--r--modules/hostmatcher/hostmatcher_test.go2
2 files changed, 9 insertions, 2 deletions
diff --git a/modules/hostmatcher/hostmatcher.go b/modules/hostmatcher/hostmatcher.go
index 6c5c2a74d9..00bbc6cb0a 100644
--- a/modules/hostmatcher/hostmatcher.go
+++ b/modules/hostmatcher/hostmatcher.go
@@ -125,13 +125,18 @@ func (hl *HostMatchList) checkIP(ip net.IP) bool {
// MatchHostName checks if the host matches an allow/deny(block) list
func (hl *HostMatchList) MatchHostName(host string) bool {
+ hostname, _, err := net.SplitHostPort(host)
+ if err != nil {
+ hostname = host
+ }
+
if hl == nil {
return false
}
- if hl.checkPattern(host) {
+ if hl.checkPattern(hostname) {
return true
}
- if ip := net.ParseIP(host); ip != nil {
+ if ip := net.ParseIP(hostname); ip != nil {
return hl.checkIP(ip)
}
return false
diff --git a/modules/hostmatcher/hostmatcher_test.go b/modules/hostmatcher/hostmatcher_test.go
index 66030a32f1..b93976df6a 100644
--- a/modules/hostmatcher/hostmatcher_test.go
+++ b/modules/hostmatcher/hostmatcher_test.go
@@ -38,6 +38,7 @@ func TestHostOrIPMatchesList(t *testing.T) {
{"", net.ParseIP("10.0.1.1"), true},
{"10.0.1.1", nil, true},
+ {"10.0.1.1:8080", nil, true},
{"", net.ParseIP("192.168.1.1"), true},
{"192.168.1.1", nil, true},
{"", net.ParseIP("fd00::1"), true},
@@ -48,6 +49,7 @@ func TestHostOrIPMatchesList(t *testing.T) {
{"mydomain.com", net.IPv4zero, false},
{"sub.mydomain.com", net.IPv4zero, true},
+ {"sub.mydomain.com:8080", net.IPv4zero, true},
{"", net.ParseIP("169.254.1.1"), true},
{"169.254.1.1", nil, true},