github-advanced-security[bot] commented on code in PR #5399: URL: https://github.com/apache/hive/pull/5399#discussion_r1724073919
########## service/src/java/org/apache/hive/service/auth/ldap/LdapAuthService.java: ########## @@ -0,0 +1,88 @@ +/* + * 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.hive.service.auth.ldap; + +import com.google.common.annotations.VisibleForTesting; +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hive.service.auth.AuthenticationProviderFactory; +import org.apache.hive.service.auth.HttpAuthService; +import org.apache.hive.service.auth.HttpAuthUtils; +import org.apache.hive.service.auth.HttpAuthenticationException; +import org.apache.hive.service.auth.PasswdAuthenticationProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.security.sasl.AuthenticationException; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.UnsupportedEncodingException; +import java.util.concurrent.TimeUnit; + +public class LdapAuthService extends HttpAuthService { + private static final Logger LOG = LoggerFactory.getLogger(LdapAuthService.class); + public static final String HIVE_SERVER2_WEBUI_AUTH_COOKIE_NAME = "hive.server2.webui.auth"; + private final PasswdAuthenticationProvider authProvider; + + public LdapAuthService(HiveConf hiveConf) { + super( + hiveConf.getVar(HiveConf.ConfVars.HIVE_SERVER2_WEBUI_HTTP_COOKIE_DOMAIN), + hiveConf.getVar(HiveConf.ConfVars.HIVE_SERVER2_WEBUI_HTTP_COOKIE_PATH), + (int) hiveConf.getTimeVar(HiveConf.ConfVars.HIVE_SERVER2_WEBUI_HTTP_COOKIE_MAX_AGE, TimeUnit.SECONDS), + hiveConf.getBoolVar(HiveConf.ConfVars.HIVE_SERVER2_USE_SSL), + HIVE_SERVER2_WEBUI_AUTH_COOKIE_NAME); + try { + this.authProvider = AuthenticationProviderFactory + .getAuthenticationProvider(AuthenticationProviderFactory.AuthMethods.LDAP, hiveConf); + // always send secure cookies for SSL mode + } catch (AuthenticationException e) { + throw new RuntimeException(e); + } + } + + @VisibleForTesting + public LdapAuthService(HiveConf hiveConf, PasswdAuthenticationProvider provider) { + super( + hiveConf.getVar(HiveConf.ConfVars.HIVE_SERVER2_WEBUI_HTTP_COOKIE_DOMAIN), + hiveConf.getVar(HiveConf.ConfVars.HIVE_SERVER2_WEBUI_HTTP_COOKIE_PATH), + (int) hiveConf.getTimeVar(HiveConf.ConfVars.HIVE_SERVER2_WEBUI_HTTP_COOKIE_MAX_AGE, TimeUnit.SECONDS), + hiveConf.getBoolVar(HiveConf.ConfVars.HIVE_SERVER2_USE_SSL), + HIVE_SERVER2_WEBUI_AUTH_COOKIE_NAME); + this.authProvider = provider; + } + + public boolean authorize(HttpServletRequest request, HttpServletResponse response) { + try { + String clientUserName = validateCookie(request); + if (clientUserName == null) { + clientUserName = getUsername(request); + authProvider.Authenticate(clientUserName, getPassword(request)); + + String cookieToken = HttpAuthUtils.createCookieToken(clientUserName); + Cookie hs2Cookie = signAndcreateCookie(cookieToken); + + response.addCookie(hs2Cookie); + LOG.info("Cookie added for clientUserName " + clientUserName); Review Comment: ## Logging should not be vulnerable to injection attacks <!--SONAR_ISSUE_KEY:AZFyAPJlETVCiPRqnXWk-->Change this code to not log user-controlled data. <p>See more on <a href="https://sonarcloud.io/project/issues?id=apache_hive&issues=AZFyAPJlETVCiPRqnXWk&open=AZFyAPJlETVCiPRqnXWk&pullRequest=5399">SonarCloud</a></p> [Show more details](https://github.com/apache/hive/security/code-scanning/124) ########## service/src/java/org/apache/hive/service/cli/thrift/ThriftHttpServlet.java: ########## @@ -160,7 +158,7 @@ // If the cookie based authentication is already enabled, parse the // request and validate the request cookies. if (isCookieAuthEnabled) { - clientUserName = validateCookie(request); + clientUserName = httpAuthService.validateCookie(request); Review Comment: ## Exceptions should not be thrown from servlet methods <!--SONAR_ISSUE_KEY:AZFyAPKLETVCiPRqnXWy-->Handle the following exception that could be thrown by "validateCookie": UnsupportedEncodingException. <p>See more on <a href="https://sonarcloud.io/project/issues?id=apache_hive&issues=AZFyAPKLETVCiPRqnXWy&open=AZFyAPKLETVCiPRqnXWy&pullRequest=5399">SonarCloud</a></p> [Show more details](https://github.com/apache/hive/security/code-scanning/130) ########## service/src/java/org/apache/hive/service/cli/thrift/ThriftHttpServlet.java: ########## @@ -257,10 +255,10 @@ if (requireNewCookie && !authType.isEnabled(HiveAuthConstants.AuthTypes.NOSASL)) { String cookieToken = HttpAuthUtils.createCookieToken(clientUserName); - Cookie hs2Cookie = createCookie(signer.signCookie(cookieToken)); + Cookie hs2Cookie = httpAuthService.signAndcreateCookie(cookieToken); Review Comment: ## Exceptions should not be thrown from servlet methods <!--SONAR_ISSUE_KEY:AZFyAPKLETVCiPRqnXWz-->Handle the following exception that could be thrown by "signAndcreateCookie": UnsupportedEncodingException. <p>See more on <a href="https://sonarcloud.io/project/issues?id=apache_hive&issues=AZFyAPKLETVCiPRqnXWz&open=AZFyAPKLETVCiPRqnXWz&pullRequest=5399">SonarCloud</a></p> [Show more details](https://github.com/apache/hive/security/code-scanning/131) ########## service/src/java/org/apache/hive/service/auth/HttpAuthService.java: ########## @@ -0,0 +1,267 @@ +/* + * 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.hive.service.auth; + +import org.apache.hive.service.CookieSigner; +import org.apache.hive.service.auth.ldap.HttpEmptyAuthenticationException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.NewCookie; +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; +import java.security.SecureRandom; +import java.util.Base64; + +public class HttpAuthService { + private static final Logger LOG = LoggerFactory.getLogger(HttpAuthService.class); + public static final String USERNAME_REQUEST_PARAM_NAME = "username"; + public static final String PASSWORD_REQUEST_PARAM_NAME = "password"; + private static final SecureRandom RAN = new SecureRandom(); + private final CookieSigner signer; + private final String cookieDomain; + private final String cookiePath; + private final int cookieMaxAge; + private final boolean isCookieSecure; + private final String authCookieName; + + public HttpAuthService(String cookieDomain, String cookiePath, int cookieMaxAge, boolean isCookieSecure, + String authCookieName) { + String secret = Long.toString(RAN.nextLong()); + this.signer = new CookieSigner(secret.getBytes()); + this.cookieMaxAge = cookieMaxAge; + this.cookieDomain = cookieDomain; + this.cookiePath = cookiePath; + this.authCookieName = authCookieName; + this.isCookieSecure = isCookieSecure; + } + + public Cookie signAndcreateCookie(String cookieToken) throws UnsupportedEncodingException { + return createCookie(signer.signCookie(cookieToken)); + } + + /** + * Generate a server side cookie given the cookie value as the input. + * @param str Signed input string token. + * @return The generated cookie. + */ + public Cookie createCookie(String str) { + if (LOG.isDebugEnabled()) { + LOG.debug("Cookie name = " + authCookieName + " value = " + str); Review Comment: ## Logging should not be vulnerable to injection attacks <!--SONAR_ISSUE_KEY:AZFyAPJrETVCiPRqnXWx-->Change this code to not log user-controlled data. <p>See more on <a href="https://sonarcloud.io/project/issues?id=apache_hive&issues=AZFyAPJrETVCiPRqnXWx&open=AZFyAPJrETVCiPRqnXWx&pullRequest=5399">SonarCloud</a></p> [Show more details](https://github.com/apache/hive/security/code-scanning/125) ########## service/src/java/org/apache/hive/service/cli/thrift/ThriftHttpServlet.java: ########## @@ -577,7 +449,7 @@ throws HttpAuthenticationException { // Each http request must have an Authorization header // Check before trying to do kerberos authentication twice - getAuthHeader(request); + httpAuthService.getAuthHeader(request); Review Comment: ## Exceptions should not be thrown from servlet methods <!--SONAR_ISSUE_KEY:AZFyAPKLETVCiPRqnXW2-->Handle the following exception that could be thrown by "getAuthHeader": HttpAuthenticationException. <p>See more on <a href="https://sonarcloud.io/project/issues?id=apache_hive&issues=AZFyAPKLETVCiPRqnXW2&open=AZFyAPKLETVCiPRqnXW2&pullRequest=5399">SonarCloud</a></p> [Show more details](https://github.com/apache/hive/security/code-scanning/127) ########## service/src/java/org/apache/hive/service/servlet/LoginServlet.java: ########## @@ -0,0 +1,43 @@ +/* + * 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.hive.service.servlet; + +import org.apache.hive.service.auth.ldap.LdapAuthService; +import org.apache.hive.service.server.HiveServer2; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +public class LoginServlet extends HttpServlet { + private final LdapAuthService ldapAuthService; + + public LoginServlet(LdapAuthService ldapAuthService) { + this.ldapAuthService = ldapAuthService; + } + + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { + ldapAuthService.authorize(request, response); + RequestDispatcher dispatcher = request.getRequestDispatcher(HiveServer2.HS2_WEBUI_ROOT_URI); + dispatcher.forward(request, response); Review Comment: ## Exceptions should not be thrown from servlet methods <!--SONAR_ISSUE_KEY:AZFyAPLAETVCiPRqnXXC-->Handle the following exceptions that could be thrown by "forward": ServletException, IOException. <p>See more on <a href="https://sonarcloud.io/project/issues?id=apache_hive&issues=AZFyAPLAETVCiPRqnXXC&open=AZFyAPLAETVCiPRqnXXC&pullRequest=5399">SonarCloud</a></p> [Show more details](https://github.com/apache/hive/security/code-scanning/132) ########## service/src/java/org/apache/hive/service/cli/thrift/ThriftHttpServlet.java: ########## @@ -537,14 +409,14 @@ */ private String doPasswdAuth(HttpServletRequest request, String authType) throws HttpAuthenticationException { - String userName = getUsername(request); + String userName = httpAuthService.getUsername(request); Review Comment: ## Exceptions should not be thrown from servlet methods <!--SONAR_ISSUE_KEY:AZFyAPKLETVCiPRqnXW1-->Handle the following exception that could be thrown by "getUsername": HttpAuthenticationException. <p>See more on <a href="https://sonarcloud.io/project/issues?id=apache_hive&issues=AZFyAPKLETVCiPRqnXW1&open=AZFyAPKLETVCiPRqnXW1&pullRequest=5399">SonarCloud</a></p> [Show more details](https://github.com/apache/hive/security/code-scanning/126) -- 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: gitbox-unsubscr...@hive.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For additional commands, e-mail: gitbox-h...@hive.apache.org