Skip to content

Reject zone IDs and brackets in netmask strings - #150

Open
mokshupadhyay wants to merge 1 commit into
ruby:masterfrom
mokshupadhyay:fix/reject-zone-and-brackets-in-mask
Open

mokshupadhyay wants to merge 1 commit into
ruby:masterfrom
mokshupadhyay:fix/reject-zone-and-brackets-in-mask

Conversation

@mokshupadhyay

@mokshupadhyay mokshupadhyay commented Dec 1, 2026 •

Copy link
Copy Markdown

IPAddr#mask! already rejects a netmask string that contains a / (IPAddr::InvalidAddressError, "invalid mask"), but a mask string with a zone ID (%eth0) or with brackets ([...]) was accepted: the string is handed to IPAddr.new, which accepts a zone ID or brackets in an address, and the result looked like a normal network.

On master:

IPAddr.new("fe80::1/ffff::%eth0")      # => fe80::/16   (accepted)
IPAddr.new("fe80::1/[ffff::]")         # => fe80::/16   (accepted)
IPAddr.new("fe80::1").mask("ffff:ffff::%eth0")  # => fe80::/32
IPAddr.new("fe80::1").mask("[ffff:ffff::]")     # => fe80::/32

A netmask is not an address with a scope or a bracketed URL host, so these are not valid masks. With this change they raise IPAddr::InvalidAddressError ("invalid mask ..."), as a mask containing / already does. The pattern that detects an invalid mask in mask! goes from %r{/} to %r{[/%\[\]]}; masks without those characters are not affected.

Tests: test_s_new (TC_IPAddr) and test_mask (TC_Operator) in test/test_ipaddr.rb get the four cases above. They fail on master ("InvalidAddressError expected but nothing was raised") and pass now. bundle exec rake test passes (44 tests, 655 assertions, 0 failures). Ruby 3.4.5.

This does not overlap with #148, which changes how mask! treats non-String, non-Integer arguments; this change only touches the pattern for String masks.

@mokshupadhyay
mokshupadhyay requested a review from knu as a code owner December 1, 2026 04:46

This branch has not been deployed

No deployments
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