]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MINOR: mqtt: reject invalid CONNECT flags and MQTT 3.1.1 CONNACK return codes
authorMartin Mirchev <mirchevmartin2203@gmail.com>
Wed, 18 Nov 2026 12:35:30 +0000 (20:35 +0800)
committerWilly Tarreau <w@1wt.eu>
Fri, 20 Nov 2026 11:36:58 +0000 (12:36 +0100)
mqtt_is_valid() and the mqtt_field_value() converter accept several
messages the specifications forbid:

  - a CONNECT with the Will QoS set to 3. The only check rejected the
    case where both QoS bits are set without the Will flag, so neither
    "Will QoS 3 with Will" nor "Will QoS 1 or Will Retain without Will"
    was caught (MQTT 3.1.1 3.1.2.6/3.1.2.7, MQTT 5.0 3.1.2.6/3.1.2.7);
  - an MQTT 3.1.1 CONNECT with the Password flag but not the User Name
    flag (MQTT 3.1.1 3.1.2.9; MQTT 5.0 allows it, so only 3.1.1 is
    checked);
  - an MQTT 3.1.1 CONNACK with a return code above 5, e.g. 200 (MQTT
    3.1.1 3.2.2.3 only defines 0 to 5).

For example mqtt_is_valid returns 1 for a 3.1.1 CONNECT with flags 0x1c
or 0x40, and for a 3.1.1 CONNACK "20 02 00 c8". Reject these messages.

This should be backported to all stable versions.

src/mqtt.c

index 2cee38b5d290723148e70457da639425afba9f7c..11a1eb4748f1492104f3233b83361a4d932951d5 100644 (file)
@@ -738,8 +738,18 @@ static int mqtt_parse_connect(struct ist parser, struct mqtt_pkt *mpkt)
        if (!isttest(parser) || (mpkt->data.connect.var_hdr.flags & MQTT_CONNECT_FL_RESERVED))
                goto end;
 
-       /* if WILL flag must be set to have WILL_QOS flag or WILL_RETAIN set */
-       if ((mpkt->data.connect.var_hdr.flags & (MQTT_CONNECT_FL_WILL|MQTT_CONNECT_FL_WILL_QOS|MQTT_CONNECT_FL_WILL_RETAIN)) == MQTT_CONNECT_FL_WILL_QOS)
+       /* WILL flag must be set to have WILL_QOS flag or WILL_RETAIN set */
+       if (!(mpkt->data.connect.var_hdr.flags & MQTT_CONNECT_FL_WILL) &&
+           (mpkt->data.connect.var_hdr.flags & (MQTT_CONNECT_FL_WILL_QOS|MQTT_CONNECT_FL_WILL_RETAIN)))
+           goto end;
+
+       /* WILL_QOS must not be 3 */
+       if ((mpkt->data.connect.var_hdr.flags & MQTT_CONNECT_FL_WILL_QOS) == MQTT_CONNECT_FL_WILL_QOS)
+           goto end;
+
+       /* in MQTT 3.1.1, PASSWORD flag requires the USERNAME flag */
+       if (mpkt->data.connect.var_hdr.protocol_version == MQTT_VERSION_3_1_1 &&
+           (mpkt->data.connect.var_hdr.flags & (MQTT_CONNECT_FL_USERNAME|MQTT_CONNECT_FL_PASSWORD)) == MQTT_CONNECT_FL_PASSWORD)
            goto end;
 
        /* read keepalive */
@@ -1025,8 +1035,12 @@ static int mqtt_parse_connack(struct ist parser, struct mqtt_pkt *mpkt)
        if (!isttest(parser))
                goto end;
 
-       /* we can leave here for MQTT 3.1.1 */
+       /* we can leave here for MQTT 3.1.1, where only return codes 0 to 5
+        * are defined
+        */
        if (mpkt->data.connack.var_hdr.protocol_version == MQTT_VERSION_3_1_1) {
+               if (mpkt->data.connack.var_hdr.reason_code > 5)
+                       goto end;
                if ((orig_len - istlen(parser)) == mpkt->fixed_hdr.remaining_length)
                        ret = MQTT_VALID_MESSAGE;
                goto end;