Parcourir la source

fixed issue with improper error handling for html clients (issue #31)

Jason Rivard il y a 9 ans
Parent
commit
19be593eb3

+ 1 - 1
src/main/java/password/pwm/http/PwmHttpRequestWrapper.java

@@ -66,7 +66,7 @@ public abstract class PwmHttpRequestWrapper {
 
     public boolean isHtmlRequest() {
         final String acceptHeader = this.readHeaderValueAsString(PwmConstants.HttpHeader.Accept);
-        return acceptHeader.contains(PwmConstants.AcceptValue.json.getHeaderValue());
+        return acceptHeader.contains(PwmConstants.AcceptValue.html.getHeaderValue());
     }
 
     public String getContextPath() {

+ 5 - 1
src/main/java/password/pwm/http/PwmRequest.java

@@ -147,7 +147,11 @@ public class PwmRequest extends PwmHttpRequestWrapper implements Serializable {
     )
             throws IOException, ServletException
     {
-        getPwmResponse().respondWithError(errorInformation, forceLogout);
+        if (forceLogout) {
+            getPwmResponse().respondWithError(errorInformation, PwmResponse.Flag.ForceLogout);
+        } else {
+            getPwmResponse().respondWithError(errorInformation);
+        }
     }
 
     public void sendRedirect(final String redirectURL)

+ 15 - 17
src/main/java/password/pwm/http/PwmResponse.java

@@ -49,7 +49,8 @@ public class PwmResponse extends PwmHttpResponseWrapper {
     final private PwmRequest pwmRequest;
 
     public enum Flag {
-        AlwaysShowMessage
+        AlwaysShowMessage,
+        ForceLogout,
     }
 
     public PwmResponse(
@@ -99,11 +100,10 @@ public class PwmResponse extends PwmHttpResponseWrapper {
 
         if (showMessage) {
             LOGGER.trace(pwmSession, "skipping success page due to configuration setting.");
-            final StringBuilder redirectURL = new StringBuilder();
-            redirectURL.append(pwmRequest.getContextPath());
-            redirectURL.append(PwmServletDefinition.Command.servletUrl());
-            redirectURL.append("?processAction=continue");
-            sendRedirect(redirectURL.toString());
+            final String redirectUrl = pwmRequest.getContextPath()
+                    +  PwmServletDefinition.Command.servletUrl()
+                    + "?processAction=continue";
+            sendRedirect(redirectUrl);
             return;
         }
 
@@ -116,7 +116,7 @@ public class PwmResponse extends PwmHttpResponseWrapper {
 
     public void respondWithError(
             final ErrorInformation errorInformation,
-            final boolean forceLogout
+            final Flag... flags
     )
             throws IOException, ServletException
     {
@@ -124,15 +124,8 @@ public class PwmResponse extends PwmHttpResponseWrapper {
 
         pwmRequest.setResponseError(errorInformation);
 
-        {
-            boolean showDetail = Helper.determineIfDetailErrorMsgShown(pwmRequest.getPwmApplication());
-            final String errorStatusText = showDetail
-                    ? errorInformation.toDebugStr()
-                    : errorInformation.toUserStr(pwmRequest.getPwmSession(),pwmRequest.getPwmApplication());
-            getHttpServletResponse().sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, errorStatusText);
-        }
-
-        if (forceLogout) {
+        if (Helper.enumArrayContainsValue(flags, Flag.ForceLogout)) {
+            LOGGER.debug(pwmRequest, "forcing logout due to error " + errorInformation.toDebugStr());
             pwmRequest.getPwmSession().unauthenticateUser(pwmRequest);
         }
 
@@ -144,8 +137,13 @@ public class PwmResponse extends PwmHttpResponseWrapper {
             } catch (PwmUnrecoverableException e) {
                 LOGGER.error("unexpected error sending user to error page: " + e.toString());
             }
+        } else {
+            boolean showDetail = Helper.determineIfDetailErrorMsgShown(pwmRequest.getPwmApplication());
+            final String errorStatusText = showDetail
+                    ? errorInformation.toDebugStr()
+                    : errorInformation.toUserStr(pwmRequest.getPwmSession(),pwmRequest.getPwmApplication());
+            getHttpServletResponse().sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, errorStatusText);
         }
-
     }
 
 

+ 2 - 2
src/main/java/password/pwm/http/servlet/ActivateUserServlet.java

@@ -117,7 +117,7 @@ public class ActivateUserServlet extends AbstractPwmServlet {
     protected void processAction(final PwmRequest pwmRequest)
             throws ServletException, ChaiUnavailableException, IOException, PwmUnrecoverableException
     {
-        //Fetch the session state bean.
+            //Fetch the session state bean.
         final PwmSession pwmSession = pwmRequest.getPwmSession();
         final PwmApplication pwmApplication = pwmRequest.getPwmApplication();
 
@@ -182,7 +182,7 @@ public class ActivateUserServlet extends AbstractPwmServlet {
         pwmApplication.getSessionStateService().clearBean(pwmRequest, ActivateUserBean.class);
         final List<FormConfiguration> configuredActivationForm = config.readSettingAsForm(PwmSetting.ACTIVATE_USER_FORM);
 
-        Map<FormConfiguration,String> formValues = new HashMap();
+        Map<FormConfiguration,String> formValues = new HashMap<>();
         try {
             //read the values from the request
             formValues = FormUtility.readFormValuesFromRequest(pwmRequest, configuredActivationForm,

+ 10 - 0
src/main/java/password/pwm/util/Helper.java

@@ -614,6 +614,12 @@ public class
         return sw.toString();
     }
 
+    /**
+     * Converts an exception to a string message.  Handles cases where the message in the exception is null
+     * and/or there are multiple nested cause exceptions.
+     * @param e The exception to convert to a string
+     * @return A string containing any meaningful extractable cause information, suitable for debugging.
+     */
     public static String readHostileExceptionMessage(Throwable e) {
         String errorMsg = e.getClass().getName();
         if (e.getMessage() != null) {
@@ -633,4 +639,8 @@ public class
 
         return errorMsg;
     }
+
+    public static <E extends Enum<E>> boolean enumArrayContainsValue(final E[] enumArray, final E enumValue) {
+        return !(enumArray == null || enumArray.length == 0) && Arrays.asList(enumArray).contains(enumValue);
+    }
 }