Skip to content

Commit 0c23771

Browse files
committed
avoid writing incorrect config files
1 parent c2f0a01 commit 0c23771

File tree

3 files changed

+12
-47
lines changed

3 files changed

+12
-47
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ Certain queues have defaults that override the defaults set in `[queue]` (this o
495495

496496
- `INSTALL_LOCK`: **false**: Controls access to the installation page. When set to "true", the installation page is not accessible.
497497
- `SECRET_KEY`: **\<random at every install\>**: Global secret key. This key is VERY IMPORTANT, if you lost it, the data encrypted by it (like 2FA secret) can't be decrypted anymore.
498-
- `SECRET_KEY_URI`: **<empty>**: Instead of defining SECRET_KEY, this option can be used to use the key stored in a file (example value: `file:/etc/gitea/secret_token`). It shouldn't be lost like SECRET_KEY.
498+
- `SECRET_KEY_URI`: **<empty>**: Instead of defining SECRET_KEY, this option can be used to use the key stored in a file (example value: `file:/etc/gitea/secret_key`). It shouldn't be lost like SECRET_KEY.
499499
- `LOGIN_REMEMBER_DAYS`: **7**: Cookie lifetime, in days.
500500
- `COOKIE_USERNAME`: **gitea\_awesome**: Name of the cookie used to store the current username.
501501
- `COOKIE_REMEMBER_NAME`: **gitea\_incredible**: Name of cookie used to store authentication

modules/setting/setting.go

+6-46
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"text/template"
2222
"time"
2323

24-
"code.gitea.io/gitea/modules/generate"
2524
"code.gitea.io/gitea/modules/json"
2625
"code.gitea.io/gitea/modules/log"
2726
"code.gitea.io/gitea/modules/user"
@@ -925,7 +924,7 @@ func loadFromConf(allowEmpty bool, extraConfig string) {
925924
InstallLock = sec.Key("INSTALL_LOCK").MustBool(false)
926925
LogInRememberDays = sec.Key("LOGIN_REMEMBER_DAYS").MustInt(7)
927926
CookieUserName = sec.Key("COOKIE_USERNAME").MustString("gitea_awesome")
928-
SecretKey = loadOrGenerateSecret(sec, "SECRET_KEY_URI", "SECRET_KEY", nil)
927+
SecretKey = loadSecret(sec, "SECRET_KEY_URI", "SECRET_KEY")
929928
if SecretKey == "" {
930929
// FIXME: https://github.com/go-gitea/gitea/issues/16832
931930
// Until it supports rotating an existing secret key, we shouldn't move users off of the widely used default value
@@ -954,7 +953,7 @@ func loadFromConf(allowEmpty bool, extraConfig string) {
954953
PasswordCheckPwn = sec.Key("PASSWORD_CHECK_PWN").MustBool(false)
955954
SuccessfulTokensCacheSize = sec.Key("SUCCESSFUL_TOKENS_CACHE_SIZE").MustInt(20)
956955

957-
InternalToken = loadOrGenerateSecret(sec, "INTERNAL_TOKEN_URI", "INTERNAL_TOKEN", generate.NewInternalToken)
956+
InternalToken = loadSecret(sec, "INTERNAL_TOKEN_URI", "INTERNAL_TOKEN")
958957

959958
cfgdata := sec.Key("PASSWORD_COMPLEXITY").Strings(",")
960959
if len(cfgdata) == 0 {
@@ -1143,14 +1142,7 @@ func parseAuthorizedPrincipalsAllow(values []string) ([]string, bool) {
11431142
return authorizedPrincipalsAllow, true
11441143
}
11451144

1146-
// loadOrGenerateSecret loads the secret if it exists in the config file,
1147-
// or generates a new one and saves it into the config file
1148-
func loadOrGenerateSecret(
1149-
sec *ini.Section,
1150-
uriKey string,
1151-
verbatimKey string,
1152-
generator func() (string, error),
1153-
) string {
1145+
func loadSecret(sec *ini.Section, uriKey, verbatimKey string) string {
11541146
// don't allow setting both URI and verbatim string
11551147
uri := sec.Key(uriKey).String()
11561148
verbatim := sec.Key(verbatimKey).String()
@@ -1160,18 +1152,6 @@ func loadOrGenerateSecret(
11601152

11611153
// if we have no URI, use verbatim
11621154
if uri == "" {
1163-
// if verbatim isn't provided, generate one
1164-
if verbatim == "" && generator != nil {
1165-
secret, err := generator()
1166-
if err != nil {
1167-
log.Fatal("Error trying to generate %s: %v", verbatimKey, err)
1168-
}
1169-
CreateOrAppendToCustomConf(sec.Name()+"."+verbatimKey, func(cfg *ini.File) {
1170-
cfg.Section(sec.Name()).Key(verbatimKey).SetValue(secret)
1171-
})
1172-
return secret
1173-
}
1174-
11751155
return verbatim
11761156
}
11771157

@@ -1182,36 +1162,16 @@ func loadOrGenerateSecret(
11821162
switch tempURI.Scheme {
11831163
case "file":
11841164
buf, err := os.ReadFile(tempURI.RequestURI())
1185-
if err != nil && !os.IsNotExist(err) {
1186-
log.Fatal("Failed to open %s (%s): %v", uriKey, uri, err)
1187-
}
1188-
1189-
// empty file; generate secret and store it
1190-
if len(buf) == 0 && generator != nil {
1191-
token, err := generator()
1192-
if err != nil {
1193-
log.Fatal("Error generating %s: %v", verbatimKey, err)
1194-
}
1195-
1196-
err = os.WriteFile(tempURI.RequestURI(), []byte(token), 0o600)
1197-
if err != nil {
1198-
log.Fatal("Error writing to %s (%s): %v", uriKey, uri, err)
1199-
}
1200-
1201-
// we assume generator gives pre-parsed token
1202-
return token
1165+
if err != nil {
1166+
log.Fatal("Failed to read %s (%s): %v", uriKey, tempURI.RequestURI(), err)
12031167
}
1204-
12051168
return strings.TrimSpace(string(buf))
12061169

12071170
// only file URIs are allowed
12081171
default:
12091172
log.Fatal("Unsupported URI-Scheme %q (INTERNAL_TOKEN_URI = %q)", tempURI.Scheme, uri)
1173+
return ""
12101174
}
1211-
1212-
// we should never get here
1213-
log.Fatal("Unknown error when loading %s", verbatimKey)
1214-
return ""
12151175
}
12161176

12171177
// MakeAbsoluteAssetURL returns the absolute asset url prefix without a trailing slash

routers/private/internal.go

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ func CheckInternalToken(next http.Handler) http.Handler {
2424
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
2525
tokens := req.Header.Get("Authorization")
2626
fields := strings.SplitN(tokens, " ", 2)
27+
if setting.InternalToken == "" {
28+
log.Warn(`The INTERNAL_TOKEN setting is missing from the configuration file: %q, internal API can't work.`, setting.CustomConf)
29+
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
30+
return
31+
}
2732
if len(fields) != 2 || fields[0] != "Bearer" || fields[1] != setting.InternalToken {
2833
log.Debug("Forbidden attempt to access internal url: Authorization header: %s", tokens)
2934
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)

0 commit comments

Comments
 (0)