Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if ipv6 is enabled, when processing socket address info #5906

Merged
merged 1 commit into from Oct 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES/5901.bugfix
@@ -0,0 +1,4 @@
Fix the error in handling the return value of `getaddrinfo`.
`getaddrinfo` will return an `(int, bytes)` tuple, if CPython could not handle the address family.
It will cause a index out of range error in aiohttp. For example, if user compile CPython with
`--disable-ipv6` option but his system enable the ipv6.
4 changes: 3 additions & 1 deletion aiohttp/resolver.py
Expand Up @@ -37,7 +37,9 @@ async def resolve(

hosts = []
for family, _, proto, _, address in infos:
if family == socket.AF_INET6 and address[3]: # type: ignore[misc]
if family == socket.AF_INET6:
if not (socket.has_ipv6 and address[3]): # type: ignore[misc]
continue
# This is essential for link-local IPv6 addresses.
# LL IPv6 is a VERY rare case. Strictly speaking, we should use
# getnameinfo() unconditionally, but performance makes sense.
Expand Down
24 changes: 24 additions & 0 deletions tests/test_resolver.py
Expand Up @@ -119,6 +119,30 @@ async def test_threaded_negative_lookup() -> None:
await resolver.resolve("doesnotexist.bla")


async def test_threaded_negative_lookup_with_unknown_result() -> None:
loop = Mock()

# If compile CPython with `--disable-ipv6` option,
# we will get an (int, bytes) tuple, instead of a Exception.
async def unknown_addrinfo(*args: Any, **kwargs: Any) -> List[Any]:
return [
(
socket.AF_INET6,
socket.SOCK_STREAM,
6,
"",
(10, b"\x01\xbb\x00\x00\x00\x00*\x04NB\x00\x1a\x00\x00"),
)
]

loop.getaddrinfo = unknown_addrinfo
derlih marked this conversation as resolved.
Show resolved Hide resolved
resolver = ThreadedResolver()
resolver._loop = loop
with patch("socket.has_ipv6", False):
res = await resolver.resolve("www.python.org")
assert len(res) == 0


async def test_close_for_threaded_resolver(loop: Any) -> None:
resolver = ThreadedResolver()
await resolver.close()
Expand Down