Repository navigation
Reject IPv6 hosts with out-of-range embedded IPv4 octets - #172
Open
amalsaidov wants to merge 1 commit into
Open
amalsaidov wants to merge 1 commit into
amalsaidov wants to merge 1 commit into
Conversation
The least-significant 32 bits of an IPv6 address may be written as an embedded IPv4address (RFC 3986 Section 3.2.2), e.g. [::ffff:192.0.2.1]. Each dec-octet of that IPv4address is limited to 0-255, but the IPv6 grammar regex does not enforce the range, so host_is_valid() accepted literals such as [::256.1.1.1]. Range-check the embedded IPv4, as is already done for a bare IPv4 host.
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
validators.host_is_valid()accepts IPv6 host literals whose embedded IPv4 address (thels32) contains out-of-range octets, e.g.[::256.1.1.1].Per RFC 3986 Section 3.2.2, the least-significant 32 bits of an
IPv6addressmay be written as an embeddedIPv4address:So
256.1.1.1is not a validIPv4address, and[::256.1.1.1]is therefore not a valid host. The IPv6 grammar regex matches the dotted-quad shape with[0-9]{1,3}but does not enforce the0-255range. The library already range-checks a bare IPv4 host viavalid_ipv4_host_address()(added in #49), but the same check was missing for IPv4 embedded in an IPv6 literal.Reproduction (before this change)
Fix
In
host_is_valid(), after the IPv6 literal matches the grammar, extract any embedded IPv4 address (stripping a trailing RFC 6874 ZoneID first) and range-check its octets with the existingvalid_ipv4_host_address()helper. Pure IPv6 literals and valid embedded IPv4 ([::ffff:192.0.2.1],[::255.255.255.255]) are unaffected.Tests
Added parametrized regression tests in
tests/test_validators.pycovering both the rejected out-of-range cases and the still-valid cases. The out-of-range tests fail before this change (DID NOT RAISE InvalidComponentsError) and pass after. Full suite:2908 passed, 13 skipped;flake8/blackclean.