Skip to content

Reserved topics for users - #13860

Merged
ArnabChatterjee20k merged 8 commits into
mainfrom
update-appwrite-push-ledger-name
Sep 24, 2026
Merged

ArnabChatterjee20k merged 8 commits into
mainfrom
update-appwrite-push-ledger-name

Conversation

@ArnabChatterjee20k

@ArnabChatterjee20k ArnabChatterjee20k commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

Preset topic without creation

await messaging.createPush({messageId: ID.unique(), targetIds:[<USER_ID_SPECIFIC>], title: "", body: ""})

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

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.
@greptile-apps

greptile-apps Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge; the latest routing changes preserve topic campaigns while correctly directing user- and target-addressed Appwrite pushes to owned per-user channels.

Fix All in Claude CodeFindings

  1. P1 Security User topic IDs collide ▶
  2. P2 Tests mirror implementation details ▶
Fix with agent prompt
### Issue 1
src/Appwrite/Messaging/Adapter/Push/Appwrite.php:123-141
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.

### Issue 2
tests/unit/Appwrite/Messaging/Adapter/Push/AppwriteTest.php:312-322
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.

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!

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

This PR introduces reserved users/<userId> MQTT topics for user-addressed Appwrite push delivery while preserving ordinary topic-campaign routing.

  • Auto-provisions deterministic per-user topic and ledger records, including collision validation.
  • Restricts subscriptions in the reserved namespace to the owning user and rejects wildcards.
  • Routes user- and target-addressed Appwrite pushes to per-user channels while retaining topic IDs for topic campaigns.
  • Adds observable unit and end-to-end coverage for ownership, delivery, repeated sends, and wildcard behavior.

Reviews (4) · Last reviewed commit: "test(mqtt): move the wildcard test out o..."

Comment on lines +121 to +134
$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];

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.

P1 security 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.

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.

Fix in Claude Code Fix in Codex

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@greptile its already fixed

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.

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.

Comment on lines +312 to +322
$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']);

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.

P2 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!

Fix in Claude Code Fix in Codex

…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.
@github-actions

github-actions Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → update-appwrite-push-ledger-name (after).

Metric Before After Change
🚀 Requests/sec 198.76 198.22 ⚪ -0.3%
⏱️ Latency P50 88.27 ms 88.34 ms ⚪ +0.1%
⏱️ Latency P95 201.33 ms 200.28 ms ⚪ -0.5%
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 88.34 200.28 12,540 198.22 -1.05
Account 161.05 304.91 660 11.04 -10.42
TablesDB 84.7 159.31 6,820 109.64 +2.69
Storage 81.66 178.44 3,300 55.04 +9.65
Functions 125.53 234.75 1,760 29.95 -13.67

Top API waits (after)

API request Max wait (ms)
tablesdb.rows.delete 460.78
account.name.update 434.76
account.prefs.update 381.79
functions.create 353.59
functions.variables.delete 351.77

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).
@ArnabChatterjee20k ArnabChatterjee20k changed the title Update appwrite push ledger name Reserved topics for users Sep 24, 2026
@ArnabChatterjee20k
ArnabChatterjee20k merged commit 7deec8d into main Sep 24, 2026
95 of 96 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the update-appwrite-push-ledger-name branch September 24, 2026 08:19
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