From bd659c322c5f4186c9ca90c275b547df1cd04e4d Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Thu, 2 Jul 2026 00:49:05 +0800 Subject: [PATCH] fix(mongodb): treat SRV URLs as seed lists --- crates/dbx-core/src/db/mongo_driver.rs | 15 +++++++++++++++ crates/dbx-core/src/models/connection.rs | 22 ++++++++++++++++++++++ 2 files changed, 37 insertions(+) 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:pass@cluster.example.net/admin?tls=true&directConnection=true&replicaSet=rs0"; + + let normalized = normalize_mongo_uri_direct_connection(uri); + + assert_eq!(normalized, "mongodb+srv://read:pass@cluster.example.net/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:pass@cluster.example.net/admin?tls=true&authSource=admin&directConnection=true&replicaSet=rs0" + .to_string(), + ); + + let url = config.connection_url(); + + assert_eq!(url, "mongodb+srv://read:pass@cluster.example.net/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"));