diff --git a/CHANGES/5110.bugfix b/CHANGES/5110.bugfix new file mode 100644 index 00000000000..14d63a9b8a2 --- /dev/null +++ b/CHANGES/5110.bugfix @@ -0,0 +1,3 @@ +Fix a variable-shadowing bug causing `ThreadedResolver.resolve` to +return the resolved IP as the "hostname" in each record, which prevented +validation of HTTPS connections. diff --git a/aiohttp/resolver.py b/aiohttp/resolver.py index 89821b2dfd1..9e77771f898 100644 --- a/aiohttp/resolver.py +++ b/aiohttp/resolver.py @@ -26,10 +26,10 @@ def __init__(self, loop: Optional[asyncio.AbstractEventLoop] = None) -> None: self._loop = get_running_loop(loop) async def resolve( - self, host: str, port: int = 0, family: int = socket.AF_INET + self, hostname: str, port: int = 0, family: int = socket.AF_INET ) -> List[Dict[str, Any]]: infos = await self._loop.getaddrinfo( - host, port, type=socket.SOCK_STREAM, family=family + hostname, port, type=socket.SOCK_STREAM, family=family ) hosts = [] @@ -46,7 +46,7 @@ async def resolve( host, port = address[:2] hosts.append( { - "hostname": host, + "hostname": hostname, "host": host, "port": port, "family": family, diff --git a/tests/test_connector.py b/tests/test_connector.py index 994b82cb2a0..d22c69f2fda 100644 --- a/tests/test_connector.py +++ b/tests/test_connector.py @@ -675,10 +675,10 @@ async def test_tcp_connector_resolve_host(loop) -> None: for rec in res: if rec["family"] == socket.AF_INET: assert rec["host"] == "127.0.0.1" - assert rec["hostname"] == "127.0.0.1" + assert rec["hostname"] == "localhost" assert rec["port"] == 8080 elif rec["family"] == socket.AF_INET6: - assert rec["hostname"] == "::1" + assert rec["hostname"] == "localhost" assert rec["port"] == 8080 if platform.system() == "Darwin": assert rec["host"] in ("::1", "fe80::1", "fe80::1%lo0") diff --git a/tests/test_resolver.py b/tests/test_resolver.py index 3b58b96e2c3..199707e7a42 100644 --- a/tests/test_resolver.py +++ b/tests/test_resolver.py @@ -130,6 +130,7 @@ async def test_threaded_resolver_positive_lookup() -> None: loop.getaddrinfo = fake_addrinfo(["127.0.0.1"]) resolver = ThreadedResolver(loop=loop) real = await resolver.resolve("www.python.org") + assert real[0]["hostname"] == "www.python.org" ipaddress.ip_address(real[0]["host"])