@@ -921,9 +921,16 @@ func loadFromConf(allowEmpty bool, extraConfig string) {
921
921
922
922
sec = Cfg .Section ("security" )
923
923
InstallLock = sec .Key ("INSTALL_LOCK" ).MustBool (false )
924
- SecretKey = sec .Key ("SECRET_KEY" ).MustString ("!#@FDEWREWR&*(" )
925
924
LogInRememberDays = sec .Key ("LOGIN_REMEMBER_DAYS" ).MustInt (7 )
926
925
CookieUserName = sec .Key ("COOKIE_USERNAME" ).MustString ("gitea_awesome" )
926
+ SecretKey = loadSecret (sec , "SECRET_KEY_URI" , "SECRET_KEY" , func () (string , error ) {
927
+ // FIXME: https://github.com/go-gitea/gitea/issues/16832
928
+ //
929
+ // Until we properly support rotating an existing secret key,
930
+ // we shouldn't move users off of the default value
931
+ return "!#@FDEWREWR&*(" , nil
932
+ })
933
+
927
934
CookieRememberName = sec .Key ("COOKIE_REMEMBER_NAME" ).MustString ("gitea_incredible" )
928
935
929
936
ReverseProxyAuthUser = sec .Key ("REVERSE_PROXY_AUTHENTICATION_USER" ).MustString ("X-WEBAUTH-USER" )
@@ -946,11 +953,7 @@ func loadFromConf(allowEmpty bool, extraConfig string) {
946
953
PasswordCheckPwn = sec .Key ("PASSWORD_CHECK_PWN" ).MustBool (false )
947
954
SuccessfulTokensCacheSize = sec .Key ("SUCCESSFUL_TOKENS_CACHE_SIZE" ).MustInt (20 )
948
955
949
- InternalToken = loadInternalToken (sec )
950
- if InstallLock && InternalToken == "" {
951
- // if Gitea has been installed but the InternalToken hasn't been generated (upgrade from an old release), we should generate
952
- generateSaveInternalToken ()
953
- }
956
+ InternalToken = loadSecret (sec , "INTERNAL_TOKEN_URI" , "INTERNAL_TOKEN" , generate .NewInternalToken )
954
957
955
958
cfgdata := sec .Key ("PASSWORD_COMPLEXITY" ).Strings ("," )
956
959
if len (cfgdata ) == 0 {
@@ -1139,51 +1142,73 @@ func parseAuthorizedPrincipalsAllow(values []string) ([]string, bool) {
1139
1142
return authorizedPrincipalsAllow , true
1140
1143
}
1141
1144
1142
- func loadInternalToken (sec * ini.Section ) string {
1143
- uri := sec .Key ("INTERNAL_TOKEN_URI" ).String ()
1145
+ func loadSecret (
1146
+ sec * ini.Section ,
1147
+ uriKey string ,
1148
+ verbatimKey string ,
1149
+ generator func () (string , error ),
1150
+ ) string {
1151
+ // don't allow setting both URI and verbatim string
1152
+ uri := sec .Key (uriKey ).String ()
1153
+ verbatim := sec .Key (verbatimKey ).String ()
1154
+ if uri != "" && verbatim != "" {
1155
+ log .Fatal ("Cannot specify both %s and %s" , uriKey , verbatimKey )
1156
+ }
1157
+
1158
+ // if we have no URI, use verbatim
1144
1159
if uri == "" {
1145
- return sec .Key ("INTERNAL_TOKEN" ).String ()
1160
+ // if verbatim isn't provided, generate one
1161
+ if verbatim == "" {
1162
+ secret , err := generator ()
1163
+ if err != nil {
1164
+ log .Fatal ("Error trying to generate %s: %v" , verbatimKey , err )
1165
+ }
1166
+ CreateOrAppendToCustomConf (func (cfg * ini.File ) {
1167
+ cfg .Section (sec .Name ()).Key (verbatimKey ).SetValue (secret )
1168
+ })
1169
+ return secret
1170
+ }
1171
+
1172
+ return verbatim
1146
1173
}
1174
+
1147
1175
tempURI , err := url .Parse (uri )
1148
1176
if err != nil {
1149
- log .Fatal ("Failed to parse INTERNAL_TOKEN_URI (%s): %v" , uri , err )
1177
+ log .Fatal ("Failed to parse %s (%s): %v" , uriKey , uri , err )
1150
1178
}
1151
1179
switch tempURI .Scheme {
1152
1180
case "file" :
1153
1181
buf , err := os .ReadFile (tempURI .RequestURI ())
1154
1182
if err != nil && ! os .IsNotExist (err ) {
1155
- log .Fatal ("Failed to open InternalTokenURI (%s): %v" , uri , err )
1183
+ log .Fatal ("Failed to open %s (%s): %v" , uriKey , uri , err )
1156
1184
}
1157
- // No token in the file, generate one and store it.
1185
+
1186
+ // empty file; generate secret and store it
1158
1187
if len (buf ) == 0 {
1159
- token , err := generate . NewInternalToken ()
1188
+ token , err := generator ()
1160
1189
if err != nil {
1161
- log .Fatal ("Error generate internal token : %v" , err )
1190
+ log .Fatal ("Error generating %s : %v" , verbatimKey , err )
1162
1191
}
1192
+
1163
1193
err = os .WriteFile (tempURI .RequestURI (), []byte (token ), 0o600 )
1164
1194
if err != nil {
1165
- log .Fatal ("Error writing to InternalTokenURI (%s): %v" , uri , err )
1195
+ log .Fatal ("Error writing to %s (%s): %v" , uriKey , uri , err )
1166
1196
}
1197
+
1198
+ // we assume generator gives pre-parsed token
1167
1199
return token
1168
1200
}
1201
+
1169
1202
return strings .TrimSpace (string (buf ))
1203
+
1204
+ // only file URIs are allowed
1170
1205
default :
1171
1206
log .Fatal ("Unsupported URI-Scheme %q (INTERNAL_TOKEN_URI = %q)" , tempURI .Scheme , uri )
1172
1207
}
1173
- return ""
1174
- }
1175
1208
1176
- // generateSaveInternalToken generates and saves the internal token to app.ini
1177
- func generateSaveInternalToken () {
1178
- token , err := generate .NewInternalToken ()
1179
- if err != nil {
1180
- log .Fatal ("Error generate internal token: %v" , err )
1181
- }
1182
-
1183
- InternalToken = token
1184
- CreateOrAppendToCustomConf (func (cfg * ini.File ) {
1185
- cfg .Section ("security" ).Key ("INTERNAL_TOKEN" ).SetValue (token )
1186
- })
1209
+ // we should never get here
1210
+ log .Fatal ("Unknown error when loading %s" , verbatimKey )
1211
+ return ""
1187
1212
}
1188
1213
1189
1214
// MakeAbsoluteAssetURL returns the absolute asset url prefix without a trailing slash
@@ -1248,6 +1273,11 @@ func MakeManifestData(appName, appURL, absoluteAssetURL string) []byte {
1248
1273
// CreateOrAppendToCustomConf creates or updates the custom config.
1249
1274
// Use the callback to set individual values.
1250
1275
func CreateOrAppendToCustomConf (callback func (cfg * ini.File )) {
1276
+ if CustomConf == "" {
1277
+ log .Error ("Custom config path must not be empty" )
1278
+ return
1279
+ }
1280
+
1251
1281
cfg := ini .Empty ()
1252
1282
isFile , err := util .IsFile (CustomConf )
1253
1283
if err != nil {
@@ -1262,15 +1292,14 @@ func CreateOrAppendToCustomConf(callback func(cfg *ini.File)) {
1262
1292
1263
1293
callback (cfg )
1264
1294
1265
- log .Info ("Settings saved to: %q" , CustomConf )
1266
-
1267
1295
if err := os .MkdirAll (filepath .Dir (CustomConf ), os .ModePerm ); err != nil {
1268
1296
log .Fatal ("failed to create '%s': %v" , CustomConf , err )
1269
1297
return
1270
1298
}
1271
1299
if err := cfg .SaveTo (CustomConf ); err != nil {
1272
1300
log .Fatal ("error saving to custom config: %v" , err )
1273
1301
}
1302
+ log .Info ("Settings saved to: %q" , CustomConf )
1274
1303
1275
1304
// Change permissions to be more restrictive
1276
1305
fi , err := os .Stat (CustomConf )
0 commit comments