Browse Source

Add hasText assertion to IpAddressMatcher constructor

Issue gh-15527

(cherry picked from commit 3a298196512de5f3002707e2af8298d650033df7)
Steve Riesenberg 9 months ago
parent
commit
ddf4542a9e

+ 1 - 0
web/src/main/java/org/springframework/security/web/util/matcher/IpAddressMatcher.java

@@ -50,6 +50,7 @@ public final class IpAddressMatcher implements RequestMatcher {
 	 * come.
 	 */
 	public IpAddressMatcher(String ipAddress) {
+		Assert.hasText(ipAddress, "ipAddress cannot be empty");
 		assertNotHostName(ipAddress);
 		if (ipAddress.indexOf('/') > 0) {
 			String[] addressAndMask = StringUtils.split(ipAddress, "/");

+ 14 - 0
web/src/test/java/org/springframework/security/web/util/matcher/IpAddressMatcherTests.java

@@ -139,4 +139,18 @@ public class IpAddressMatcherTests {
 		assertThat(this.v4matcher.matches((String) null)).isFalse();
 	}
 
+	// gh-15527
+	@Test
+	public void constructorWhenRequiredAddressIsNullThenThrowsIllegalArgumentException() {
+		assertThatIllegalArgumentException().isThrownBy(() -> new IpAddressMatcher(null))
+			.withMessage("ipAddress cannot be empty");
+	}
+
+	// gh-15527
+	@Test
+	public void constructorWhenRequiredAddressIsEmptyThenThrowsIllegalArgumentException() {
+		assertThatIllegalArgumentException().isThrownBy(() -> new IpAddressMatcher(""))
+			.withMessage("ipAddress cannot be empty");
+	}
+
 }