Skip to content

Reject IPv6 hosts with out-of-range embedded IPv4 octets - #172

Open
amalsaidov wants to merge 1 commit into
python-hyper:mainfrom
amalsaidov:fix-ipv6-embedded-ipv4-octet-range
Open

amalsaidov wants to merge 1 commit into
python-hyper:mainfrom
amalsaidov:fix-ipv6-embedded-ipv4-octet-range

Conversation

@amalsaidov

Copy link
Copy Markdown

Summary

validators.host_is_valid() accepts IPv6 host literals whose embedded IPv4 address (the ls32) 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 IPv6address may be written as an embedded IPv4address:

ls32        = ( h16 ":" h16 ) / IPv4address
IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
dec-octet   = DIGIT / %x31-39 DIGIT / "1" 2DIGIT / "2" %x30-34 DIGIT / "25" %x30-35   ; 0-255

So 256.1.1.1 is not a valid IPv4address, 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 the 0-255 range. The library already range-checks a bare IPv4 host via valid_ipv4_host_address() (added in #49), but the same check was missing for IPv4 embedded in an IPv6 literal.

Reproduction (before this change)

>>> from rfc3986.validators import host_is_valid
>>> host_is_valid("[::256.1.1.1]")
True            # should be False
>>> host_is_valid("[::ffff:999.999.999.999]")
True            # should be False
>>> host_is_valid("[1:2:3:4:5:6:300.1.1.1]")
True            # should be False

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 existing valid_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.py covering 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/black clean.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant