From 4cad3d740695448ca30c0f9e8d29b715f7d64ee6 Mon Sep 17 00:00:00 2001 From: "Jeffrey A. Clark" Date: Wed, 26 Mar 2025 14:56:31 -0400 Subject: [PATCH 1/2] PYTHON-4575 Remove hostname length check --- pymongo/asynchronous/srv_resolver.py | 2 -- pymongo/synchronous/srv_resolver.py | 2 -- test/test_uri_parser.py | 6 ++++++ 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/pymongo/asynchronous/srv_resolver.py b/pymongo/asynchronous/srv_resolver.py index 8b811e5dc2..cedd0749e9 100644 --- a/pymongo/asynchronous/srv_resolver.py +++ b/pymongo/asynchronous/srv_resolver.py @@ -96,8 +96,6 @@ def __init__( except Exception: raise ConfigurationError(_INVALID_HOST_MSG % (fqdn,)) from None self.__slen = len(self.__plist) - if self.__slen < 2: - raise ConfigurationError(_INVALID_HOST_MSG % (fqdn,)) async def get_options(self) -> Optional[str]: from dns import resolver diff --git a/pymongo/synchronous/srv_resolver.py b/pymongo/synchronous/srv_resolver.py index 1b36efd1c9..afe5b23a93 100644 --- a/pymongo/synchronous/srv_resolver.py +++ b/pymongo/synchronous/srv_resolver.py @@ -96,8 +96,6 @@ def __init__( except Exception: raise ConfigurationError(_INVALID_HOST_MSG % (fqdn,)) from None self.__slen = len(self.__plist) - if self.__slen < 2: - raise ConfigurationError(_INVALID_HOST_MSG % (fqdn,)) def get_options(self) -> Optional[str]: from dns import resolver diff --git a/test/test_uri_parser.py b/test/test_uri_parser.py index 0baefa0c3a..d96cad6b6a 100644 --- a/test/test_uri_parser.py +++ b/test/test_uri_parser.py @@ -24,6 +24,7 @@ sys.path[0:0] = [""] from test import unittest +from unittest.mock import patch from bson.binary import JAVA_LEGACY from pymongo import ReadPreference @@ -553,6 +554,11 @@ def test_port_with_whitespace(self): with self.assertRaisesRegex(ValueError, r"Port contains whitespace character: '\\n'"): parse_uri("mongodb://localhost:27\n017") + def test_allow_srv_hosts_with_fewer_than_three_dot_separated_parts(self): + with patch("dns.resolver.resolve"): + parse_uri("mongodb+srv://localhost/") + parse_uri("mongodb+srv://mongo.local/") + if __name__ == "__main__": unittest.main() From 118810e30e6fd7fdf5c3df15e3e63f5ed01369be Mon Sep 17 00:00:00 2001 From: "Jeffrey A. Clark" Date: Wed, 26 Mar 2025 15:10:17 -0400 Subject: [PATCH 2/2] PYTHON-4575 Remove hostname length check --- test/asynchronous/test_dns.py | 6 ------ test/test_dns.py | 6 ------ 2 files changed, 12 deletions(-) diff --git a/test/asynchronous/test_dns.py b/test/asynchronous/test_dns.py index d0e801e123..7316b4df9a 100644 --- a/test/asynchronous/test_dns.py +++ b/test/asynchronous/test_dns.py @@ -186,12 +186,6 @@ def create_tests(cls): class TestParsingErrors(AsyncPyMongoTestCase): async def test_invalid_host(self): - with self.assertRaisesRegex(ConfigurationError, "Invalid URI host: mongodb is not"): - client = self.simple_client("mongodb+srv://mongodb") - await client.aconnect() - with self.assertRaisesRegex(ConfigurationError, "Invalid URI host: mongodb.com is not"): - client = self.simple_client("mongodb+srv://mongodb.com") - await client.aconnect() with self.assertRaisesRegex(ConfigurationError, "Invalid URI host: an IP address is not"): client = self.simple_client("mongodb+srv://127.0.0.1") await client.aconnect() diff --git a/test/test_dns.py b/test/test_dns.py index 0290eb16d9..c478e481f6 100644 --- a/test/test_dns.py +++ b/test/test_dns.py @@ -184,12 +184,6 @@ def create_tests(cls): class TestParsingErrors(PyMongoTestCase): def test_invalid_host(self): - with self.assertRaisesRegex(ConfigurationError, "Invalid URI host: mongodb is not"): - client = self.simple_client("mongodb+srv://mongodb") - client._connect() - with self.assertRaisesRegex(ConfigurationError, "Invalid URI host: mongodb.com is not"): - client = self.simple_client("mongodb+srv://mongodb.com") - client._connect() with self.assertRaisesRegex(ConfigurationError, "Invalid URI host: an IP address is not"): client = self.simple_client("mongodb+srv://127.0.0.1") client._connect()