Skip to content

mqtt: a rejected filter in a multi-filter SUBSCRIBE must not leave the earlier filters of the packet subscribed #3237

Open

Description

@jfallows

Problem

By reading MqttServerFactory (the MQTT 5 SUBSCRIBE option loop in decodeSubscribeV5, found while fixing #3216; not run): when a filter in a SUBSCRIBE is rejected (No Local on a shared subscription, wildcard rejections, Maximum QoS 3, reserved option bits, a repeated Subscription Identifier), the loop breaks, but the subscriptions collected from the earlier filters of the same packet are still processed after the DISCONNECT has been sent. The connection is closed, yet subscription state for filters of a packet that is malformed as a whole may have been created. MQTT 3.1.1 (decodeSubscribeV4) has to be checked for the same behaviour.

Specification statements (to be re-read from the OASIS text before writing scripts)

MQTT 5.0:

  • 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".
  • 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".
  • 1.2: "Malformed Packet: A control packet that cannot be parsed according to this specification."

MQTT 3.1.1:

  • 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."

The clause numbers differ between the body and the summary tables of the OASIS text, so quote the sentence, not only the id. Settle in this issue whether a packet treated as malformed may establish any subscription at all, from the text of 3.8.4 and 4.13.

Expected

Test plan (test-first)

For v5 and v4, a script pair in specs/binding-mqtt.spec with a valid first filter followed by an invalid second filter, then a PUBLISH matching the first filter that must not be delivered, and the runtime server IT methods in the existing SubscribeIT classes. The first commit holds the failing scripts. If the code already behaves correctly for a case, say so with the passing run instead of changing code.

Notes

Follow-up from #3216 (PR #3225). Part of the MQTT conformance work under #512.

Activity

  1. self-assigned this
    on Nov 14, 2026
  2. jfallows commented on Nov 14, 2026

    @jfallows
    ContributorAuthor

    Analysis: the defect is in the code, but it is not observable on the wire

    What the code does (current develop, MqttServerFactory.onDecodeSubscribeV5 and the v4 decode)

    Each rejection in the filter loop calls onDecodeError, sets decoder = decodeIgnoreAll and does break. That break leaves only the for loop, not the enclosing decode: block, so the filters collected before the rejected one are still processed. onDecodeError has already called cleanupStreamsUsingAbort, so the session stream is aborted first.

    What I measured

    I added a temporary System.err probe to doSessionData (reverted, no production change) and ran three scenarios (valid first filter, rejected second filter):

    • MQTT 5, second filter with reserved option bit 6 (DISCONNECT 0x81)
    • MQTT 5, second filter with Maximum QoS 3 (DISCONNECT 0x82)
    • MQTT 3.1.1, second filter with malformed options (connection closed)

    In all three the probe printed data after abort=true: the session state carrying the earlier filter is still written to the session stream after that stream's ABORT. doSessionData is guarded by !StreamState.closed(state), which needs both directions closed, and only the initial direction has been aborted.

    Other effects, read from the code only (not run)

    • subscribePacketIds.put(filter, packetId) runs inside the loop, before the packet is known to be valid.
    • session.unAckedSubscriptions.addAll(newSubscriptions) runs after the loop for the earlier filters.
    • If one of the earlier filters has no route, subscribeFailed is true and doSubscribeFailure still runs after the DISCONNECT.

    Why no k3po script can observe it

    The session stream has already delivered aborted to the application script, which has finished, so the DATA frame that follows the ABORT cannot be read by a script. The subscribe streams are only opened when the application echoes the state back, and that stream is gone, so no subscription is established downstream in these scenarios.

    I therefore wrote the scripts as the issue describes (valid first filter, invalid second filter, connection closed, no SUBACK, no session state) and ran them against the unchanged code. They pass:

    • runtime server SubscribeIT: shouldRejectSecondFilterReservedOptionBit6, shouldRejectSecondFilterMaximumQos3 (v5), shouldRejectSecondFilterMalformedSubscriptionOptions (v4)
    • the matching spec-level network and application ITs

    Per the maintainer's rules I have not changed production code. A failing test for the leaked state would need an observation that is not on the wire.

    Spec text

    The OASIS text does not settle whether a rejected packet may leave earlier filters subscribed. What I read (extracts of the v5.0 OS and v3.1.1 OS HTML; neither document in full):

    • v5.0 3.8.4: "If a Server receives a SUBSCRIBE packet that contains multiple Topic Filters it MUST handle that packet as if it had received a sequence of multiple SUBSCRIBE packets, except that it combines their responses into a single SUBACK response [MQTT-3.8.4-5]."
    • v5.0 4.13.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 [MQTT-4.13.1-1]."
    • v5.0 4.13.2: "If the Control Packet contains multiple errors the receiver of the Packet can validate the Packet in any order and take the appropriate action for any of the errors found."
    • v5.0 1.2: "Malformed Packet: A control packet that cannot be parsed according to this specification."
    • v5.0 3.8.3: "The Server MUST treat a SUBSCRIBE packet as malformed if any of Reserved bits in the Payload are non-zero [MQTT-3.8.3-5]."
    • v3.1.1 3.8.3: "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 [MQTT-3-8.3-4]."
    • v3.1.1 3.8.4: "...MUST handle that packet as if it had received a sequence of multiple SUBSCRIBE packets, except that it combines their responses into a single SUBACK response [MQTT-3.8.4-4]."

    The maintainer chose to treat a rejected packet as void as a whole (no session state, no subscribePacketIds entries, no SUBACK). That choice is not mandated by the text.

    The scripts and ITs are being added as coverage only, so this issue stays open.


    Generated by Claude Code

  3. jfallows commented on Nov 14, 2026

    @jfallows
    ContributorAuthor

    The Retain Handling 3 check in calculateSubscribeFlags (called from the v5 SUBSCRIBE loop) is one more instance of the post-error fall-through recorded here: onDecodeError is called outside the loop's decoder = decodeIgnoreAll; break exit, so the loop keeps building state after the DISCONNECT. As with the multi-filter case, it cannot be observed on the wire, because the session stream has already been aborted. The analysis, with code references and spec text, is in #3238 (comment). #3238 is closed as not planned.


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions