Skip to content

Throw an exception instead of trying to transmit a header frame that exceeds max frame size #363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/main/java/com/rabbitmq/client/impl/AMQCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,22 @@ public void transmit(AMQChannel channel) throws IOException {

synchronized (assembler) {
Method m = this.assembler.getMethod();
connection.writeFrame(m.toFrame(channelNumber));
if (m.hasContent()) {
byte[] body = this.assembler.getContentBody();

connection.writeFrame(this.assembler.getContentHeader()
.toFrame(channelNumber, body.length));
Frame headerFrame = this.assembler.getContentHeader().toFrame(channelNumber, body.length);

int frameMax = connection.getFrameMax();
int bodyPayloadMax = (frameMax == 0) ? body.length : frameMax
- EMPTY_FRAME_SIZE;

if (headerFrame.size() > frameMax) {
throw new IllegalArgumentException("Content headers exceeded max frame size: " +
headerFrame.size() + " > " + frameMax);
}
connection.writeFrame(m.toFrame(channelNumber));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved this code down to after size check to avoid leaving channel in invalid state in case exception was thrown

connection.writeFrame(headerFrame);

for (int offset = 0; offset < body.length; offset += bodyPayloadMax) {
int remaining = body.length - offset;

Expand All @@ -121,6 +126,8 @@ public void transmit(AMQChannel channel) throws IOException {
offset, fragmentLength);
connection.writeFrame(frame);
}
} else {
connection.writeFrame(m.toFrame(channelNumber));
}
}

Expand Down
47 changes: 47 additions & 0 deletions src/test/java/com/rabbitmq/client/test/functional/FrameMax.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@
package com.rabbitmq.client.test.functional;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeoutException;

Expand All @@ -35,6 +40,7 @@
import com.rabbitmq.client.GetResponse;
import com.rabbitmq.client.impl.AMQCommand;
import com.rabbitmq.client.impl.AMQConnection;
import com.rabbitmq.client.impl.ContentHeaderPropertyWriter;
import com.rabbitmq.client.impl.Frame;
import com.rabbitmq.client.impl.FrameHandler;
import com.rabbitmq.client.impl.LongStringHelper;
Expand Down Expand Up @@ -104,6 +110,47 @@ public FrameMax() {
expectError(AMQP.FRAME_ERROR);
}

/* client should throw exception if headers exceed negotiated
* frame size */
@Test public void rejectHeadersExceedingFrameMax()
throws IOException, TimeoutException {
declareTransientTopicExchange("x");
String queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, "x", "foobar");

Map<String, Object> headers = new HashMap<String, Object>();
String headerName = "x-huge-header";

// create headers with zero-length value to calculate maximum header value size before exceeding frame_max
headers.put(headerName, LongStringHelper.asLongString(new byte[0]));
AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder().headers(headers).build();
Frame minimalHeaderFrame = properties.toFrame(0, 0);
int maxHeaderValueSize = FRAME_MAX - minimalHeaderFrame.size();

// create headers with maximum header value size (frame size equals frame_max)
headers.put(headerName, LongStringHelper.asLongString(new byte[maxHeaderValueSize]));
properties = new AMQP.BasicProperties.Builder().headers(headers).build();

basicPublishVolatile(new byte[100], "x", "foobar", properties);
assertDelivered(queueName, 1);

// create headers with frame size exceeding frame_max by 1
headers.put(headerName, LongStringHelper.asLongString(new byte[maxHeaderValueSize + 1]));
properties = new AMQP.BasicProperties.Builder().headers(headers).build();
try {
basicPublishVolatile(new byte[100], "x", "foobar", properties);
fail("expected rejectHeadersExceedingFrameMax to throw");
} catch (IllegalArgumentException iae) {
assertTrue(iae.getMessage().startsWith("Content headers exceeded max frame size"));
// check that the channel is still operational
assertDelivered(queueName, 0);
}

// cleanup
deleteExchange("x");
}


/* ConnectionFactory that uses MyFrameHandler rather than
* SocketFrameHandler. */
private static class MyConnectionFactory extends ConnectionFactory {
Expand Down