Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ Common
(#2152)
[Miguel Caballer - @micafer]

- [Utils] Fix ``is_valid_ip_address`` raising ``ValueError`` instead of
returning ``False`` for an address containing an embedded null byte
(e.g. ``"1.2.3.4\x00"``). ``socket.inet_pton`` raises ``ValueError`` rather
than ``OSError`` in that case, which was not caught.
(#2185)
[Ashish - @Ashishjob]

Compute
~~~~~~~

Expand Down
5 changes: 5 additions & 0 deletions libcloud/test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,9 @@ def test_is_valid_ip_address(self):
"256.256.256.256",
"0.567.567.567",
"192.168.0.257",
# Embedded null byte makes inet_pton raise ValueError, not OSError
"192.168.1.100\x00",
"10.0.0.1\x00extra",
]

valid_ipv6_addresses = [
Expand All @@ -436,6 +439,8 @@ def test_is_valid_ip_address(self):
invalid_ipv6_addresses = [
"2607:f0d",
"2607:f0d0:0004",
# Embedded null byte makes inet_pton raise ValueError, not OSError
"::1\x00",
]

for address in valid_ipv4_addresses:
Expand Down
7 changes: 7 additions & 0 deletions libcloud/utils/networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ def is_valid_ip_address(address, family=socket.AF_INET):

:return: ``bool`` True if the provided address is valid.
"""
# inet_pton handles an embedded null byte (e.g. "1.2.3.4\x00")
# inconsistently across interpreters -- CPython raises ValueError while
# PyPy silently accepts it -- and such a string is never a valid address,
# so reject it explicitly for consistent behaviour.
if "\x00" in address:
return False

try:
socket.inet_pton(family, address)
except OSError:
Expand Down