Fix is_valid_ip_address raising ValueError on embedded null byte - #2185
Merged
micafer merged 3 commits intoSep 2, 2026
Conversation
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 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## trunk #2185 +/- ##
=======================================
Coverage 83.59% 83.59%
=======================================
Files 352 352
Lines 81852 81854 +2
Branches 8771 8772 +1
=======================================
+ Hits 68417 68421 +4
+ Misses 10562 10561 -1
+ Partials 2873 2872 -1
🚀 New features to boost your workflow:
|
Contributor
|
Tests are failing could you check it? |
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 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
libcloud.utils.networking.is_valid_ip_addressis a predicate that should returnTrue/Falsefor whether a string is a valid IP address. It callssocket.inet_ptonand catchesOSErrorto returnFalsefor malformed input.However,
socket.inet_ptonraisesValueError(notOSError) when the address string contains an embedded null byte, e.g."1.2.3.4\x00". ThatValueErrorwas not caught, so instead of returningFalsethe function propagated an exception to the caller:Fix
Catch
ValueErroras well and returnFalse, since a string with an embedded null byte is not a valid address.Tests
Added regression cases (IPv4 and IPv6 addresses containing embedded null bytes) to the existing
test_is_valid_ip_address. Verified they fail before the change (uncaughtValueError) and pass after it. Fulllibcloud/test/test_utils.pypasses (23 passed, 1 skipped).Changelog
Changelog entry added under Common in a follow-up commit referencing this PR number.