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 — MqttDecoder accepts malformed MQTT 5 property sections and reclassifi… · netty/netty@6549fd4

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
@@ -1013,6 +1013,15 @@ private Result<MqttProperties> decodeProperties(ByteBuf buffer, int maxAllowedRe
10131013
//shouldn't reach here
10141014
throw new DecoderException("Unknown property type: " + propertyIdValue);
10151015
}
1016+
if (numberOfBytesConsumed - propertiesLengthNumBytes > totalPropertiesLength) {
1017+
// The typed value of the last decoded property read past the declared Property
1018+
// Length, e.g. a Content Type string whose length prefix claims more bytes than
1019+
// are left in the Properties field. MQTT 5.0 section 2.2.2.2 states a Property
1020+
// whose value is not of the specified data type is a Malformed Packet, so reject
1021+
// it instead of letting the read spill into the following packet section.
1022+
throw new DecoderException(
1023+
"MQTT5 property overran declared Property Length: propertyId=" + propertyIdValue);
1024+
}
10161025
}
10171026

10181027
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
@@ -48,6 +48,7 @@
4848

4949
import static io.netty.handler.codec.mqtt.MqttProperties.AUTHENTICATION_DATA;
5050
import static io.netty.handler.codec.mqtt.MqttProperties.AUTHENTICATION_METHOD;
51+
import static io.netty.handler.codec.mqtt.MqttProperties.CONTENT_TYPE;
5152
import static io.netty.handler.codec.mqtt.MqttProperties.MAXIMUM_PACKET_SIZE;
5253
import static io.netty.handler.codec.mqtt.MqttProperties.MAXIMUM_QOS;
5354
import static io.netty.handler.codec.mqtt.MqttProperties.PAYLOAD_FORMAT_INDICATOR;
@@ -898,6 +899,74 @@ public void testPublishMqtt5SingleBytePropertiesLengthIsUnaffected() throws Exce
898899
}
899900
}
900901

902+
@Test
903+
public void testPublishMqtt5PropertyLengthOverreadIsRejected() throws Exception {
904+
EmbeddedChannel channel = new EmbeddedChannel(new MqttDecoder());
905+
try {
906+
assertTrue(channel.writeInbound(newMqtt5Connect()));
907+
ReferenceCountUtil.release(channel.readInbound());
908+
909+
// 30 0a 00 01 61 02 03 00 03 78 79 7a
910+
ByteBuf malformed = channel.alloc().buffer();
911+
malformed.writeByte(0x30); // PUBLISH, DUP=0, QoS=0, RETAIN=0
912+
malformed.writeByte(0x0a); // Remaining Length = 10
913+
writeMqttUtf8String(malformed, "a"); // topic name "a"
914+
malformed.writeByte(0x02); // Property Length = 2 (too short for what follows)
915+
malformed.writeByte(CONTENT_TYPE); // 0x03: starts a Content Type string property
916+
malformed.writeByte(0x00); // string length prefix, msb -- Properties section ends here
917+
malformed.writeByte(0x03); // string length prefix lsb -- actually outside Property Length
918+
malformed.writeByte('x');
919+
malformed.writeByte('y');
920+
malformed.writeByte('z');
921+
922+
assertTrue(channel.writeInbound(malformed));
923+
924+
MqttMessage decoded = channel.readInbound();
925+
try {
926+
assertTrue(decoded.decoderResult().isFailure());
927+
assertInstanceOf(DecoderException.class, decoded.decoderResult().cause());
928+
} finally {
929+
ReferenceCountUtil.release(decoded);
930+
}
931+
} finally {
932+
channel.finishAndReleaseAll();
933+
}
934+
}
935+
936+
@Test
937+
public void testPublishMqtt5PropertyLengthCoveringFullStringIsAccepted() throws Exception {
938+
EmbeddedChannel channel = new EmbeddedChannel(new MqttDecoder());
939+
try {
940+
assertTrue(channel.writeInbound(newMqtt5Connect()));
941+
ReferenceCountUtil.release(channel.readInbound());
942+
943+
// 30 0a 00 01 61 06 03 00 03 78 79 7a
944+
ByteBuf wellFormed = channel.alloc().buffer();
945+
wellFormed.writeByte(0x30);
946+
wellFormed.writeByte(0x0a);
947+
writeMqttUtf8String(wellFormed, "a");
948+
wellFormed.writeByte(0x06); // Property Length = 6, covers the full Content Type string
949+
wellFormed.writeByte(CONTENT_TYPE);
950+
writeMqttUtf8String(wellFormed, "xyz");
951+
952+
assertTrue(channel.writeInbound(wellFormed));
953+
954+
MqttPublishMessage decoded = channel.readInbound();
955+
try {
956+
assertFalse(decoded.decoderResult().isFailure());
957+
assertEquals("a", decoded.variableHeader().topicName());
958+
assertEquals("xyz",
959+
((MqttProperties.StringProperty) decoded.variableHeader()
960+
.properties().getProperty(CONTENT_TYPE)).value);
961+
assertEquals(0, decoded.payload().readableBytes());
962+
} finally {
963+
ReferenceCountUtil.release(decoded);
964+
}
965+
} finally {
966+
channel.finishAndReleaseAll();
967+
}
968+
}
969+
901970
// Minimal well-formed MQTT 5 CONNECT (empty client id) whose only purpose is to make the decoder
902971
// record MQTT 5 for the channel so subsequent packets have their properties decoded.
903972
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.