Copilot commented on code in PR #931:
URL: https://github.com/apache/ranger/pull/931#discussion_r3158526043
##########
security-admin/src/main/java/org/apache/ranger/security/context/RangerAPIMapping.java:
##########
@@ -344,6 +344,7 @@ private void mapUGWithAPIs() {
apiAssociatedWithUserAndGroups.add(RangerAPIList.SECURE_GET_X_USER);
apiAssociatedWithUserAndGroups.add(RangerAPIList.UPDATE_X_AUDIT_MAP);
apiAssociatedWithUserAndGroups.add(RangerAPIList.UPDATE_X_PERM_MAP);
+ apiAssociatedWithUserAndGroups.add(RangerAPIList.VALIDATE_CONFIG);
Review Comment:
Adding VALIDATE_CONFIG to the Users/Groups tab (and removing it from
Resource Based Policies) does not make the validateConfig API admin-only,
because VALIDATE_CONFIG is still mapped under other tabs (e.g., Tag Based
Policies and Key Manager earlier in this same file). Since isAPIAccessible()
grants access if the user has *any* of the associated tab permissions,
non-admin users with those modules will still be able to call
/services/validateConfig. To restrict this API to Ranger admin only, remove
VALIDATE_CONFIG from the other tab mappings and/or change the endpoint
authorization to use isAdminRole() (or equivalent) instead of tab-based access.
```suggestion
```
##########
hive-agent/src/main/java/org/apache/ranger/services/hive/client/HiveClient.java:
##########
@@ -674,6 +674,7 @@ private void initConnection(String userName, String
password) throws HadoopExcep
String driverClassName =
prop.getProperty("jdbc.driverClassName");
String url = prop.getProperty("jdbc.url");
+ JdbcUrlValidator.validate(url);
if (driverClassName != null) {
Review Comment:
The PR title/description only mention restricting the security-admin
validateConfig API to Ranger admin users, but this PR also introduces Hive JDBC
URL validation (new JdbcUrlValidator + extensive tests) and wires it into
HiveClient. If these Hive changes are intentional, please update the PR
description to cover the additional scope and rationale; otherwise, consider
splitting the Hive-agent changes into a separate PR to keep review/rollback
risk isolated.
##########
hive-agent/src/main/java/org/apache/ranger/services/hive/client/JdbcUrlValidator.java:
##########
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.ranger.services.hive.client;
+
+import org.apache.ranger.plugin.client.HadoopException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.net.URLDecoder;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+public final class JdbcUrlValidator {
+ private static final Logger LOG =
LoggerFactory.getLogger(JdbcUrlValidator.class);
+ private static final Set<String> BLOCKED_PARAMS =
Collections.unmodifiableSet(
+ new HashSet<>(Arrays.asList(
+ "socketfactory", "socketfactoryarg", "sslfactory",
"sslfactoryarg",
+ "sslhostnameverifier", "authenticationpluginclassname",
"loggerclassname",
+ "kerberosservername", "gssdelegatecred",
"sslpasswordcallback")));
+
+ private JdbcUrlValidator() {
+ }
+
+ public static void validate(String jdbcUrl) throws HadoopException {
+ if (jdbcUrl == null || jdbcUrl.trim().isEmpty()) {
+ HadoopException e = new HadoopException("jdbc.url must not be null
or empty");
+ e.generateResponseDataMap(false, "Validation failed", "jdbc.url is
required",
+ null, "jdbc.url");
+ throw e;
+ }
+ String trimmed = jdbcUrl.trim();
+ int queryStart = trimmed.indexOf('?');
+ if (queryStart == -1) {
+ queryStart = trimmed.indexOf(';');
+ }
+ if (queryStart != -1) {
+ String queryString = trimmed.substring(queryStart + 1);
+ validateQueryString(queryString, trimmed);
+ }
+ LOG.debug("jdbc.url passed validation: {}", sanitizeForLog(trimmed));
+ }
+
+ private static void validateQueryString(String queryString, String
fullUrl) throws HadoopException {
+ String[] tokens = queryString.split("[&;]");
+ for (String token : tokens) {
+ if (token.trim().isEmpty()) {
+ continue;
+ }
+ int eqIdx = token.indexOf('=');
+ String paramName = (eqIdx >= 0 ? token.substring(0, eqIdx) :
token).trim();
+ String decodedParamName = paramName;
+ try {
+ decodedParamName = URLDecoder.decode(paramName, "UTF-8");
+ } catch (Exception e) {
+ LOG.warn("Failed to decode parameter name: {}", paramName);
+ }
+
+ String normalized =
decodedParamName.toLowerCase().trim().replaceAll("[._-]", "");
+ if (BLOCKED_PARAMS.contains(normalized)) {
+ logAndThrow("blocked parameter", normalized, paramName,
fullUrl);
+ }
+ String[] dangerPatterns = {"socketfactory", "sslfactory",
"autodeserialize"};
+ for (String danger : dangerPatterns) {
+ if (normalized.contains(danger)) {
+ logAndThrow("dangerous pattern '" + danger + "'",
normalized, paramName, fullUrl);
+ }
+ }
+ if (normalized.contains("factory") &&
(normalized.contains("socket") || normalized.contains("ssl") ||
+ normalized.contains("connection") ||
normalized.contains("auth") ||
+ normalized.contains("driver") ||
normalized.contains("datasource"))) {
+ logAndThrow("potentially dangerous factory parameter",
normalized, paramName, fullUrl);
+ }
+ }
+ }
+
+ static String sanitizeForLog(String url) {
+ if (url == null) {
+ return "<null>";
+ }
+ int q = url.indexOf('?');
+ return q >= 0 ? url.substring(0, q) + "?<params_redacted>" : url;
Review Comment:
sanitizeForLog() only redacts parameters when the URL contains '?', but Hive
JDBC URLs commonly use ';' as the parameter separator (and validate()
explicitly supports parsing ';'). This means credentials or other secrets in
';' parameters can be logged in cleartext via the debug log in validate() and
the warn log in logAndThrow(). Update sanitizeForLog() to also redact when
parameters start with ';' (and ideally redact for either delimiter).
```suggestion
int s = url.indexOf(';');
int paramStart;
if (q >= 0 && s >= 0) {
paramStart = Math.min(q, s);
} else if (q >= 0) {
paramStart = q;
} else {
paramStart = s;
}
return paramStart >= 0 ? url.substring(0, paramStart) +
"?<params_redacted>" : url;
```
--
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]