Skip to content

Commit f67a103

Browse files
authored
Add tests for the host checking logic, clarify the behaviors (go-gitea#20328)
Before, the combination of AllowedDomains/BlockedDomains/AllowLocalNetworks is confusing. This PR adds tests for the logic, clarify the behaviors.
1 parent d94f517 commit f67a103

File tree

5 files changed

+54
-8
lines changed

5 files changed

+54
-8
lines changed

custom/conf/app.example.ini

+1
Original file line numberDiff line numberDiff line change
@@ -2232,6 +2232,7 @@ ROUTER = console
22322232
;BLOCKED_DOMAINS =
22332233
;;
22342234
;; Allow private addresses defined by RFC 1918, RFC 1122, RFC 4632 and RFC 4291 (false by default)
2235+
;; If a domain is allowed by ALLOWED_DOMAINS, this option will be ignored.
22352236
;ALLOW_LOCALNETWORKS = false
22362237

22372238
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

docs/content/doc/advanced/config-cheat-sheet.en-us.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ Task queue configuration has been moved to `queue.task`. However, the below conf
10831083
- `RETRY_BACKOFF`: **3**: Backoff time per http/https request retry (seconds)
10841084
- `ALLOWED_DOMAINS`: **\<empty\>**: Domains allowlist for migrating repositories, default is blank. It means everything will be allowed. Multiple domains could be separated by commas. Wildcard is supported: `github.com, *.github.com`.
10851085
- `BLOCKED_DOMAINS`: **\<empty\>**: Domains blocklist for migrating repositories, default is blank. Multiple domains could be separated by commas. When `ALLOWED_DOMAINS` is not blank, this option has a higher priority to deny domains. Wildcard is supported.
1086-
- `ALLOW_LOCALNETWORKS`: **false**: Allow private addresses defined by RFC 1918, RFC 1122, RFC 4632 and RFC 4291
1086+
- `ALLOW_LOCALNETWORKS`: **false**: Allow private addresses defined by RFC 1918, RFC 1122, RFC 4632 and RFC 4291. If a domain is allowed by `ALLOWED_DOMAINS`, this option will be ignored.
10871087
- `SKIP_TLS_VERIFY`: **false**: Allow skip tls verify
10881088

10891089
## Federation (`federation`)

modules/hostmatcher/hostmatcher.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,14 @@ 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+
if hl == nil {
129+
return false
130+
}
131+
128132
hostname, _, err := net.SplitHostPort(host)
129133
if err != nil {
130134
hostname = host
131135
}
132-
133-
if hl == nil {
134-
return false
135-
}
136136
if hl.checkPattern(hostname) {
137137
return true
138138
}

services/migrations/migrate.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ func IsMigrateURLAllowed(remoteURL string, doer *user_model.User) error {
8484

8585
// some users only use proxy, there is no DNS resolver. it's safe to ignore the LookupIP error
8686
addrList, _ := net.LookupIP(hostName)
87+
return checkByAllowBlockList(hostName, addrList)
88+
}
8789

90+
func checkByAllowBlockList(hostName string, addrList []net.IP) error {
8891
var ipAllowed bool
8992
var ipBlocked bool
9093
for _, addr := range addrList {
@@ -93,12 +96,12 @@ func IsMigrateURLAllowed(remoteURL string, doer *user_model.User) error {
9396
}
9497
var blockedError error
9598
if blockList.MatchHostName(hostName) || ipBlocked {
96-
blockedError = &models.ErrInvalidCloneAddr{Host: u.Host, IsPermissionDenied: true}
99+
blockedError = &models.ErrInvalidCloneAddr{Host: hostName, IsPermissionDenied: true}
97100
}
98-
// if we have an allow-list, check the allow-list first
101+
// if we have an allow-list, check the allow-list before return to get the more accurate error
99102
if !allowList.IsEmpty() {
100103
if !allowList.MatchHostName(hostName) && !ipAllowed {
101-
return &models.ErrInvalidCloneAddr{Host: u.Host, IsPermissionDenied: true}
104+
return &models.ErrInvalidCloneAddr{Host: hostName, IsPermissionDenied: true}
102105
}
103106
}
104107
// otherwise, we always follow the blocked list
@@ -474,5 +477,7 @@ func Init() error {
474477
allowList.AppendBuiltin(hostmatcher.MatchBuiltinPrivate)
475478
allowList.AppendBuiltin(hostmatcher.MatchBuiltinLoopback)
476479
}
480+
// TODO: at the moment, if ALLOW_LOCALNETWORKS=false, ALLOWED_DOMAINS=domain.com, and domain.com has IP 127.0.0.1, then it's still allowed.
481+
// if we want to block such case, the private&loopback should be added to the blockList when ALLOW_LOCALNETWORKS=false
477482
return nil
478483
}

services/migrations/migrate_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package migrations
66

77
import (
8+
"net"
89
"path/filepath"
910
"testing"
1011

@@ -74,3 +75,42 @@ func TestMigrateWhiteBlocklist(t *testing.T) {
7475

7576
setting.ImportLocalPaths = old
7677
}
78+
79+
func TestAllowBlockList(t *testing.T) {
80+
init := func(allow, block string, local bool) {
81+
setting.Migrations.AllowedDomains = allow
82+
setting.Migrations.BlockedDomains = block
83+
setting.Migrations.AllowLocalNetworks = local
84+
assert.NoError(t, Init())
85+
}
86+
87+
// default, allow all external, block none, no local networks
88+
init("", "", false)
89+
assert.NoError(t, checkByAllowBlockList("domain.com", []net.IP{net.ParseIP("1.2.3.4")}))
90+
assert.Error(t, checkByAllowBlockList("domain.com", []net.IP{net.ParseIP("127.0.0.1")}))
91+
92+
// allow all including local networks (it could lead to SSRF in production)
93+
init("", "", true)
94+
assert.NoError(t, checkByAllowBlockList("domain.com", []net.IP{net.ParseIP("1.2.3.4")}))
95+
assert.NoError(t, checkByAllowBlockList("domain.com", []net.IP{net.ParseIP("127.0.0.1")}))
96+
97+
// allow wildcard, block some subdomains. if the domain name is allowed, then the local network check is skipped
98+
init("*.domain.com", "blocked.domain.com", false)
99+
assert.NoError(t, checkByAllowBlockList("sub.domain.com", []net.IP{net.ParseIP("1.2.3.4")}))
100+
assert.NoError(t, checkByAllowBlockList("sub.domain.com", []net.IP{net.ParseIP("127.0.0.1")}))
101+
assert.Error(t, checkByAllowBlockList("blocked.domain.com", []net.IP{net.ParseIP("1.2.3.4")}))
102+
assert.Error(t, checkByAllowBlockList("sub.other.com", []net.IP{net.ParseIP("1.2.3.4")}))
103+
104+
// allow wildcard (it could lead to SSRF in production)
105+
init("*", "", false)
106+
assert.NoError(t, checkByAllowBlockList("domain.com", []net.IP{net.ParseIP("1.2.3.4")}))
107+
assert.NoError(t, checkByAllowBlockList("domain.com", []net.IP{net.ParseIP("127.0.0.1")}))
108+
109+
// local network can still be blocked
110+
init("*", "127.0.0.*", false)
111+
assert.NoError(t, checkByAllowBlockList("domain.com", []net.IP{net.ParseIP("1.2.3.4")}))
112+
assert.Error(t, checkByAllowBlockList("domain.com", []net.IP{net.ParseIP("127.0.0.1")}))
113+
114+
// reset
115+
init("", "", false)
116+
}

0 commit comments

Comments
 (0)