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 — Auto-port 5.0: MqttDecoder accepts malformed MQTT 5 property sections… · netty/netty@72b975d

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

10171026
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;
@@ -900,6 +901,74 @@ public void testPublishMqtt5SingleBytePropertiesLengthIsUnaffected() throws Exce
900901
}
901902
}
902903

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