Skip to content

Commit de562cf

Browse files
Certificate and VM hostname validation improvements
1 parent 47f6019 commit de562cf

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVmIpAddressCommandWrapper.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public Answer execute(final GetVmIpAddressCommand command, final LibvirtComputin
4242
String ip = null;
4343
boolean result = false;
4444
String vmName = command.getVmName();
45+
if (!NetUtils.verifyDomainNameLabel(vmName, true)) {
46+
return new Answer(command, result, ip);
47+
}
4548
String sanitizedVmName = sanitizeBashCommandArgument(vmName);
4649
String networkCidr = command.getVmNetworkCidr();
4750
List<String[]> commands = new ArrayList<>();

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtSetupDirectDownloadCertificateCommandWrapper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.cloud.utils.FileUtil;
3838
import com.cloud.utils.PropertiesUtil;
3939
import com.cloud.utils.exception.CloudRuntimeException;
40+
import com.cloud.utils.net.NetUtils;
4041
import com.cloud.utils.script.Script;
4142

4243
@ResourceWrapper(handles = SetupDirectDownloadCertificateCommand.class)
@@ -132,6 +133,9 @@ protected void cleanupTemporaryFile(String temporaryFile) {
132133
public Answer execute(SetupDirectDownloadCertificateCommand cmd, LibvirtComputingResource serverResource) {
133134
String certificate = cmd.getCertificate();
134135
String certificateName = cmd.getCertificateName();
136+
if (!NetUtils.verifyDomainNameLabel(certificateName, false)) {
137+
return new Answer(cmd, false, "The provided certificate name is invalid");
138+
}
135139

136140
try {
137141
File agentFile = getAgentPropertiesFile();

utils/src/main/java/com/cloud/utils/net/NetUtils.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ public class NetUtils {
9999
public final static int IPV6_EUI64_11TH_BYTE = -1;
100100
public final static int IPV6_EUI64_12TH_BYTE = -2;
101101

102+
// Regex
103+
public final static Pattern HOSTNAME_PATTERN = Pattern.compile("[a-zA-Z0-9-]+");
104+
public final static Pattern START_HOSTNAME_PATTERN = Pattern.compile("^[0-9-].*");
105+
102106
public static String extractHost(String uri) throws URISyntaxException {
103107
return (new URI(uri)).getHost();
104108
}
@@ -1061,13 +1065,13 @@ public static boolean verifyDomainNameLabel(final String hostName, final boolean
10611065
if (hostName.length() > 63 || hostName.length() < 1) {
10621066
s_logger.warn("Domain name label must be between 1 and 63 characters long");
10631067
return false;
1064-
} else if (!hostName.toLowerCase().matches("[a-z0-9-]*")) {
1068+
} else if (!HOSTNAME_PATTERN.matcher(hostName).matches()) {
10651069
s_logger.warn("Domain name label may contain only the ASCII letters 'a' through 'z' (in a case-insensitive manner)");
10661070
return false;
10671071
} else if (hostName.startsWith("-") || hostName.endsWith("-")) {
10681072
s_logger.warn("Domain name label can not start with a hyphen and digit, and must not end with a hyphen");
10691073
return false;
1070-
} else if (isHostName && hostName.matches("^[0-9-].*")) {
1074+
} else if (isHostName && START_HOSTNAME_PATTERN.matcher(hostName).matches()) {
10711075
s_logger.warn("Host name can't start with digit");
10721076
return false;
10731077
}

0 commit comments

Comments
 (0)