Skip to content

Commit 4fdf941

Browse files
committed
Fix InetAddressMatchers#matchExternal to exclude null and any local addresses
This commit fixes the incorrect behavior of `InetAddressMatchers#matchExternal`, where `matchExternal` returns an incorrect answer when passing a null / local address argument, which is not reliable. Closes: gh-19072 Signed-off-by: Andrey Litvitski <andrey1010102008@gmail.com>
1 parent 5fcde78 commit 4fdf941

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

core/src/main/java/org/springframework/security/util/matcher/InetAddressMatchers.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* strategies for IP addresses.
3232
*
3333
* @author Rob Winch
34+
* @author Andrey Litvitski
3435
* @since 7.1
3536
*/
3637
public final class InetAddressMatchers {
@@ -256,6 +257,9 @@ public boolean matches(@Nullable InetAddress address) {
256257
if (address == null) {
257258
return false;
258259
}
260+
if (address.isAnyLocalAddress()) {
261+
return true;
262+
}
259263
if (address.isLoopbackAddress() || address.isLinkLocalAddress() || address.isSiteLocalAddress()) {
260264
return true;
261265
}
@@ -335,6 +339,9 @@ private ExternalInetAddressMatcher() {
335339

336340
@Override
337341
public boolean matches(@Nullable InetAddress address) {
342+
if (address == null) {
343+
return false;
344+
}
338345
return !this.internalMatcher.matches(address);
339346
}
340347

core/src/test/java/org/springframework/security/util/matcher/InetAddressMatchersTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* Tests for {@link InetAddressMatchers}.
3232
*
3333
* @author Rob Winch
34+
* @author Andrey Litvitski
3435
*/
3536
class InetAddressMatchersTests {
3637

@@ -51,6 +52,12 @@ void matchInternalWhenInvokedThenReturnsBuilder() {
5152
assertThat(builder).isNotNull();
5253
}
5354

55+
@Test
56+
void matchesWhenInetAddressNullThenReturnsFalse() {
57+
InetAddressMatcher matcher = InetAddressMatchers.matchExternal().build();
58+
assertThat(matcher.matches((InetAddress) null)).isFalse();
59+
}
60+
5461
@Nested
5562
class BuilderTests {
5663

@@ -410,6 +417,13 @@ void matchesWhenIpv6PublicThenReturnsFalse() throws Exception {
410417
assertThat(matcher.matches(InetAddress.getByName("2001:4860:4860::8888"))).isFalse();
411418
}
412419

420+
@ParameterizedTest
421+
@ValueSource(strings = { "0.0.0.0", "::" })
422+
void matchesWhenWildcardAddressThenReturnsFalse(String address) throws Exception {
423+
InetAddressMatcher matcher = InetAddressMatchers.matchExternal().build();
424+
assertThat(matcher.matches(InetAddress.getByName(address))).isFalse();
425+
}
426+
413427
}
414428

415429
@Nested

0 commit comments

Comments
 (0)