Skip to content

Commit f0bd1e9

Browse files
techknowlogickdelvh6543
authored
Add protection to disable Gitea when run as root (#17168)
Co-authored-by: delvh <dev.lh@web.de> Co-authored-by: 6543 <6543@obermui.de>
1 parent 4afdb1e commit f0bd1e9

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

.drone.yml

+21-3
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,14 @@ steps:
207207
commands:
208208
- git update-ref refs/heads/tag_test ${DRONE_COMMIT_SHA}
209209

210+
- name: fix-permissions
211+
image: gitea/test_env:linux-amd64 # https://gitea.com/gitea/test-env
212+
commands:
213+
- chown -R gitea:gitea .
214+
210215
- name: unit-test
211-
image: golang:1.17
216+
image: gitea/test_env:linux-amd64 # https://gitea.com/gitea/test-env
217+
user: gitea
212218
commands:
213219
- make unit-test-coverage test-check
214220
environment:
@@ -220,7 +226,8 @@ steps:
220226

221227
- name: unit-test-gogit
222228
pull: always
223-
image: golang:1.17
229+
image: gitea/test_env:linux-amd64 # https://gitea.com/gitea/test-env
230+
user: gitea
224231
commands:
225232
- make unit-test-coverage test-check
226233
environment:
@@ -232,6 +239,7 @@ steps:
232239

233240
- name: test-mysql
234241
image: gitea/test_env:linux-amd64 # https://gitea.com/gitea/test-env
242+
user: gitea
235243
commands:
236244
- make test-mysql-migration integration-test-coverage
237245
environment:
@@ -246,6 +254,7 @@ steps:
246254

247255
- name: test-mysql8
248256
image: gitea/test_env:linux-amd64 # https://gitea.com/gitea/test-env
257+
user: gitea
249258
commands:
250259
- timeout -s ABRT 40m make test-mysql8-migration test-mysql8
251260
environment:
@@ -259,6 +268,7 @@ steps:
259268

260269
- name: test-mssql
261270
image: gitea/test_env:linux-amd64 # https://gitea.com/gitea/test-env
271+
user: gitea
262272
commands:
263273
- make test-mssql-migration test-mssql
264274
environment:
@@ -343,9 +353,15 @@ steps:
343353
exclude:
344354
- pull_request
345355

356+
- name: fix-permissions
357+
image: gitea/test_env:linux-arm64 # https://gitea.com/gitea/test-env
358+
commands:
359+
- chown -R gitea:gitea .
360+
346361
- name: build
347362
pull: always
348-
image: golang:1.17
363+
image: gitea/test_env:linux-arm64 # https://gitea.com/gitea/test-env
364+
user: gitea
349365
commands:
350366
- make backend
351367
environment:
@@ -355,6 +371,7 @@ steps:
355371

356372
- name: test-sqlite
357373
image: gitea/test_env:linux-arm64 # https://gitea.com/gitea/test-env
374+
user: gitea
358375
commands:
359376
- timeout -s ABRT 40m make test-sqlite-migration test-sqlite
360377
environment:
@@ -368,6 +385,7 @@ steps:
368385

369386
- name: test-pgsql
370387
image: gitea/test_env:linux-arm64 # https://gitea.com/gitea/test-env
388+
user: gitea
371389
commands:
372390
- timeout -s ABRT 40m make test-pgsql-migration test-pgsql
373391
environment:

modules/setting/setting.go

+12
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,9 @@ func NewContext() {
902902
}
903903

904904
RunUser = Cfg.Section("").Key("RUN_USER").MustString(user.CurrentUsername())
905+
// The following is a purposefully undocumented option. Please do not run Gitea as root. It will only cause future headaches.
906+
// Please don't use root as a bandaid to "fix" something that is broken, instead the broken thing should instead be fixed properly.
907+
unsafeAllowRunAsRoot := Cfg.Section("").Key("I_AM_BEING_UNSAFE_RUNNING_AS_ROOT").MustBool(false)
905908
RunMode = Cfg.Section("").Key("RUN_MODE").MustString("prod")
906909
// Does not check run user when the install lock is off.
907910
if InstallLock {
@@ -911,6 +914,15 @@ func NewContext() {
911914
}
912915
}
913916

917+
// check if we run as root
918+
if os.Getuid() == 0 {
919+
if !unsafeAllowRunAsRoot {
920+
// Special thanks to VLC which inspired the wording of this messaging.
921+
log.Fatal("Gitea is not supposed to be run as root. Sorry. If you need to use privileged TCP ports please instead use setcap and the `cap_net_bind_service` permission")
922+
}
923+
log.Critical("You are running Gitea using the root user, and have purposely chosen to skip built-in protections around this. You have been warned against this.")
924+
}
925+
914926
SSH.BuiltinServerUser = Cfg.Section("server").Key("BUILTIN_SSH_SERVER_USER").MustString(RunUser)
915927

916928
newRepository()

0 commit comments

Comments
 (0)