Skip to content

Commit 89d5292

Browse files
authored
Fix token generation when using INTERNAL_TOKEN_URI (go-gitea#21669) (go-gitea#21670)
Backport go-gitea#21669 Fix go-gitea#21666 Caused by go-gitea#19663 Before: when install, the INTERNAL_TOKEN was always generated and saved. But the internal token may be already there by INTERNAL_TOKEN_URI After: INTERNAL_TOKEN_URI file must be non-empty. When install, skip internal token generation if the token exists.
1 parent 3a0d000 commit 89d5292

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

modules/setting/setting.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,8 @@ func parseAuthorizedPrincipalsAllow(values []string) ([]string, bool) {
11561156
return authorizedPrincipalsAllow, true
11571157
}
11581158

1159+
// loadSecret load the secret from ini by uriKey or verbatimKey, only one of them could be set
1160+
// If the secret is loaded from uriKey (file), the file should be non-empty, to guarantee the behavior stable and clear.
11591161
func loadSecret(sec *ini.Section, uriKey, verbatimKey string) string {
11601162
// don't allow setting both URI and verbatim string
11611163
uri := sec.Key(uriKey).String()
@@ -1179,7 +1181,15 @@ func loadSecret(sec *ini.Section, uriKey, verbatimKey string) string {
11791181
if err != nil {
11801182
log.Fatal("Failed to read %s (%s): %v", uriKey, tempURI.RequestURI(), err)
11811183
}
1182-
return strings.TrimSpace(string(buf))
1184+
val := strings.TrimSpace(string(buf))
1185+
if val == "" {
1186+
// The file shouldn't be empty, otherwise we can not know whether the user has ever set the KEY or KEY_URI
1187+
// For example: if INTERNAL_TOKEN_URI=file:///empty-file,
1188+
// Then if the token is re-generated during installation and saved to INTERNAL_TOKEN
1189+
// Then INTERNAL_TOKEN and INTERNAL_TOKEN_URI both exist, that's a fatal error (they shouldn't)
1190+
log.Fatal("Failed to read %s (%s): the file is empty", uriKey, tempURI.RequestURI())
1191+
}
1192+
return val
11831193

11841194
// only file URIs are allowed
11851195
default:

routers/install/install.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -473,12 +473,16 @@ func SubmitInstall(ctx *context.Context) {
473473

474474
cfg.Section("security").Key("INSTALL_LOCK").SetValue("true")
475475

476-
var internalToken string
477-
if internalToken, err = generate.NewInternalToken(); err != nil {
478-
ctx.RenderWithErr(ctx.Tr("install.internal_token_failed", err), tplInstall, &form)
479-
return
476+
// the internal token could be read from INTERNAL_TOKEN or INTERNAL_TOKEN_URI (the file is guaranteed to be non-empty)
477+
// if there is no InternalToken, generate one and save to security.INTERNAL_TOKEN
478+
if setting.InternalToken == "" {
479+
var internalToken string
480+
if internalToken, err = generate.NewInternalToken(); err != nil {
481+
ctx.RenderWithErr(ctx.Tr("install.internal_token_failed", err), tplInstall, &form)
482+
return
483+
}
484+
cfg.Section("security").Key("INTERNAL_TOKEN").SetValue(internalToken)
480485
}
481-
cfg.Section("security").Key("INTERNAL_TOKEN").SetValue(internalToken)
482486

483487
// if there is already a SECRET_KEY, we should not overwrite it, otherwise the encrypted data will not be able to be decrypted
484488
if setting.SecretKey == "" {

0 commit comments

Comments
 (0)