Skip to content

Commit 4d1e2b8

Browse files
authored
Fix token generation when using INTERNAL_TOKEN_URI (#21669)
Fix #21666 Caused by #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 3e86189 commit 4d1e2b8

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
@@ -1158,6 +1158,8 @@ func parseAuthorizedPrincipalsAllow(values []string) ([]string, bool) {
11581158
return authorizedPrincipalsAllow, true
11591159
}
11601160

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

11861196
// only file URIs are allowed
11871197
default:

routers/install/install.go

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

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

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

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

0 commit comments

Comments
 (0)