From 86299b6238ff610618dfe7dc09fbba51e3911a95 Mon Sep 17 00:00:00 2001 From: Ashishjob Date: Wed, 2 Sep 2026 01:48:21 -0500 Subject: [PATCH 1/3] Fix is_valid_ip_address raising ValueError on embedded null byte socket.inet_pton raises ValueError (not OSError) when the address string contains an embedded null byte, e.g. "1.2.3.4\x00". is_valid_ip_address only caught OSError, so such input propagated the ValueError instead of being reported as invalid. A validity predicate should return False for a malformed address, never raise. Catch ValueError as well and add regression cases (IPv4 and IPv6 addresses with embedded null bytes) to test_is_valid_ip_address. Co-Authored-By: Claude Opus 4.8 --- libcloud/test/test_utils.py | 5 +++++ libcloud/utils/networking.py | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/libcloud/test/test_utils.py b/libcloud/test/test_utils.py index 49a480587f..7a8d084386 100644 --- a/libcloud/test/test_utils.py +++ b/libcloud/test/test_utils.py @@ -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 = [ @@ -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: diff --git a/libcloud/utils/networking.py b/libcloud/utils/networking.py index 349abac3de..999068edc0 100644 --- a/libcloud/utils/networking.py +++ b/libcloud/utils/networking.py @@ -80,6 +80,11 @@ def is_valid_ip_address(address, family=socket.AF_INET): socket.inet_pton(family, address) except OSError: return False + except ValueError: + # inet_pton raises ValueError (not OSError) when the address contains + # an embedded null byte, e.g. "1.2.3.4\x00". Such a string is not a + # valid address, so return False instead of propagating the error. + return False return True From 207cbf8b5baa2cf8b4998f771d9aa81b08263ab8 Mon Sep 17 00:00:00 2001 From: Ashishjob Date: Wed, 2 Sep 2026 01:49:18 -0500 Subject: [PATCH 2/3] Add CHANGES entry for is_valid_ip_address null-byte fix (#2185) Co-Authored-By: Claude Opus 4.8 --- CHANGES.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 45ba37e7fa..8ca49f6ce8 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 ~~~~~~~ From 1e4c5622fc42e45c3dcdd69c20241ada641dfc11 Mon Sep 17 00:00:00 2001 From: Ashishjob Date: Wed, 2 Sep 2026 07:27:36 -0500 Subject: [PATCH 3/3] Reject embedded null bytes explicitly for cross-interpreter consistency CPython's socket.inet_pton raises ValueError on an embedded null byte while PyPy silently accepts it, so catching ValueError alone left is_valid_ip_address returning True for e.g. "1.2.3.4\x00" on PyPy (and failing the regression test there). A string with a null byte is never a valid address, so reject it explicitly before calling inet_pton, giving the same result on every interpreter. Co-Authored-By: Claude Opus 4.8 --- libcloud/utils/networking.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/libcloud/utils/networking.py b/libcloud/utils/networking.py index 999068edc0..4ce7bc6a11 100644 --- a/libcloud/utils/networking.py +++ b/libcloud/utils/networking.py @@ -76,15 +76,17 @@ 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: return False - except ValueError: - # inet_pton raises ValueError (not OSError) when the address contains - # an embedded null byte, e.g. "1.2.3.4\x00". Such a string is not a - # valid address, so return False instead of propagating the error. - return False return True