From d4fa6a846a46481a076794881fc16b2c9f90e4cc Mon Sep 17 00:00:00 2001
From: Giteabot <teabot@gitea.io>
Date: Thu, 20 Jul 2023 05:56:36 -0400
Subject: [PATCH] Fix env config parsing for "GITEA____APP_NAME" (#26001)
 (#26013)

Backport #26001 by @wxiaoguang

Regression of #24832

Fix the bug and add a test for it

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
(cherry picked from commit 4d5e3b937214965e7824f2b5a4fc59a1674ab11c)
---
 modules/setting/config_env.go      | 2 +-
 modules/setting/config_env_test.go | 6 ++++++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/modules/setting/config_env.go b/modules/setting/config_env.go
index e208b2479b..4acb977936 100644
--- a/modules/setting/config_env.go
+++ b/modules/setting/config_env.go
@@ -86,7 +86,7 @@ func decodeEnvSectionKey(encoded string) (ok bool, section, key string) {
 		key += remaining
 	}
 	section = strings.ToLower(section)
-	ok = section != "" && key != ""
+	ok = key != ""
 	if !ok {
 		section = ""
 		key = ""
diff --git a/modules/setting/config_env_test.go b/modules/setting/config_env_test.go
index c86302ff98..96689aee18 100644
--- a/modules/setting/config_env_test.go
+++ b/modules/setting/config_env_test.go
@@ -49,6 +49,12 @@ func TestDecodeEnvironmentKey(t *testing.T) {
 	assert.Equal(t, "", key)
 	assert.False(t, file)
 
+	ok, section, key, file = decodeEnvironmentKey(prefix, suffix, "GITEA____KEY")
+	assert.True(t, ok)
+	assert.Equal(t, "", section)
+	assert.Equal(t, "KEY", key)
+	assert.False(t, file)
+
 	ok, section, key, file = decodeEnvironmentKey(prefix, suffix, "GITEA__SEC__KEY")
 	assert.True(t, ok)
 	assert.Equal(t, "sec", section)