Skip to content

Warn on bracketed IPv6 address with a prefix length - #130

Merged
taketo1113 merged 1 commit into
ruby:masterfrom
taketo1113:warning-bracketed-with-prefix
Oct 17, 2026
Merged

taketo1113 merged 1 commit into
ruby:masterfrom
taketo1113:warning-bracketed-with-prefix

Conversation

@taketo1113

Copy link
Copy Markdown
Collaborator

Summary

IPAddr.new("[2001:db8::]/32") is currently accepted and treated the same as IPAddr.new("2001:db8::/32").
However, square brackets are a delimiter for embedding an address in a larger syntax such as host:port, and no notation is defined for a prefix length after the closing bracket.

This change prints a warning for this form, saying that it will raise IPAddr::InvalidAddressError in a future version. The result is unchanged for now.

Why [2001:db8::]/32 is invalid

RFC 5952: Section 6 defines how an IPv6 address is combined with a port number in text. It says the [] style from RFC 3986 should be used by default (e.g. [2001:db8::1]:80), and that URIs containing IPv6 literals must follow RFC 3986.

In other words, square brackets wrap the address only, as a delimiter for embedding it in a larger syntax.
The RFC does not define a form in which a prefix length follows the closing bracket.
The valid forms are [2001:db8::] (bare address in bracketed style) and 2001:db8::/32 (address with a prefix length, no brackets).

Changes

This Pull Request adds a warning that is printed when a bracketed IPv6 address is followed by a prefix length or a mask.

Behavior

Input Before After
"[2001:db8::]" valid valid (no change)
"2001:db8::/32" valid valid (no change)
"[2001:db8::]/32" accepted accepted, with a warning

Example:

irb> IPAddr.new("[2001:db8::]/32")
(irb):1: warning: bracketed IPv6 address with a prefix length is an invalid address and will raise IPAddr::InvalidAddressError in a future version: [2001:db8::]/32
=> #<IPAddr: IPv6:2001:0db8:0000:0000:0000:0000:0000:0000/ffff:ffff:0000:0000:0000:0000:0000:0000>

IPAddr.new("[2001:db8::]/32") is currently accepted and treated the
same as IPAddr.new("2001:db8::/32"). However, square brackets are a
delimiter for embedding an address in a larger syntax such as
host:port (RFC 5952 section 6), and no notation is defined for a
prefix length after the closing bracket.

Print a warning for this form, saying that it will raise
IPAddr::InvalidAddressError in a future version. The result is
unchanged for now. "[2001:db8::]" and "2001:db8::/32" are not
affected.
@taketo1113
taketo1113 requested a review from knu as a code owner October 16, 2026 08:03
@taketo1113
taketo1113 merged commit 53403d6 into ruby:master Oct 17, 2026
29 checks passed
@taketo1113
taketo1113 deleted the warning-bracketed-with-prefix branch October 17, 2026 02:35
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