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: Reject malformed no-payload packets with non-zero Remaining Len… · netty/netty@d89cf88

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
@@ -508,7 +508,11 @@ private Object decodePayload(
508508
return decodePublishPayload(buffer);
509509

510510
default:
511-
// unknown payload , no byte consumed
511+
// No payload for this message type. If the fixed header's Remaining Length
512+
// claimed bytes beyond what the variable header consumed (e.g. a PINGREQ
513+
// with non-zero Remaining Length), the frame is malformed.
514+
// See https://github.com/netty/netty/issues/16851
515+
validateNoBytesRemain(0);
512516
return null;
513517
}
514518
}
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
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,41 @@ public void testDisconnectMessage() throws Exception {
534534
testMessageWithOnlyFixedHeader(MqttMessage.DISCONNECT);
535535
}
536536

537+
@Test
538+
public void testPingReqWithNonZeroRemainingLengthIsRejected() throws Exception {
539+
// Regression for https://github.com/netty/netty/issues/16851: PINGREQ is a 2-byte
540+
// fixed-header-only packet (0xC0 0x00). The bytes below claim Remaining Length 2,
541+
// which makes the trailing 0xD0 0x00 part of the same (malformed) PINGREQ frame
542+
// rather than a separate PINGRESP. The decoder must reject this as one invalid
543+
// message rather than silently accept two.
544+
EmbeddedChannel channel = new EmbeddedChannel(new MqttDecoder());
545+
ByteBuf byteBuf = channel.alloc().buffer();
546+
// Fixed header byte 1: PINGREQ (type 12), all flags 0.
547+
byteBuf.writeByte(0xC0);
548+
// Remaining Length 2 - invalid per MQTT 3.1.1 / 5.0 spec (PINGREQ has no variable
549+
// header or payload, so Remaining Length must be 0).
550+
byteBuf.writeByte(0x02);
551+
// Two leftover bytes still inside the malformed packet's frame:
552+
byteBuf.writeByte(0xD0);
553+
byteBuf.writeByte(0x00);
554+
555+
try {
556+
assertTrue(channel.writeInbound(byteBuf));
557+
MqttMessage first = channel.readInbound();
558+
try {
559+
assertTrue(first.decoderResult().isFailure(),
560+
"expected a failed message for the malformed PINGREQ");
561+
assertInstanceOf(DecoderException.class, first.decoderResult().cause());
562+
} finally {
563+
ReferenceCountUtil.release(first);
564+
}
565+
// No second message: the trailing bytes belong to the malformed frame.
566+
assertNull(channel.readInbound());
567+
} finally {
568+
assertFalse(channel.finishAndReleaseAll());
569+
}
570+
}
571+
537572
//All 0..F message type codes are valid in MQTT 5
538573
@Test
539574
public void testUnknownMessageType() throws Exception {

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.