Skip to content

Commit c1adeef

Browse files
FdHerrerajzheaux
authored andcommitted
Add validation IpAddressMatcher
Closes gh-13621
1 parent d7599ab commit c1adeef

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

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

+9
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public final class IpAddressMatcher implements RequestMatcher {
4747
* come.
4848
*/
4949
public IpAddressMatcher(String ipAddress) {
50+
assertStartsWithHexa(ipAddress);
5051
if (ipAddress.indexOf('/') > 0) {
5152
String[] addressAndMask = StringUtils.split(ipAddress, "/");
5253
ipAddress = addressAndMask[0];
@@ -67,6 +68,7 @@ public boolean matches(HttpServletRequest request) {
6768
}
6869

6970
public boolean matches(String address) {
71+
assertStartsWithHexa(address);
7072
InetAddress remoteAddress = parseAddress(address);
7173
if (!this.requiredAddress.getClass().equals(remoteAddress.getClass())) {
7274
return false;
@@ -89,6 +91,13 @@ public boolean matches(String address) {
8991
return true;
9092
}
9193

94+
private void assertStartsWithHexa(String ipAddress) {
95+
Assert.isTrue(
96+
ipAddress.charAt(0) == '[' || ipAddress.charAt(0) == ':'
97+
|| Character.digit(ipAddress.charAt(0), 16) != -1,
98+
"ipAddress must start with a [, :, or a hexadecimal digit");
99+
}
100+
92101
private InetAddress parseAddress(String address) {
93102
try {
94103
return InetAddress.getByName(address);

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

+6
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,10 @@ public void ipv6RequiredAddressMaskTooLongThenIllegalArgumentException() {
105105
"fe80::21f:5bff:fe33:bd68", 129));
106106
}
107107

108+
@Test
109+
public void invalidAddressThenIllegalArgumentException() {
110+
assertThatIllegalArgumentException().isThrownBy(() -> new IpAddressMatcher("invalid-ip"))
111+
.withMessage("ipAddress must start with a [, :, or a hexadecimal digit");
112+
}
113+
108114
}

0 commit comments

Comments
 (0)