Skip to content

Allow -, . and ~ in IPv6 zone IDs - #132

Merged
taketo1113 merged 1 commit into
ruby:masterfrom
wleeaf:zone-id-unreserved-characters
Oct 25, 2026
Merged

taketo1113 merged 1 commit into
ruby:masterfrom
wleeaf:zone-id-unreserved-characters

Conversation

@wleeaf

@wleeaf wleeaf commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

IPAddr.new and IPAddr#zone_id= only accept \w characters after %. So zone IDs for common interface names raise InvalidAddressError:

IPAddr.new("fe80::1%eth0.100")   # VLAN interface
IPAddr.new("fe80::1%br-lan")     # bridge, e.g. OpenWrt
# => IPAddr::InvalidAddressError: invalid address: fe80::1%br-lan

ip = IPAddr.new("fe80::1")
ip.zone_id = "%br-lan"
# => IPAddr::InvalidAddressError: invalid zone identifier for address

RFC 6874 (cited when zone IDs were added in #24) defines ZoneID = 1*( unreserved / pct-encoded ), where unreserved is ALPHA / DIGIT / "-" / "." / "_" / "~". Resolv::IPv6 in the resolv gem already accepts %[-0-9A-Za-z._~]+, and Python's ipaddress accepts these addresses too. This change uses the same character class in both places. Characters outside it, such as a space or /, are still rejected.

Tests: added cases to test_to_s_with_zone_id and test_zone_id. Both tests fail before the change (2 errors) and pass after it. bundle exec rake test: 41 tests, 607 assertions, 0 failures.

I compared IPAddr.new with Python's ipaddress on about 50k generated and mutated addresses. Apart from deliberate differences (bracketed addresses, address/mask forms, rejected zero-padded prefixes, mixed notation for ::ffff:0:0/96 in to_s), zone IDs like these were the only valid addresses IPAddr rejected. Python also accepts zone IDs containing : or a newline; this change still rejects those.

IPAddr.new and IPAddr#zone_id= only accepted \w characters after %, so zone IDs for interfaces such as eth0.100 (VLAN) or br-lan raised InvalidAddressError. RFC 6874 defines ZoneID as unreserved characters (ALPHA, DIGIT, -, ., _, ~), which is also what Resolv::IPv6 accepts (%[-0-9A-Za-z._~]+). Use the same character class in both places.
@wleeaf
wleeaf requested a review from knu as a code owner October 24, 2026 23:17
Copilot AI balanced review requested due to automatic review settings October 24, 2026 23:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@taketo1113 taketo1113 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although RFC 6874 has been obsoleted by RFC 9844, RFC 9844 does not define which characters are allowed in a zone ID.
Since characters such as . and - are actually used in environments like OpenWrt, I think it is reasonable to base the allowed characters on unreserved, as referenced by RFC 6874.

References:

@taketo1113
taketo1113 merged commit 349da7e into ruby:master Oct 25, 2026
29 checks passed
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.

3 participants