yuqi1129 commented on code in PR #7548:
URL: https://github.com/apache/gravitino/pull/7548#discussion_r2181406883
##########
catalogs/catalog-jdbc-common/src/main/java/org/apache/gravitino/catalog/jdbc/utils/DataSourceUtils.java:
##########
@@ -76,6 +102,65 @@ private static Properties getProperties(JdbcConfig
jdbcConfig) {
return properties;
}
+ private static String recursiveDecode(String url) {
+ String prev;
+ String decoded = url;
+ int max = 5;
+
+ do {
+ prev = decoded;
+ try {
+ decoded = java.net.URLDecoder.decode(prev, "UTF-8");
+ } catch (Exception e) {
+ throw new GravitinoRuntimeException("Unable to decode JDBC URL");
+ }
+ } while (!prev.equals(decoded) && --max > 0);
+
+ return decoded;
+ }
+
+ private static void checkUnsafeParameters(
+ String url, Map<String, String> config, List<String> unsafeParams,
String dbType) {
+
+ String lowerUrl = url.toLowerCase();
+
+ for (String param : unsafeParams) {
+ String lowerParam = param.toLowerCase();
+ if (lowerUrl.contains(lowerParam) || containsKeyIgnoreCase(config,
param)) {
+ throw new GravitinoRuntimeException(
+ "Unsafe %s parameter '%s' detected in JDBC URL", dbType, param);
+ }
+ }
+ }
+
+ private static void validateJdbcConfig(JdbcConfig jdbcConfig) {
+ String driver = jdbcConfig.getJdbcDriver();
+ String url = jdbcConfig.getJdbcUrl();
+ Map<String, String> all = jdbcConfig.getAllConfig();
+ String lowerUrl = url.toLowerCase();
+ String decodedUrl = recursiveDecode(lowerUrl);
+
+ if (driver != null) {
+ if (decodedUrl.startsWith("jdbc:mysql")) {
+ checkUnsafeParameters(decodedUrl, all, mysqlParameters, "MySQL");
+ } else if (decodedUrl.startsWith("jdbc:mariadb")) {
+ checkUnsafeParameters(decodedUrl, all, mysqlParameters, "MariaDB");
+ } else if (decodedUrl.startsWith("jdbc:postgresql")) {
+ checkUnsafeParameters(decodedUrl, all, pgParameters, "PostgreSQL");
+ }
+ }
+ }
+
+ private static boolean containsKeyIgnoreCase(Map<String, String> map, String
key) {
Review Comment:
I'm still confused. Why do we need it? Assuming the following properties for
JDBC catalog:
```
jdbc-driver=com.jdbc.mysql.driver
jdbc-user=xxx
jdbc-password=xxx
maxAllowedPacket=1000 // this configuration won't be applied to the data
source because JDBC catalog only accepts predefined keys and then applies them
to the data source.
allowloadlocalinfile=true // ditto
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]