This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch coheigea/undertow in repository https://gitbox.apache.org/repos/asf/cxf.git
commit e8458ec3094c8df588c774a41e6c40a2e51a4444 Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Thu Nov 19 07:47:58 2026 +0000 Do not echo exception messages in the Undertow 500 error page --- .../http_undertow/UndertowHTTPHandler.java | 50 +++++++++++++---- .../UndertowHTTPServerEngineTest.java | 64 ++++++++++++++++++++++ .../http_undertow/UndertowHTTPTestHandler.java | 15 +---- 3 files changed, 104 insertions(+), 25 deletions(-) diff --git a/rt/transports/http-undertow/src/main/java/org/apache/cxf/transport/http_undertow/UndertowHTTPHandler.java b/rt/transports/http-undertow/src/main/java/org/apache/cxf/transport/http_undertow/UndertowHTTPHandler.java index 96ee5100dda..ab01f82221a 100644 --- a/rt/transports/http-undertow/src/main/java/org/apache/cxf/transport/http_undertow/UndertowHTTPHandler.java +++ b/rt/transports/http-undertow/src/main/java/org/apache/cxf/transport/http_undertow/UndertowHTTPHandler.java @@ -19,10 +19,17 @@ package org.apache.cxf.transport.http_undertow; +import java.nio.charset.StandardCharsets; +import java.util.logging.Level; +import java.util.logging.Logger; import jakarta.servlet.ServletContext; import jakarta.servlet.http.HttpServletResponse; import org.apache.cxf.Bus; +import org.apache.cxf.common.logging.LogUtils; +import org.apache.cxf.common.util.PropertyUtils; +import org.apache.cxf.message.Message; +import org.apache.cxf.transport.commons_text.StringEscapeUtils; import io.undertow.io.Sender; import io.undertow.server.HttpHandler; @@ -38,6 +45,11 @@ import io.undertow.util.Headers; public class UndertowHTTPHandler implements HttpHandler { + private static final Logger LOG = LogUtils.getLogger(UndertowHTTPHandler.class); + private static final String ERROR_PAGE_START = "<html><head><title>Error</title>" + + "</head><body>Internal Error 500"; + private static final String ERROR_PAGE_END = "</body></html>"; + private static final String SSL_CIPHER_SUITE_ATTRIBUTE = "jakarta.servlet.request.cipher_suite"; private static final String SSL_PEER_CERT_CHAIN_ATTRIBUTE = "jakarta.servlet.request.X509Certificate"; private static final String METHOD_TRACE = "TRACE"; @@ -122,20 +134,36 @@ public class UndertowHTTPHandler implements HttpHandler { undertowHTTPDestination.doService(servletContext, request, response); } catch (Throwable t) { - t.printStackTrace(); - if (undertowExchange.isResponseChannelAvailable()) { - undertowExchange.setStatusCode(500); - final String errorPage = "<html><head><title>Error</title>" - + "</head><body>Internal Error 500" + t.getMessage() - + "</body></html>"; - undertowExchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, - Integer.toString(errorPage.length())); - undertowExchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/html"); - Sender sender = undertowExchange.getResponseSender(); - sender.send(errorPage); + sendInternalError(undertowExchange, t); + } + } + + protected void sendInternalError(HttpServerExchange undertowExchange, Throwable t) { + LOG.log(Level.WARNING, "Error processing request", t); + if (undertowExchange.isResponseChannelAvailable()) { + String errorPage = ERROR_PAGE_START; + if (isIncludeExceptionMessage() && t.getMessage() != null) { + errorPage += " " + StringEscapeUtils.escapeHtml4(t.getMessage()); } + errorPage += ERROR_PAGE_END; + byte[] body = errorPage.getBytes(StandardCharsets.UTF_8); + undertowExchange.setStatusCode(500); + undertowExchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, Integer.toString(body.length)); + undertowExchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/html; charset=UTF-8"); + Sender sender = undertowExchange.getResponseSender(); + sender.send(errorPage, StandardCharsets.UTF_8); } } + /** + * The exception message is only included (HTML-escaped) in the error page if the + * {@link Message#EXCEPTION_MESSAGE_CAUSE_ENABLED} bus property is set to "true", as it may contain + * request data or internal details. The exception is always logged. + */ + private boolean isIncludeExceptionMessage() { + Bus b = getBus(); + return b != null && PropertyUtils.isTrue(b.getProperty(Message.EXCEPTION_MESSAGE_CAUSE_ENABLED)); + } + } diff --git a/rt/transports/http-undertow/src/test/java/org/apache/cxf/transport/http_undertow/UndertowHTTPServerEngineTest.java b/rt/transports/http-undertow/src/test/java/org/apache/cxf/transport/http_undertow/UndertowHTTPServerEngineTest.java index 0b0c07b8e36..3efeb137ada 100644 --- a/rt/transports/http-undertow/src/test/java/org/apache/cxf/transport/http_undertow/UndertowHTTPServerEngineTest.java +++ b/rt/transports/http-undertow/src/test/java/org/apache/cxf/transport/http_undertow/UndertowHTTPServerEngineTest.java @@ -27,12 +27,16 @@ import java.lang.management.ManagementFactory; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.Set; import javax.management.ObjectName; +import jakarta.servlet.ServletContext; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import org.apache.cxf.Bus; import org.apache.cxf.configuration.Configurer; import org.apache.cxf.configuration.jsse.TLSServerParameters; @@ -40,6 +44,7 @@ import org.apache.cxf.configuration.spring.ConfigurerImpl; import org.apache.cxf.helpers.CastUtils; import org.apache.cxf.helpers.IOUtils; import org.apache.cxf.management.InstrumentationManager; +import org.apache.cxf.message.Message; import org.apache.cxf.testutil.common.TestUtil; import org.junit.After; @@ -47,8 +52,11 @@ import org.junit.Before; import org.junit.Test; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -233,6 +241,62 @@ public class UndertowHTTPServerEngineTest { assertEquals("Could not find 0 Undertow Server: " + s, 0, s.size()); } + @Test + public void testInternalErrorPageDoesNotEchoExceptionMessage() throws Exception { + UndertowHTTPDestination destination = mock(UndertowHTTPDestination.class); + doThrow(new RuntimeException("<script>alert('xss')</script>")) + .when(destination).doService(any(ServletContext.class), any(HttpServletRequest.class), + any(HttpServletResponse.class)); + + UndertowHTTPServerEngine engine = factory.createUndertowHTTPServerEngine(PORT1, "http"); + String urlStr = "http://localhost:" + PORT1 + "/error"; + engine.addServant(new URL(urlStr), new UndertowHTTPHandler(destination, true)); + try { + HttpURLConnection connection = (HttpURLConnection)new URL(urlStr).openConnection(); + assertEquals(500, connection.getResponseCode()); + String body; + try (InputStream in = connection.getErrorStream()) { + body = IOUtils.toString(in); + } + assertTrue(body, body.contains("Internal Error 500")); + assertFalse(body, body.contains("<script>")); + assertFalse(body, body.contains("xss")); + } finally { + engine.removeServant(new URL(urlStr)); + } + } + + @Test + public void testInternalErrorPageEscapesExceptionMessageWhenEnabled() throws Exception { + Bus destinationBus = mock(Bus.class); + when(destinationBus.getProperty(Message.EXCEPTION_MESSAGE_CAUSE_ENABLED)) + .thenReturn("true"); + UndertowHTTPDestination destination = mock(UndertowHTTPDestination.class); + when(destination.getBus()).thenReturn(destinationBus); + doThrow(new RuntimeException("<script>alert('xss')</script> \u00e9")) + .when(destination).doService(any(ServletContext.class), any(HttpServletRequest.class), + any(HttpServletResponse.class)); + + UndertowHTTPServerEngine engine = factory.createUndertowHTTPServerEngine(PORT1, "http"); + String urlStr = "http://localhost:" + PORT1 + "/error2"; + engine.addServant(new URL(urlStr), new UndertowHTTPHandler(destination, true)); + try { + HttpURLConnection connection = (HttpURLConnection)new URL(urlStr).openConnection(); + assertEquals(500, connection.getResponseCode()); + byte[] bytes; + try (InputStream in = connection.getErrorStream()) { + bytes = IOUtils.readBytesFromStream(in); + } + assertEquals(connection.getContentLength(), bytes.length); + String body = new String(bytes, StandardCharsets.UTF_8); + assertTrue(body, body.contains("Internal Error 500")); + assertTrue(body, body.contains("<script>alert('xss')</script>")); + assertFalse(body, body.contains("<script>")); + } finally { + engine.removeServant(new URL(urlStr)); + } + } + private static String getResponse(String target) throws Exception { URL url = new URL(target); diff --git a/rt/transports/http-undertow/src/test/java/org/apache/cxf/transport/http_undertow/UndertowHTTPTestHandler.java b/rt/transports/http-undertow/src/test/java/org/apache/cxf/transport/http_undertow/UndertowHTTPTestHandler.java index 5280f9ec25f..dd2b9843bc5 100644 --- a/rt/transports/http-undertow/src/test/java/org/apache/cxf/transport/http_undertow/UndertowHTTPTestHandler.java +++ b/rt/transports/http-undertow/src/test/java/org/apache/cxf/transport/http_undertow/UndertowHTTPTestHandler.java @@ -19,13 +19,11 @@ package org.apache.cxf.transport.http_undertow; -import io.undertow.io.Sender; import io.undertow.server.HttpServerExchange; import io.undertow.servlet.handlers.ServletRequestContext; import io.undertow.servlet.spec.HttpServletRequestImpl; import io.undertow.servlet.spec.HttpServletResponseImpl; import io.undertow.servlet.spec.ServletContextImpl; -import io.undertow.util.Headers; public class UndertowHTTPTestHandler extends UndertowHTTPHandler { @@ -58,18 +56,7 @@ public class UndertowHTTPTestHandler extends UndertowHTTPHandler { response.getOutputStream().write(responseStr.getBytes()); response.flushBuffer(); } catch (Throwable t) { - t.printStackTrace(); - if (undertowExchange.isResponseChannelAvailable()) { - undertowExchange.setStatusCode(500); - final String errorPage = "<html><head><title>Error</title>" - + "</head><body>Internal Error 500" + t.getMessage() - + "</body></html>"; - undertowExchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, - Integer.toString(errorPage.length())); - undertowExchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/html"); - Sender sender = undertowExchange.getResponseSender(); - sender.send(errorPage); - } + sendInternalError(undertowExchange, t); } }
