Skip to content

Commit

Permalink
Fixed field form validation for whitelist host.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTekton committed Nov 10, 2019
1 parent db13a2b commit 48e3063
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 54 deletions.
62 changes: 62 additions & 0 deletions src/main/java/org/jenkinsci/plugins/gwt/global/WhitelistItem.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package org.jenkinsci.plugins.gwt.global;

import com.github.jgonian.ipmath.Ipv4;
import com.github.jgonian.ipmath.Ipv4Range;
import com.github.jgonian.ipmath.Ipv6;
import com.github.jgonian.ipmath.Ipv6Range;
import com.google.common.net.InetAddresses;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
Expand Down Expand Up @@ -101,4 +106,61 @@ public FormValidation doCheckHmacCredentialIdItems(@QueryParameter final String
return CredentialsHelper.doCheckFillCredentialsId(value);
}
}

private Boolean validateIpValue(String ipValue) {
Boolean isValid = false;

Boolean isCIDR = false;
Boolean isRange = false;

String[] hostParts = ipValue.split("/");

if (hostParts.length == 2) {
isCIDR = true;
} else {
hostParts = ipValue.split("-");
if (hostParts.length == 2) {
isRange = true;
}
}

if (isCIDR || isRange) {
int leftValueLength = InetAddresses.forString(hostParts[0]).getAddress().length;
if (leftValueLength == 4) {
if (Ipv4Range.parse(ipValue) != null) {
isValid = true;
}
} else if (leftValueLength == 16) {
if (Ipv6Range.parse(ipValue) != null) {
isValid = true;
}
}
} else {
int ipValueLength = InetAddresses.forString(hostParts[0]).getAddress().length;
if (ipValueLength == 4) {
if (Ipv4.parse(ipValue) != null) {
isValid = true;
}
} else if (ipValueLength == 16) {
if (Ipv6.parse(ipValue) != null) {
isValid = true;
}
}
}

return isValid;
}

public FormValidation doCheckHost(@QueryParameter String value) throws Exception {
try {
if (validateIpValue(value)) {
return FormValidation.ok();
} else {
return FormValidation.error(
"IP address must be in IPV4 or IPV6 CIDR or IP range notation.");
}
} catch (Exception e) {
return FormValidation.error("Invalid IP address.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
import com.github.jgonian.ipmath.Ipv6Range;
import com.google.common.base.Optional;
import com.google.common.net.InetAddresses;
import hudson.util.FormValidation;
import java.util.List;
import java.util.Map;
import org.jenkinsci.plugins.gwt.global.CredentialsHelper;
import org.jenkinsci.plugins.gwt.global.Whitelist;
import org.jenkinsci.plugins.gwt.global.WhitelistItem;
import org.jenkinsci.plugins.plaincredentials.StringCredentials;
import org.kohsuke.stapler.QueryParameter;

public class WhitelistVerifier {

Expand Down Expand Up @@ -166,56 +164,4 @@ static void whitelistVerify(
throw new WhitelistException(
"Sending host \"" + remoteHost + "\" was not matched by whitelist.");
}

private Boolean validateIpValue(String ipValue) {
Boolean isValid = false;

Boolean isCIDR = false;
Boolean isRange = false;

String[] hostParts = ipValue.split("/");

if (hostParts.length == 2) {
isCIDR = true;
} else {
hostParts = ipValue.split("-");
if (hostParts.length == 2) {
isRange = true;
}
}

if (isCIDR || isRange) {
int leftValueLength = InetAddresses.forString(hostParts[0]).getAddress().length;
if (leftValueLength == 4) {
if (Ipv4Range.parse(ipValue) != null) {
isValid = true;
}
} else if (leftValueLength == 16) {
if (Ipv6Range.parse(ipValue) != null) {
isValid = true;
}
}
} else {
int ipValueLength = InetAddresses.forString(hostParts[0]).getAddress().length;
if (ipValueLength == 4) {
if (Ipv4.parse(ipValue) != null) {
isValid = true;
}
} else if (ipValueLength == 16) {
if (Ipv6.parse(ipValue) != null) {
isValid = true;
}
}
}

return isValid;
}

public FormValidation checkIP(@QueryParameter String value) {
if (validateIpValue(value)) {
return FormValidation.ok();
} else {
return FormValidation.error("IP Address must be in IPV4 or IPV6 CIDR or IP range notation.");
}
}
}

0 comments on commit 48e3063

Please sign in to comment.