1
0
Эх сурвалжийг харах

conf: add allowlist for accessing local network (#6842)

Joe Chen 3 жил өмнө
parent
commit
714383a063

+ 1 - 0
CHANGELOG.md

@@ -22,6 +22,7 @@ All notable changes to Gogs are documented in this file.
 ### Fixed
 ### Fixed
 
 
 - Unable to use LDAP authentication on ARM machines. [#6761](https://github.com/gogs/gogs/issues/6761)
 - Unable to use LDAP authentication on ARM machines. [#6761](https://github.com/gogs/gogs/issues/6761)
+- _Regression:_ Unable to migrate repository from other local Git hosting. Added a new configuration option `[security] LOCAL_NETWORK_ALLOWLIST`, which is a comma separated list of hostnames that are explicitly allowed to be accessed within the local network. [#6841](https://github.com/gogs/gogs/issues/6841)
 
 
 ### Removed
 ### Removed
 
 

+ 2 - 0
conf/app.ini

@@ -173,6 +173,8 @@ COOKIE_SECURE = false
 ENABLE_LOGIN_STATUS_COOKIE = false
 ENABLE_LOGIN_STATUS_COOKIE = false
 ; The cookie name to store user login status.
 ; The cookie name to store user login status.
 LOGIN_STATUS_COOKIE_NAME = login_status
 LOGIN_STATUS_COOKIE_NAME = login_status
+; A comma separated list of hostnames that are explicitly allowed to be accessed within the local network.
+LOCAL_NETWORK_ALLOWLIST =
 
 
 [email]
 [email]
 ; Whether to enable the email service.
 ; Whether to enable the email service.

+ 1 - 0
conf/locale/locale_en-US.ini

@@ -1248,6 +1248,7 @@ config.security.cookie_secure = Enable secure cookie
 config.security.reverse_proxy_auth_user = Reverse proxy authentication header
 config.security.reverse_proxy_auth_user = Reverse proxy authentication header
 config.security.enable_login_status_cookie = Enable login status cookie
 config.security.enable_login_status_cookie = Enable login status cookie
 config.security.login_status_cookie_name = Login status cookie
 config.security.login_status_cookie_name = Login status cookie
+config.security.local_network_allowlist = Local network allowlist
 
 
 config.email_config = Email configuration
 config.email_config = Email configuration
 config.email.enabled = Enabled
 config.email.enabled = Enabled

+ 2 - 0
docs/dev/local_development.md

@@ -154,6 +154,8 @@ task web --watch
 When you are actively working on HTML templates and static files during development, you may want to enable the following configuration to avoid recompiling and restarting Gogs every time you make a change to files under `template/` and `public/` directories:
 When you are actively working on HTML templates and static files during development, you may want to enable the following configuration to avoid recompiling and restarting Gogs every time you make a change to files under `template/` and `public/` directories:
 
 
 ```ini
 ```ini
+RUN_MODE = dev
+
 [server]
 [server]
 LOAD_ASSETS_FROM_DISK = true
 LOAD_ASSETS_FROM_DISK = true
 ```
 ```

Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 2 - 2
internal/assets/conf/conf_gen.go


Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 2 - 2
internal/assets/templates/templates_gen.go


+ 1 - 0
internal/conf/static.go

@@ -101,6 +101,7 @@ var (
 		CookieSecure            bool
 		CookieSecure            bool
 		EnableLoginStatusCookie bool
 		EnableLoginStatusCookie bool
 		LoginStatusCookieName   string
 		LoginStatusCookieName   string
+		LocalNetworkAllowlist   []string `delim:","`
 	}
 	}
 
 
 	// Email settings
 	// Email settings

+ 1 - 0
internal/conf/testdata/TestInit.golden.ini

@@ -77,6 +77,7 @@ COOKIE_USERNAME=gogs_awesome
 COOKIE_SECURE=false
 COOKIE_SECURE=false
 ENABLE_LOGIN_STATUS_COOKIE=false
 ENABLE_LOGIN_STATUS_COOKIE=false
 LOGIN_STATUS_COOKIE_NAME=login_status
 LOGIN_STATUS_COOKIE_NAME=login_status
+LOCAL_NETWORK_ALLOWLIST=
 
 
 [email]
 [email]
 ENABLED=true
 ENABLED=true

+ 2 - 1
internal/form/repo.go

@@ -12,6 +12,7 @@ import (
 	"github.com/unknwon/com"
 	"github.com/unknwon/com"
 	"gopkg.in/macaron.v1"
 	"gopkg.in/macaron.v1"
 
 
+	"gogs.io/gogs/internal/conf"
 	"gogs.io/gogs/internal/db"
 	"gogs.io/gogs/internal/db"
 	"gogs.io/gogs/internal/netutil"
 	"gogs.io/gogs/internal/netutil"
 )
 )
@@ -71,7 +72,7 @@ func (f MigrateRepo) ParseRemoteAddr(user *db.User) (string, error) {
 			return "", db.ErrInvalidCloneAddr{IsURLError: true}
 			return "", db.ErrInvalidCloneAddr{IsURLError: true}
 		}
 		}
 
 
-		if netutil.IsLocalHostname(u.Hostname()) {
+		if netutil.IsLocalHostname(u.Hostname(), conf.Security.LocalNetworkAllowlist) {
 			return "", db.ErrInvalidCloneAddr{IsURLError: true}
 			return "", db.ErrInvalidCloneAddr{IsURLError: true}
 		}
 		}
 
 

+ 9 - 2
internal/netutil/netutil.go

@@ -47,8 +47,15 @@ func init() {
 	}
 	}
 }
 }
 
 
-// IsLocalHostname returns true if given hostname is a known local address.
-func IsLocalHostname(hostname string) bool {
+// IsLocalHostname returns true if given hostname is resolved to local network
+// address, except exempted from the allowlist.
+func IsLocalHostname(hostname string, allowlist []string) bool {
+	for _, allow := range allowlist {
+		if hostname == allow {
+			return false
+		}
+	}
+
 	ips, err := net.LookupIP(hostname)
 	ips, err := net.LookupIP(hostname)
 	if err != nil {
 	if err != nil {
 		return true
 		return true

+ 7 - 3
internal/netutil/netutil_test.go

@@ -12,8 +12,9 @@ import (
 
 
 func TestIsLocalHostname(t *testing.T) {
 func TestIsLocalHostname(t *testing.T) {
 	tests := []struct {
 	tests := []struct {
-		hostname string
-		want     bool
+		hostname  string
+		allowlist []string
+		want      bool
 	}{
 	}{
 		{hostname: "localhost", want: true},
 		{hostname: "localhost", want: true},
 		{hostname: "127.0.0.1", want: true},
 		{hostname: "127.0.0.1", want: true},
@@ -27,10 +28,13 @@ func TestIsLocalHostname(t *testing.T) {
 		{hostname: "gogs.io", want: false},
 		{hostname: "gogs.io", want: false},
 		{hostname: "google.com", want: false},
 		{hostname: "google.com", want: false},
 		{hostname: "165.232.140.255", want: false},
 		{hostname: "165.232.140.255", want: false},
+
+		{hostname: "192.168.123.45", allowlist: []string{"10.0.0.17"}, want: true},
+		{hostname: "gogs.local", allowlist: []string{"gogs.local"}, want: false},
 	}
 	}
 	for _, test := range tests {
 	for _, test := range tests {
 		t.Run("", func(t *testing.T) {
 		t.Run("", func(t *testing.T) {
-			assert.Equal(t, test.want, IsLocalHostname(test.hostname))
+			assert.Equal(t, test.want, IsLocalHostname(test.hostname, test.allowlist))
 		})
 		})
 	}
 	}
 }
 }

+ 1 - 1
internal/route/repo/webhook.go

@@ -128,7 +128,7 @@ func validateWebhook(actor *db.User, l macaron.Locale, w *db.Webhook) (field, ms
 			return "PayloadURL", l.Tr("repo.settings.webhook.err_cannot_parse_payload_url", err), false
 			return "PayloadURL", l.Tr("repo.settings.webhook.err_cannot_parse_payload_url", err), false
 		}
 		}
 
 
-		if netutil.IsLocalHostname(payloadURL.Hostname()) {
+		if netutil.IsLocalHostname(payloadURL.Hostname(), conf.Security.LocalNetworkAllowlist) {
 			return "PayloadURL", l.Tr("repo.settings.webhook.err_cannot_use_local_addresses"), false
 			return "PayloadURL", l.Tr("repo.settings.webhook.err_cannot_use_local_addresses"), false
 		}
 		}
 	}
 	}

+ 2 - 0
templates/admin/config.tmpl

@@ -210,6 +210,8 @@
 						<dd><i class="fa fa{{if .Security.EnableLoginStatusCookie}}-check{{end}}-square-o"></i></dd>
 						<dd><i class="fa fa{{if .Security.EnableLoginStatusCookie}}-check{{end}}-square-o"></i></dd>
 						<dt>{{.i18n.Tr "admin.config.security.login_status_cookie_name"}}</dt>
 						<dt>{{.i18n.Tr "admin.config.security.login_status_cookie_name"}}</dt>
 						<dd>{{.Security.LoginStatusCookieName}}</dd>
 						<dd>{{.Security.LoginStatusCookieName}}</dd>
+						<dt>{{.i18n.Tr "admin.config.security.local_network_allowlist"}}</dt>
+						<dd><code>{{.Security.LocalNetworkAllowlist}}</code></dd>
 					</dl>
 					</dl>
 				</div>
 				</div>
 
 

Энэ ялгаанд хэт олон файл өөрчлөгдсөн тул зарим файлыг харуулаагүй болно