diff --git a/src/net/url/url.go b/src/net/url/url.go index 3c49f0527d..092588de0c 100644 --- a/src/net/url/url.go +++ b/src/net/url/url.go @@ -617,6 +617,13 @@ func parseHost(scheme, host string) (string, error) { // continue to permit it for postgres:// URLs only. // https://go.dev/issue/75223 i = lastColon + } else if scheme == "mongodb" || scheme == "mongodb+srv" { + // In a MongoDB connection URI, commas are used to separate + // multiple host addresses when connecting to a replica set or a sharded cluster. + // https://www.mongodb.com/docs/manual/reference/connection-string-formats/ + // Example: + // mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017 + i = lastColon } else if urlstrictcolons.Value() == "0" { urlstrictcolons.IncNonDefault() i = lastColon diff --git a/src/net/url/url_test.go b/src/net/url/url_test.go index b048989b6c..790b025fda 100644 --- a/src/net/url/url_test.go +++ b/src/net/url/url_test.go @@ -629,6 +629,27 @@ var urltests = []URLTest{ }, "postgresql://host1:1,host2:2,host3:3", }, + // Mongodb URLs can include a comma-separated list of host:post hosts. + { + "mongodb://user:password@host1:1,host2:2,host3:3", + &URL{ + Scheme: "mongodb", + User: UserPassword("user", "password"), + Host: "host1:1,host2:2,host3:3", + Path: "", + }, + "", + }, + { + "mongodb+srv://user:password@host1:1,host2:2,host3:3", + &URL{ + Scheme: "mongodb+srv", + User: UserPassword("user", "password"), + Host: "host1:1,host2:2,host3:3", + Path: "", + }, + "", + }, } // more useful string for debugging than fmt's struct printer