Skip to content

Commit ab6440d

Browse files
clementkngjzheaux
authored andcommitted
Throws exception when passed IP address with too long mask
Fixes gh-2790
1 parent d5e5ac0 commit ab6440d

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -22,6 +22,7 @@
2222

2323
import org.springframework.security.web.util.matcher.RequestMatcher;
2424
import org.springframework.util.StringUtils;
25+
import org.springframework.util.Assert;
2526

2627
/**
2728
* Matches a request based on IP Address or subnet mask matching against the remote
@@ -55,6 +56,9 @@ public IpAddressMatcher(String ipAddress) {
5556
nMaskBits = -1;
5657
}
5758
requiredAddress = parseAddress(ipAddress);
59+
Assert.isTrue(requiredAddress.getAddress().length * 8 >= nMaskBits,
60+
String.format("IP address %s is too short for bitmask of length %d",
61+
ipAddress, nMaskBits));
5862
}
5963

6064
public boolean matches(HttpServletRequest request) {

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -84,4 +84,24 @@ public void zeroMaskMatchesAnything() throws Exception {
8484
assertThat(matcher.matches("123.4.5.6")).isTrue();
8585
assertThat(matcher.matches("192.168.0.159")).isTrue();
8686
}
87+
88+
// SEC-2576
89+
@Test
90+
public void ipv4RequiredAddressMaskTooLongThenIllegalArgumentException() {
91+
String ipv4AddressWithTooLongMask = "192.168.1.104/33";
92+
assertThatCode(() -> new IpAddressMatcher(ipv4AddressWithTooLongMask))
93+
.isInstanceOf(IllegalArgumentException.class)
94+
.hasMessage(String.format("IP address %s is too short for bitmask of " +
95+
"length %d", "192.168.1.104", 33));
96+
}
97+
98+
// SEC-2576
99+
@Test
100+
public void ipv6RequiredAddressMaskTooLongThenIllegalArgumentException() {
101+
String ipv6AddressWithTooLongMask = "fe80::21f:5bff:fe33:bd68/129";
102+
assertThatCode(() -> new IpAddressMatcher(ipv6AddressWithTooLongMask))
103+
.isInstanceOf(IllegalArgumentException.class)
104+
.hasMessage(String.format("IP address %s is too short for bitmask of " +
105+
"length %d", "fe80::21f:5bff:fe33:bd68", 129));
106+
}
87107
}

0 commit comments

Comments
 (0)