GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — MQTT: Fix maxBytesInMessage guard misfiring on fragmented variable he… · netty/netty@910f019

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-mqtt/src
    • main/java/io/netty/handler/codec/mqtt
      • MqttDecoder.java
    • test/java/io/netty/handler/codec/mqtt
      • MqttCodecTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-mqtt/src/main/java/io/netty/handler/codec/mqtt/MqttDecoder.java‎

Copy file name to clipboardExpand all lines: codec-mqtt/src/main/java/io/netty/handler/codec/mqtt/MqttDecoder.java
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> ou
9797

9898
case READ_VARIABLE_HEADER: try {
9999
int bytesRemainingBeforeVariableHeader = bytesRemainingInVariablePart;
100-
int initialAvailableBytes = buffer.readableBytes();
100+
// Use actualReadableBytes() rather than buffer.readableBytes(): the latter is a
101+
// ReplayingDecoderByteBuf whose readableBytes() returns Integer.MAX_VALUE - readerIndex,
102+
// which would make the maxBytesInMessage check below misfire and reject a message whose
103+
// variable header has merely not fully arrived yet as if it were too long.
104+
int initialAvailableBytes = actualReadableBytes();
101105
boolean bailOut = false;
102106
try {
103107
variableHeader = decodeVariableHeader(ctx, buffer, mqttFixedHeader);
Collapse file

‎codec-mqtt/src/test/java/io/netty/handler/codec/mqtt/MqttCodecTest.java‎

Copy file name to clipboardExpand all lines: codec-mqtt/src/test/java/io/netty/handler/codec/mqtt/MqttCodecTest.java
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,39 @@ public void testPublishMessage() throws Exception {
311311
validatePublishPayload(message.payload(), decodedMessage.payload());
312312
}
313313

314+
@Test
315+
public void testPublishMessageWithFragmentedVariableHeaderIsNotRejectedAsTooLong() throws Exception {
316+
// When the variable header arrives in chunks, decodeVariableHeader throws a REPLAY Signal so
317+
// the decoder waits for the rest. The READ_VARIABLE_HEADER guard must use actualReadableBytes()
318+
// to decide whether the message is within maxBytesInMessage; using the ReplayingDecoderByteBuf's
319+
// readableBytes() (Integer.MAX_VALUE - readerIndex) made the guard misfire and reject a perfectly
320+
// valid, merely-incomplete message as TooLongFrameException.
321+
final MqttPublishMessage message = createPublishMessage();
322+
ByteBuf byteBuf = MqttEncoder.doEncode(ctx, message);
323+
try {
324+
final int total = byteBuf.readableBytes();
325+
// 2-byte fixed header + 1 byte of the variable header: enough to decode the fixed header and
326+
// enter READ_VARIABLE_HEADER, but not enough to read the 2-byte topic length, forcing a REPLAY.
327+
ByteBuf firstChunk = byteBuf.readRetainedSlice(3);
328+
ByteBuf secondChunk = byteBuf.readRetainedSlice(total - 3);
329+
330+
mqttDecoder.channelRead(ctx, firstChunk);
331+
// The decoder must ask for more data, not emit a (failed) message.
332+
assertEquals(0, out.size());
333+
334+
mqttDecoder.channelRead(ctx, secondChunk);
335+
assertEquals(1, out.size());
336+
337+
final MqttPublishMessage decodedMessage = (MqttPublishMessage) out.get(0);
338+
assertTrue(decodedMessage.decoderResult().isSuccess());
339+
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
340+
validatePublishVariableHeader(message.variableHeader(), decodedMessage.variableHeader());
341+
validatePublishPayload(message.payload(), decodedMessage.payload());
342+
} finally {
343+
byteBuf.release();
344+
}
345+
}
346+
314347
@Test
315348
public void testPubAckMessage() throws Exception {
316349
testMessageWithOnlyFixedHeaderAndMessageIdVariableHeader(MqttMessageType.PUBACK);

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.