Skip to content

Commit 554df6f

Browse files
committed
Fix NPE in IpAddressMatcher
Closes gh-15527 (cherry picked from commit 52de894)
1 parent f197f21 commit 554df6f

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

web/src/main/java/org/springframework/security/web/util/matcher/IpAddressMatcher.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ public boolean matches(HttpServletRequest request) {
7171
}
7272

7373
public boolean matches(String address) {
74+
// Do not match null or blank address
75+
if (!StringUtils.hasText(address)) {
76+
return false;
77+
}
78+
7479
assertNotHostName(address);
7580
InetAddress remoteAddress = parseAddress(address);
7681
if (!this.requiredAddress.getClass().equals(remoteAddress.getClass())) {

web/src/test/java/org/springframework/security/web/util/matcher/IpAddressMatcherTests.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -126,4 +126,17 @@ public void numericDomainNameThenIllegalArgumentException() {
126126
.withMessage("ipAddress 123.156.7.18.org doesn't look like an IP Address. Is it a host name?");
127127
}
128128

129+
// gh-15527
130+
@Test
131+
public void matchesWhenIpAddressIsLoopbackAndAddressIsNullThenFalse() {
132+
IpAddressMatcher ipAddressMatcher = new IpAddressMatcher("127.0.0.1");
133+
assertThat(ipAddressMatcher.matches((String) null)).isFalse();
134+
}
135+
136+
// gh-15527
137+
@Test
138+
public void matchesWhenAddressIsNullThenFalse() {
139+
assertThat(this.v4matcher.matches((String) null)).isFalse();
140+
}
141+
129142
}

0 commit comments

Comments
 (0)