Repository navigation
fix(realtime): validate presence message ID and metadata like the HTTP endpoint - #14829
Conversation
The realtime presence message accepted any 36-character id and skipped metadata validation, unlike the HTTP presence endpoint. Validate the id like a custom document id and run the metadata validator. Subscribe messages had no upper bound on channels or queries per subscription, nor on the subscriptions one connection holds. Cap channels and queries at the array parameter limit, channel names at the array element limit, and subscriptions per connection at a limit large enough for an SDK to resend every subscription in one frame on reconnect. Co-Authored-By: Claude <[email protected]>
…ive topic A second CONNECT on an accepted connection switched its identity while the broker kept the subscriptions it had granted to the first one. MQTT treats a second CONNECT as a protocol violation, so refuse it, which closes the connection. Push ledger rows are keyed by the public topic id and outlived topic deletion, so a topic recreated with the same id could replay the deleted topic's messages. Replay only rows written since the current topic was created, and delete the deleted topic's rows with it. Co-Authored-By: Claude <[email protected]>
|
Review complete. No issues found — approved ✅. This PR hardens the MQTT and realtime presence stack: it refuses a second CONNECT on an already-active connection, caps per-connection realtime subscriptions via a new
Reviewed commit: 1fb4b7c |
🔵 Tier A · Mergeable after minor fixes
The realtime presence handler now validates presence IDs with
Note Not approving while a bug finding is open: Encoded JSON strings pass the metadata object check
Fix with agent prompt### Issue 1
src/Appwrite/Realtime/Message/Handlers/Presence.php:34
**Encoded JSON strings pass the metadata object check**
`JSONObject` also accepts strings containing encoded JSON objects, so a frame like `{"metadata":"{\"x\":1}"}` passes validation. This handler accepts the value as `mixed` and stores it unchanged, unlike the HTTP action's `array|stdClass` parameter, so metadata can be persisted and returned as a string rather than an object.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.📂 Walkthrough · 2
Reviewed |
A topic recreated after the cleanup read found no replacement could publish before the delete ran, and the unbounded query would remove those new rows. Only delete rows older than the read, or than the recreated topic. Co-Authored-By: Claude <[email protected]>
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
…CT to close Replay now only serves ledger rows written since the topic was created, so the fixtures create topics on the test clock before their ledger rows. A second CONNECT on an accepted connection is refused, which replaces the old identity-swap expectation. Adds a regression for rows left by a deleted topic with the same id. Co-Authored-By: Claude <[email protected]>
|
@hansi-codes review |
Scoping replay by comparing ledger and topic timestamps depends on clocks agreeing across workers, and a skewed clock could drop a live message from replay. Delete the ledger rows in the same request that deletes the topic instead: a ledger write locks the topic row first, so no row can land for the topic after it is gone, and a topic recreated with the same ID starts with an empty ledger. Co-Authored-By: Claude <[email protected]>
…iene # Conflicts: # tests/unit/Mqtt/RevokeTest.php
|
@hansi-codes review |
# Conflicts: # tests/e2e/Services/Mqtt/MqttServerTest.php
…rker on main Main now keys pushLedger rows by topicInternalId and the deletes worker removes a deleted topic's rows by that id, so a recreated topic cannot replay the old one's messages. The request-time purge by public topic id duplicated that and could race a recreated topic's first publish, so it is dropped.
…und reused IDs The per-connection cap was counted before the console-tail lookups, which can yield, so two concurrent frames could both pass on the old state. Both caps are now checked right before the in-memory subscribe, with nothing that yields in between. A reused subscription ID adds channels to the ones it already has, so the 100-channel cap now applies to its accumulated set, not only to each frame.
|
@hansi-codes review |
… a new unit file testSubscribeLimits already checked the channel, query and per-connection caps through the realtime API. It now also covers an oversized subscribe frame and an over-long channel name, so the separate SubscribePayloadTest file is dropped.
|
@hansi-codes review |
Subscribe frame and per-connection limits are being handled in a separate change, and the second-CONNECT fix and its coverage already live on main, so this PR now only aligns the realtime presence message with PUT /v1/presences/:presenceId.
|
@hansi-codes review |
| ->param('presenceId', 'unique()', new Text(36), 'Presence document ID', true) | ||
| ->param('metadata', null, new JSONObject(), 'Optional metadata payload', true, [], true) | ||
| ->param('presenceId', CustomId::UNIQUE, new CustomId(), 'Presence document ID', true) | ||
| ->param('metadata', null, new Nullable(new JSONObject()), 'Optional metadata payload', true) |
There was a problem hiding this comment.
Encoded JSON strings pass the metadata object check
JSONObject also accepts strings containing encoded JSON objects, so a frame like {"metadata":"{\"x\":1}"} passes validation. This handler accepts the value as mixed and stores it unchanged, unlike the HTTP action's array|stdClass parameter, so metadata can be persisted and returned as a string rather than an object.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Appwrite/Realtime/Message/Handlers/Presence.php
Line: 34
Comment:
**Encoded JSON strings pass the metadata object check**
`JSONObject` also accepts strings containing encoded JSON objects, so a frame like `{"metadata":"{\"x\":1}"}` passes validation. This handler accepts the value as `mixed` and stores it unchanged, unlike the HTTP action's `array|stdClass` parameter, so metadata can be persisted and returned as a string rather than an object.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.🟡 Minor · bug · Reply if this doesn't apply.
…iene fix(realtime): validate presence message ID and metadata like the HTTP endpoint (cherry picked from commit 15c1cd4)
What does this PR do?
The realtime
presencemessage validatedpresenceIdwithText(36)and skipped validation formetadataentirely, unlikePUT /v1/presences/:presenceId. A presence frame could therefore store an ID the HTTP endpoint would refuse, or list-shaped metadata where an object is expected.Realtime/Message/Handlers/Presence.phpnow validatespresenceIdwithCustomId(which still acceptsunique()) and validatesmetadatawithNullable(JSONObject)instead of skipping validation, sonullis still allowed.Subscribe frame and per-connection subscription limits are handled in a separate change. The second-CONNECT fix and its coverage already live on main.
Test Plan
PresenceRealtimeClientTest::testPresenceValidationErrorsReturnErrorOnlysends a presence frame with an invalid ID and one with list-shaped metadata, checks each gets an error naming the parameter, and checks no presence event is emitted.tests/unit/Realtime.Related PRs and Issues
Related: appwrite-labs/cloud#6674
Checklist