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 — Port 4.1: MqttDecoder accepts malformed MQTT 5 property sections and … · netty/netty@fcdf0b6

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
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,15 @@ private Result<MqttProperties> decodeProperties(ByteBuf buffer, int maxAllowedRe
988988
//shouldn't reach here
989989
throw new DecoderException("Unknown property type: " + propertyType);
990990
}
991+
if (numberOfBytesConsumed - propertiesLengthNumBytes > totalPropertiesLength) {
992+
// The typed value of the last decoded property read past the declared Property
993+
// Length, e.g. a Content Type string whose length prefix claims more bytes than
994+
// are left in the Properties field. MQTT 5.0 section 2.2.2.2 states a Property
995+
// whose value is not of the specified data type is a Malformed Packet, so reject
996+
// it instead of letting the read spill into the following packet section.
997+
throw new DecoderException(
998+
"MQTT5 property overran declared Property Length: propertyId=" + propertyIdValue);
999+
}
9911000
}
9921001

9931002
return new Result<MqttProperties>(decodedProperties, numberOfBytesConsumed);
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
+69Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151

5252
import static io.netty.handler.codec.mqtt.MqttProperties.MqttPropertyType.AUTHENTICATION_DATA;
5353
import static io.netty.handler.codec.mqtt.MqttProperties.MqttPropertyType.AUTHENTICATION_METHOD;
54+
import static io.netty.handler.codec.mqtt.MqttProperties.MqttPropertyType.CONTENT_TYPE;
5455
import static io.netty.handler.codec.mqtt.MqttProperties.MqttPropertyType.MAXIMUM_PACKET_SIZE;
5556
import static io.netty.handler.codec.mqtt.MqttProperties.MqttPropertyType.MAXIMUM_QOS;
5657
import static io.netty.handler.codec.mqtt.MqttProperties.MqttPropertyType.PAYLOAD_FORMAT_INDICATOR;
@@ -937,6 +938,74 @@ public void testPublishMqtt5SingleBytePropertiesLengthIsUnaffected() throws Exce
937938
}
938939
}
939940

941+
@Test
942+
public void testPublishMqtt5PropertyLengthOverreadIsRejected() throws Exception {
943+
EmbeddedChannel channel = new EmbeddedChannel(new MqttDecoder());
944+
try {
945+
assertTrue(channel.writeInbound(newMqtt5Connect()));
946+
ReferenceCountUtil.release(channel.readInbound());
947+
948+
// 30 0a 00 01 61 02 03 00 03 78 79 7a
949+
ByteBuf malformed = channel.alloc().buffer();
950+
malformed.writeByte(0x30); // PUBLISH, DUP=0, QoS=0, RETAIN=0
951+
malformed.writeByte(0x0a); // Remaining Length = 10
952+
writeMqttUtf8String(malformed, "a"); // topic name "a"
953+
malformed.writeByte(0x02); // Property Length = 2 (too short for what follows)
954+
malformed.writeByte(CONTENT_TYPE.value()); // 0x03: starts a Content Type string property
955+
malformed.writeByte(0x00); // string length prefix, msb -- Properties section ends here
956+
malformed.writeByte(0x03); // string length prefix lsb -- actually outside Property Length
957+
malformed.writeByte('x');
958+
malformed.writeByte('y');
959+
malformed.writeByte('z');
960+
961+
assertTrue(channel.writeInbound(malformed));
962+
963+
MqttMessage decoded = channel.readInbound();
964+
try {
965+
assertTrue(decoded.decoderResult().isFailure());
966+
assertInstanceOf(DecoderException.class, decoded.decoderResult().cause());
967+
} finally {
968+
ReferenceCountUtil.release(decoded);
969+
}
970+
} finally {
971+
channel.finishAndReleaseAll();
972+
}
973+
}
974+
975+
@Test
976+
public void testPublishMqtt5PropertyLengthCoveringFullStringIsAccepted() throws Exception {
977+
EmbeddedChannel channel = new EmbeddedChannel(new MqttDecoder());
978+
try {
979+
assertTrue(channel.writeInbound(newMqtt5Connect()));
980+
ReferenceCountUtil.release(channel.readInbound());
981+
982+
// 30 0a 00 01 61 06 03 00 03 78 79 7a
983+
ByteBuf wellFormed = channel.alloc().buffer();
984+
wellFormed.writeByte(0x30);
985+
wellFormed.writeByte(0x0a);
986+
writeMqttUtf8String(wellFormed, "a");
987+
wellFormed.writeByte(0x06); // Property Length = 6, covers the full Content Type string
988+
wellFormed.writeByte(CONTENT_TYPE.value());
989+
writeMqttUtf8String(wellFormed, "xyz");
990+
991+
assertTrue(channel.writeInbound(wellFormed));
992+
993+
MqttPublishMessage decoded = channel.readInbound();
994+
try {
995+
assertFalse(decoded.decoderResult().isFailure());
996+
assertEquals("a", decoded.variableHeader().topicName());
997+
assertEquals("xyz",
998+
((MqttProperties.StringProperty) decoded.variableHeader()
999+
.properties().getProperty(CONTENT_TYPE.value())).value);
1000+
assertEquals(0, decoded.payload().readableBytes());
1001+
} finally {
1002+
ReferenceCountUtil.release(decoded);
1003+
}
1004+
} finally {
1005+
channel.finishAndReleaseAll();
1006+
}
1007+
}
1008+
9401009
// Minimal well-formed MQTT 5 CONNECT (empty client id) whose only purpose is to make the decoder
9411010
// record MQTT 5 for the channel so subsequent packets have their properties decoded.
9421011
private static ByteBuf newMqtt5Connect() {

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.