diff --git a/driver-core/src/main/com/mongodb/internal/dns/DefaultDnsResolver.java b/driver-core/src/main/com/mongodb/internal/dns/DefaultDnsResolver.java index 2f57c6ddf7..ef450e744a 100644 --- a/driver-core/src/main/com/mongodb/internal/dns/DefaultDnsResolver.java +++ b/driver-core/src/main/com/mongodb/internal/dns/DefaultDnsResolver.java @@ -70,8 +70,7 @@ public List resolveHostFromSrvRecords(final String srvHost, final String List srvHostParts = asList(srvHost.split("\\.")); String srvHostDomain; - boolean srvHostHasLessThanThreeParts = srvHostParts.size() < 3; - if (srvHostHasLessThanThreeParts) { + if (srvHostParts.size() < 3) { srvHostDomain = srvHost; } else { srvHostDomain = srvHost.substring(srvHost.indexOf('.') + 1); @@ -89,14 +88,14 @@ public List resolveHostFromSrvRecords(final String srvHost, final String for (String srvRecord : srvAttributeValues) { String[] split = srvRecord.split(" "); String resolvedHost = split[3].endsWith(".") ? split[3].substring(0, split[3].length() - 1) : split[3]; - if (srvHostHasLessThanThreeParts && resolvedHost.equals(srvHost)) { - throw new MongoConfigurationException( - format("The SRV host name '%s' has less than three parts and the resolved host '%s' is identical.", - srvHost, resolvedHost) - ); + boolean sameParentDomain; + if (!resolvedHost.contains(".")) { + sameParentDomain = false; + } else { + String resolvedHostDomain = resolvedHost.substring(resolvedHost.indexOf('.') + 1); + sameParentDomain = sameParentDomain(srvHostDomainParts, resolvedHostDomain); } - String resolvedHostDomain = resolvedHost.substring(resolvedHost.indexOf('.') + 1); - if (!sameParentDomain(srvHostDomainParts, resolvedHostDomain)) { + if (!sameParentDomain) { throw new MongoConfigurationException( format("The SRV host name '%s' resolved to a host '%s 'that is not in a sub-domain of the SRV host.", srvHost, resolvedHost)); diff --git a/driver-core/src/test/unit/com/mongodb/internal/connection/InitialDnsSeedListDiscoveryProseTest.java b/driver-core/src/test/unit/com/mongodb/internal/connection/InitialDnsSeedListDiscoveryProseTest.java index 4647cc1cbf..4cd4eef0e3 100644 --- a/driver-core/src/test/unit/com/mongodb/internal/connection/InitialDnsSeedListDiscoveryProseTest.java +++ b/driver-core/src/test/unit/com/mongodb/internal/connection/InitialDnsSeedListDiscoveryProseTest.java @@ -59,7 +59,7 @@ void tearDown() { "mongo.local, driver.mongo.local" }) @DisplayName("1. Allow SRVs with fewer than 3 '.' separated parts") - void test1(final String srvHost, final String resolvedHost) { + void test_allow_SRVs_with_fewer_than_three_parts(final String srvHost, final String resolvedHost) { doTest(srvHost, resolvedHost, false); } @@ -70,7 +70,7 @@ void test1(final String srvHost, final String resolvedHost) { "blogs.mongodb.com, blogs.evil.com" }) @DisplayName("2. Throw when return address does not end with SRV domain") - void test2(final String srvHost, final String resolvedHost) { + void test_throw_when_return_address_doesnot_end_with_SRV_domain(final String srvHost, final String resolvedHost) { doTest(srvHost, resolvedHost, true); } @@ -79,8 +79,8 @@ void test2(final String srvHost, final String resolvedHost) { "localhost, localhost", "mongo.local, mongo.local" }) - @DisplayName("3. Throw when return address is identical to SRV hostname and return address does not contain '.' separating shared part of domain") - void test3(final String srvHost, final String resolvedHost) { + @DisplayName("3. Throw when return address is identical to SRV hostname and the SRV hostname has fewer than three `.` separated parts") + void test_throw_when_return_address_is_identical_to_SRV_hostname(final String srvHost, final String resolvedHost) { doTest(srvHost, resolvedHost, true); } @@ -91,7 +91,7 @@ void test3(final String srvHost, final String resolvedHost) { "blogs.mongodb.com, cluster.testmongodb.com" }) @DisplayName("4. Throw when return address does not contain '.' separating shared part of domain") - void test4(final String srvHost, final String resolvedHost) { + void test_throw_when_return_address_doesnot_contain_shared_part_of_domain(final String srvHost, final String resolvedHost) { doTest(srvHost, resolvedHost, true); }