Skip to content

Fix family mismatch crash in IpInetAddressMatcher - #19734

Open
minwoo-3 wants to merge 1 commit into
spring-projects:mainfrom
minwoo-3:gh-19733
Open

minwoo-3 wants to merge 1 commit into
spring-projects:mainfrom
minwoo-3:gh-19733

Conversation

@minwoo-3

Copy link
Copy Markdown

Fixes #19733

IpInetAddressMatcher#matches(InetAddress) (backing IpAddressMatcher /
InetAddressMatchers, added in 7.1) compares the byte arrays of the
configured address and the address being checked without first verifying
they're the same length.

The class-level Javadoc on IpAddressMatcher documents:

a matcher which is configured with an IPv4 address will never match a
request which returns an IPv6 address, and vice-versa.

When the configured CIDR mask requires more full bytes than the shorter of
the two addresses has (e.g. an IPv6 /64 matcher checked against an IPv4
address), and the leading bytes happen to be equal up to the shorter
array's length, the comparison loop indexes past the end of the shorter
array and throws ArrayIndexOutOfBoundsException instead of returning
false as documented.

Repro (see #19733 for the full write-up):

IpAddressMatcher matcher = new IpAddressMatcher("2001:db8::/64");
matcher.matches("32.1.13.184"); // throws ArrayIndexOutOfBoundsException

This PR adds a length check that returns false immediately when the two
addresses are different lengths (different families), matching the
documented contract, plus two regression tests covering both directions
(IPv6-configured matcher checked against IPv4, and vice versa).

Ran ./gradlew format and :spring-security-core:check locally; both
pass, along with the full IpInetAddressMatcherTests suite.

IpInetAddressMatcher#matches(InetAddress) compared the byte
arrays of the two addresses without first checking that they
are the same length. When the configured CIDR mask required
more full bytes than the shorter of the two addresses had
(for example an IPv6 /64 matcher checked against an IPv4
address), and the leading bytes happened to be equal up to
the shorter array's length, the comparison loop indexed past
the end of the shorter array and threw
ArrayIndexOutOfBoundsException instead of returning false, as
documented on IpAddressMatcher.

Closes spring-projectsgh-19733

Signed-off-by: minwoo-3 <s26002@gsm.hs.kr>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IpInetAddressMatcher throws ArrayIndexOutOfBoundsException instead of returning false for mismatched IPv4/IPv6 families

2 participants