This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch coheigea/common-fault in repository https://gitbox.apache.org/repos/asf/cxf.git
commit c055fe8803d17669d8f4238ae8fc9125d274e236 Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Wed Nov 18 16:02:47 2026 +0000 Don't leak fault cause details in XML binding faults --- .../org/apache/cxf/interceptor/FaultUtils.java | 70 ++++++++++++++++++ .../org/apache/cxf/interceptor/FaultUtilsTest.java | 84 ++++++++++++++++++++++ .../soap/interceptor/AbstractSoapInterceptor.java | 21 ------ .../interceptor/Soap11FaultOutInterceptor.java | 3 +- .../interceptor/Soap12FaultOutInterceptor.java | 3 +- .../xml/interceptor/XMLFaultOutInterceptor.java | 4 +- .../interceptor/XMLFaultOutInterceptorTest.java | 42 ++++++++++- 7 files changed, 201 insertions(+), 26 deletions(-) diff --git a/core/src/main/java/org/apache/cxf/interceptor/FaultUtils.java b/core/src/main/java/org/apache/cxf/interceptor/FaultUtils.java new file mode 100644 index 00000000000..38e761191a6 --- /dev/null +++ b/core/src/main/java/org/apache/cxf/interceptor/FaultUtils.java @@ -0,0 +1,70 @@ +/** + * 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.cxf.interceptor; + +import org.apache.cxf.message.Message; +import org.apache.cxf.message.MessageUtils; + +/** + * Helpers shared by the binding-specific fault out interceptors. + */ +public final class FaultUtils { + + /** + * Message property that, when set, is used verbatim as the fault string. + */ + public static final String FORCED_FAULT_STRING = "forced.faultstring"; + + static final String DEFAULT_FAULT_STRING = "Fault occurred while processing."; + + private FaultUtils() { + } + + /** + * Returns the human-readable fault string to send to the peer for the given fault. + * <p> + * By default only the fault's own message is returned. Details of the underlying + * cause are only included when {@link Message#EXCEPTION_MESSAGE_CAUSE_ENABLED} + * is enabled on the message, so that internal exception details are not disclosed + * to remote callers unless explicitly configured. + */ + public static String getFaultString(Message message, Fault fault) { + Object forced = message.get(FORCED_FAULT_STRING); + if (forced != null) { + return (String) forced; + } + boolean config = MessageUtils.getContextualBoolean(message, Message.EXCEPTION_MESSAGE_CAUSE_ENABLED, false); + Throwable cause = fault.getCause(); + if (fault.getMessage() != null) { + if (config && cause != null + && cause.getMessage() != null && !fault.getMessage().equals(cause.getMessage())) { + return fault.getMessage() + " Caused by: " + cause.getMessage(); + } + return fault.getMessage(); + } else if (config && cause != null) { + if (cause.getMessage() != null) { + return cause.getMessage(); + } + return cause.toString(); + } else { + return DEFAULT_FAULT_STRING; + } + } +} diff --git a/core/src/test/java/org/apache/cxf/interceptor/FaultUtilsTest.java b/core/src/test/java/org/apache/cxf/interceptor/FaultUtilsTest.java new file mode 100644 index 00000000000..bd6f6167d00 --- /dev/null +++ b/core/src/test/java/org/apache/cxf/interceptor/FaultUtilsTest.java @@ -0,0 +1,84 @@ +/** + * 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.cxf.interceptor; + +import java.util.ResourceBundle; + +import org.apache.cxf.common.i18n.Message; +import org.apache.cxf.message.MessageImpl; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class FaultUtilsTest { + + private static Fault fault(String msg, Throwable cause) { + return new Fault(new Message(msg, (ResourceBundle) null), cause); + } + + private static MessageImpl message(boolean causeEnabled) { + MessageImpl m = new MessageImpl(); + if (causeEnabled) { + m.put(org.apache.cxf.message.Message.EXCEPTION_MESSAGE_CAUSE_ENABLED, Boolean.TRUE); + } + return m; + } + + @Test + public void testForcedFaultString() { + MessageImpl m = message(true); + m.put(FaultUtils.FORCED_FAULT_STRING, "forced"); + assertEquals("forced", FaultUtils.getFaultString(m, fault("msg", new RuntimeException("internal")))); + } + + @Test + public void testCauseHiddenByDefault() { + Fault f = fault("msg", new IllegalStateException("internal detail")); + assertEquals("msg", FaultUtils.getFaultString(message(false), f)); + } + + @Test + public void testCauseIncludedWhenEnabled() { + Fault f = fault("msg", new IllegalStateException("internal detail")); + assertEquals("msg Caused by: internal detail", FaultUtils.getFaultString(message(true), f)); + } + + @Test + public void testCauseWithSameMessageNotDuplicated() { + Fault f = new Fault(new IllegalStateException("same")); + assertEquals("same", FaultUtils.getFaultString(message(true), f)); + assertEquals("same", FaultUtils.getFaultString(message(false), f)); + } + + @Test + public void testNullMessage() { + Fault f = fault(null, new IllegalStateException("internal detail")); + assertEquals(FaultUtils.DEFAULT_FAULT_STRING, FaultUtils.getFaultString(message(false), f)); + assertEquals("internal detail", FaultUtils.getFaultString(message(true), f)); + } + + @Test + public void testNullMessageAndNullCauseMessage() { + Fault f = fault(null, new IllegalStateException()); + assertEquals(FaultUtils.DEFAULT_FAULT_STRING, FaultUtils.getFaultString(message(false), f)); + assertEquals(IllegalStateException.class.getName(), FaultUtils.getFaultString(message(true), f)); + } +} diff --git a/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/AbstractSoapInterceptor.java b/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/AbstractSoapInterceptor.java index cb95aa384ba..4a74960fc53 100644 --- a/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/AbstractSoapInterceptor.java +++ b/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/AbstractSoapInterceptor.java @@ -119,25 +119,4 @@ public abstract class AbstractSoapInterceptor extends AbstractPhaseInterceptor<S } } } - - static String getFaultMessage(SoapMessage message, SoapFault fault) { - if (message.get("forced.faultstring") != null) { - return (String) message.get("forced.faultstring"); - } - boolean config = MessageUtils.getContextualBoolean(message, Message.EXCEPTION_MESSAGE_CAUSE_ENABLED, false); - if (fault.getMessage() != null) { - if (config && fault.getCause() != null - && fault.getCause().getMessage() != null && !fault.getMessage().equals(fault.getCause().getMessage())) { - return fault.getMessage() + " Caused by: " + fault.getCause().getMessage(); - } - return fault.getMessage(); - } else if (config && fault.getCause() != null) { - if (fault.getCause().getMessage() != null) { - return fault.getCause().getMessage(); - } - return fault.getCause().toString(); - } else { - return "Fault occurred while processing."; - } - } } diff --git a/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/Soap11FaultOutInterceptor.java b/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/Soap11FaultOutInterceptor.java index 95ebee9f7c4..f8a10b9fd0b 100644 --- a/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/Soap11FaultOutInterceptor.java +++ b/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/Soap11FaultOutInterceptor.java @@ -35,6 +35,7 @@ import org.apache.cxf.binding.soap.interceptor.Soap12FaultOutInterceptor.Soap12F import org.apache.cxf.common.logging.LogUtils; import org.apache.cxf.common.util.StringUtils; import org.apache.cxf.interceptor.Fault; +import org.apache.cxf.interceptor.FaultUtils; import org.apache.cxf.message.Message; import org.apache.cxf.message.MessageUtils; import org.apache.cxf.phase.AbortedInvocationException; @@ -110,7 +111,7 @@ public class Soap11FaultOutInterceptor extends AbstractSoapInterceptor { if (!StringUtils.isEmpty(lang)) { writer.writeAttribute("xml", "http://www.w3.org/XML/1998/namespace", "lang", lang); } - writer.writeCharacters(getFaultMessage(message, fault)); + writer.writeCharacters(FaultUtils.getFaultString(message, fault)); writer.writeEndElement(); prepareStackTrace(message, fault); diff --git a/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/Soap12FaultOutInterceptor.java b/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/Soap12FaultOutInterceptor.java index e766282c028..b28b6e6a194 100644 --- a/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/Soap12FaultOutInterceptor.java +++ b/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/Soap12FaultOutInterceptor.java @@ -36,6 +36,7 @@ import org.apache.cxf.binding.soap.interceptor.Soap11FaultOutInterceptor.Soap11F import org.apache.cxf.common.logging.LogUtils; import org.apache.cxf.common.util.StringUtils; import org.apache.cxf.interceptor.Fault; +import org.apache.cxf.interceptor.FaultUtils; import org.apache.cxf.message.Message; import org.apache.cxf.message.MessageUtils; import org.apache.cxf.phase.AbortedInvocationException; @@ -131,7 +132,7 @@ public class Soap12FaultOutInterceptor extends AbstractSoapInterceptor { lang = getLangCode(); } writer.writeAttribute("xml", "http://www.w3.org/XML/1998/namespace", "lang", lang); - writer.writeCharacters(getFaultMessage(message, fault)); + writer.writeCharacters(FaultUtils.getFaultString(message, fault)); writer.writeEndElement(); writer.writeEndElement(); diff --git a/rt/bindings/xml/src/main/java/org/apache/cxf/binding/xml/interceptor/XMLFaultOutInterceptor.java b/rt/bindings/xml/src/main/java/org/apache/cxf/binding/xml/interceptor/XMLFaultOutInterceptor.java index 400ee5bf387..2a05e81ae48 100644 --- a/rt/bindings/xml/src/main/java/org/apache/cxf/binding/xml/interceptor/XMLFaultOutInterceptor.java +++ b/rt/bindings/xml/src/main/java/org/apache/cxf/binding/xml/interceptor/XMLFaultOutInterceptor.java @@ -32,6 +32,7 @@ import org.apache.cxf.common.i18n.BundleUtils; import org.apache.cxf.helpers.NSStack; import org.apache.cxf.interceptor.AbstractOutDatabindingInterceptor; import org.apache.cxf.interceptor.Fault; +import org.apache.cxf.interceptor.FaultUtils; import org.apache.cxf.message.Message; import org.apache.cxf.phase.Phase; import org.apache.cxf.staxutils.StaxUtils; @@ -67,8 +68,7 @@ public class XMLFaultOutInterceptor extends AbstractOutDatabindingInterceptor { XMLConstants.NS_XML_FORMAT); StaxUtils.writeStartElement(writer, prefix, XMLFault.XML_FAULT_STRING, XMLConstants.NS_XML_FORMAT); - Throwable t = xmlFault.getCause(); - writer.writeCharacters(t == null ? xmlFault.getMessage() : t.toString()); + writer.writeCharacters(FaultUtils.getFaultString(message, xmlFault)); // fault string writer.writeEndElement(); // call StaxUtils to write Fault detail. diff --git a/rt/bindings/xml/src/test/java/org/apache/cxf/binding/xml/interceptor/XMLFaultOutInterceptorTest.java b/rt/bindings/xml/src/test/java/org/apache/cxf/binding/xml/interceptor/XMLFaultOutInterceptorTest.java index f0eb2f75313..dce16f6a1de 100644 --- a/rt/bindings/xml/src/test/java/org/apache/cxf/binding/xml/interceptor/XMLFaultOutInterceptorTest.java +++ b/rt/bindings/xml/src/test/java/org/apache/cxf/binding/xml/interceptor/XMLFaultOutInterceptorTest.java @@ -23,6 +23,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.OutputStream; +import java.util.ResourceBundle; import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamWriter; @@ -34,6 +35,7 @@ import jakarta.xml.bind.Marshaller; import org.apache.cxf.binding.xml.XMLConstants; import org.apache.cxf.binding.xml.XMLFault; import org.apache.cxf.interceptor.Fault; +import org.apache.cxf.message.Message; import org.apache.cxf.staxutils.DepthXMLStreamReader; import org.apache.cxf.staxutils.StaxUtils; import org.apache.hello_world_doc_lit.PingMeFault; @@ -80,7 +82,8 @@ public class XMLFaultOutInterceptorTest extends TestBase { dxr.nextTag(); StaxUtils.toNextElement(dxr); assertEquals(XMLFault.XML_FAULT_STRING, dxr.getLocalName()); - assertEquals(fault.toString(), dxr.getElementText()); + // the cause class name must not be leaked, only the fault message + assertEquals("TEST_FAULT", dxr.getElementText()); dxr.nextTag(); StaxUtils.toNextElement(dxr); @@ -91,6 +94,43 @@ public class XMLFaultOutInterceptorTest extends TestBase { assertEquals("faultDetail", dxr.getLocalName()); } + @Test + public void testFaultCauseNotLeakedByDefault() throws Exception { + Fault fault = new Fault(new org.apache.cxf.common.i18n.Message("Application fault", + (ResourceBundle) null), + new IllegalStateException("internal db host=10.0.0.1")); + assertEquals("Application fault", writeAndReadFaultString(fault)); + } + + @Test + public void testFaultCauseIncludedWhenEnabled() throws Exception { + xmlMessage.put(Message.EXCEPTION_MESSAGE_CAUSE_ENABLED, Boolean.TRUE); + Fault fault = new Fault(new org.apache.cxf.common.i18n.Message("Application fault", + (ResourceBundle) null), + new IllegalStateException("internal detail")); + assertEquals("Application fault Caused by: internal detail", writeAndReadFaultString(fault)); + } + + private String writeAndReadFaultString(Fault fault) throws Exception { + OutputStream outputStream = new ByteArrayOutputStream(); + xmlMessage.setContent(OutputStream.class, outputStream); + XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(outputStream); + xmlMessage.setContent(XMLStreamWriter.class, writer); + xmlMessage.setContent(Exception.class, fault); + + out.handleMessage(xmlMessage); + outputStream.flush(); + + DepthXMLStreamReader dxr = new DepthXMLStreamReader(getXMLReader()); + dxr.nextTag(); + StaxUtils.toNextElement(dxr); + assertEquals(XMLFault.XML_FAULT_ROOT, dxr.getLocalName()); + dxr.nextTag(); + StaxUtils.toNextElement(dxr); + assertEquals(XMLFault.XML_FAULT_STRING, dxr.getLocalName()); + return dxr.getElementText(); + } + private XMLStreamReader getXMLReader() throws Exception { ByteArrayOutputStream o = (ByteArrayOutputStream) xmlMessage.getContent(OutputStream.class); InputStream in = new ByteArrayInputStream(o.toByteArray());
