Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ public boolean matches(@Nullable InetAddress toCheck) {
}
byte[] remAddr = toCheck.getAddress();
byte[] reqAddr = this.requiredAddress.getAddress();
if (remAddr.length != reqAddr.length) {
// Different address families (IPv4 vs IPv6): never match, per the class-level
// contract. Without this check, a mask wide enough to exceed the shorter
// array's length throws ArrayIndexOutOfBoundsException instead of returning
// false.
return false;
}
int nMaskFullBytes = this.nMaskBits / 8;
byte finalByte = (byte) (0xFF00 >> (this.nMaskBits & 0x07));
for (int i = 0; i < nMaskFullBytes; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,21 @@ void matchesWhenIpv6AndIpv4AddressThenReturnsFalse() throws Exception {
assertThat(matcher.matches(InetAddress.getByName("192.168.1.1"))).isFalse();
}

@Test
void matchesWhenIpv6CidrAndIpv4AddressThenReturnsFalse() throws Exception {
// The mask spans more full bytes (8) than the IPv4 address has (4). Without a
// family/length check, walking the byte arrays runs past the shorter one and
// throws ArrayIndexOutOfBoundsException instead of returning false.
IpInetAddressMatcher matcher = new IpInetAddressMatcher("2001:db8::/64");
assertThat(matcher.matches(InetAddress.getByName("32.1.13.184"))).isFalse();
}

@Test
void matchesWhenIpv4CidrAndIpv6AddressThenReturnsFalse() throws Exception {
IpInetAddressMatcher matcher = new IpInetAddressMatcher("192.168.1.0/24");
assertThat(matcher.matches(InetAddress.getByName("2001:db8::"))).isFalse();
}

@Test
void matchesWhenStringIpv4MatchThenReturnsTrue() {
IpInetAddressMatcher matcher = new IpInetAddressMatcher("192.168.1.1");
Expand Down