Skip to content

Commit c2bc931

Browse files
committed
Review fixes
1 parent c767cd4 commit c2bc931

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

pymongo/asynchronous/srv_resolver.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ async def _get_srv_response_and_hosts(
137137

138138
# Validate hosts
139139
for node in nodes:
140-
if self.__fqdn == node[0].lower() and len(node[0].split(".")) < 3:
140+
srv_host = node[0].lower()
141+
if self.__fqdn == srv_host and len(srv_host.split(".")) < 3:
141142
raise ConfigurationError(
142143
"Invalid SRV host: return address is identical to SRV hostname"
143144
)

pymongo/synchronous/srv_resolver.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ def _get_srv_response_and_hosts(
137137

138138
# Validate hosts
139139
for node in nodes:
140-
if self.__fqdn == node[0].lower() and len(node[0].split(".")) < 3:
140+
srv_host = node[0].lower()
141+
if self.__fqdn == srv_host and len(srv_host.split(".")) < 3:
141142
raise ConfigurationError(
142143
"Invalid SRV host: return address is identical to SRV hostname"
143144
)

test/asynchronous/test_dns.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,15 @@ async def mock_resolve(query, record_type, *args, **kwargs):
220220
mock_resolver.side_effect = mock_resolve
221221
domain = case["query"].split("._tcp.")[1]
222222
connection_string = f"mongodb+srv://{domain}"
223-
try:
223+
if "expected_error" not in case:
224224
await parse_uri(connection_string)
225-
except ConfigurationError as e:
226-
self.assertIn(case["expected_error"], str(e))
227225
else:
228-
self.fail(f"ConfigurationError was not raised for query: {case['query']}")
226+
try:
227+
await parse_uri(connection_string)
228+
except ConfigurationError as e:
229+
self.assertIn(case["expected_error"], str(e))
230+
else:
231+
self.fail(f"ConfigurationError was not raised for query: {case['query']}")
229232

230233
async def test_1_allow_srv_hosts_with_fewer_than_three_dot_separated_parts(self):
231234
with patch("dns.asyncresolver.resolve"):
@@ -264,6 +267,12 @@ async def test_3_throw_when_return_address_is_identical_to_srv_hostname(self):
264267
"mock_target": "mongo.local",
265268
"expected_error": "Invalid SRV host",
266269
},
270+
# When the SRV hostname has three or more dot-separated parts
271+
# it is valid for the returned hostnames to be identical.
272+
{
273+
"query": "_mongodb._tcp.blogs.mongodb.com",
274+
"mock_target": "blogs.mongodb.com",
275+
},
267276
]
268277
await self.run_initial_dns_seedlist_discovery_prose_tests(test_cases)
269278

test/test_dns.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,15 @@ def mock_resolve(query, record_type, *args, **kwargs):
218218
mock_resolver.side_effect = mock_resolve
219219
domain = case["query"].split("._tcp.")[1]
220220
connection_string = f"mongodb+srv://{domain}"
221-
try:
221+
if "expected_error" not in case:
222222
parse_uri(connection_string)
223-
except ConfigurationError as e:
224-
self.assertIn(case["expected_error"], str(e))
225223
else:
226-
self.fail(f"ConfigurationError was not raised for query: {case['query']}")
224+
try:
225+
parse_uri(connection_string)
226+
except ConfigurationError as e:
227+
self.assertIn(case["expected_error"], str(e))
228+
else:
229+
self.fail(f"ConfigurationError was not raised for query: {case['query']}")
227230

228231
def test_1_allow_srv_hosts_with_fewer_than_three_dot_separated_parts(self):
229232
with patch("dns.resolver.resolve"):
@@ -262,6 +265,12 @@ def test_3_throw_when_return_address_is_identical_to_srv_hostname(self):
262265
"mock_target": "mongo.local",
263266
"expected_error": "Invalid SRV host",
264267
},
268+
# When the SRV hostname has three or more dot-separated parts
269+
# it is valid for the returned hostnames to be identical.
270+
{
271+
"query": "_mongodb._tcp.blogs.mongodb.com",
272+
"mock_target": "blogs.mongodb.com",
273+
},
265274
]
266275
self.run_initial_dns_seedlist_discovery_prose_tests(test_cases)
267276

0 commit comments

Comments
 (0)