Repository navigation
fix(mqtt): end connections with the session or token they were opened with - #14804
Conversation
|
Review complete. 🟡 1 medium 💬 Inline comments (1)
This PR reworks how realtime and MQTT connections authenticate and stay authenticated: credential expiry is tracked per connection, revocations propagate to live connections, and MFA factor enforcement moves onto shared User document helpers.
Reviewed commit: 5024007 |
🟢 Tier S · Ready to merge
MQTT connection identities now retain their session and token expiry, and the broker rechecks those credentials during revocation and message delivery. Session, password, and MFA changes publish revocation events, and CONNECT rate limits are scoped per project. Unit and E2E tests cover credential-specific disconnects, session checks, expiry, and limiter keys.
📂 Walkthrough · 18
✅ Fixed since the last review · 1
Merged |
There was a problem hiding this comment.
This PR revamps realtime/MQTT authentication with centralized credential-expiry tracking, revocation propagation, and MFA factor enforcement helpers on the User document.
Key findings
- 🟡 Realtime impersonation skips impersonator MFA factor check — realtime.php:963
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
5024007 to
6cb2374
Compare
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
6cb2374 to
7cd38bd
Compare
… with An MQTT connection only remembered its project and user, so it outlived a sign out, a deleted session and an expired token, and the broker could only drop users who were blocked or removed. The identity now carries the session and the credential's expiry; the authorizer checks them, the session must hold its required MFA factors as over HTTP, delivery closes connections whose credential expired, and revoking checks each connection against its own session. Session deletion and the password changes that end sessions now ask the broker to revoke. The CONNECT limiter is also keyed on the project, since user ids are only unique within one. Co-Authored-By: Claude <[email protected]>
Enabling MFA or verifying an authenticator can leave other sessions short of factors, so those paths now ask the broker to revoke. A session's recorded expiry is no longer a hard limit: once it passes, delivery checks the session again, since it may have been extended, while a token's own expiry still ends the connection. Co-Authored-By: Claude <[email protected]>
7cd38bd to
9b08918
Compare
A session can end or fall short of its MFA factors without a revoke reaching the broker, for example when verifying an email or phone raises the factors MFA requires. Delivery now checks a session-bound connection again at least once a minute and when its session is due to expire, so it ends with the session as HTTP access would. Co-Authored-By: Claude <[email protected]>
|
@hansi-codes review |
A check on the session's expiry second still read the session as active and pushed the next check a minute out. The check now waits until that second has passed. Co-Authored-By: Claude <[email protected]>
|
@hansi-codes review |
OAuth and ID-token sign-in replace the caller's current session, and the session limit evicts the oldest ones, without telling the broker. These paths now ask it to revoke, so MQTT connections on those sessions end straight away instead of at their next session check. Co-Authored-By: Claude <[email protected]>
|
@hansi-codes review |
# Conflicts: # app/init/constants.php
Co-Authored-By: Claude <[email protected]>
|
@hansi-codes review |
…dential The per-credential check keyed on the whole identity, including when each connection is next due for a session check, so connections opened at different times on the same session were each checked against the database. Co-Authored-By: Claude <[email protected]>
|
@hansi-codes review |
After a periodic session check passed, the next one was set a full interval later even when the session expired sooner. It is now never set past the expiry the session had at connect. Co-Authored-By: Claude <[email protected]>
|
@hansi-codes review |
The JWT library rejects a token once the clock reaches its exp, while the broker kept accepting it through that second. Co-Authored-By: Claude <[email protected]>
|
@hansi-codes review |
fix(mqtt): end connections with the session or token they were opened with (cherry picked from commit 5ce150d)
Summary
MQTT connections now end with the credential they were opened with. The connection identity carries the session and the token's expiry, the session must hold its required MFA factors as over HTTP, and ending a session or changing MFA asks the broker to revoke. The CONNECT limiter is also keyed per project.
Builds on #14803 (it uses
User::sessionFactorsMet()from there); please merge that first. Until then this diff includes its commits.Changes
app/init/mqtt/connection.php: the authenticator returnssessionId,expire(the JWTexp) andsessionExpire(the session's expiry at connect) with the identity, and refuses a session, or a JWT bound to one, that lacks its required factors. The authorizer refuses an expired token, and a session that has ended or lacks its factors.Mqtt\Handler::revoke()checks each of the user's connections against its own identity (one check per distinct credential), so ending one session closes only the connections opened with it. The disconnect reason is now the generic unauthorized message, since a revoke is no longer only a block.Mqtt\Handler::deliver()closes a connection whose token has expired. Once a session passes its recorded expiry, delivery checks it again (it may have been extended) and closes the connection only if it no longer authorizes.mqtt:connect:{projectId}:{userId}, as user ids are only unique within a project.Mqtt::revoke()is now called when sessions are deleted (DELETE /account/sessions,DELETE /account/sessions/:sessionId,DELETE /users/:userId/sessions,DELETE /users/:userId/sessions/:sessionId), after the password changes that end sessions (PATCH /account/password,PUT /account/recovery,PUT /account/recovery/otp,PATCH /users/:userId/password), and when MFA requirements change (PATCH /account/mfa,PATCH /users/:userId/mfa,PUT /account/mfa/authenticators/:type), following the existing pattern inUsers/Status/Update.php.Tests
tests/unit/Mqtt):RevokeTest::testRevokeDisconnectsOnlyEndedSession,RevokeTest::testRevokeChecksEachCredentialOnce,RevokeTest::testConnectLimitIsKeptPerProject(with a recording Redis fake),HandlerTest::testExpiredCredentialIsDisconnectedOnDelivery,HandlerTest::testExtendedSessionKeepsReceiving,HandlerTest::testEndedSessionIsDisconnectedOnDelivery.mqttgroup):testSessionMissingFactorConnectRejected,testDeletedSessionOpenConnectionIsDisconnected(the other session's connection stays open),testEnablingMfaDisconnectsSessionMissingFactor,testAccountLogoutOpenConnectionIsDisconnected,testSessionJwtOpenConnectionIsDisconnectedWithSessions.Related: appwrite-labs/cloud#6666
Issue (private, full explanation): https://github.com/appwrite-labs/cloud/issues/6908