Skip to content

fix(messaging): enforce target ownership in createSubscriber - #14019

Merged
ChiragAgg5k merged 2 commits into
mainfrom
fix/messaging-subscriber-target-ownership
Sep 30, 2026
Merged

ChiragAgg5k merged 2 commits into
mainfrom
fix/messaging-subscriber-target-ownership

Conversation

@ChiragAgg5k

Copy link
Copy Markdown
Member

What does this PR do?

Fixes an authorization flaw in createSubscriber (POST /v1/messaging/topics/:topicId/subscribers) reported privately by Alpesh Bhagwatkar.

An authenticated session or JWT caller could pass any user's targetId. The handler loaded the target with authorization skipped and never checked ownership, so a normal project user could bind another user's email, SMS or push target to a topic they can subscribe to. The 201 response also returned the victim's target.identifier (email address, phone number or push token) and userName, disclosing that data to the attacker.

The only preconditions are the default subscribers.write scope, a topic whose subscribe allows users/any, and knowledge of the victim's targetId (custom push target IDs can be predictable).

Fix

The current user is now injected into the action. Unless the caller is an API key or admin console request, the target's userId must match the caller — the same ownership rule account.updatePushTarget already enforces. Non-owners get 404 USER_TARGET_NOT_FOUND, which both avoids a target-existence oracle and closes the identifier disclosure since a non-owner never reaches the response. The subscriber document keeps deriving its userId, permissions and userName from the target owner, so API key / admin subscriptions on behalf of a user are unchanged.

Note: the reporter suggested returning 401. I used 404 to match the existing account.updatePushTarget convention and to avoid confirming that a target exists.

Test

testCreateSubscriberForeignTargetDenied in MessagingBase:

  • client side (session): binding another user's target returns 404, the response body contains no identifier, and no subscriber is created.
  • server / console side (API key / admin): binding any user's target still returns 201.

Verified red→green against a live stack: before the change the cross-user call returned 201 with the victim's email in the body; after it returns 404 with no leak, while the owner path and the API-key path still return 201 with the subscriber correctly owned by the target owner.

Have you read the Contributing Guidelines on issues?

Yes.

A session or JWT caller could pass any user's targetId to
POST /messaging/topics/:topicId/subscribers. The handler loaded the
target with authorization skipped and never checked ownership, so an
authenticated user could bind another user's email, SMS or push target
to a topic and the 201 response disclosed the victim's identifier.

Inject the current user and require the target's userId to match the
caller, mirroring account.updatePushTarget. Admin console and API key
callers act for the project and stay exempt. Non-owners now get a 404
(USER_TARGET_NOT_FOUND), which also avoids a target-existence oracle and
closes the identifier disclosure. The subscriber document keeps deriving
its userId, permissions and userName from the target owner.
@ghost

ghost commented Sep 30, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The incremental fixture updates correctly use privileged authentication for cross-user subscriptions and introduce no identified defects.

The pull request restricts subscriber creation to targets owned by the authenticated user, while preserving API-key and privileged-console access. Subscriber metadata and permissions remain tied to the target owner. Shared E2E coverage checks foreign-target denial, identifier non-disclosure, and privileged success; existing messaging fixtures now use API keys when subscribing targets belonging to separately created users.

Latest changes: The newest commits switch cross-user subscriber setup in messaging delivery tests and fixtures to explicit API-key requests, matching the new ownership restriction.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 2
File Change
src/Appwrite/Platform/Modules/Messaging/Http/Topics/Subscribers/Create.php Injects the caller and enforces target ownership for ordinary users while retaining target-owner subscriber metadata.
tests/e2e/Services/Messaging/MessagingBase.php Adds foreign-target authorization coverage and updates delivery and query fixtures to subscribe other users' targets using API keys.

Reviewed the commits since 50e8406 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Tier S · Looks good to merge. Summary

@greptile-apps

greptile-apps Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Critical risk] Adds permission check to subscriber creation endpoint.

The PR appears safe to merge, though the existing privileged-path ownership test could be stronger.

Fix All in Claude CodeFindings

  1. P2 Subscriber ownership goes untested ▶
Fix with agent prompt
### Issue 1
tests/e2e/Services/Messaging/MessagingBase.php:1785-1786
The privileged branch checks only that creation succeeds and the response contains the requested target ID. The handler also changes the subscriber's owner, permissions, and user name, so those fields could regress while this test still passes. Please check that the subscriber belongs to the target's owner and that the owner can read or delete it.

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

The PR restricts subscriber creation to targets owned by the session or JWT caller while retaining privileged creation on behalf of users. The follow-up changes use API-key credentials for test setup that subscribes targets belonging to other users.

Reviews (2) · Last reviewed commit: "test(messaging): subscribe foreign targe..."

Comment on lines +1782 to +1783
$this->assertEquals(201, $response['headers']['status-code']);
$this->assertEquals($victimTarget['body']['$id'], $response['body']['target']['$id']);

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 Subscriber ownership goes untested The privileged branch checks only that creation succeeds and the response contains the requested target ID. The handler also changes the subscriber's owner, permissions, and user name, so those fields could regress while this test still passes. Please check that the subscriber belongs to the target's owner and that the owner can read or delete it.

Prompt To Fix With AI
This is a comment left during a code review.
Path: tests/e2e/Services/Messaging/MessagingBase.php
Line: 1782-1783

Comment:
**Subscriber ownership goes untested** The privileged branch checks only that creation succeeds and the response contains the requested target ID. The handler also changes the subscriber's owner, permissions, and user name, so those fields could regress while this test still passes. Please check that the subscriber belongs to the target's owner and that the owner can read or delete it.

---

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

@github-actions

github-actions Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/messaging-subscriber-target-ownership (after).

Metric Before After Change
🚀 Requests/sec 184.45 190.77 ⚪ +3.4%
⏱️ Latency P50 93.73 ms 90.53 ms ⚪ -3.4%
⏱️ Latency P95 220.45 ms 209.72 ms ⚪ -4.9%
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 90.53 209.72 12,255 190.77 -10.73
Account 175.06 320.55 645 10.69 -15.54
TablesDB 88.4 167.94 6,665 106.5 -7.64
Storage 82.16 176.05 3,225 52.92 -13.09
Functions 125.53 262.88 1,720 28.78 -5.73

Top API waits (after)

API request Max wait (ms)
account.name.update 594.01
functions.create 538.41
functions.variables.create 496.58
functions.variables.update 488.3
functions.variables.delete 416.25

The push, email-attachment, reply-to and sub-query tests, plus the SMS,
push and email send-setup helpers, subscribed a target owned by a freshly
created user while calling as the session user. That only passed because
createSubscriber skipped the ownership check; with the check in place the
session caller correctly gets a 404.

These calls only need the subscription to exist, so make them server (API
key) calls, which act for the project and may subscribe any user's target.
The owner-path and side-specific subscriber tests keep using the session.
@ChiragAgg5k
ChiragAgg5k merged commit ddb7e96 into main Sep 30, 2026
50 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the fix/messaging-subscriber-target-ownership branch September 30, 2026 12:31
abnegate added a commit that referenced this pull request Sep 30, 2026
Brings in main's OAuth2 state and nonce validation (#14012), which cloud
needs, along with the realtime origin alignment (#14020), subscriber
target ownership (#14019), GitLab primary email (#14010), impersonation
MFA (#14017), write-only webhook passwords (#14016), chunked-upload
resume ownership (#14014), preview authorize validation (#14013),
nightly forcing (#14015) and scheduled-function enqueue checks (#14011).

The merge had no textual conflicts, and main left composer.json and
composer.lock unchanged. So the lock keeps the branch's database and
async pins.

Main's assertResumeAllowed() in Storage Buckets/Files/Create checks
Input(Database::PERMISSION_UPDATE, ...). utopia-php/database 8 has no
PERMISSION_* constants, so every chunked resume would fail with an
undefined-constant error. It now passes PermissionType::Update, as the
rest of that action already does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

1 participant