From: Martin Mirchev Date: Wed, 18 Nov 2026 12:35:30 +0000 (+0800) Subject: BUG/MINOR: mqtt: reject invalid CONNECT flags and MQTT 3.1.1 CONNACK return codes X-Git-Url: https://git.kaiwu.me/http/control.html?a=commitdiff_plain;h=795b4688ef325242fc9a645e411ccae1cdd596a1;p=haproxy.git BUG/MINOR: mqtt: reject invalid CONNECT flags and MQTT 3.1.1 CONNACK return codes 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. --- diff --git a/src/mqtt.c b/src/mqtt.c index 2cee38b5d..11a1eb474 100644 --- a/src/mqtt.c +++ b/src/mqtt.c @@ -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;