From d88ebbff2add52145e227352ac1b410eceb27c9d Mon Sep 17 00:00:00 2001 From: Andre Masella Date: Thu, 24 Sep 2026 17:38:15 -0400 Subject: [PATCH 1/2] JAVA-6235 Add SRV allow hosts option to legacy driver Adds options in original JAVA-6235 to legacy driver interface. --- .../main/com/mongodb/MongoClientOptions.java | 36 +++++++++++++++++++ .../src/main/com/mongodb/MongoClientURI.java | 7 ++++ .../MongoClientOptionsSpecification.groovy | 7 ++++ 3 files changed, 50 insertions(+) diff --git a/driver-legacy/src/main/com/mongodb/MongoClientOptions.java b/driver-legacy/src/main/com/mongodb/MongoClientOptions.java index 07061250741..b670770935a 100644 --- a/driver-legacy/src/main/com/mongodb/MongoClientOptions.java +++ b/driver-legacy/src/main/com/mongodb/MongoClientOptions.java @@ -22,6 +22,7 @@ import com.mongodb.annotations.NotThreadSafe; import com.mongodb.annotations.Reason; import com.mongodb.connection.ClusterConnectionMode; +import com.mongodb.connection.ClusterSettings; import com.mongodb.connection.ConnectionPoolSettings; import com.mongodb.event.ClusterListener; import com.mongodb.event.CommandListener; @@ -579,6 +580,22 @@ public String getSrvServiceName() { return wrapped.getClusterSettings().getSrvServiceName(); } + /** + * Gets the SRV allowed hosts suffix used to validate hosts returned via SRV lookup. + * + *

If present, its value is used as the domain for SRV host name validation, replacing the domain inferred from + * the SRV host name. The value is normalized to always begin with {@code "."}.

+ * + *

WARNING: Modifying the default SRV domain name validation can create vulnerabilities.

+ * + * @return the normalized SRV allowed hosts suffix, always beginning with {@code "."}. Defaults to null. + * @since 5.13 + * @see ClusterSettings.Builder#srvAllowedHostsSuffix(String) + */ + @Nullable + public String getSrvAllowedHostsSuffix() { + return wrapped.getClusterSettings().getSrvAllowedHostsSuffix(); + } /** * Gets the server API to use when sending commands to the server. * @@ -1429,6 +1446,25 @@ public Builder srvServiceName(final String srvServiceName) { return this; } + /** + * Sets the SRV allowed hosts suffix used to validate hosts returned via SRV lookup. + * + *

If set, its value is used as the domain for SRV host name validation, replacing the domain inferred from + * the SRV host name. The value is normalized: a leading {@code "."} is prepended if absent, so + * {@link #getSrvAllowedHostsSuffix()} always returns a value beginning with {@code "."}. This setting is only + * used with SRV. Specifying an overly broad suffix (for example a bare TLD) weakens SRV host name validation and + * is the responsibility of the caller.

+ * + * @param srvAllowedHostsSuffix the SRV allowed hosts suffix; may not be null or empty + * @return this + * @since 5.13 + * @see #getSrvAllowedHostsSuffix() + */ + public Builder srvAllowedHostsSuffix(final String srvAllowedHostsSuffix) { + notNull("srvAllowedHostsSuffix", srvAllowedHostsSuffix); + wrapped.applyToClusterSettings(builder -> builder.srvAllowedHostsSuffix(srvAllowedHostsSuffix)); + return this; + } /** * Sets the time limit, in milliseconds for the full execution of an operation. * diff --git a/driver-legacy/src/main/com/mongodb/MongoClientURI.java b/driver-legacy/src/main/com/mongodb/MongoClientURI.java index e7ce89566dd..d1041361466 100644 --- a/driver-legacy/src/main/com/mongodb/MongoClientURI.java +++ b/driver-legacy/src/main/com/mongodb/MongoClientURI.java @@ -110,6 +110,13 @@ * * *

Connection pool configuration:

diff --git a/driver-legacy/src/test/unit/com/mongodb/MongoClientOptionsSpecification.groovy b/driver-legacy/src/test/unit/com/mongodb/MongoClientOptionsSpecification.groovy index a386cd7f684..eaaebf980e3 100644 --- a/driver-legacy/src/test/unit/com/mongodb/MongoClientOptionsSpecification.groovy +++ b/driver-legacy/src/test/unit/com/mongodb/MongoClientOptionsSpecification.groovy @@ -80,6 +80,7 @@ class MongoClientOptionsSpecification extends Specification { options.getSrvMaxHosts() == null options.getSrvServiceName() == 'mongodb' + options.getSrvAllowedHostsSuffix() == null } def 'should handle illegal arguments'() { @@ -100,6 +101,10 @@ class MongoClientOptionsSpecification extends Specification { builder.dbEncoderFactory(null) then: thrown(IllegalArgumentException) + when: + builder.srvAllowedHostsSuffix(null) + then: + thrown(IllegalArgumentException) } def 'should build with set options'() { @@ -320,9 +325,11 @@ class MongoClientOptionsSpecification extends Specification { settings.clusterSettings == ClusterSettings.builder().srvHost('test3.test.build.10gen.cc') .srvServiceName('test') .srvMaxHosts(4) + .srvAllowedHostsSuffix(".build.10gen.cc") .build() options.getSrvServiceName() == 'test' options.getSrvMaxHosts() == 4 + options.getSrvAllowedHostsSuffix() == ".build.10gen.cc" } def 'should be easy to create new options from existing'() { From ced821d312234ceef30627b21e1cac120b07ee38 Mon Sep 17 00:00:00 2001 From: Andre Masella Date: Fri, 25 Sep 2026 13:18:47 -0400 Subject: [PATCH 2/2] fixup --- driver-core/src/main/com/mongodb/ConnectionString.java | 5 +++-- .../src/main/com/mongodb/MongoClientOptions.java | 6 +++--- driver-legacy/src/main/com/mongodb/MongoClientURI.java | 9 +++++++-- .../com/mongodb/MongoClientOptionsSpecification.groovy | 7 +++++++ .../unit/com/mongodb/MongoClientURISpecification.groovy | 4 +++- 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/driver-core/src/main/com/mongodb/ConnectionString.java b/driver-core/src/main/com/mongodb/ConnectionString.java index 1cd77140920..5b80d711cdf 100644 --- a/driver-core/src/main/com/mongodb/ConnectionString.java +++ b/driver-core/src/main/com/mongodb/ConnectionString.java @@ -269,8 +269,9 @@ *
  • {@code srvAllowedHostsSuffix=string}: The hostname suffix used to validate hosts returned via SRV lookup, replacing the domain * inferred from the SRV host name. Only valid with the mongodb+srv protocol. WARNING: Modifying the default SRV domain name * validation can create vulnerabilities. This option relaxes a built-in DNS spoofing safeguard. Use the most specific suffix possible for - * your deployment rather than a broad company-wide domain. For example, instead of {@code "mongodb+srv://cluster.test.internal.example - * .com/?srvAllowedHostsSuffix=.example.com"} which would accept any host across the entire domain, scope it further like so: + * your deployment rather than a broad company-wide domain. For example, instead of + * {@code "mongodb+srv://cluster.test.internal.example.com/?srvAllowedHostsSuffix=.example.com"} which would accept any host across the + * entire domain, scope it further like so: * {@code "mongodb+srv://cluster.test.internal.example.com/?srvAllowedHostsSuffix=.internal.example.com"}. *
  • * diff --git a/driver-legacy/src/main/com/mongodb/MongoClientOptions.java b/driver-legacy/src/main/com/mongodb/MongoClientOptions.java index b670770935a..fe814fa6aa6 100644 --- a/driver-legacy/src/main/com/mongodb/MongoClientOptions.java +++ b/driver-legacy/src/main/com/mongodb/MongoClientOptions.java @@ -22,7 +22,6 @@ import com.mongodb.annotations.NotThreadSafe; import com.mongodb.annotations.Reason; import com.mongodb.connection.ClusterConnectionMode; -import com.mongodb.connection.ClusterSettings; import com.mongodb.connection.ConnectionPoolSettings; import com.mongodb.event.ClusterListener; import com.mongodb.event.CommandListener; @@ -590,12 +589,13 @@ public String getSrvServiceName() { * * @return the normalized SRV allowed hosts suffix, always beginning with {@code "."}. Defaults to null. * @since 5.13 - * @see ClusterSettings.Builder#srvAllowedHostsSuffix(String) + * @see Builder#srvAllowedHostsSuffix(String) */ @Nullable public String getSrvAllowedHostsSuffix() { return wrapped.getClusterSettings().getSrvAllowedHostsSuffix(); } + /** * Gets the server API to use when sending commands to the server. * @@ -1461,10 +1461,10 @@ public Builder srvServiceName(final String srvServiceName) { * @see #getSrvAllowedHostsSuffix() */ public Builder srvAllowedHostsSuffix(final String srvAllowedHostsSuffix) { - notNull("srvAllowedHostsSuffix", srvAllowedHostsSuffix); wrapped.applyToClusterSettings(builder -> builder.srvAllowedHostsSuffix(srvAllowedHostsSuffix)); return this; } + /** * Sets the time limit, in milliseconds for the full execution of an operation. * diff --git a/driver-legacy/src/main/com/mongodb/MongoClientURI.java b/driver-legacy/src/main/com/mongodb/MongoClientURI.java index d1041361466..7e8289648ac 100644 --- a/driver-legacy/src/main/com/mongodb/MongoClientURI.java +++ b/driver-legacy/src/main/com/mongodb/MongoClientURI.java @@ -113,8 +113,9 @@ *
  • {@code srvAllowedHostsSuffix=string}: The hostname suffix used to validate hosts returned via SRV lookup, replacing the domain * inferred from the SRV host name. Only valid with the mongodb+srv protocol. WARNING: Modifying the default SRV domain name * validation can create vulnerabilities. This option relaxes a built-in DNS spoofing safeguard. Use the most specific suffix possible for - * your deployment rather than a broad company-wide domain. For example, instead of {@code "mongodb+srv://cluster.test.internal.example - * .com/?srvAllowedHostsSuffix=.example.com"} which would accept any host across the entire domain, scope it further like so: + * your deployment rather than a broad company-wide domain. For example, instead of + * {@code "mongodb+srv://cluster.test.internal.example.com/?srvAllowedHostsSuffix=.example.com"} which would accept any host across the + * entire domain, scope it further like so: * {@code "mongodb+srv://cluster.test.internal.example.com/?srvAllowedHostsSuffix=.internal.example.com"}. *
  • * @@ -479,6 +480,10 @@ public MongoClientOptions getOptions() { if (srvServiceName != null) { builder.srvServiceName(srvServiceName); } + String srvAllowedHostsSuffix = proxied.getSrvAllowedHostsSuffix(); + if (srvAllowedHostsSuffix != null) { + builder.srvAllowedHostsSuffix(srvAllowedHostsSuffix); + } Long timeout = proxied.getTimeout(); if (timeout != null) { builder.timeout(timeout); diff --git a/driver-legacy/src/test/unit/com/mongodb/MongoClientOptionsSpecification.groovy b/driver-legacy/src/test/unit/com/mongodb/MongoClientOptionsSpecification.groovy index eaaebf980e3..16f1604838a 100644 --- a/driver-legacy/src/test/unit/com/mongodb/MongoClientOptionsSpecification.groovy +++ b/driver-legacy/src/test/unit/com/mongodb/MongoClientOptionsSpecification.groovy @@ -101,10 +101,16 @@ class MongoClientOptionsSpecification extends Specification { builder.dbEncoderFactory(null) then: thrown(IllegalArgumentException) + when: builder.srvAllowedHostsSuffix(null) then: thrown(IllegalArgumentException) + + when: + builder.srvAllowedHostsSuffix("") + then: + thrown(IllegalArgumentException) } def 'should build with set options'() { @@ -317,6 +323,7 @@ class MongoClientOptionsSpecification extends Specification { def options = MongoClientOptions.builder() .srvServiceName('test') .srvMaxHosts(4) + .srvAllowedHostsSuffix(".build.10gen.cc") .build() settings = options.asMongoClientSettings(null, 'test3.test.build.10gen.cc', MULTIPLE, null) diff --git a/driver-legacy/src/test/unit/com/mongodb/MongoClientURISpecification.groovy b/driver-legacy/src/test/unit/com/mongodb/MongoClientURISpecification.groovy index 3de1f77b6da..7e8fc5e31c2 100644 --- a/driver-legacy/src/test/unit/com/mongodb/MongoClientURISpecification.groovy +++ b/driver-legacy/src/test/unit/com/mongodb/MongoClientURISpecification.groovy @@ -308,7 +308,8 @@ class MongoClientURISpecification extends Specification { def 'should apply SRV parameters'() { when: - def uri = new MongoClientURI('mongodb+srv://test3.test.build.10gen.cc/?srvMaxHosts=4&srvServiceName=test') + def uri = new MongoClientURI( + 'mongodb+srv://test3.test.build.10gen.cc/?srvMaxHosts=4&srvServiceName=test&srvAllowedHostsSuffix=.build.10gen.cc') then: uri.getSrvMaxHosts() == 4 @@ -320,6 +321,7 @@ class MongoClientURISpecification extends Specification { then: options.getSrvMaxHosts() == 4 options.getSrvServiceName() == 'test' + options.getSrvAllowedHostsSuffix() == '.build.10gen.cc' } def 'should respect MongoClientOptions builder'() {