summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2023-10-10 01:10:37 +0800
committerGitHub <noreply@github.com>2023-10-10 01:10:37 +0800
commite2e0280108cd0cfa4aaa7a875b2c81f17b1352e1 (patch)
tree218d26d6906b32b385bdc78cfa53d950cdcf3fe6 /modules
parent28ead9ea62de9640b034b8d00c57b88a85a5665b (diff)
downloadgitea-e2e0280108cd0cfa4aaa7a875b2c81f17b1352e1.tar.gz
gitea-e2e0280108cd0cfa4aaa7a875b2c81f17b1352e1.zip
Fix `environment-to-ini` inherited key bug (#27543)
Fix #27541 The INI package has a quirk: by default, the keys are inherited. When maintaining the keys, the newly added sub key should not be affected by the parent key.
Diffstat (limited to 'modules')
-rw-r--r--modules/setting/config_env.go3
-rw-r--r--modules/setting/config_env_test.go26
2 files changed, 28 insertions, 1 deletions
diff --git a/modules/setting/config_env.go b/modules/setting/config_env.go
index b30e44de30..242f40914a 100644
--- a/modules/setting/config_env.go
+++ b/modules/setting/config_env.go
@@ -149,8 +149,9 @@ func EnvironmentToConfig(cfg ConfigProvider, envs []string) (changed bool) {
continue
}
}
- key := section.Key(keyName)
+ key := ConfigSectionKey(section, keyName)
if key == nil {
+ changed = true
key, err = section.NewKey(keyName, keyValue)
if err != nil {
log.Error("Error creating key: %s in section: %s with value: %s : %v", keyName, sectionName, keyValue, err)
diff --git a/modules/setting/config_env_test.go b/modules/setting/config_env_test.go
index e14d5ecb41..7d07c479a1 100644
--- a/modules/setting/config_env_test.go
+++ b/modules/setting/config_env_test.go
@@ -115,3 +115,29 @@ key = old
EnvironmentToConfig(cfg, []string{"GITEA__sec__key__FILE=" + tmpFile})
assert.Equal(t, "value-from-file\n", cfg.Section("sec").Key("key").String())
}
+
+func TestEnvironmentToConfigSubSecKey(t *testing.T) {
+ // the INI package has a quirk: by default, the keys are inherited.
+ // when maintaining the keys, the newly added sub key should not be affected by the parent key.
+ cfg, err := NewConfigProviderFromData(`
+[sec]
+key = some
+`)
+ assert.NoError(t, err)
+
+ changed := EnvironmentToConfig(cfg, []string{"GITEA__sec_0X2E_sub__key=some"})
+ assert.True(t, changed)
+
+ tmpFile := t.TempDir() + "/test-sub-sec-key.ini"
+ defer os.Remove(tmpFile)
+ err = cfg.SaveTo(tmpFile)
+ assert.NoError(t, err)
+ bs, err := os.ReadFile(tmpFile)
+ assert.NoError(t, err)
+ assert.Equal(t, `[sec]
+key = some
+
+[sec.sub]
+key = some
+`, string(bs))
+}