From b16011e79226c59e0da247c2f3e9ebdd92f55b0b Mon Sep 17 00:00:00 2001 From: James McMullan Date: Tue, 4 Jun 2024 08:44:00 -0400 Subject: [PATCH] HPCC4J-605 Connection: Improve Invalid URL Error Message - Added explicit check for underscores in hostname Signed-off-by: James McMullan James.McMullan@lexisnexis.com --- .../ws/client/utils/Connection.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/wsclient/src/main/java/org/hpccsystems/ws/client/utils/Connection.java b/wsclient/src/main/java/org/hpccsystems/ws/client/utils/Connection.java index a28da183d..0fb11e43f 100644 --- a/wsclient/src/main/java/org/hpccsystems/ws/client/utils/Connection.java +++ b/wsclient/src/main/java/org/hpccsystems/ws/client/utils/Connection.java @@ -12,6 +12,8 @@ import java.util.Base64; import java.util.Base64.Decoder; import java.util.Base64.Encoder; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -205,6 +207,9 @@ public int hashCode() private StringBuffer baseUrl; private StringBuffer uriAndParams; + // Note: this pattern is very basic and is only meant to extract hostnames from URLs + private final static Pattern URL_HOSTNAME_PATTERN = Pattern.compile("((https?|ftp|file):\\/\\/)?(?([\\da-z\\.-_]+)\\.([a-z\\.]{2,6}))(:\\d{2,6})?.*"); + /** Constant CONNECT_TIMEOUT_PARAM="connecttimeoutmillis" */ final static public String CONNECT_TIMEOUT_PARAM = "connecttimeoutmillis"; /** Constant READ_TIMEOUT_PARAM="readtimeoutmillis" */ @@ -287,7 +292,27 @@ public static boolean isSslProtocol(String protocol) */ public Connection(String connectionstring) throws MalformedURLException { - URL theurl = new URL(connectionstring); + URL theurl = null; + try + { + theurl = new URL(connectionstring); + } + catch (MalformedURLException e) + { + Matcher matcher = URL_HOSTNAME_PATTERN.matcher(connectionstring); + if (matcher.matches()) + { + String hostName = matcher.group("hostname"); + if (hostName.contains("_")) + { + throw new MalformedURLException("Invalid URL: Hostname contains invalid underscores: '" + connectionstring + "': " + e.getMessage()); + } + } + else + { + throw e; + } + } setProtocol(theurl.getProtocol());