Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(config): mask credentials in Go startup configuration logs (#20796)

adwsd4f0ecd836e7
diff --git a/internal/admin/handler.go b/internal/admin/handler.go
index 5593b932bd..89896bcd70 100644
--- a/internal/admin/handler.go
+++ b/internal/admin/handler.go
@@ -682,7 +682,8 @@ func (h *Handler) SetVariable(c *gin.Context) {
common.SuccessNoData(c, "Set variable successfully")
}
-// ListConfigs handle list configs
+// ListConfigs returns configurations, including plaintext credentials, for
+// superuser inspection. AuthMiddleware enforces superuser access.
func (h *Handler) ListConfigs(c *gin.Context) {
configs, err := h.service.ListAllConfigs()
if err != nil {
diff --git a/internal/handler/system.go b/internal/handler/system.go
index 9bf2e61013..cbb0991651 100644
--- a/internal/handler/system.go
+++ b/internal/handler/system.go
@@ -84,24 +84,6 @@ func (h *SystemHandler) GetConfig(c *gin.Context) {
common.SuccessWithData(c, config, "success")
}
-// GetConfigs get all system configurations
-// @Summary Get All System Configurations
-// @Description Get all system configurations from globalConfig
-// @Tags system
-// @Accept json
-// @Produce json
-// @Success 200 {object} config.Config
-// @Router /v1/system/configs [get]
-func (h *SystemHandler) GetConfigs(c *gin.Context) {
- cfg := server.GetConfig()
- if cfg == nil {
- common.ResponseWithHttpCodeData(c, http.StatusInternalServerError, 500, nil, "Configuration not initialized")
- return
- }
-
- common.SuccessWithData(c, cfg, "success")
-}
-
// GetStatus get RAGFlow status
func (h *SystemHandler) GetStatus(c *gin.Context) {
_, errorCode, errorMessage := GetUser(c)
diff --git a/internal/server/config.go b/internal/server/config.go
index d9b61a2c1c..be68b1a5d6 100644
--- a/internal/server/config.go
+++ b/internal/server/config.go
@@ -169,8 +169,9 @@ func GetConfig() *config.Config {
return globalConfig
}
-// GetAllConfigs collects every backend's connection settings as exportable
-// maps, for diagnostics and the admin configuration view.
+// GetAllConfigs collects backend connection settings for administrator inspection.
+// ExportConfigs intentionally retains plaintext credentials; the admin configuration
+// endpoint restricts access to superusers, while PrintAll masks credentials in logs.
func GetAllConfigs() ([]map[string]interface{}, error) {
var allConfigs []map[string]interface{}
@@ -276,21 +277,65 @@ func GetAllConfigs() ([]map[string]interface{}, error) {
return allConfigs, nil
}
-// PrintAll prints all configuration settings
+var sensitiveConfigKeys = map[string]struct{}{
+ "password": {},
+ "mail_password": {},
+ "secret": {},
+ "secret_key": {},
+ "client_secret": {},
+ "api_key": {},
+ "host_api_key": {},
+ "http_app_key": {},
+ "http_secret_key": {},
+ "access_key": {},
+ "secret_id": {},
+ "session_token": {},
+ "sas_token": {},
+
+ "open_dataloader_api_key": {},
+ "paddle_ocr_access_token": {},
+ "somark_api_key": {},
+ "tcad_api_key": {},
+ "docling_api_key": {},
+ "mineru_api_key": {},
+ "tavily_api_key": {},
+ "querit_api_key": {},
+}
+
+// PrintAll logs all configuration settings with credentials masked.
func PrintAll() {
if globalViper == nil {
common.Info("Configuration not initialized")
return
}
- allSettings := globalViper.AllSettings()
- // The standalone MCP key grants every self-host client the same identity.
- if mcp, ok := allSettings["mcp"].(map[string]interface{}); ok {
- delete(mcp, "host_api_key")
- }
+ allSettings := maskConfigCredentials(globalViper.AllSettings()).(map[string]any)
common.Info("=== All Configurations ===")
for key, value := range allSettings {
common.Info("config", zap.String("key", key), zap.Any("value", value))
}
common.Info("=== End Configurations ===")
}
+
+func maskConfigCredentials(value any) any {
+ switch value := value.(type) {
+ case map[string]any:
+ settings := make(map[string]any, len(value))
+ for key, child := range value {
+ if _, sensitive := sensitiveConfigKeys[strings.ToLower(key)]; sensitive {
+ settings[key] = "******"
+ continue
+ }
+ settings[key] = maskConfigCredentials(child)
+ }
+ return settings
+ case []any:
+ settings := make([]any, len(value))
+ for i, child := range value {
+ settings[i] = maskConfigCredentials(child)
+ }
+ return settings
+ default:
+ return value
+ }
+}
diff --git a/internal/server/config_getallconfigs_test.go b/internal/server/config_getallconfigs_test.go
index 9525682ac6..92f215c701 100644
--- a/internal/server/config_getallconfigs_test.go
+++ b/internal/server/config_getallconfigs_test.go
@@ -18,16 +18,89 @@ package server
import (
"fmt"
+ "github.com/spf13/viper"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
"os"
"path/filepath"
"ragflow/internal/common"
+ "reflect"
"strings"
"testing"
)
+func TestPrintAllMasksCredentials(t *testing.T) {
+ oldViper, oldLogger := globalViper, common.Logger
+ t.Cleanup(func() {
+ globalViper, common.Logger = oldViper, oldLogger
+ })
+ globalViper = viper.New()
+ globalViper.SetConfigType("yaml")
+ if err := globalViper.ReadConfig(strings.NewReader(`
+password: test-only-root-password
+os: {hosts: search.example, username: admin, password: test-only-search-password}
+mcp: {host_api_key: test-only-mcp-key}
+backends:
+ - config: {password: test-only-nested-password, enabled: true}
+ - password: ""
+ - password: null
+password_policy: strict
+disable_password_login: true
+max_token: 64
+`)); err != nil {
+ t.Fatal(err)
+ }
+ wantCredentials := make(map[string]any)
+ for _, key := range []string{
+ "mail_password", "secret", "secret_key", "client_secret", "api_key",
+ "host_api_key", "http_app_key", "http_secret_key", "access_key", "secret_id", "session_token", "sas_token",
+ "open_dataloader_api_key", "paddle_ocr_access_token", "somark_api_key", "tcad_api_key",
+ "docling_api_key", "mineru_api_key", "tavily_api_key", "querit_api_key",
+ } {
+ globalViper.Set("credentials."+key, "test-only-credential")
+ wantCredentials[key] = "******"
+ }
+ original := fmt.Sprint(globalViper.AllSettings())
+ core, logs := observer.New(zapcore.InfoLevel)
+ common.Logger = zap.New(core)
+ PrintAll()
+
+ settings := make(map[string]any)
+ for _, entry := range logs.FilterMessage("config").All() {
+ fields := entry.ContextMap()
+ settings[fields["key"].(string)] = fields["value"]
+ }
+ want := map[string]any{
+ "password": "******",
+ "os": map[string]any{"hosts": "search.example", "username": "admin", "password": "******"},
+ "mcp": map[string]any{"host_api_key": "******"},
+ "backends": []any{
+ map[string]any{"config": map[string]any{"password": "******", "enabled": true}},
+ map[string]any{"password": "******"},
+ map[string]any{"password": "******"},
+ },
+ "credentials": wantCredentials,
+ "password_policy": "strict",
+ "disable_password_login": true,
+ "max_token": int64(64),
+ }
+ if !reflect.DeepEqual(settings, want) {
+ t.Fatalf("logged settings = %#v, want %#v", settings, want)
+ }
+ if fmt.Sprint(globalViper.AllSettings()) != original {
+ t.Fatal("logging mutated the loaded configuration")
+ }
+}
+
+func TestMaskConfigCredentialsIgnoresCase(t *testing.T) {
+ input := map[string]any{"API_KEY": "test-only-secret", "PASSWORD_POLICY": "strict"}
+ want := map[string]any{"API_KEY": "******", "PASSWORD_POLICY": "strict"}
+ if got := maskConfigCredentials(input); !reflect.DeepEqual(got, want) {
+ t.Fatalf("masked settings = %#v, want %#v", got, want)
+ }
+}
+
// TestGetAllConfigsKvrocksCacheEngine reproduces Finding 1: when the Go stack is
// configured with `cache_engine: kvrocks`, GetAllConfigs must export the Kvrocks
// connection settings instead of failing with "not supported cache engine".
diff --git a/internal/service/system.go b/internal/service/system.go
index b6e335eeb6..5c2c3c04d1 100644
--- a/internal/service/system.go
+++ b/internal/service/system.go
@@ -460,18 +460,6 @@ func (s *SystemService) SetVariable(ctx context.Context, varName, varValue strin
return s.systemSettingsDAO.Create(ctx, dao.DB, newSetting)
}
-// Config methods
-
-// ListAllConfigs list all configs
-// Returns all service configurations from the config file
-func (s *SystemService) ListAllConfigs() ([]map[string]interface{}, error) {
- result, err := server.GetAllConfigs()
- if err != nil {
- return nil, err
- }
- return result, nil
-}
-
// Environment methods
// ListEnvironments list all environments