Skip to content

Commit 1541cfe

Browse files
embhornclaude
andcommitted
Add MQTT v3.1.1 spec compliance checks
Enforce [MQTT-3.1.2-22]: reject CONNECT when password is set without username. Add [MQTT-2.2.3]: validate Variable Byte Integer max value (268,435,455) in MqttEncode_Vbi. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e1caa8e commit 1541cfe

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

src/mqtt_packet.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,11 @@ int MqttEncode_Vbi(byte *buf, word32 x)
255255
int rc = 0;
256256
byte encodedByte;
257257

258+
/* [MQTT-2.2.3]: Max value is 268,435,455 (0x0FFFFFFF) */
259+
if (x > MQTT_PACKET_MAX_REMAIN_LEN) {
260+
return MQTT_TRACE_ERROR(MQTT_CODE_ERROR_MALFORMED_DATA);
261+
}
262+
258263
do {
259264
encodedByte = (x & ~MQTT_PACKET_LEN_ENCODE_MASK) & 0xFF;
260265
x >>= 7;
@@ -712,6 +717,12 @@ int MqttEncode_Connect(byte *tx_buf, int tx_buf_len, MqttConnect *mc_connect)
712717
return MQTT_TRACE_ERROR(MQTT_CODE_ERROR_BAD_ARG);
713718
}
714719

720+
/* [MQTT-3.1.2-22]: If the User Name Flag is set to 0, the Password Flag
721+
* MUST be set to 0 */
722+
if (mc_connect->password != NULL && mc_connect->username == NULL) {
723+
return MQTT_TRACE_ERROR(MQTT_CODE_ERROR_BAD_ARG);
724+
}
725+
715726
/* Determine packet length */
716727
/* MQTT Version 4/5 header is 10 bytes */
717728
remain_len = sizeof(MqttConnectPacket);

wolfmqtt/mqtt_packet.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ enum MqttPacketFlags {
260260
/* Packet Header: Size is variable 2 - 5 bytes */
261261
#define MQTT_PACKET_MAX_LEN_BYTES 4
262262
#define MQTT_PACKET_LEN_ENCODE_MASK 0x80UL
263+
/* [MQTT-2.2.3] Maximum value encodable by the Variable Byte Integer scheme */
264+
#define MQTT_PACKET_MAX_REMAIN_LEN 0x0FFFFFFFUL /* 268,435,455 */
263265
typedef struct _MqttPacket {
264266
/* Type = bits 4-7, Flags = 0-3 are flags */
265267
byte type_flags; /* MqttPacketType and MqttPacketFlags */

0 commit comments

Comments
 (0)