Skip to content

Commit dd1beee

Browse files
zeripathsapk
andauthored
Enforce Gitea environment for pushes (#8982)
* Enforce Gitea environment for pushes * Update custom/conf/app.ini.sample Co-Authored-By: Antoine GIRARD <[email protected]>
1 parent 3621944 commit dd1beee

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed

cmd/hook.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"code.gitea.io/gitea/models"
1717
"code.gitea.io/gitea/modules/git"
1818
"code.gitea.io/gitea/modules/private"
19+
"code.gitea.io/gitea/modules/setting"
1920

2021
"github.com/urfave/cli"
2122
)
@@ -55,7 +56,13 @@ var (
5556

5657
func runHookPreReceive(c *cli.Context) error {
5758
if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 {
58-
return nil
59+
if setting.OnlyAllowPushIfGiteaEnvironmentSet {
60+
fail(`Rejecting changes as Gitea environment not set.
61+
If you are pushing over SSH you must push with a key managed by
62+
Gitea or set your environment appropriately.`, "")
63+
} else {
64+
return nil
65+
}
5966
}
6067

6168
setup("hooks/pre-receive.log")
@@ -115,7 +122,13 @@ func runHookPreReceive(c *cli.Context) error {
115122

116123
func runHookUpdate(c *cli.Context) error {
117124
if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 {
118-
return nil
125+
if setting.OnlyAllowPushIfGiteaEnvironmentSet {
126+
fail(`Rejecting changes as Gitea environment not set.
127+
If you are pushing over SSH you must push with a key managed by
128+
Gitea or set your environment appropriately.`, "")
129+
} else {
130+
return nil
131+
}
119132
}
120133

121134
setup("hooks/update.log")
@@ -125,7 +138,13 @@ func runHookUpdate(c *cli.Context) error {
125138

126139
func runHookPostReceive(c *cli.Context) error {
127140
if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 {
128-
return nil
141+
if setting.OnlyAllowPushIfGiteaEnvironmentSet {
142+
fail(`Rejecting changes as Gitea environment not set.
143+
If you are pushing over SSH you must push with a key managed by
144+
Gitea or set your environment appropriately.`, "")
145+
} else {
146+
return nil
147+
}
129148
}
130149

131150
setup("hooks/post-receive.log")

custom/conf/app.ini.sample

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ PROTOCOL = http
190190
DOMAIN = localhost
191191
ROOT_URL = %(PROTOCOL)s://%(DOMAIN)s:%(HTTP_PORT)s/
192192
; when STATIC_URL_PREFIX is empty it will follow APP_URL
193-
STATIC_URL_PREFIX =
193+
STATIC_URL_PREFIX =
194194
; The address to listen on. Either a IPv4/IPv6 address or the path to a unix socket.
195195
HTTP_ADDR = 0.0.0.0
196196
HTTP_PORT = 3000
@@ -383,6 +383,8 @@ MIN_PASSWORD_LENGTH = 6
383383
IMPORT_LOCAL_PATHS = false
384384
; Set to true to prevent all users (including admin) from creating custom git hooks
385385
DISABLE_GIT_HOOKS = false
386+
; Set to false to allow pushes to gitea repositories despite having an incomplete environment - NOT RECOMMENDED
387+
ONLY_ALLOW_PUSH_IF_GITEA_ENVIRONMENT_SET = true
386388
;Comma separated list of character classes required to pass minimum complexity.
387389
;If left empty or no valid values are specified, the default values ("lower,upper,digit,spec") will be used.
388390
;Use "off" to disable checking.
@@ -515,9 +517,9 @@ SKIP_TLS_VERIFY = false
515517
; Number of history information in each page
516518
PAGING_NUM = 10
517519
; Proxy server URL, support http://, https//, socks://, blank will follow environment http_proxy/https_proxy
518-
PROXY_URL =
520+
PROXY_URL =
519521
; Comma separated list of host names requiring proxy. Glob patterns (*) are accepted; use ** to match all hosts.
520-
PROXY_HOSTS =
522+
PROXY_HOSTS =
521523

522524
[mailer]
523525
ENABLED = false

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ relation to port exhaustion.
244244
authentication provided email.
245245
- `DISABLE_GIT_HOOKS`: **false**: Set to `true` to prevent all users (including admin) from creating custom
246246
git hooks.
247+
- `ONLY_ALLOW_PUSH_IF_GITEA_ENVIRONMENT_SET`: **true**: Set to `false` to allow local users to push to gitea-repositories without setting up the Gitea environment. This is not recommended and if you want local users to push to gitea repositories you should set the environment appropriately.
247248
- `IMPORT_LOCAL_PATHS`: **false**: Set to `false` to prevent all users (including admin) from importing local path on server.
248249
- `INTERNAL_TOKEN`: **\<random at every install if no uri set\>**: Secret used to validate communication within Gitea binary.
249250
- `INTERNAL_TOKEN_URI`: **<empty>**: Instead of defining internal token in the configuration, this configuration option can be used to give Gitea a path to a file that contains the internal token (example value: `file:/etc/gitea/internal_token`)

modules/setting/setting.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,19 @@ var (
140140
}
141141

142142
// Security settings
143-
InstallLock bool
144-
SecretKey string
145-
LogInRememberDays int
146-
CookieUserName string
147-
CookieRememberName string
148-
ReverseProxyAuthUser string
149-
ReverseProxyAuthEmail string
150-
MinPasswordLength int
151-
ImportLocalPaths bool
152-
DisableGitHooks bool
153-
PasswordComplexity []string
154-
PasswordHashAlgo string
143+
InstallLock bool
144+
SecretKey string
145+
LogInRememberDays int
146+
CookieUserName string
147+
CookieRememberName string
148+
ReverseProxyAuthUser string
149+
ReverseProxyAuthEmail string
150+
MinPasswordLength int
151+
ImportLocalPaths bool
152+
DisableGitHooks bool
153+
OnlyAllowPushIfGiteaEnvironmentSet bool
154+
PasswordComplexity []string
155+
PasswordHashAlgo string
155156

156157
// UI settings
157158
UI = struct {
@@ -778,6 +779,7 @@ func NewContext() {
778779
MinPasswordLength = sec.Key("MIN_PASSWORD_LENGTH").MustInt(6)
779780
ImportLocalPaths = sec.Key("IMPORT_LOCAL_PATHS").MustBool(false)
780781
DisableGitHooks = sec.Key("DISABLE_GIT_HOOKS").MustBool(false)
782+
OnlyAllowPushIfGiteaEnvironmentSet = sec.Key("ONLY_ALLOW_PUSH_IF_GITEA_ENVIRONMENT_SET").MustBool(true)
781783
PasswordHashAlgo = sec.Key("PASSWORD_HASH_ALGO").MustString("pbkdf2")
782784
CSRFCookieHTTPOnly = sec.Key("CSRF_COOKIE_HTTP_ONLY").MustBool(true)
783785

0 commit comments

Comments
 (0)