Skip to content

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

fix: avoid KeyError when database config has no password (#11051) (#16064)

Ryker_Fengb537f3926097
diff --git a/common/config_utils.py b/common/config_utils.py
index 06db79debf..4218cd5340 100644
--- a/common/config_utils.py
+++ b/common/config_utils.py
@@ -137,8 +137,18 @@ def decrypt_database_password(password):
def decrypt_database_config(database=None, passwd_key="password", name="database"):
- if not database:
- database = get_base_config(name, {})
+ # Only fall back to the global config when no `database` argument is passed.
+ # An explicitly supplied (possibly empty) dict must be honoured as-is so that
+ # callers can opt out of the global lookup.
+ if database is None:
+ database = get_base_config(name, {}) or {}
+
+ # Some deployments omit the password field entirely (e.g. externally managed
+ # databases or credentials injected by other means). Skip decryption instead
+ # of raising `KeyError: 'password'`, which previously crashed service start-up
+ # (#11051).
+ if passwd_key not in database:
+ return database
database[passwd_key] = decrypt_database_password(database[passwd_key])
return database
diff --git a/test/unit_test/common/test_config_utils.py b/test/unit_test/common/test_config_utils.py
new file mode 100644
index 0000000000..81a6ae85d5
--- /dev/null
+++ b/test/unit_test/common/test_config_utils.py
@@ -0,0 +1,90 @@
+#
+# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+from common import config_utils
+from common.config_utils import decrypt_database_config
+
+
+class TestDecryptDatabaseConfig:
+ def test_missing_password_key_does_not_raise(self):
+ """A config section without `password` must not crash (regression #11051)."""
+ database = {"user": "root", "host": "localhost"}
+ result = decrypt_database_config(database=database)
+ # Returns the section untouched and does NOT fabricate a password key.
+ assert result == {"user": "root", "host": "localhost"}
+ assert "password" not in result
+
+ def test_explicit_empty_dict_is_honoured(self):
+ """An explicitly supplied empty dict must not fall back to global config."""
+ result = decrypt_database_config(database={})
+ assert result == {}
+
+ def test_none_database_falls_back_to_global_config(self, monkeypatch):
+ """When no database is passed, the global config is used."""
+ monkeypatch.setattr(
+ config_utils,
+ "get_base_config",
+ lambda name, default=None: {"user": "root", "host": "localhost"},
+ )
+ result = decrypt_database_config(name="mysql")
+ assert result == {"user": "root", "host": "localhost"}
+ assert "password" not in result
+
+ def test_none_database_with_missing_section(self, monkeypatch):
+ """A missing config section resolves to an empty dict, not a crash."""
+ monkeypatch.setattr(
+ config_utils,
+ "get_base_config",
+ lambda name, default=None: default,
+ )
+ result = decrypt_database_config(name="nonexistent")
+ assert result == {}
+
+ def test_password_is_decrypted(self, monkeypatch):
+ """When `password` is present it is passed through the decryptor."""
+ monkeypatch.setattr(
+ config_utils,
+ "decrypt_database_password",
+ lambda password: f"decrypted::{password}",
+ )
+ database = {"user": "root", "password": "secret"}
+ result = decrypt_database_config(database=database)
+ assert result["password"] == "decrypted::secret"
+ assert result["user"] == "root"
+
+ def test_empty_password_value_is_preserved(self, monkeypatch):
+ """An empty password string is a valid value and must round-trip."""
+ monkeypatch.setattr(
+ config_utils,
+ "decrypt_database_password",
+ lambda password: password,
+ )
+ database = {"user": "root", "password": ""}
+ result = decrypt_database_config(database=database)
+ assert result["password"] == ""
+
+ def test_custom_passwd_key(self, monkeypatch):
+ """A non-default passwd_key is respected for both presence check and decode."""
+ monkeypatch.setattr(
+ config_utils,
+ "decrypt_database_password",
+ lambda password: f"decrypted::{password}",
+ )
+ # Missing custom key -> untouched.
+ assert decrypt_database_config(database={"user": "root"}, passwd_key="secret_key") == {"user": "root"}
+ # Present custom key -> decrypted.
+ result = decrypt_database_config(database={"secret_key": "abc"}, passwd_key="secret_key")
+ assert result["secret_key"] == "decrypted::abc"