Skip to content

refactor(dpi/mqtt): include UNSUBSCRIBE in flag-validation match#294

Merged
domcyrus merged 1 commit into
domcyrus:mainfrom
obchain:refactor/mqtt-validate-unsubscribe-flags
May 20, 2026
Merged

refactor(dpi/mqtt): include UNSUBSCRIBE in flag-validation match#294
domcyrus merged 1 commit into
domcyrus:mainfrom
obchain:refactor/mqtt-validate-unsubscribe-flags

Conversation

@obchain
Copy link
Copy Markdown
Contributor

@obchain obchain commented May 20, 2026

Summary

Adds 10 (UNSUBSCRIBE) to the outer match arm in is_mqtt_packet (src/network/dpi/mqtt.rs:19) so the inner 8 | 10 => 0x02 flag-validation rule actually runs for that packet type.

Closes #293.

Why

The outer arm previously listed 1 | 2 | 4 | 6 | 7 | 8 | 9 | 11 | 12 | 13 | 14 — note 10 missing. UNSUBSCRIBE fell into the _ => {} catch-all (intended for PUBLISH/PUBREC's flexible nibble), which silently accepted any flag value despite the inner table claiming UNSUBSCRIBE requires 0x02. The 10 => 0x02 arm was unreachable.

This is invariant-preserving: is_mqtt_packet still returns true only for CONNECT (the strong-signature gate at line 52 is unchanged), so no caller sees a behavior change today. It restores the MQTT §2.1.2 invariant the comment promises and removes a misleading unreachable arm, in the same spirit as the recent refactor(dpi/...) cleanups (#279, #289, #290).

I also reformatted the inline comment for readability — same content, one source of truth per packet type.

Test plan

  • cargo clippy --all-targets --all-features -- -D warnings — clean
  • cargo fmt --check — clean
  • cargo test --lib mqtt — 21 mqtt tests pass, no regressions
  • cargo test --lib — full 361-test suite passes
  • Manually traced: SUBSCRIBE(8) with flag 0x00 still rejected; SUBSCRIBE with 0x02 still accepted; UNSUBSCRIBE(10) still returns false at line 52 regardless of flags

The flag-validation block in `is_mqtt_packet` listed UNSUBSCRIBE(10)
in the inner expected-flag table (`8 | 10 => 0x02`) but not in the
outer match arm, so packet type 10 never reached the inner table and
its required `0x02` flag bit was never checked. The fall-through
arm's comment ("PUBLISH(3), PUBREC(5) — flags carry DUP/QoS/RETAIN")
was inadvertently also accepting UNSUBSCRIBE despite that not being
the documented intent.

Add 10 to the outer match so the validation actually applies, and
reformat the inline comment so each entry has a single source of
truth. `is_mqtt_packet` still only returns true for CONNECT (the
only packet type with a port-independent signature), so this change
is invariant-preserving — it removes an unreachable branch and
restores the stated MQTT §2.1.2 invariant for callers that may later
relax that gate.
@domcyrus
Copy link
Copy Markdown
Owner

@obchain thanks a lot also for this PR. LGTM!

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.

refactor(dpi/mqtt): UNSUBSCRIBE flag check is unreachable in is_mqtt_packet

2 participants