From d0dab2e88b44702d26dd0883c9940b97ee03fdeb Mon Sep 17 00:00:00 2001 From: Andrei Shakirin Date: Wed, 7 Feb 2018 19:28:55 +0100 Subject: [PATCH] AMQ-6894: limit poison exception message to 1024 --- .../openwire/v1/BaseDataStreamMarshaller.java | 13 ++++-- .../v10/BaseDataStreamMarshaller.java | 13 ++++-- .../v1/BaseDataStreamMarshallerTest.java | 40 +++++++++++++++++++ .../v10/BaseDataStreamMarshallerTest.java | 40 +++++++++++++++++++ 4 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 activemq-client/src/test/java/org/apache/activemq/openwire/v1/BaseDataStreamMarshallerTest.java create mode 100644 activemq-client/src/test/java/org/apache/activemq/openwire/v10/BaseDataStreamMarshallerTest.java diff --git a/activemq-client/src/main/java/org/apache/activemq/openwire/v1/BaseDataStreamMarshaller.java b/activemq-client/src/main/java/org/apache/activemq/openwire/v1/BaseDataStreamMarshaller.java index 0b07a3b3ab..b2c997ea73 100644 --- a/activemq-client/src/main/java/org/apache/activemq/openwire/v1/BaseDataStreamMarshaller.java +++ b/activemq-client/src/main/java/org/apache/activemq/openwire/v1/BaseDataStreamMarshaller.java @@ -30,6 +30,7 @@ import org.apache.activemq.util.ByteSequence; public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller { public static final Constructor STACK_TRACE_ELEMENT_CONSTRUCTOR; + private static final int MAX_EXCEPTION_MESSAGE_SIZE = 1024; static { Constructor constructor = null; @@ -244,7 +245,7 @@ public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller { int rc = 0; bs.writeBoolean(true); rc += tightMarshalString1(o.getClass().getName(), bs); - rc += tightMarshalString1(o.getMessage(), bs); + rc += tightMarshalString1(cutMessageIfNeeded(o.getMessage()), bs); if (wireFormat.isStackTraceEnabled()) { rc += 2; StackTraceElement[] stackTrace = o.getStackTrace(); @@ -265,7 +266,7 @@ public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller { BooleanStream bs) throws IOException { if (bs.readBoolean()) { tightMarshalString2(o.getClass().getName(), dataOut, bs); - tightMarshalString2(o.getMessage(), dataOut, bs); + tightMarshalString2(cutMessageIfNeeded(o.getMessage()), dataOut, bs); if (wireFormat.isStackTraceEnabled()) { StackTraceElement[] stackTrace = o.getStackTrace(); dataOut.writeShort(stackTrace.length); @@ -551,7 +552,7 @@ public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller { dataOut.writeBoolean(o != null); if (o != null) { looseMarshalString(o.getClass().getName(), dataOut); - looseMarshalString(o.getMessage(), dataOut); + looseMarshalString(cutMessageIfNeeded(o.getMessage()), dataOut); if (wireFormat.isStackTraceEnabled()) { StackTraceElement[] stackTrace = o.getStackTrace(); dataOut.writeShort(stackTrace.length); @@ -642,4 +643,10 @@ public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller { } return rc; } + + protected String cutMessageIfNeeded(final String message) { + return (message.length() > MAX_EXCEPTION_MESSAGE_SIZE)? + message.substring(0, MAX_EXCEPTION_MESSAGE_SIZE - 3) + "..." : message; + + } } diff --git a/activemq-client/src/main/java/org/apache/activemq/openwire/v10/BaseDataStreamMarshaller.java b/activemq-client/src/main/java/org/apache/activemq/openwire/v10/BaseDataStreamMarshaller.java index 091743ebde..a570d3d17d 100644 --- a/activemq-client/src/main/java/org/apache/activemq/openwire/v10/BaseDataStreamMarshaller.java +++ b/activemq-client/src/main/java/org/apache/activemq/openwire/v10/BaseDataStreamMarshaller.java @@ -29,6 +29,7 @@ import org.apache.activemq.util.ByteSequence; public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller { public static final Constructor STACK_TRACE_ELEMENT_CONSTRUCTOR; + private static final int MAX_EXCEPTION_MESSAGE_SIZE = 1024; static { Constructor constructor = null; @@ -243,7 +244,7 @@ public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller { int rc = 0; bs.writeBoolean(true); rc += tightMarshalString1(o.getClass().getName(), bs); - rc += tightMarshalString1(o.getMessage(), bs); + rc += tightMarshalString1(cutMessageIfNeeded(o.getMessage()), bs); if (wireFormat.isStackTraceEnabled()) { rc += 2; StackTraceElement[] stackTrace = o.getStackTrace(); @@ -264,7 +265,7 @@ public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller { BooleanStream bs) throws IOException { if (bs.readBoolean()) { tightMarshalString2(o.getClass().getName(), dataOut, bs); - tightMarshalString2(o.getMessage(), dataOut, bs); + tightMarshalString2(cutMessageIfNeeded(o.getMessage()), dataOut, bs); if (wireFormat.isStackTraceEnabled()) { StackTraceElement[] stackTrace = o.getStackTrace(); dataOut.writeShort(stackTrace.length); @@ -550,7 +551,7 @@ public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller { dataOut.writeBoolean(o != null); if (o != null) { looseMarshalString(o.getClass().getName(), dataOut); - looseMarshalString(o.getMessage(), dataOut); + looseMarshalString(cutMessageIfNeeded(o.getMessage()), dataOut); if (wireFormat.isStackTraceEnabled()) { StackTraceElement[] stackTrace = o.getStackTrace(); dataOut.writeShort(stackTrace.length); @@ -641,4 +642,10 @@ public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller { } return rc; } + + protected String cutMessageIfNeeded(final String message) { + return (message.length() > MAX_EXCEPTION_MESSAGE_SIZE)? + message.substring(0, MAX_EXCEPTION_MESSAGE_SIZE - 3) + "..." : message; + + } } diff --git a/activemq-client/src/test/java/org/apache/activemq/openwire/v1/BaseDataStreamMarshallerTest.java b/activemq-client/src/test/java/org/apache/activemq/openwire/v1/BaseDataStreamMarshallerTest.java new file mode 100644 index 0000000000..cc2640d1eb --- /dev/null +++ b/activemq-client/src/test/java/org/apache/activemq/openwire/v1/BaseDataStreamMarshallerTest.java @@ -0,0 +1,40 @@ +/** + * 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.activemq.openwire.v1; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; + +import javax.jms.MessageFormatException; + +import org.junit.Test; + +public class BaseDataStreamMarshallerTest { + + private static final int MAX_MESSAGE_LENGTH = 1024; + + @Test + public void cutMessageIfNeeded() throws MessageFormatException { + char[] message = new char[2056]; + Arrays.fill(message, '1'); + String cutMessage = (new WireFormatInfoMarshaller()).cutMessageIfNeeded(String.valueOf(message)); + assertEquals("Expected length " + MAX_MESSAGE_LENGTH, MAX_MESSAGE_LENGTH, cutMessage.length()); + assertTrue("Expected message tail ...", cutMessage.endsWith("...")); + } +} \ No newline at end of file diff --git a/activemq-client/src/test/java/org/apache/activemq/openwire/v10/BaseDataStreamMarshallerTest.java b/activemq-client/src/test/java/org/apache/activemq/openwire/v10/BaseDataStreamMarshallerTest.java new file mode 100644 index 0000000000..6aebe272bb --- /dev/null +++ b/activemq-client/src/test/java/org/apache/activemq/openwire/v10/BaseDataStreamMarshallerTest.java @@ -0,0 +1,40 @@ +/** + * 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.activemq.openwire.v10; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; + +import javax.jms.MessageFormatException; + +import org.junit.Test; + +public class BaseDataStreamMarshallerTest { + + private static final int MAX_MESSAGE_LENGTH = 1024; + + @Test + public void cutMessageIfNeeded() throws MessageFormatException { + char[] message = new char[2056]; + Arrays.fill(message, '1'); + String cutMessage = (new WireFormatInfoMarshaller()).cutMessageIfNeeded(String.valueOf(message)); + assertEquals("Expected length " + MAX_MESSAGE_LENGTH, MAX_MESSAGE_LENGTH, cutMessage.length()); + assertTrue("Expected message tail ...", cutMessage.endsWith("...")); + } +} \ No newline at end of file