diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2022-11-04 04:55:09 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-03 20:55:09 +0000 |
commit | 4d1e2b8334b6f18b82ef4e646dd122200fb0b6c3 (patch) | |
tree | 5206d06ef008bbf7d02c1def0cfd75ddd0eea1d0 /modules | |
parent | 3e8618949e088cf3606a0b2ad4828953936d4247 (diff) | |
download | gitea-4d1e2b8334b6f18b82ef4e646dd122200fb0b6c3.tar.gz gitea-4d1e2b8334b6f18b82ef4e646dd122200fb0b6c3.zip |
Fix token generation when using INTERNAL_TOKEN_URI (#21669)
Fix https://github.com/go-gitea/gitea/issues/21666
Caused by https://github.com/go-gitea/gitea/pull/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.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/setting/setting.go | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 043acb733d..2e5bb17b6a 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -1158,6 +1158,8 @@ func parseAuthorizedPrincipalsAllow(values []string) ([]string, bool) { return authorizedPrincipalsAllow, true } +// loadSecret load the secret from ini by uriKey or verbatimKey, only one of them could be set +// If the secret is loaded from uriKey (file), the file should be non-empty, to guarantee the behavior stable and clear. func loadSecret(sec *ini.Section, uriKey, verbatimKey string) string { // don't allow setting both URI and verbatim string uri := sec.Key(uriKey).String() @@ -1181,7 +1183,15 @@ func loadSecret(sec *ini.Section, uriKey, verbatimKey string) string { if err != nil { log.Fatal("Failed to read %s (%s): %v", uriKey, tempURI.RequestURI(), err) } - return strings.TrimSpace(string(buf)) + val := strings.TrimSpace(string(buf)) + if val == "" { + // The file shouldn't be empty, otherwise we can not know whether the user has ever set the KEY or KEY_URI + // For example: if INTERNAL_TOKEN_URI=file:///empty-file, + // Then if the token is re-generated during installation and saved to INTERNAL_TOKEN + // Then INTERNAL_TOKEN and INTERNAL_TOKEN_URI both exist, that's a fatal error (they shouldn't) + log.Fatal("Failed to read %s (%s): the file is empty", uriKey, tempURI.RequestURI()) + } + return val // only file URIs are allowed default: |