Repository navigation
Reserved topics for users - #13860
Conversation
A connection may subscribe to users/<its own id> (served like any exact topic: QoS and offline replay come from its auto-provisioned row). Any wildcard under users/ and any other user's users/<id> are refused, so the namespace can never span users. Deeper users/<id>/... paths remain ordinary topics.
The push worker maps each appwrite-provider recipient to users/<userId> (a user's targets collapse to one topic; other providers are untouched). The push adapter auto-provisions that topic on first publish, keyed by md5 of the name so the row is a singleton via its primary key (no name index, no migration), then reuses the existing sequence/ledger/replay path.
Unit: a users/<id> target auto-provisions its row and fans out on the name, and a second publish reuses the row. E2E: subscribe ownership (own granted; other user, users/#, users/+ refused) and a user-targeted campaign reaching the owner.
|
| $topic = $authorization->skip(fn () => $this->dbForProject->getDocument('topics', $id)); | ||
| if ($topic->isEmpty()) { | ||
| try { | ||
| $authorization->skip(fn () => $this->dbForProject->createDocument('topics', new Document([ | ||
| '$id' => $id, | ||
| 'name' => $name, | ||
| 'sequence' => 0, | ||
| ]))); | ||
| } catch (Duplicate) { | ||
| // A concurrent first publish created it; the row now exists for persist(). | ||
| } | ||
| } | ||
|
|
||
| return [$id, $name]; |
There was a problem hiding this comment.
A caller can create a normal topic whose custom ID equals md5("users/<victimId>"). This code accepts that unrelated topic without checking its name, then advances its sequence and stores the victim's push payload under the same ledger identity. A subscriber to the colliding normal topic could therefore receive the victim's notification during replay. Validate the existing row's reserved name or use an ID namespace that normal topics cannot occupy.
How this was verified: Custom 32-character hexadecimal topic IDs are accepted, and user pushes persist their payload under that deterministic topic ID without checking the existing document's name.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Appwrite/Messaging/Adapter/Push/Appwrite.php
Line: 121-134
Comment:
**User topic IDs collide**
A caller can create a normal topic whose custom ID equals `md5("users/<victimId>")`. This code accepts that unrelated topic without checking its name, then advances its sequence and stores the victim's push payload under the same ledger identity. A subscriber to the colliding normal topic could therefore receive the victim's notification during replay. Validate the existing row's reserved name or use an ID namespace that normal topics cannot occupy.
**How this was verified:** Custom 32-character hexadecimal topic IDs are accepted, and user pushes persist their payload under that deterministic topic ID without checking the existing document's name.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.There was a problem hiding this comment.
Thanks for the update. Confirmed — createUserTopic() now re-reads after a concurrent Duplicate and verifies that the existing row’s name matches the reserved users/<id> target before persisting, so a colliding normal topic cannot receive the user push. This resolves the reported issue.
| $topic = $this->topicByName('users/user-1'); | ||
| $this->assertFalse($topic->isEmpty(), 'the user topic was not auto-provisioned'); | ||
| $this->assertSame(1, $topic->getAttribute('sequence')); | ||
| $this->assertNull($topic->getAttribute('qos'), 'user topic qos stays subscriber-chosen'); | ||
|
|
||
| // Ledger is keyed by the topic id; the fan-out carries the users/<id> name. | ||
| $ledger = $this->ledger(); | ||
| $this->assertCount(1, $ledger); | ||
| $this->assertSame($topic->getId(), $ledger[0]->getAttribute('topic')); | ||
| $this->assertCount(1, $broker->published); | ||
| $this->assertSame(['users/user-1'], $broker->published[0]['channels']); |
There was a problem hiding this comment.
Tests mirror implementation details
These tests assert internal topic-row creation, nullable qos, ledger foreign keys, exact sequence counters, and the fake broker's channel structure. The same pattern also appears at lines 325–335. This violates the repository directive to test observable behavior rather than mirror source code or configuration. Replace these assertions with behavioral coverage showing that user-addressed pushes are delivered, repeated sends remain deliverable, and unauthorized users cannot observe them. This repository requirement must be satisfied before merging.
Context Used: Call out and harshly judge implementation-coupled tests. We don't mirror source code, configuration, or version pins in assertions. We test observable behavior; use linters for syntax and schema checks. (source)
Prompt To Fix With AI
This is a comment left during a code review.
Path: tests/unit/Appwrite/Messaging/Adapter/Push/AppwriteTest.php
Line: 312-322
Comment:
**Tests mirror implementation details**
These tests assert internal topic-row creation, nullable `qos`, ledger foreign keys, exact sequence counters, and the fake broker's channel structure. The same pattern also appears at lines 325–335. This violates the repository directive to test observable behavior rather than mirror source code or configuration. Replace these assertions with behavioral coverage showing that user-addressed pushes are delivered, repeated sends remain deliverable, and unauthorized users cannot observe them. This repository requirement must be satisfied before merging.
**Context Used:** Call out and harshly judge implementation-coupled tests. We don't mirror source code, configuration, or version pins in assertions. We test observable behavior; use linters for syntax and schema checks. ([source](https://app.greptile.com/review/custom-context?memory=instruction-0))
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
…ledger-name # Conflicts: # src/Appwrite/Platform/Workers/Messaging.php
The per-user topic id is md5 of the reserved name, which shares the id space with developer-created topics. Verify the resolved row's name is the reserved one before publishing, so a topic squatting the hash can never carry another user's pushes.
Cover user-addressed pushes by their delivery and channel rather than internal sequence/qos/ledger rows, and assert a refusal carries a reason without pinning its exact wording.
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
The Appwrite push provider now maps to users/<userId> only for user- and target-addressed campaigns; topic campaigns keep delivering on the topic's own channel. Recipients are tagged with their source so topic fan-out is no longer misrouted to per-user topics.
users/ wildcards are refused by design, so the wildcard-delivery test uses a regular hierarchical topic (scores/+/live).
Preset topic without creation
So subscribers would be auto subscribed to users/<CLIENT_ID>
And blocking creation of topic named users/<CLIENT_ID> and no
users/#,users/+connection to stop sniffing