Skip to content

fix(mqtt): scope wildcard subscriptions and reserved user topics - #14030

Merged
levivannoort merged 2 commits into
mainfrom
fix/mqtt-topic-scope
Sep 30, 2026
Merged

levivannoort merged 2 commits into
mainfrom
fix/mqtt-topic-scope

Conversation

@levivannoort

@levivannoort levivannoort commented Sep 30, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Tightens topic scoping on the MQTT push broker (src/Appwrite/Mqtt/Handler.php):

  • Subscribe: a filter with a wildcard first level (#, +, +/…) gets SUBACK 0x80, the same as users/ wildcards already do. Wildcards below the first level, such as scores/+/live, are still granted.
  • Deliver: the reserved users/<id> topic goes only to connections whose identity is <id>, whatever filter matched. A deeper users/<id>/… path stays an ordinary topic, as deniesUserTopic() already treated it.

Behaviour change: a client can no longer subscribe to a first-level wildcard on the push broker. It gets 0x80 for that filter, and its other filters are unaffected.

Test Plan

tests/e2e/Services/Mqtt/MqttServerTest.php:

  • testUserTopicOwnershipOnSubscribe now also expects 0x80 for #, +, +/+, +/# and +/<otherId>.
  • The new testUserTargetedPushReachesOnlyItsUser sends a users-addressed push and checks that only that user receives it, whatever filters other connections in the project hold.

tests/unit/Mqtt/HandlerTest.php (new): once SUBSCRIBE refuses these filters, no E2E test can reach the ownership check in deliver(), because the broker only installs granted filters. This test drives Utopia\Mqtt\Server through an in-process adapter with a broker handler that grants every filter, which stands in for a subscription the gate didn't catch. It then asserts what Handler::deliver() sends to each connection:

  • users/<id> reaches only its owner, even though other users hold #, +/+ and +/<id>.
  • The owner still receives it through their own wildcard.
  • Ordinary topics, and a deeper users/<id>/… path, still fan out to wildcards.

With the ownership check removed from deliver(), testUserTopicReachesOnlyItsOwner fails.

I ran the MQTT E2E group against a local stack, with the broker carrying each version of Handler.php:

Broker …OwnershipOnSubscribe …ReachesOnlyItsUser
before this change fails fails
delivery change only fails passes
this PR passes passes

With this PR, MqttServerTest passed 25/25 in two whole-file runs. One earlier run stalled on the last test (testCleanStartDiscardsSession); that test passed on its own and in both later runs, and I couldn't reproduce the stall. Pint and PHPStan pass on the changed files.

Related PRs and Issues

  • None

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

Refuse a wildcard first level (# or +) at SUBSCRIBE, as users/ wildcards
already are, and deliver the reserved users/<id> topic only to that
user's connections. Wildcards below the first level are unaffected.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your trial has ended. Reactivate Greptile to resume code reviews.

@levivannoort levivannoort self-assigned this Sep 30, 2026
@ghost

ghost commented Sep 30, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The pull request rejects MQTT subscription filters with a wildcard first level and restricts delivery on reserved users/<id> topics to the owning identity. E2E coverage checks subscription rejection and user-targeted push isolation, while new in-process unit tests exercise the delivery guard independently with granted wildcard subscriptions.

Latest changes: The newest commits add named broker, transport, and pub/sub fakes plus unit regressions for owner-only delivery, owner wildcard delivery, and ordinary-topic fan-out.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 6
File Change
src/Appwrite/Mqtt/Handler.php Reject first-level wildcard filters and restrict reserved user-topic delivery to the owning identity.
tests/e2e/Services/Mqtt/MqttServerTest.php Cover additional rejected filters and user-targeted push isolation.
tests/unit/Mqtt/Fakes/GrantingHandler.php Grant subscriptions through the broker lifecycle to test delivery independently of authorization.
tests/unit/Mqtt/Fakes/NullPubSub.php Provide an inert pub/sub adapter for local handler tests.
tests/unit/Mqtt/Fakes/RecordingAdapter.php Feed packets into an in-process broker and capture outbound publishes without coroutine work.
tests/unit/Mqtt/HandlerTest.php Test reserved-topic ownership with installed wildcard subscriptions and preserve ordinary-topic fan-out.

Reviewed the commits since a56ac2f · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · Looks good to merge. Summary

@github-actions

github-actions Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/mqtt-topic-scope (after).

Metric Before After Change
🚀 Requests/sec 193.41 202.92 ⚪ +4.9%
⏱️ Latency P50 89.58 ms 85.66 ms ⚪ -4.4%
⏱️ Latency P95 209.36 ms 197.54 ms 🟢 -5.6%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 85.66 197.54 12,540 202.92 -11.82
Account 156.44 291.34 660 11.28 -48.2
TablesDB 83.12 155.92 6,820 112.39 -9.18
Storage 79 175.84 3,300 56.31 -2.41
Functions 121.64 232.8 1,760 30.81 -22.91

Top API waits (after)

API request Max wait (ms)
account.name.update 419.49
functions.variables.update 416.68
account.prefs.update 403.2
storage.buckets.create 401.9
functions.variables.create 362.33

…talled

The e2e outcome test can no longer reach deliver()'s ownership check once
SUBSCRIBE refuses first-level wildcards, since the broker only installs
granted filters. Drive Utopia\Mqtt\Server through an in-process adapter
with a handler that grants every filter, then assert what
Handler::deliver() sends to each connection.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your trial has ended. Reactivate Greptile to resume code reviews.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@levivannoort
levivannoort merged commit f577d20 into main Sep 30, 2026
55 checks passed
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.

2 participants