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 — Fix MQTT decoder size check after variable header replay (#16787) · netty/netty@72df658

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
+4-5Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,19 +124,18 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> ou
124124

125125
case READ_VARIABLE_HEADER: try {
126126
int bytesRemainingBeforeVariableHeader = bytesRemainingInVariablePart;
127-
int initialAvailableBytes = buffer.readableBytes();
128127
boolean bailOut = false;
129128
try {
130129
variableHeader = decodeVariableHeader(ctx, buffer, mqttFixedHeader);
131130
} catch (Signal signal) {
132-
if (initialAvailableBytes < maxBytesInMessage) {
133-
// Ask for REPLAY if the buffer was less than maxBytesInMessage
134-
throw signal;
135-
} else {
131+
if (bytesRemainingBeforeVariableHeader > maxBytesInMessage) {
136132
// We couldn't parse the complete message, and it's already too large.
137133
// Swallow the Signal (we don't need more data) and instead bail out
138134
// and throw the TooLongFrameException below.
139135
bailOut = true;
136+
} else {
137+
// Ask for REPLAY if the current message is within maxBytesInMessage.
138+
throw signal;
140139
}
141140
}
142141
if (bailOut || bytesRemainingBeforeVariableHeader > maxBytesInMessage) {
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
+73Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.netty.buffer.UnpooledByteBufAllocator;
2222
import io.netty.channel.Channel;
2323
import io.netty.channel.ChannelHandlerContext;
24+
import io.netty.channel.embedded.EmbeddedChannel;
2425
import io.netty.handler.codec.DecoderException;
2526
import io.netty.handler.codec.EncoderException;
2627
import io.netty.handler.codec.TooLongFrameException;
@@ -336,6 +337,78 @@ public void testPublishMessage() throws Exception {
336337
validatePublishPayload(message.payload(), decodedMessage.payload());
337338
}
338339

340+
@Test
341+
public void testPublishMessageIncompleteVariableHeaderDoesNotUseCumulationSizeForTooLongCheck() throws Exception {
342+
// The leading PUBLISH is hand-crafted rather than going through MqttEncoder because the
343+
// bug under test only triggers when variable-header decoding asks for REPLAY mid-message,
344+
// which in turn requires a deliberately malformed packet (topic-name length prefix larger
345+
// than the bytes we actually supply). MqttEncoder only produces well-formed messages.
346+
final int maxBytesInMessage = 16;
347+
// bytes after the fixed header; < 128 so it fits in a 1-byte Variable Byte Integer.
348+
final int currentPacketRemainingLength = 10;
349+
// > the bytes we write below, so the decoder must REPLAY mid-variable-header.
350+
final int claimedTopicNameLength = 32;
351+
final int followingPingReqPackets = 3;
352+
EmbeddedChannel channel = new EmbeddedChannel(new MqttDecoder(maxBytesInMessage));
353+
ByteBuf byteBuf = ALLOCATOR.buffer();
354+
// Leading PUBLISH packet (incomplete - missing most of the topic name):
355+
// Fixed header byte 1: PUBLISH (type 3), DUP=0, QoS=0, RETAIN=0.
356+
byteBuf.writeByte(0x30);
357+
// Fixed header remaining-length, encoded as a Variable Byte Integer (single byte for values < 128).
358+
byteBuf.writeByte(currentPacketRemainingLength);
359+
// Variable header: 2-byte topic-name length prefix.
360+
byteBuf.writeShort(claimedTopicNameLength);
361+
// ... + only 8 of the 32 topic-name bytes the prefix claims (so the decoder will ask for REPLAY).
362+
byteBuf.writeZero(currentPacketRemainingLength - 2);
363+
// Trailing PINGREQ packets - the cumulation bytes that the buggy size check used to look at.
364+
// Each PINGREQ is just a 2-byte fixed header: 0xC0 (type 12, flags 0) and remaining-length 0.
365+
for (int i = 0; i < followingPingReqPackets; i++) {
366+
byteBuf.writeByte(0xC0);
367+
byteBuf.writeByte(0);
368+
}
369+
370+
try {
371+
assertFalse(channel.writeInbound(byteBuf));
372+
assertNull(channel.readInbound());
373+
} finally {
374+
channel.finishAndReleaseAll();
375+
}
376+
}
377+
378+
@Test
379+
public void testPublishMessageIncompleteVariableHeaderStillFailsWhenCurrentPacketTooLarge() throws Exception {
380+
// Same hand-crafting rationale as the test above: a malformed (incomplete-topic) PUBLISH
381+
// is needed so variable-header decoding asks for REPLAY, which is the code path under test.
382+
final int maxBytesInMessage = 16;
383+
// Declared packet size already exceeds the limit; the in-flight check must still flag it.
384+
final int currentPacketRemainingLength = maxBytesInMessage + 1;
385+
// > the bytes we write below, so the decoder still asks for REPLAY mid-variable-header.
386+
final int claimedTopicNameLength = 32;
387+
EmbeddedChannel channel = new EmbeddedChannel(new MqttDecoder(maxBytesInMessage));
388+
ByteBuf byteBuf = ALLOCATOR.buffer();
389+
// Fixed header byte 1: PUBLISH (type 3), all flags 0.
390+
byteBuf.writeByte(0x30);
391+
// Fixed header remaining-length Variable Byte Integer: 17 (still a single byte since < 128).
392+
byteBuf.writeByte(currentPacketRemainingLength);
393+
// Variable header: 2-byte topic-name length prefix claiming 32 bytes.
394+
byteBuf.writeShort(claimedTopicNameLength);
395+
// ... + 14 zero bytes - fewer than the 32 claimed, so the decoder will ask for REPLAY.
396+
byteBuf.writeZero(maxBytesInMessage - 2);
397+
398+
try {
399+
assertTrue(channel.writeInbound(byteBuf));
400+
MqttMessage decodedMessage = channel.readInbound();
401+
try {
402+
assertTrue(decodedMessage.decoderResult().isFailure());
403+
assertInstanceOf(TooLongFrameException.class, decodedMessage.decoderResult().cause());
404+
} finally {
405+
ReferenceCountUtil.release(decodedMessage);
406+
}
407+
} finally {
408+
channel.finishAndReleaseAll();
409+
}
410+
}
411+
339412
@Test
340413
public void testPubAckMessage() throws Exception {
341414
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.