From 50b01ffd6385516a6b26e2d8937e0c1820c49e2c Mon Sep 17 00:00:00 2001 From: exceptionfactory Date: Wed, 28 Jun 2023 15:24:42 -0500 Subject: [PATCH] NIFI-11714 Added Error Handler to Jetty Server - Configured Error Handler with Stack Traces disabled for NiFi and Registry Signed-off-by: Joe Gresock This closes #7447. --- .../org/apache/nifi/minifi/c2/jetty/JettyServer.java | 10 ++++++++++ .../java/org/apache/nifi/web/server/JettyServer.java | 9 +++++++++ .../jetty/handler/StandardHandlerProvider.java | 10 ++++++++++ 3 files changed, 29 insertions(+) diff --git a/minifi/minifi-c2/minifi-c2-jetty/src/main/java/org/apache/nifi/minifi/c2/jetty/JettyServer.java b/minifi/minifi-c2/minifi-c2-jetty/src/main/java/org/apache/nifi/minifi/c2/jetty/JettyServer.java index c960f6abbc..7002a19e0c 100644 --- a/minifi/minifi-c2/minifi-c2-jetty/src/main/java/org/apache/nifi/minifi/c2/jetty/JettyServer.java +++ b/minifi/minifi-c2/minifi-c2-jetty/src/main/java/org/apache/nifi/minifi/c2/jetty/JettyServer.java @@ -48,6 +48,7 @@ import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.handler.HandlerCollection; +import org.eclipse.jetty.servlet.ErrorPageErrorHandler; import org.eclipse.jetty.webapp.WebAppClassLoader; import org.eclipse.jetty.webapp.WebAppContext; import org.slf4j.Logger; @@ -150,6 +151,7 @@ public class JettyServer { final WebAppContext webappContext = new WebAppContext(warFile.getPath(), contextPath); webappContext.setContextPath(contextPath); webappContext.setDisplayName(contextPath); + webappContext.setErrorHandler(getErrorHandler()); // instruction jetty to examine these jars for tlds, web-fragments, etc webappContext.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/[^/]*servlet-api-[^/]*\\.jar$|.*/javax.servlet.jsp.jstl-.*\\\\.jar$|.*/[^/]*taglibs.*\\.jar$" ); @@ -189,4 +191,12 @@ public class JettyServer { logger.info("Loading WAR: " + warFile.getAbsolutePath() + " with context path set to " + contextPath); return webappContext; } + + private static ErrorPageErrorHandler getErrorHandler() { + final ErrorPageErrorHandler errorHandler = new ErrorPageErrorHandler(); + errorHandler.setShowServlet(false); + errorHandler.setShowStacks(false); + errorHandler.setShowMessageInTitle(false); + return errorHandler; + } } diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-jetty/src/main/java/org/apache/nifi/web/server/JettyServer.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-jetty/src/main/java/org/apache/nifi/web/server/JettyServer.java index d60cd8d77d..5db019eb1b 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-jetty/src/main/java/org/apache/nifi/web/server/JettyServer.java +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-jetty/src/main/java/org/apache/nifi/web/server/JettyServer.java @@ -77,6 +77,7 @@ import org.eclipse.jetty.server.handler.HandlerCollection; import org.eclipse.jetty.server.handler.HandlerList; import org.eclipse.jetty.server.handler.gzip.GzipHandler; import org.eclipse.jetty.servlet.DefaultServlet; +import org.eclipse.jetty.servlet.ErrorPageErrorHandler; import org.eclipse.jetty.servlet.FilterHolder; import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.util.thread.QueuedThreadPool; @@ -598,6 +599,7 @@ public class JettyServer implements NiFiServer, ExtensionUiLoader { webappContext.setServerClasses(serverClasses.toArray(new String[0])); webappContext.setDefaultsDescriptor(WEB_DEFAULTS_XML); webappContext.getMimeTypes().addMimeMapping("ttf", "font/ttf"); + webappContext.setErrorHandler(getErrorHandler()); // get the temp directory for this webapp File tempDir = new File(props.getWebWorkingDirectory(), warFile.getName()); @@ -1095,6 +1097,13 @@ public class JettyServer implements NiFiServer, ExtensionUiLoader { } + private ErrorPageErrorHandler getErrorHandler() { + final ErrorPageErrorHandler errorHandler = new ErrorPageErrorHandler(); + errorHandler.setShowServlet(false); + errorHandler.setShowStacks(false); + errorHandler.setShowMessageInTitle(false); + return errorHandler; + } /** * Holds the result of loading WARs for custom UIs. diff --git a/nifi-registry/nifi-registry-core/nifi-registry-jetty/src/main/java/org/apache/nifi/registry/jetty/handler/StandardHandlerProvider.java b/nifi-registry/nifi-registry-core/nifi-registry-jetty/src/main/java/org/apache/nifi/registry/jetty/handler/StandardHandlerProvider.java index a86c217872..d72d9a7802 100644 --- a/nifi-registry/nifi-registry-core/nifi-registry-jetty/src/main/java/org/apache/nifi/registry/jetty/handler/StandardHandlerProvider.java +++ b/nifi-registry/nifi-registry-core/nifi-registry-jetty/src/main/java/org/apache/nifi/registry/jetty/handler/StandardHandlerProvider.java @@ -22,6 +22,7 @@ import org.apache.nifi.registry.security.crypto.CryptoKeyProvider; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.handler.HandlerCollection; import org.eclipse.jetty.servlet.DefaultServlet; +import org.eclipse.jetty.servlet.ErrorPageErrorHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.webapp.WebAppClassLoader; import org.eclipse.jetty.webapp.WebAppContext; @@ -152,6 +153,7 @@ public class StandardHandlerProvider implements HandlerProvider { webAppContext.setDefaultsDescriptor(DEFAULTS_DESCRIPTOR); webAppContext.setMaxFormContentSize(MAX_FORM_CONTENT_SIZE); webAppContext.setAttribute(WEB_INF_JAR_PATTERN_ATTRIBUTE, WEB_INF_JAR_PATTERN); + webAppContext.setErrorHandler(getErrorHandler()); final File tempDirectory = getTempDirectory(workDirectory, applicationFile.getName()); webAppContext.setTempDirectory(tempDirectory); @@ -174,6 +176,14 @@ public class StandardHandlerProvider implements HandlerProvider { return applicationFiles[0]; } + private ErrorPageErrorHandler getErrorHandler() { + final ErrorPageErrorHandler errorHandler = new ErrorPageErrorHandler(); + errorHandler.setShowServlet(false); + errorHandler.setShowStacks(false); + errorHandler.setShowMessageInTitle(false); + return errorHandler; + } + private File getTempDirectory(final File webWorkingDirectory, final String filename) { final File tempDirectory = new File(webWorkingDirectory, filename); if (tempDirectory.isDirectory()) {