From d440cf281bae41c7f8b5b4cf8f656b660164db99 Mon Sep 17 00:00:00 2001 From: labkey-jeckels Date: Sun, 11 Oct 2026 12:56:41 -0700 Subject: [PATCH 1/4] Return 400 for unparseable request parameters and multipart bodies Malformed and scanner requests were reported to mothership as server errors; they now get a 4xx response, logged only at debug level. --- .../api/action/SpringActionController.java | 6 ++ .../org/labkey/api/util/ExceptionUtil.java | 74 +++++++++++++++++++ .../org/labkey/core/webdav/DavController.java | 20 ++++- 3 files changed, 98 insertions(+), 2 deletions(-) diff --git a/api/src/org/labkey/api/action/SpringActionController.java b/api/src/org/labkey/api/action/SpringActionController.java index f4bca9a0ba1..27ee1bac129 100644 --- a/api/src/org/labkey/api/action/SpringActionController.java +++ b/api/src/org/labkey/api/action/SpringActionController.java @@ -74,6 +74,7 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.web.HttpRequestMethodNotSupportedException; +import org.springframework.web.multipart.MultipartException; import org.springframework.web.multipart.support.StandardServletMultipartResolver; import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.ModelAndView; @@ -540,6 +541,11 @@ public ModelAndView handleRequest(HttpServletRequest request, @NotNull HttpServl // TODO Better/custom error report page/response if (x.getCause() instanceof BadRequestException) return handleBadRequestException(request, response, controller, (BadRequestException)x.getCause()); + if (x instanceof MultipartException) + { + _log.debug("Unable to parse multipart request", x); + return handleBadRequestException(request, response, controller, new BadRequestException(x.getMessage(), x)); + } throw x; } } diff --git a/api/src/org/labkey/api/util/ExceptionUtil.java b/api/src/org/labkey/api/util/ExceptionUtil.java index 07a2ee356a6..7266d35bf2e 100644 --- a/api/src/org/labkey/api/util/ExceptionUtil.java +++ b/api/src/org/labkey/api/util/ExceptionUtil.java @@ -22,6 +22,7 @@ import org.apache.logging.log4j.message.Message; import org.apache.logging.log4j.simple.SimpleLogger; import org.apache.logging.log4j.util.PropertiesUtil; +import org.apache.tomcat.util.http.InvalidParameterException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.junit.Assert; @@ -690,9 +691,32 @@ public static boolean isIgnorable(Throwable ex) null != decorations.get(ExceptionInfo.SkipMothershipLogging) || ex instanceof SkipMothershipLogging || isClientAbortException(ex) || + isInvalidParameterException(ex) || (ex instanceof IllegalStateException && "Page needs a session and none is available".equalsIgnoreCase(ex.getMessage())); } + /** Tomcat throws this for an unparseable query string or form body, and rethrows it from every later parameter read */ + public static boolean isInvalidParameterException(Throwable ex) + { + return ex instanceof InvalidParameterException; + } + + private static @Nullable InvalidParameterException getInvalidParameterException(Throwable ex, HttpServletRequest request) + { + if (ex instanceof InvalidParameterException ipe) + return ipe; + + try + { + request.getParameterMap(); + return null; + } + catch (InvalidParameterException ipe) + { + return ipe; + } + } + static class WebPartErrorView extends WebPartView { private final ErrorRenderer _renderer; @@ -858,6 +882,7 @@ static ActionURL handleException(@NotNull HttpServletRequest request, @NotNull H User user = (User) request.getUserPrincipal(); boolean isGET = "GET".equals(request.getMethod()); Map headers = new TreeMap<>(); + InvalidParameterException invalidParameter = getInvalidParameterException(ex, request); if (ContextListener.isShuttingDown()) { @@ -871,6 +896,20 @@ static ActionURL handleException(@NotNull HttpServletRequest request, @NotNull H } return null; } + // Any error page reads request parameters, which would rethrow, so respond without one + else if (invalidParameter != null) + { + log.debug("Invalid request parameters", invalidParameter); + try + { + response.sendError(invalidParameter.getErrorCode(), "Invalid request parameters"); + } + catch (IOException e) + { + // Do nothing + } + return null; + } else if (ex instanceof ApiUsageException) { responseStatus = HttpServletResponse.SC_BAD_REQUEST; @@ -1357,6 +1396,11 @@ static class ExceptionResponse public static class TestCase extends Assert { ExceptionResponse handleIt(final User user, Exception ex) + { + return handleIt(user, ex, null); + } + + ExceptionResponse handleIt(final User user, Exception ex, @Nullable InvalidParameterException parameterFailure) { final MockServletResponse res = new MockServletResponse(); InvocationHandler h = (_, method, objects) -> { @@ -1387,6 +1431,14 @@ public String getMethod() { return "GET"; } + + @Override + public Map getParameterMap() + { + if (parameterFailure != null) + throw parameterFailure; + return super.getParameterMap(); + } }; ExceptionUtil.decorateException(ex, ExceptionInfo.SkipMothershipLogging, "true", true); ActionURL url = ExceptionUtil.handleException(req, res, ex, null, false, dummySearch, dummyLog, null, null); @@ -1475,6 +1527,28 @@ public void testNotFound() assertTrue(answer.response.headers.containsKey("SearchService.notFound")); } + @Test + public void testInvalidParameters() + { + User guest = UserManager.getGuestUser(); + InvalidParameterException ipe = new InvalidParameterException("Invalid chunk ignored"); + ExceptionResponse answer; + + answer = handleIt(guest, ipe, ipe); + assertEquals(HttpServletResponse.SC_BAD_REQUEST, answer.response.status); + assertFalse(answer.response.headers.containsKey("Logger.error")); + + // Rendering the 404 page would read the unparseable parameters + answer = handleIt(guest, new NotFoundException("Not here"), ipe); + assertEquals(HttpServletResponse.SC_BAD_REQUEST, answer.response.status); + assertFalse(answer.response.headers.containsKey("Logger.error")); + + answer = handleIt(guest, new NotFoundException("Not here"), new InvalidParameterException("Too big", HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE)); + assertEquals(HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE, answer.response.status); + + assertTrue(isIgnorable(new InvalidParameterException("Character decoding failed"))); + } + @Test public void testServerError() { diff --git a/core/src/org/labkey/core/webdav/DavController.java b/core/src/org/labkey/core/webdav/DavController.java index f171e828460..f21ae72e98a 100644 --- a/core/src/org/labkey/core/webdav/DavController.java +++ b/core/src/org/labkey/core/webdav/DavController.java @@ -342,8 +342,21 @@ public ModelAndView handleRequest(HttpServletRequest request, HttpServletRespons return null; } + String skipProfiling; + try + { + skipProfiling = request.getParameter("skip-profiling"); + } + catch (IllegalStateException x) + { + if (!ExceptionUtil.isInvalidParameterException(x)) + throw x; + ExceptionUtil.handleException(request, response, x, null, false); + return null; + } + Class actionClass = action.getClass(); - if (actionClass.isAnnotationPresent(IgnoresAllocationTracking.class) || "true".equals(request.getParameter("skip-profiling"))) + if (actionClass.isAnnotationPresent(IgnoresAllocationTracking.class) || "true".equals(skipProfiling)) { MemTracker.get().ignore(); } @@ -455,7 +468,10 @@ WebdavStatus sendError(WebdavStatus status) WebdavStatus sendError(WebdavStatus status, Exception x) { - ExceptionUtil.logExceptionToMothership(getRequest(), x); + if (status.code >= HttpServletResponse.SC_INTERNAL_SERVER_ERROR) + ExceptionUtil.logExceptionToMothership(getRequest(), x); + else + _log.debug("{} response", status.code, x); if (x instanceof ConfigurationException) { // These exceptions are skipped in the logExceptionToMothership() call. They're useful to log here From 05d201f2d500d460fa5d41853d64130ebc0c1440 Mon Sep 17 00:00:00 2001 From: labkey-jeckels Date: Sun, 11 Oct 2026 13:27:10 -0700 Subject: [PATCH 2/4] Return 413 for oversized multipart uploads with the root cause message Also tolerate an already-committed response when rejecting invalid parameters. --- .../api/action/SpringActionController.java | 21 +++++++++++++------ .../org/labkey/api/util/ExceptionUtil.java | 4 ++-- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/api/src/org/labkey/api/action/SpringActionController.java b/api/src/org/labkey/api/action/SpringActionController.java index 27ee1bac129..7b827e3ec94 100644 --- a/api/src/org/labkey/api/action/SpringActionController.java +++ b/api/src/org/labkey/api/action/SpringActionController.java @@ -21,6 +21,7 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.exception.ExceptionUtils; import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -74,6 +75,7 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.web.HttpRequestMethodNotSupportedException; +import org.springframework.web.multipart.MaxUploadSizeExceededException; import org.springframework.web.multipart.MultipartException; import org.springframework.web.multipart.support.StandardServletMultipartResolver; import org.springframework.web.servlet.DispatcherServlet; @@ -370,6 +372,11 @@ private ModelAndView handleBadRequestException(HttpServletRequest request, HttpS } private ModelAndView handleBadRequestException(HttpServletRequest request, HttpServletResponse response, Controller controller, BadRequestException x) throws Exception + { + return handleBadRequestException(request, response, controller, x, HttpServletResponse.SC_BAD_REQUEST); + } + + private ModelAndView handleBadRequestException(HttpServletRequest request, HttpServletResponse response, Controller controller, BadRequestException x, int status) throws Exception { // TODO: we are assuming that all ApiActions respond with JSON and all !ApiAction respond HTML. Would be better to be explicit. boolean jsonResponse = controller instanceof BaseApiAction; @@ -382,13 +389,13 @@ private ModelAndView handleBadRequestException(HttpServletRequest request, HttpS { // Issue 53120: Set content-type, status when responding with JSON content response.setContentType(ApiJsonWriter.CONTENT_TYPE_JSON); - response.setStatus(HttpServletResponse.SC_BAD_REQUEST); + response.setStatus(status); } Writer w = response.getWriter(); JSONObject json = new JSONObject(); json.put("success", false); - json.put("status", HttpServletResponse.SC_BAD_REQUEST); + json.put("status", status); json.put("exception", x.getMessage()); if (htmlWrappedJson) w.write("