Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(mongodb): treat SRV URLs as seed lists

t8y2bd659c322c5f
diff --git a/crates/dbx-core/src/db/mongo_driver.rs b/crates/dbx-core/src/db/mongo_driver.rs
index 46dedff285..e35a551759 100644
--- a/crates/dbx-core/src/db/mongo_driver.rs
+++ b/crates/dbx-core/src/db/mongo_driver.rs
@@ -73,6 +73,11 @@ fn normalize_mongo_uri_direct_connection(uri: &str) -> String {
}
fn is_multi_host_mongo_uri(url: &str) -> bool {
+ if url.get(..14).is_some_and(|scheme| scheme.eq_ignore_ascii_case("mongodb+srv://")) {
+ // SRV URLs expand to a DNS seed list during driver parsing, so forcing
+ // directConnection would be rejected even though the URI shows one host.
+ return true;
+ }
let rest = match url.strip_prefix("mongodb://").or_else(|| url.strip_prefix("mongodb+srv://")) {
Some(r) => r,
None => return false,
@@ -925,6 +930,16 @@ mod tests {
assert_eq!(normalized, "mongodb://host1:27017,host2:27017/admin?authSource=admin#read");
}
+ #[test]
+ fn srv_uri_removes_direct_connection_true_before_driver_parse() {
+ let uri = "mongodb+srv://read:[email protected]/admin?tls=true&directConnection=true&replicaSet=rs0";
+
+ let normalized = normalize_mongo_uri_direct_connection(uri);
+
+ assert_eq!(normalized, "mongodb+srv://read:[email protected]/admin?tls=true&replicaSet=rs0");
+ assert!(is_multi_host_mongo_uri(&normalized));
+ }
+
#[test]
fn single_seed_uri_keeps_direct_connection_true() {
let uri = "mongodb://host1:27017/admin?directConnection=true&authSource=admin";
diff --git a/crates/dbx-core/src/models/connection.rs b/crates/dbx-core/src/models/connection.rs
index 53cf53dca0..98e3c0e0c7 100644
--- a/crates/dbx-core/src/models/connection.rs
+++ b/crates/dbx-core/src/models/connection.rs
@@ -1245,11 +1245,20 @@ fn normalize_mongo_uri_query_path(uri: &str) -> String {
}
fn mongo_uri_has_multiple_seeds(uri: &str) -> bool {
+ if mongo_uri_is_srv(uri) {
+ // The Rust MongoDB driver resolves SRV records into a seed list, so an
+ // SRV URL must not keep directConnection=true even with one hostname.
+ return true;
+ }
mongo_uri_host_section(uri)
.map(|hosts| hosts.split(',').filter(|host| !host.trim().is_empty()).count() > 1)
.unwrap_or(false)
}
+fn mongo_uri_is_srv(uri: &str) -> bool {
+ uri.get(..14).is_some_and(|scheme| scheme.eq_ignore_ascii_case("mongodb+srv://"))
+}
+
fn mongo_uri_host_section(uri: &str) -> Option<&str> {
let rest = uri.strip_prefix("mongodb://").or_else(|| uri.strip_prefix("mongodb+srv://"))?;
let authority = rest.split('/').next()?.split('?').next().unwrap_or(rest);
@@ -2409,6 +2418,19 @@ mod tests {
assert_eq!(url, "mongodb://read:pass@host1:27017,host2:27017/admin?replicaSet=rs0&authSource=admin");
}
+ #[test]
+ fn mongodb_srv_connection_string_removes_direct_connection_true() {
+ let mut config = mongodb_config("root", "secret", Some("admin"));
+ config.connection_string = Some(
+ "mongodb+srv://read:[email protected]/admin?tls=true&authSource=admin&directConnection=true&replicaSet=rs0"
+ .to_string(),
+ );
+
+ let url = config.connection_url();
+
+ assert_eq!(url, "mongodb+srv://read:[email protected]/admin?tls=true&authSource=admin&replicaSet=rs0");
+ }
+
#[test]
fn mongodb_single_seed_connection_string_keeps_direct_connection_true() {
let mut config = mongodb_config("root", "secret", Some("admin"));