Skip to content

Commit 10e7dbd

Browse files
author
lucaijun
committed
AJ-551: throw error when invalid hostname
1 parent 6f25190 commit 10e7dbd

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/com/xxdb/SimpleDBConnectionPoolConfig.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.xxdb;
22

3+
import org.apache.commons.validator.routines.InetAddressValidator;
34
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
56
import sun.net.util.IPAddressUtil;
@@ -130,7 +131,7 @@ public void validate() {
130131
hostName = "localhost";
131132
log.warn("HostName not set, use the default value 'localhost'");
132133
}
133-
if (checkHostNameValid(hostName))
134+
if (!checkHostNameValid(hostName))
134135
throw new RuntimeException(String.format("Invalid hostName: %s", hostName));
135136
if (port <= 0) {
136137
port = 8848;
@@ -154,15 +155,37 @@ private static String getNullIfEmpty(String text) {
154155

155156
private static boolean checkHostNameValid(String hostName) {
156157
return hostName.equals("localhost") ||
157-
IPAddressUtil.isIPv4LiteralAddress(hostName) ||
158+
isIPV4(hostName) ||
158159
IPAddressUtil.isIPv6LiteralAddress(hostName) ||
159160
isDomain(hostName);
160161
}
161162

162163
private static boolean isDomain(String hostName) {
163-
String regex = "^[a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?(\\.[a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?)*$";
164+
String regex = "^([a-z0-9]+(-[a-z0-9]+)*\\.)+[a-z]{2,}$";
164165
return hostName.matches(regex);
165166
}
167+
168+
private static boolean isIPV4(String hostName) {
169+
String regex = "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$";
170+
if (hostName == null || hostName.trim().isEmpty()) {
171+
return false;
172+
}
173+
if (!hostName.matches(regex)) {
174+
return false;
175+
}
176+
String[] parts = hostName.split("\\.");
177+
try {
178+
for (String segment : parts) {
179+
if (Integer.parseInt(segment) > 255 ||
180+
(segment.length() > 1 && segment.startsWith("0"))) {
181+
return false;
182+
}
183+
}
184+
} catch (NumberFormatException e) {
185+
return false;
186+
}
187+
return true;
188+
}
166189
}
167190

168191

0 commit comments

Comments
 (0)