Repository navigation
fix(mqtt): limit SUBSCRIBE rate, filters, keep-alive and connections per user, by plan - #15056
Conversation
Lets other Server tests reuse it, and lets a test hand it a timer. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Each filter costs the handler authorization work, and a SUBSCRIBE was bounded only by the packet size. The broker now disconnects a client whose SUBSCRIBE carries more than APP_LIMIT_MQTT_FILTERS (100) filters, with Quota exceeded (0x97) on MQTT 5.0, before the handler runs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A CONNECT with keep-alive 0 was never scheduled for reaping, so an idle authenticated socket held a broker slot forever. The broker now enforces at most APP_LIMIT_MQTT_KEEP_ALIVE (1200s) and uses it for a disabled keep-alive. MQTT 5.0 clients are told through Server Keep Alive on the CONNACK; 3.1.1 lets a server disconnect an inactive client whatever its keep-alive. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Each SUBSCRIBE runs several project database reads, and one WebSocket frame can pack thousands of them, all dispatched with no limit. A connection may now send 32 SUBSCRIBE packets a minute, as a realtime connection may send 32 messages, behind _APP_OPTIONS_ABUSE. Past that the broker disconnects it with Message rate too high (0x96) before any authorization work, and the Server no longer answers or records a SUBSCRIBE whose handler closed the connection. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Only CONNECT attempts were rate-limited (128 a minute), so one user could keep opening sockets that count toward the broker's shared connection limit. As realtime does with APP_LIMIT_REALTIME_CONNECTIONS, a user may now hold at most APP_LIMIT_MQTT_CONNECTIONS (32) open connections per project on one worker; another CONNECT is refused with Quota exceeded until one closes. MQTT has no guest connections, so there is no per-IP owner to aggregate. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Review complete. 🟡 2 medium 💬 Inline comments (2)
This PR layers anti-abuse controls onto the MQTT realtime path and extends the
Note: one test-shard scan (test-only files) ended without publishing a report; its interim conclusion reported no defects in those files. Reviewed commit: d99c287 |
🟢 Tier S · Ready to mergeThis change adds plan- and environment-backed limits for MQTT connections and SUBSCRIBE work, a per-user SUBSCRIBE rate limit, and a broker keep-alive ceiling. It also updates the MQTT server and tests for quota disconnects, keep-alive negotiation, and handling SUBSCRIBE packets after a handler closes a connection.
📂 Walkthrough · 17
🔇 Filtered out · 2Findings Hansi considered but did not post.
Reviewed |
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
A handler's own Server Keep Alive was kept next to the broker's, and the property may appear only once. The broker's enforced value now replaces it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@hansi-codes review |
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
There was a problem hiding this comment.
Adds MQTT broker abuse limits (per-user connection, per-fd subscribe rate, keep-alive cap) and unit-test fakes for the realtime MQTT server.
Key findings
- 🟡 Subscribe limiter keyed on fd resets on reconnect — Handler.php:367
- 🟡 Silent keep-alive cap drops compliant MQTT 3.1.1 clients — Server.php:322
As realtime takes its per-connection limits from the plan, the broker now reads mqttConnections from the plan its getPlan resource resolves for a project, falling back to APP_LIMIT_MQTT_CONNECTIONS. That default reads _APP_LIMIT_MQTT_CONNECTIONS (32), and the CE getPlan returns no plan, so self-hosted behaviour is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The filter cap now lives in the handler, which reads mqttFilters from the project's plan and falls back to APP_LIMIT_MQTT_FILTERS, read from _APP_LIMIT_MQTT_FILTERS (100), as realtime does for its channel limit. A per-plan value cannot be a broker-wide Server option, so maxFilters leaves the package. An oversized SUBSCRIBE is still refused with Quota exceeded before any authorization work. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@hansi-codes review |
The limiter was keyed on the socket, so reconnecting or opening more connections reset or multiplied the 32 a minute. It is now keyed on the authenticated project and user, and a SUBSCRIBE on a connection that never completed CONNECT is refused before the limiter or the authorizer. Co-Authored-By: Claude Opus 5.5 <[email protected]>
3.1.1 has no way to tell a client a shorter interval, so lowering a long keep-alive dropped compliant clients that pinged as they declared. A 3.1.1 keep-alive now stands; only a disabled one is replaced by the ceiling, which 3.1.2.10 allows. 5.0 clients are still lowered and told through Server Keep Alive. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@hansi-codes review |
What does this PR do?
Adds resource limits to the MQTT broker after CONNECT, mirroring realtime. The two per-plan limits resolve the same way realtime's per-connection limits do: the plan's value if it has one, otherwise an env-driven
APP_LIMIT_*default.Appwrite\Mqtt\HandlerAPP_LIMIT_MQTT_CONNECTIONS=_APP_LIMIT_MQTT_CONNECTIONS(32) per project user per workermqttConnectionsAppwrite\Mqtt\HandlerAPP_LIMIT_MQTT_FILTERS=_APP_LIMIT_MQTT_FILTERS(100)mqttFiltersAppwrite\Mqtt\Handler_APP_OPTIONS_ABUSEpackages/mqttServer(maxKeepAlive)APP_LIMIT_MQTT_KEEP_ALIVE= 1200sPlan hook.
app/mqtt.phpregisters agetPlancontainer resource,fn (string $projectId): array, before$registerConnectionResources($container)runs. In CE it returns[], so every limit is its default. A wrapper can register its owngetPlaninside$registerConnectionResources(the same way it overridesgetCache/getConsoleDB) and return a plan array withmqttConnections/mqttFilters. No CE changes are needed for that.Also,
Serverno longer answers or records a SUBSCRIBE whose handler closed the connection, and the CONNACK carries a single Server Keep Alive.MQTT connections always authenticate as a user (there are no guests), so the per-user cap doesn't need realtime's per-IP and IPv6 /64 grouping. The CONNECT auth flow is unchanged.
Test Plan
tests/unit/Mqtt/LimitsTest.php:mqttFiltersoverriding it.mqttConnectionsoverriding it.ServerTest: keep-alive ceiling (disabled, above, within, 3.1.1 disabled, 3.1.1 above the ceiling kept, no ceiling), a single Server Keep Alive on the CONNACK, and no SUBACK after the handler closed the connection._APP_LIMIT_MQTT_CONNECTIONS=2, the 3rd connection got 0x97.composer lint, PHPStan on the touched app files, Rector ontests/unit/Mqtt, andbin/monorepo check mqttall pass.Related PRs and Issues
Tracking: appwrite-labs/cloud#7059
🤖 Generated with Claude Code