Skip to content

fix(binding-mqtt): reject a malformed SUBSCRIBE - #3225

Merged
jfallows merged 3 commits into
test/3209-mqtt-qos2-interleaved-acksfrom
fix/3216-mqtt-reject-malformed-subscribe
Nov 14, 2026
Merged

jfallows merged 3 commits into
test/3209-mqtt-qos2-interleaved-acksfrom
fix/3216-mqtt-reject-malformed-subscribe

Conversation

@jfallows

Copy link
Copy Markdown
Contributor

Description

MqttServerFactory accepted several malformed SUBSCRIBE packets. It now answers them as the specifications call for: on MQTT 5, DISCONNECT with the reason code and the connection closes; on MQTT 3.1.1, the connection closes.

Changes in decodeSubscribeV4 / decodeSubscribeV5 and onDecodeSubscribeV5:

  • No payload, packet id 0 (both versions): rejected in the same condition that decodeUnsubscribeV4 / decodeUnsubscribeV5 use for UNSUBSCRIBE (payload().sizeof() == 0 || packetId() == 0, Protocol Error 0x82). This is the implementation mirrored.
  • MQTT 5 reserved option bits 6 and 7: Malformed Packet 0x81.
  • MQTT 5 Maximum QoS 3: Protocol Error 0x82. The option checks mirror the existing No Local check in the same loop.
  • MQTT 5 Subscription Identifier present more than once: Protocol Error 0x82, in the property loop beside the existing value-0 check.

Already handled before this change (no code changed, passing runs below): MQTT 5 Retain Handling 3, MQTT 5 Subscription Identifier 0, MQTT 3.1.1 QoS above 2 and reserved bits.

Fixes #3216

Specification statements

Read in the OASIS pages (MQTT 5.0 and 3.1.1 standard), quoted by sentence:

MQTT 5.0

  • 3.8.3.1: "It is a Protocol Error if the Maximum QoS field has the value 3."
  • 3.8.3.1: "It is a Protocol Error to send a Retain Handling value of 3."
  • 3.8.3.1 [MQTT-3.8.3-5]: "The Server MUST treat a SUBSCRIBE packet as malformed if any of Reserved bits in the Payload are non-zero" (bits 6 and 7).
  • 3.8.3 [MQTT-3.8.3-2]: "The Payload MUST contain at least one Topic Filter and Subscription Options pair" and "A SUBSCRIBE packet with no Payload is a Protocol Error."
  • 3.8.2.1.2: "It is a Protocol Error if the Subscription Identifier has a value of 0. It is a Protocol Error to include the Subscription Identifier more than once."
  • 2.2.1 [MQTT-2.2.1-3]: "Each time a Client sends a new SUBSCRIBE, UNSUBSCRIBE,or PUBLISH (where QoS > 0) MQTT Control Packet it MUST assign it a non-zero Packet Identifier that is currently unused".
  • 4.13.1 [MQTT-4.13.1-1]: "When a Server detects a Malformed Packet or Protocol Error, and a Reason Code is given in the specification, it MUST close the Network Connection"; "In the case of an error in any other packet it SHOULD send a DISCONNECT packet containing the Reason Code before closing the Network Connection. Use Reason Code 0x81 (Malformed Packet) or 0x82 (Protocol Error)".
  • 1.2: "Malformed Packet: A control packet that cannot be parsed according to this specification." "Protocol Error: An error that is detected after the packet has been parsed and found to contain data that is not allowed by the protocol or is inconsistent with the state of the Client or Server."

MQTT 3.1.1

  • 3.8.3 [MQTT-3.8.3-3]: "The payload of a SUBSCRIBE packet MUST contain at least one Topic Filter / QoS pair. A SUBSCRIBE packet with no payload is a protocol violation."
  • 3.8.3 [MQTT-3-8.3-4] (the id is misspelled in the standard): "The Server MUST treat a SUBSCRIBE packet as malformed and close the Network Connection if any of Reserved bits in the payload are non-zero, or QoS is not 0,1 or 2."
  • 2.3.1 [MQTT-2.3.1-1]: "SUBSCRIBE, UNSUBSCRIBE, and PUBLISH (in cases where QoS > 0) Control Packets MUST contain a non-zero 16-bit Packet Identifier."
  • 4.8 [MQTT-4.8.0-1]: "if either the Server or Client encounters a protocol violation, it MUST close the Network Connection on which it received that Control Packet which caused the protocol violation".

Packet id 0, decision: for 5.0, [MQTT-2.2.1-3] binds only the Client and the text does not say what the Server does on receiving 0. A SUBSCRIBE with id 0 parses, and carries data the protocol does not allow, which is the 1.2 definition of a Protocol Error. 4.13.1 then requires the Server to close the connection and says to send DISCONNECT with 0x82. On 3.1.1, [MQTT-2.3.1-1] binds the packet itself, so [MQTT-4.8.0-1] applies and the connection closes. This is also what the UNSUBSCRIBE decode already does.

Read: the clauses above, in full text. Not read: the remaining sections of both documents (for example 5.0 3.8.4 onwards and 3.1.1 3.8.4 onwards).

Script corrections (scripts that violated the protocol)

  • v5/subscribe.reject.malformed.subscription.options is renamed subscribe.reject.maximum.qos.3. It expected DISCONNECT 0x81, but 3.8.3.1 says Maximum QoS 3 is a Protocol Error, so it now expects 0x82. It also sent options 0x07 (QoS 3 plus No Local), which would be rejected for the No Local bit alone when noLocal is off; it now sends 0x03 so the case isolates QoS 3.
  • v5/subscribe.reject.missing.topic.filters carried the DISCONNECT bytes in the wrong order (properties length before reason code), and the server script labelled the packet SUBACK. Order and label are fixed; the 0x82 reason is unchanged. Neither script ran against the binding before.

Test evidence

Commit 1 holds only scripts and ITs (new .rpt pairs and methods in the existing SubscribeIT classes, no new IT classes). Run against the unchanged production code (mvn -B clean verify -pl runtime/binding-mqtt -Dit.test=SubscribeIT), 8 new runtime IT methods failed for the right reason: the script waits for the DISCONNECT, or for the connection close, which never arrives, and times out after 10 s with Specified behavior did not match expected / TestTimedOutException: test timed out after 10 seconds:

  • v5: shouldRejectMaximumQos3, shouldRejectReservedOptionBit6, shouldRejectReservedOptionBit7, shouldRejectSubscriptionIdRepeated, shouldRejectMissingTopicFilters, shouldRejectPacketIdZero
  • v4: shouldRejectMissingTopicFilters, shouldRejectPacketIdZero

Passing against the unchanged code, so already handled and not changed: v5 shouldRejectRetainHandling3 (the RETAIN_HANDLING_MASK case in calculateSubscribeFlags), v5 shouldReceiveOneMessageWithInvalidSubscriptionId (Subscription Identifier 0), v4 shouldRejectMaximumQos3 and v4 shouldRejectMalformedSubscriptionOptions (options > EXACTLY_ONCE).

After the fix (commit 2), cache skipped with -Dmaven.build.cache.skipCache=true, checkstyle and license checks on:

  • mvn -B -U clean verify -pl runtime/binding-mqtt: 44 unit tests; 430 ITs, 0 failures, 6 skipped. v5 SubscribeIT 72 and v4 SubscribeIT 45, all passing.
  • mvn -B -U clean verify -pl specs/binding-mqtt.spec: 78 unit tests (4 skipped); 526 ITs, 0 failures, 2 skipped, including the script-against-script runs of the new pairs.
  • Upstream modules were installed with -am first. The wrapper could not download its jar (HTTP 429), so the system mvn 3.9.11 was used.

Not run: the mqtt-kafka modules. No script they share was changed (only the mqtt modules reference the two changed script names); the other modules and the full reactor were not built. The new scripts were not run against a real MQTT client.

Not delivered

Follow-ups, not part of this change:

  • In the v5 option loop, after a rejected filter the loop breaks but the subscriptions collected so far in the same packet are still processed. This is how the existing No Local, wildcard and shared-subscription rejections already behave; the new checks follow it. A multi-filter packet with a malformed later filter should not subscribe the earlier ones.
  • Retain Handling 3 calls onDecodeError from calculateSubscribeFlags without setting decodeIgnoreAll or leaving the loop, so the subscription is still processed after the DISCONNECT is sent.
  • subscribe.reject.missing.packet.id (v4 and v5) omits the packet id bytes rather than sending id 0, so it does not test this case; it is not run against the binding.

Generated by Claude Code

…acks' into fix/3216-mqtt-reject-malformed-subscribe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mqtt: a malformed SUBSCRIBE (QoS 3, reserved option bits, no payload, packet id 0, invalid Subscription Identifier) is not rejected

2 participants