Skip to content

Commit 9ed6789

Browse files
committed
Make stricter IP format check in IpAddressMatcher
Closes: gh-17499 Signed-off-by: Andrey Litvitski <andrey1010102008@gmail.com>
1 parent e48fdd5 commit 9ed6789

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@
3535
*
3636
* @author Luke Taylor
3737
* @author Steve Riesenberg
38+
* @author Andrey Litvitski
3839
* @since 3.0.2
3940
*/
4041
public final class IpAddressMatcher implements RequestMatcher {
4142

42-
private static Pattern IPV4 = Pattern.compile("\\d{0,3}.\\d{0,3}.\\d{0,3}.\\d{0,3}(/\\d{0,3})?");
43+
private static Pattern IPV4 = Pattern.compile("^(\\d{1,3}\\.){3}\\d{1,3}(/\\d{1,3})?$");
4344

4445
private final InetAddress requiredAddress;
4546

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
/**
2929
* @author Luke Taylor
30+
* @author Andrey Litvitski
3031
*/
3132
public class IpAddressMatcherTests {
3233

@@ -167,4 +168,12 @@ public void toStringWhenOnlyIpIsProvidedThenReturnsIpAddressOnly() {
167168
assertThat(matcher.toString()).hasToString("IpAddress [127.0.0.1]");
168169
}
169170

171+
// gh-17499
172+
@Test
173+
public void constructorRejectsInvalidIpv4WithX() {
174+
String badIp = "10x1x1x1";
175+
assertThatIllegalArgumentException().isThrownBy(() -> new IpAddressMatcher(badIp))
176+
.withMessage("ipAddress 10x1x1x1 doesn't look like an IP Address. Is it a host name?");
177+
}
178+
170179
}

0 commit comments

Comments
 (0)