|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.gravitino.utils; |
| 21 | + |
| 22 | +import java.net.URLDecoder; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import org.apache.gravitino.exceptions.GravitinoRuntimeException; |
| 27 | + |
| 28 | +/** |
| 29 | + * Utility class for validating JDBC URLs and configurations. It checks for unsafe parameters |
| 30 | + * specific to MySQL, MariaDB, and PostgreSQL JDBC URLs. Unsafe parameters can lead to security |
| 31 | + * vulnerabilities or unexpected behavior, so this utility helps ensure that JDBC configurations are |
| 32 | + * safe to use. |
| 33 | + */ |
| 34 | +public class JdbcUrlUtils { |
| 35 | + |
| 36 | + // Unsafe parameters for MySQL and MariaDB, other parameters like |
| 37 | + // trustCertificateKeyStoreUrl, serverTimezone, characterEncoding, |
| 38 | + // useSSL are also risky, please use it with caution. |
| 39 | + private static final List<String> UNSAFE_MYSQL_PARAMETERS = |
| 40 | + Arrays.asList( |
| 41 | + "maxAllowedPacket", |
| 42 | + "autoDeserialize", |
| 43 | + "queryInterceptors", |
| 44 | + "statementInterceptors", |
| 45 | + "detectCustomCollations", |
| 46 | + "allowloadlocalinfile", |
| 47 | + "allowUrlInLocalInfile", |
| 48 | + "allowLoadLocalInfileInPath"); |
| 49 | + |
| 50 | + private static final List<String> UNSAFE_POSTGRES_PARAMETERS = |
| 51 | + Arrays.asList( |
| 52 | + "socketFactory", |
| 53 | + "socketFactoryArg", |
| 54 | + "sslfactory", |
| 55 | + "sslhostnameverifier", |
| 56 | + "sslpasswordcallback", |
| 57 | + "authenticationPluginClassName"); |
| 58 | + |
| 59 | + private JdbcUrlUtils() { |
| 60 | + // Utility class, no instantiation allowed |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Validates the JDBC configuration by checking the driver and URL for unsafe parameters. |
| 65 | + * |
| 66 | + * @param driver the JDBC driver class name |
| 67 | + * @param url the JDBC URL |
| 68 | + * @param all the JDBC configuration properties |
| 69 | + */ |
| 70 | + public static void validateJdbcConfig(String driver, String url, Map<String, String> all) { |
| 71 | + String lowerUrl = url.toLowerCase(); |
| 72 | + String decodedUrl = recursiveDecode(lowerUrl); |
| 73 | + |
| 74 | + // As H2 is only used for testing, we do not check unsafe parameters for H2. |
| 75 | + if (driver != null) { |
| 76 | + if (decodedUrl.startsWith("jdbc:mysql")) { |
| 77 | + checkUnsafeParameters(decodedUrl, all, UNSAFE_MYSQL_PARAMETERS, "MySQL"); |
| 78 | + } else if (decodedUrl.startsWith("jdbc:mariadb")) { |
| 79 | + checkUnsafeParameters(decodedUrl, all, UNSAFE_MYSQL_PARAMETERS, "MariaDB"); |
| 80 | + } else if (decodedUrl.startsWith("jdbc:postgresql")) { |
| 81 | + checkUnsafeParameters(decodedUrl, all, UNSAFE_POSTGRES_PARAMETERS, "PostgreSQL"); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private static void checkUnsafeParameters( |
| 87 | + String url, Map<String, String> config, List<String> unsafeParams, String dbType) { |
| 88 | + |
| 89 | + String lowerUrl = url.toLowerCase(); |
| 90 | + |
| 91 | + for (String param : unsafeParams) { |
| 92 | + String lowerParam = param.toLowerCase(); |
| 93 | + if (lowerUrl.contains(lowerParam) || containsValueIgnoreCase(config, param)) { |
| 94 | + throw new GravitinoRuntimeException( |
| 95 | + "Unsafe %s parameter '%s' detected in JDBC URL", dbType, param); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private static String recursiveDecode(String url) { |
| 101 | + String prev; |
| 102 | + String decoded = url; |
| 103 | + int max = 5; |
| 104 | + |
| 105 | + do { |
| 106 | + prev = decoded; |
| 107 | + try { |
| 108 | + decoded = URLDecoder.decode(prev, "UTF-8"); |
| 109 | + } catch (Exception e) { |
| 110 | + throw new GravitinoRuntimeException("Unable to decode JDBC URL"); |
| 111 | + } |
| 112 | + } while (!prev.equals(decoded) && --max > 0); |
| 113 | + |
| 114 | + return decoded; |
| 115 | + } |
| 116 | + |
| 117 | + private static boolean containsValueIgnoreCase(Map<String, String> map, String value) { |
| 118 | + for (String keyValue : map.values()) { |
| 119 | + if (keyValue != null && keyValue.equalsIgnoreCase(value)) { |
| 120 | + return true; |
| 121 | + } |
| 122 | + } |
| 123 | + return false; |
| 124 | + } |
| 125 | +} |
0 commit comments