Add validation IpAddressMatcher
Closes gh-13621
This commit is contained in:
committed by
Josh Cummings
parent
d7599ab192
commit
c1adeef0da
@@ -47,6 +47,7 @@ public final class IpAddressMatcher implements RequestMatcher {
|
||||
* come.
|
||||
*/
|
||||
public IpAddressMatcher(String ipAddress) {
|
||||
assertStartsWithHexa(ipAddress);
|
||||
if (ipAddress.indexOf('/') > 0) {
|
||||
String[] addressAndMask = StringUtils.split(ipAddress, "/");
|
||||
ipAddress = addressAndMask[0];
|
||||
@@ -67,6 +68,7 @@ public final class IpAddressMatcher implements RequestMatcher {
|
||||
}
|
||||
|
||||
public boolean matches(String address) {
|
||||
assertStartsWithHexa(address);
|
||||
InetAddress remoteAddress = parseAddress(address);
|
||||
if (!this.requiredAddress.getClass().equals(remoteAddress.getClass())) {
|
||||
return false;
|
||||
@@ -89,6 +91,13 @@ public final class IpAddressMatcher implements RequestMatcher {
|
||||
return true;
|
||||
}
|
||||
|
||||
private void assertStartsWithHexa(String ipAddress) {
|
||||
Assert.isTrue(
|
||||
ipAddress.charAt(0) == '[' || ipAddress.charAt(0) == ':'
|
||||
|| Character.digit(ipAddress.charAt(0), 16) != -1,
|
||||
"ipAddress must start with a [, :, or a hexadecimal digit");
|
||||
}
|
||||
|
||||
private InetAddress parseAddress(String address) {
|
||||
try {
|
||||
return InetAddress.getByName(address);
|
||||
|
||||
Reference in New Issue
Block a user