Skip to content

Commit 0cbc00f

Browse files
committed
PYTHON-4575 Remove hostname length check
- Add test
1 parent 426d061 commit 0cbc00f

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

pymongo/asynchronous/srv_resolver.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ def __init__(
9696
except Exception:
9797
raise ConfigurationError(_INVALID_HOST_MSG % (fqdn,)) from None
9898
self.__slen = len(self.__plist)
99+
if fqdn == self._resolve_uri(True)[0].target.to_text():
100+
raise ConfigurationError(
101+
"Invalid SRV host: return address is identical to SRV hostname"
102+
)
99103

100104
async def get_options(self) -> Optional[str]:
101105
from dns import resolver

pymongo/synchronous/srv_resolver.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ def __init__(
9696
except Exception:
9797
raise ConfigurationError(_INVALID_HOST_MSG % (fqdn,)) from None
9898
self.__slen = len(self.__plist)
99+
if fqdn == self._resolve_uri(True)[0].target.to_text():
100+
raise ConfigurationError(
101+
"Invalid SRV host: return address is identical to SRV hostname"
102+
)
99103

100104
def get_options(self) -> Optional[str]:
101105
from dns import resolver

test/test_uri_parser.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,21 @@ def test_error_when_return_address_does_not_end_with_srv_domain(self):
576576
"mock_target": "test_1.evil.com",
577577
"expected_error": "Invalid SRV host",
578578
},
579+
{
580+
"query": "_mongodb._tcp.localhost",
581+
"mock_target": "localhost",
582+
"expected_error": "Invalid SRV host",
583+
},
584+
{
585+
"query": "_mongodb._tcp.localhost",
586+
"mock_target": "localhost",
587+
"expected_error": "Invalid SRV host",
588+
},
589+
{
590+
"query": "_mongodb._tcp.mongo.local",
591+
"mock_target": "mongo.local",
592+
"expected_error": "Invalid SRV host",
593+
},
579594
]
580595
for case in test_cases:
581596
with patch("dns.resolver.resolve") as mock_resolver:

0 commit comments

Comments
 (0)