diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2023-08-12 23:28:35 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-12 15:28:35 +0000 |
commit | bcccf4c0d6149e5e7382226a191abd54848f9416 (patch) | |
tree | b837d0ccd708f80dd33ce69e54ac87394bc7e0b9 /modules/setting | |
parent | 2eb456dde21b51be4cfeb185193cdcfd2a66f315 (diff) | |
download | gitea-bcccf4c0d6149e5e7382226a191abd54848f9416.tar.gz gitea-bcccf4c0d6149e5e7382226a191abd54848f9416.zip |
Remove last newline from config file (#26468)
When users put the secrets into a file (GITEA__sec__KEY__FILE), the
newline sometimes is different to avoid (eg: echo/vim/...)
So the last newline could be removed when reading, it makes the users
easier to maintain the secret files.
Co-authored-by: Giteabot <teabot@gitea.io>
Diffstat (limited to 'modules/setting')
-rw-r--r-- | modules/setting/config_env.go | 6 | ||||
-rw-r--r-- | modules/setting/config_env_test.go | 15 |
2 files changed, 21 insertions, 0 deletions
diff --git a/modules/setting/config_env.go b/modules/setting/config_env.go index bd479253dc..b30e44de30 100644 --- a/modules/setting/config_env.go +++ b/modules/setting/config_env.go @@ -4,6 +4,7 @@ package setting import ( + "bytes" "os" "regexp" "strconv" @@ -131,6 +132,11 @@ func EnvironmentToConfig(cfg ConfigProvider, envs []string) (changed bool) { log.Error("Error reading file for %s : %v", envKey, envValue, err) continue } + if bytes.HasSuffix(fileContent, []byte("\r\n")) { + fileContent = fileContent[:len(fileContent)-2] + } else if bytes.HasSuffix(fileContent, []byte("\n")) { + fileContent = fileContent[:len(fileContent)-1] + } keyValue = string(fileContent) } diff --git a/modules/setting/config_env_test.go b/modules/setting/config_env_test.go index edd23a24aa..e14d5ecb41 100644 --- a/modules/setting/config_env_test.go +++ b/modules/setting/config_env_test.go @@ -99,4 +99,19 @@ key = old changed = EnvironmentToConfig(cfg, []string{"GITEA__sec__key__FILE=" + tmpFile}) assert.True(t, changed) assert.Equal(t, "value-from-file", cfg.Section("sec").Key("key").String()) + + cfg, _ = NewConfigProviderFromData("") + _ = os.WriteFile(tmpFile, []byte("value-from-file\n"), 0o644) + EnvironmentToConfig(cfg, []string{"GITEA__sec__key__FILE=" + tmpFile}) + assert.Equal(t, "value-from-file", cfg.Section("sec").Key("key").String()) + + cfg, _ = NewConfigProviderFromData("") + _ = os.WriteFile(tmpFile, []byte("value-from-file\r\n"), 0o644) + EnvironmentToConfig(cfg, []string{"GITEA__sec__key__FILE=" + tmpFile}) + assert.Equal(t, "value-from-file", cfg.Section("sec").Key("key").String()) + + cfg, _ = NewConfigProviderFromData("") + _ = os.WriteFile(tmpFile, []byte("value-from-file\n\n"), 0o644) + EnvironmentToConfig(cfg, []string{"GITEA__sec__key__FILE=" + tmpFile}) + assert.Equal(t, "value-from-file\n", cfg.Section("sec").Key("key").String()) } |