Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(config): reject malformed environment-expanded YAML (#4770)
钱晓缺4beff972f1bc
diff --git a/internal/config/config.go b/internal/config/config.goindex c3315cff1..5213a9f05 100644--- a/internal/config/config.go+++ b/internal/config/config.go@@ -578,7 +578,9 @@ func LoadConfig() (*Config, error) {})// 使用处理后的配置内容- viper.ReadConfig(strings.NewReader(result))+ if err := viper.ReadConfig(strings.NewReader(result)); err != nil {+ return nil, fmt.Errorf("error parsing expanded config: %w", err)+ }// 解析配置到结构体var cfg Configdiff --git a/internal/config/config_read_test.go b/internal/config/config_read_test.gonew file mode 100644index 000000000..367e3c49a--- /dev/null+++ b/internal/config/config_read_test.go@@ -0,0 +1,46 @@+package config++import (+ "os"+ "path/filepath"+ "testing"++ "github.com/spf13/viper"+ "github.com/stretchr/testify/require"+)++func TestLoadConfigParsesExpandedYAML(t *testing.T) {+ for _, tc := range []struct {+ name string+ value string+ valid bool+ }{+ {"valid expansion", "127.0.0.9", true},+ {"malformed expansion", "[", false},+ } {+ t.Run(tc.name, func(t *testing.T) {+ viper.Reset()+ t.Cleanup(viper.Reset)+ previousDir := resolvedConfigDir+ t.Cleanup(func() { resolvedConfigDir = previousDir })+ t.Setenv("WEKNORA_TEST_CONFIG_HOST", tc.value)+ t.Setenv("SERVER_HOST", "")+ path := filepath.Join(t.TempDir(), "config.yaml")+ contents := "server:\n port: 8080\n host: ${WEKNORA_TEST_CONFIG_HOST}\n" ++ "knowledge_base:\n chunk_size: 512\n chunk_overlap: 80\n"+ require.NoError(t, os.WriteFile(path, []byte(contents), 0o600))+ viper.AddConfigPath(filepath.Dir(path))++ cfg, err := LoadConfig()+ if !tc.valid {+ var parseErr viper.ConfigParseError+ require.ErrorAs(t, err, &parseErr)+ require.Nil(t, cfg)+ return+ }+ require.NoError(t, err)+ require.NotNil(t, cfg.Server)+ require.Equal(t, tc.value, cfg.Server.Host)+ })+ }+}