Merge pull request #274 from ashakirin/master

AMQ-6894: limit poison exception message to 1024
This commit is contained in:
Jean-Baptiste Onofré 2021-03-22 06:02:38 +01:00 committed by GitHub
commit ea5b2d8813
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 100 additions and 6 deletions

View File

@ -30,6 +30,7 @@ import org.apache.activemq.util.ByteSequence;
public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller { public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller {
public static final Constructor STACK_TRACE_ELEMENT_CONSTRUCTOR; public static final Constructor STACK_TRACE_ELEMENT_CONSTRUCTOR;
private static final int MAX_EXCEPTION_MESSAGE_SIZE = 1024;
static { static {
Constructor constructor = null; Constructor constructor = null;
@ -244,7 +245,7 @@ public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller {
int rc = 0; int rc = 0;
bs.writeBoolean(true); bs.writeBoolean(true);
rc += tightMarshalString1(o.getClass().getName(), bs); rc += tightMarshalString1(o.getClass().getName(), bs);
rc += tightMarshalString1(o.getMessage(), bs); rc += tightMarshalString1(cutMessageIfNeeded(o.getMessage()), bs);
if (wireFormat.isStackTraceEnabled()) { if (wireFormat.isStackTraceEnabled()) {
rc += 2; rc += 2;
StackTraceElement[] stackTrace = o.getStackTrace(); StackTraceElement[] stackTrace = o.getStackTrace();
@ -265,7 +266,7 @@ public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller {
BooleanStream bs) throws IOException { BooleanStream bs) throws IOException {
if (bs.readBoolean()) { if (bs.readBoolean()) {
tightMarshalString2(o.getClass().getName(), dataOut, bs); tightMarshalString2(o.getClass().getName(), dataOut, bs);
tightMarshalString2(o.getMessage(), dataOut, bs); tightMarshalString2(cutMessageIfNeeded(o.getMessage()), dataOut, bs);
if (wireFormat.isStackTraceEnabled()) { if (wireFormat.isStackTraceEnabled()) {
StackTraceElement[] stackTrace = o.getStackTrace(); StackTraceElement[] stackTrace = o.getStackTrace();
dataOut.writeShort(stackTrace.length); dataOut.writeShort(stackTrace.length);
@ -551,7 +552,7 @@ public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller {
dataOut.writeBoolean(o != null); dataOut.writeBoolean(o != null);
if (o != null) { if (o != null) {
looseMarshalString(o.getClass().getName(), dataOut); looseMarshalString(o.getClass().getName(), dataOut);
looseMarshalString(o.getMessage(), dataOut); looseMarshalString(cutMessageIfNeeded(o.getMessage()), dataOut);
if (wireFormat.isStackTraceEnabled()) { if (wireFormat.isStackTraceEnabled()) {
StackTraceElement[] stackTrace = o.getStackTrace(); StackTraceElement[] stackTrace = o.getStackTrace();
dataOut.writeShort(stackTrace.length); dataOut.writeShort(stackTrace.length);
@ -642,4 +643,10 @@ public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller {
} }
return rc; return rc;
} }
protected String cutMessageIfNeeded(final String message) {
return (message.length() > MAX_EXCEPTION_MESSAGE_SIZE)?
message.substring(0, MAX_EXCEPTION_MESSAGE_SIZE - 3) + "..." : message;
}
} }

View File

@ -29,6 +29,7 @@ import org.apache.activemq.util.ByteSequence;
public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller { public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller {
public static final Constructor STACK_TRACE_ELEMENT_CONSTRUCTOR; public static final Constructor STACK_TRACE_ELEMENT_CONSTRUCTOR;
private static final int MAX_EXCEPTION_MESSAGE_SIZE = 1024;
static { static {
Constructor constructor = null; Constructor constructor = null;
@ -243,7 +244,7 @@ public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller {
int rc = 0; int rc = 0;
bs.writeBoolean(true); bs.writeBoolean(true);
rc += tightMarshalString1(o.getClass().getName(), bs); rc += tightMarshalString1(o.getClass().getName(), bs);
rc += tightMarshalString1(o.getMessage(), bs); rc += tightMarshalString1(cutMessageIfNeeded(o.getMessage()), bs);
if (wireFormat.isStackTraceEnabled()) { if (wireFormat.isStackTraceEnabled()) {
rc += 2; rc += 2;
StackTraceElement[] stackTrace = o.getStackTrace(); StackTraceElement[] stackTrace = o.getStackTrace();
@ -264,7 +265,7 @@ public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller {
BooleanStream bs) throws IOException { BooleanStream bs) throws IOException {
if (bs.readBoolean()) { if (bs.readBoolean()) {
tightMarshalString2(o.getClass().getName(), dataOut, bs); tightMarshalString2(o.getClass().getName(), dataOut, bs);
tightMarshalString2(o.getMessage(), dataOut, bs); tightMarshalString2(cutMessageIfNeeded(o.getMessage()), dataOut, bs);
if (wireFormat.isStackTraceEnabled()) { if (wireFormat.isStackTraceEnabled()) {
StackTraceElement[] stackTrace = o.getStackTrace(); StackTraceElement[] stackTrace = o.getStackTrace();
dataOut.writeShort(stackTrace.length); dataOut.writeShort(stackTrace.length);
@ -550,7 +551,7 @@ public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller {
dataOut.writeBoolean(o != null); dataOut.writeBoolean(o != null);
if (o != null) { if (o != null) {
looseMarshalString(o.getClass().getName(), dataOut); looseMarshalString(o.getClass().getName(), dataOut);
looseMarshalString(o.getMessage(), dataOut); looseMarshalString(cutMessageIfNeeded(o.getMessage()), dataOut);
if (wireFormat.isStackTraceEnabled()) { if (wireFormat.isStackTraceEnabled()) {
StackTraceElement[] stackTrace = o.getStackTrace(); StackTraceElement[] stackTrace = o.getStackTrace();
dataOut.writeShort(stackTrace.length); dataOut.writeShort(stackTrace.length);
@ -641,4 +642,10 @@ public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller {
} }
return rc; return rc;
} }
protected String cutMessageIfNeeded(final String message) {
return (message.length() > MAX_EXCEPTION_MESSAGE_SIZE)?
message.substring(0, MAX_EXCEPTION_MESSAGE_SIZE - 3) + "..." : message;
}
} }

View File

@ -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("..."));
}
}

View File

@ -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("..."));
}
}