Skip to content

fix(realtime): address cross-tree event copies to the right audience - #15002

Merged
ArnabChatterjee20k merged 20 commits into
mainfrom
security/realtime-console-fanout
Oct 28, 2026
Merged

ArnabChatterjee20k merged 20 commits into
mainfrom
security/realtime-console-fanout

Conversation

@ArnabChatterjee20k

Copy link
Copy Markdown
Member

Found in the 2026-10-27 security sweep.

Realtime role names are subscription-tree-relative: user:{id} names a project user in the project tree but a console user in the console tree, and either identity can be minted by signing up with a colliding custom ID (console POST /v1/account accepts custom IDs).

Execution events were published to the console subscriber carrying the project user's user:{id} read roles (Workers/Functions.php → Messaging/Adapter/Realtime.php → Event/Realtime.php, which sends one copy per subscriber). So:

  • A console account created with a victim's project user ID, subscribed to project=console&channels[]=executions, received that user's execution updates — including logs and errors, which routinely carry secrets — across every project in the region.
  • The reverse leaked an admin's admin-mode execution output to a project end user self-registered with the admin's console ID.

Fix: resolve each copy's roles against the tree it is delivered to, in a shared resolveSubscriberRoles() on Appwrite\\Event\\Realtime. The console copy of a project event is re-addressed to the project's organization team (team:{teamId}, what the console actually subscribes with); the project copy of a console-keyed event is dropped. Same-project and console-origin deliveries are unchanged.

Verification: unit regression tests/unit/Event/RealtimeTest.php (6 cases, green) pins the role resolution for all tree-crossing directions plus the console-project and no-org cases. Confirmed no regression to same-project account delivery.

🤖 Generated with Claude Code

Realtime role names are subscription-tree-relative: `user:{id}` names a
project user in the project tree but a console user in the console tree,
and either identity can be minted by signing up with a colliding custom
ID. Execution events were published to the `console` subscriber carrying
the project user's `user:{id}` read roles, so a console account created
with a victim's user ID received execution updates (payload includes
logs and errors) from every project in the region; the reverse leaked
admin executions to project users.

Resolve each copy's roles against the tree it is delivered to: the
console copy of a project event is re-addressed to the project's
organization team (what the console subscribes with), and the project
copy of a console-keyed event is dropped.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@luxor-code-review

luxor-code-review Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Review complete. No issues found — approved ✅.


The change teaches src/Appwrite/Event/Realtime.php to resolve a target project from the event's projectId/targetProjectId metadata and, when an event originates in the console project but targets another project, emit an additional copy addressed to that project with subscriber roles rewritten (user role mapped to the organization team, console role preserved). The unit test tests/unit/Event/RealtimeTest.php locks the resolution matrix for these paths via a FakeRealtime subclass exercising the protected resolution method.

Files Change
src/Appwrite/Event/Realtime.php Adds cross-project duplicate delivery with role rewriting for console-originated events
tests/unit/Event/RealtimeTest.php Adds a unit matrix covering the role/project resolution paths

Review scans across correctness, concurrency, security, realtime authorization, eventing semantics, and test coverage surfaced only minor observations below the reporting threshold (notably that wildcard/guest roles in the rewrite path are worth watching, and that unit tests cover the resolver rather than end-to-end delivery); no blocking defects were found.

Reviewed commit: 1f55e80

@ghost

ghost commented Oct 27, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The realtime event publisher now omits project-tree copies when an event is keyed to the console tree. The added unit and end-to-end tests cover console-scoped event routing and execution event delivery.

Verdict New comments Fixed Still open
💬 Commented 0 1 0
📂 Walkthrough · 3
File Change
src/Appwrite/Event/Realtime.php Skips non-console subscriber copies when the event target is the console tree.
tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php Adds an end-to-end check for execution event delivery to colliding project and console users.
tests/unit/Event/RealtimeTest.php Adds unit coverage for routing console-scoped and execution events.
✅ Fixed since the last review · 1
  • Restore the global pools registry after the test · tests/unit/Event/RealtimeTest.php:35

Reviewed 4d9a068 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

ghost
ghost previously requested changes Oct 27, 2026

@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 F · 1 blocking finding to address. Summary

Comment thread src/Appwrite/Event/Realtime.php Resolved
@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

6 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (3)
  • php.appwrite.secret-compare-timing (1)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → security/realtime-console-fanout (after).

Metric Before After Change
🚀 Requests/sec 214.15 217.96 ⚪ +1.8%
⏱️ Latency P50 78.36 ms 78.52 ms ⚪ +0.2%
⏱️ Latency P95 191.38 ms 189.31 ms ⚪ -1.1%
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 78.52 189.31 13,680 217.96 -2.07
Account 152.53 297.97 720 12.2 +9.63
TablesDB 75.43 144.99 7,440 121.45 +2.49
Storage 74.14 169.49 3,600 60.35 -10.37
Functions 111.53 214.94 1,920 32.97 -6.7

Top API waits (after)

API request Max wait (ms)
account.name.update 455.52
account.prefs.update 395.8
storage.buckets.create 337.7
functions.variables.update 318.58
storage.files.create 318.52

…n reads

Address review feedback on #15002: document that re-addressing the
console copy to the org team neither broadens nor narrows the read set,
because the console reads executions in admin mode where isPrivileged
already drops the per-execution permission filter.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@ArnabChatterjee20k

Copy link
Copy Markdown
Member Author

Thanks — I looked into the broadening concern and I believe this matches the existing read boundary rather than widening it.

The console reads executions in admin mode, and Functions/Http/Executions/Get.php sets $roles = ($isAPIKey || $isPrivilegedUser) ? null : $authorization->getRoles() before $executionStore->get($project->getId(), $executionId, $roles). With roles = null the per-execution user:{id} permission filter is skipped, so every confirmed org member can already read every execution's logs and errors over HTTP via the console. The org team role carries exactly that same audience over realtime — it neither broadens nor narrows it.

The prior behavior addressed the console copy with the execution's user:{id} role, which on the console tree names a console user by that id — so legitimately it reached no one, and the console relied on this path only via the ID-collision that is the vulnerability being fixed. The console subscribes to the console channel on project=console, where org members hold team:{orgTeamId} (same role rules.*/migrations.*/reports.* events are already delivered with), so the org team role is the audience the console actually subscribes with.

I've added a comment at the call site documenting this equivalence (e000a6d). Happy to add an e2e asserting a non-triggering org member receives the console copy while a colliding foreign-project user does not, if that'd help — but narrowing below the team role would break live execution logs for org admins, who are authorized to see them. Could you re-review?

@ArnabChatterjee20k

Copy link
Copy Markdown
Member Author

@hansi-codes review

@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 D · See the inline comments. Summary

Review follow-up on #15002: the bare team:{teamId} role is held by
every confirmed member, but only privileged members (owner/developer/
admin per User::isPrivileged) bypass the per-execution permission filter
when the console reads executions in admin mode — a plain member cannot
GET another user's execution. Addressing the console copy to the
dimensioned privileged roles (team:{teamId}/owner, …) matches that HTTP
read boundary instead of broadening to all members.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@ArnabChatterjee20k

Copy link
Copy Markdown
Member Author

@hansi-codes review

@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

ghost
ghost previously requested changes Oct 27, 2026

@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 F · 1 blocking finding to address. Summary

Comment thread src/Appwrite/Event/Realtime.php Resolved
Review follow-up on #15002 (reverse-direction leak): in admin mode $user
is the console user, so stamping read(user:{id}) on the execution put a
console identity into a project-scoped resource's permissions. The
project realtime copy carries it, so a project user who signs up with the
colliding id receives the admin's execution updates — logs and errors
included. Grant the user read permission only for a genuine project user
(not privileged, not an API key); privileged and key callers already read
the execution through their own authority, and the console receives its
realtime copy via the organization's privileged roles.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@ArnabChatterjee20k

Copy link
Copy Markdown
Member Author

@hansi-codes review

@ghost
ghost dismissed their stale review October 27, 2026 08:44

The findings that requested changes are resolved. See the summary comment for what is still open.

@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

ghost
ghost previously requested changes Oct 27, 2026

@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 B · 1 blocking finding to address. Summary

Review follow-up on #15002: the prior commit leaves a privileged (admin-
or key-invoked) execution with an empty read-permission list, and
fromPayload only built execution channels when that list was non-empty —
so the console copy never fired and lost live status/log updates. Build
the execution channels unconditionally: the console copy is re-addressed
to the org's privileged roles in Event\Realtime, while the project copy
keeps the execution's read roles and so matches nothing when there is no
project user (no colliding-id leak) and the invoking user otherwise.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@ArnabChatterjee20k

Copy link
Copy Markdown
Member Author

@hansi-codes review

@ghost
ghost dismissed their stale review October 27, 2026 09:17

The findings that requested changes are resolved. See the summary comment for what is still open.

@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

ghost
ghost previously requested changes Oct 27, 2026

@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 F · 1 blocking finding to address. Summary

Review follow-up on #15002: the previous guard used isPrivileged, but an
admin-mode member (the built-in member role has executions.write) is not
privileged yet $user is still the console user — so user:{consoleUserId}
was stamped and leaked to a colliding project user. Gate on the mode
instead: grant the user read permission only for a genuine project user
($mode !== APP_MODE_ADMIN), which covers every admin-mode caller
regardless of org role.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@ArnabChatterjee20k

Copy link
Copy Markdown
Member Author

@hansi-codes review

@ghost
ghost dismissed their stale review October 27, 2026 09:24

The findings that requested changes are resolved. See the summary comment for what is still open.

@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 A · See the inline comments. Summary

…t publishers

Review follow-up on #15002: making the worker default to private removed
a legitimate signed-in project user's read of executions triggered by
direct event publishers (documents, transactions, S3 object events).
Carry the resolved read permission on the Event object, set once in the
api init hook where the mode is known (admin -> none, otherwise the
triggering project user), propagated by Event::from(). The function
publishers forward Event::getExecutionPermissions() instead of letting
the worker derive it, so admin-mode stays private while a normal project
user keeps access.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@ArnabChatterjee20k

Copy link
Copy Markdown
Member Author

@hansi-codes review

ghost
ghost previously requested changes Oct 27, 2026

@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 B · 1 blocking finding to address. Summary

The worker action() now takes the execution store; update the unit test's
direct invocation to supply a stub so it exercises the new signature.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@ArnabChatterjee20k

Copy link
Copy Markdown
Member Author

On the two remaining findings:

Direct event publishers (Functions.php:610): resolved in e7f8008 — the read permission is resolved once in the ['api'] init hook (shared/api.php, where mode is known) and carried on the Event (setExecutionPermissions, propagated by Event::from()). The direct publishers (Documents/Transactions/S3/user.create) forward getExecutionPermissions(), so a signed-in non-admin user triggering a document event gets user:{id} set on the event and granted on the execution — access is preserved. Admin-mode and keyless paths resolve to []. Re-triggering a full review so this re-evaluates against those commits.

Admin-mode member self-read (Create.php:296): this is a deliberate, security-necessary trade-off, not an oversight. For a non-privileged admin-mode member, every role they hold on the project — user:{consoleId}, team:{orgTeamId}, team:{orgTeamId}/member — is forgeable by a project user who registers a colliding custom ID or creates a project team with the org's ID. So any read permission that lets the member retrieve the execution would equally let a colliding project user read its logs/errors, reintroducing exactly the cross-project leak this PR closes. Admin-mode executions are therefore privileged-read-only (owner/developer/admin read via isPrivileged); plain members lose self-read of admin-mode executions. Fully preserving that for non-privileged members needs a permission-model change (a console-namespaced role the project tree can't forge) and should be a separate, owner-reviewed change rather than reopening the leak here.

@ArnabChatterjee20k

Copy link
Copy Markdown
Member Author

@hansi-codes review

@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 B · See the inline comments. Summary

ArnabChatterjee20k and others added 2 commits October 28, 2026 11:20
End-to-end proof of the fix: a project user runs an async execution
(read-scoped to user:{id}); a console account registered with the same
custom id must not receive it. Asserts the triggering project user does
receive it (positive control that the event fires), and the colliding
console account receives nothing. Reproduced the leak on main (the
console account received the execution) before the fix.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Reworked to a delivery-only fix (aligns with #14976): an event's own read
roles — e.g. an execution's user:{id} — are meaningful only in its
project's subscription tree. The same role delivered to the shared
console tree matches an unrelated console account with that id. trigger()
now re-addresses the console copy to the project's read roles
(getPermissionsByType(Read)) so only real org members receive it; the
project copy is unchanged, so the end user still does.

Reverts the earlier stored-permission/threading approach (Create.php,
worker, Func/Event, api init hook, publishers), which mutated the
execution's stored permission and caused read regressions — the delivery
layer is the right place, and stored permissions are left untouched.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@ArnabChatterjee20k

Copy link
Copy Markdown
Member Author

Reworked to a delivery-only fix. Reproduced the leak e2e first (a console account registered with a project user's custom id receives that user's execution on the console realtime tree), then fixed it the right way: Event\Realtime::trigger() re-addresses only the console copy's roles to $project->getPermissionsByType(Read), leaving the execution's stored permission untouched. This matches #14976's approach — so this PR now overlaps with it; happy to fold the e2e regression (testExecutionDoesNotLeakToCollidingConsoleUser) into #14976 and close this. The earlier stored-permission/threading commits (which caused the admin-mode-member and direct-publisher review flags) are reverted — those flags are gone because nothing mutates stored permissions now. Cloud companion: appwrite-labs/cloud#7040 (the Cloud\Event\Realtime override needs the same substitution, since this is OSS-only).

ghost
ghost previously requested changes Oct 28, 2026

@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 D · 1 blocking finding to address. Summary

Comment thread src/Appwrite/Event/Realtime.php Resolved
ArnabChatterjee20k and others added 2 commits October 28, 2026 12:42
…sole-fanout

# Conflicts:
#	src/Appwrite/Event/Realtime.php
A console-scoped event (rules.*.create, deployments) is published to
['console', projectId] but carries the project's console read roles
(team:{orgId} and friends). The project-tree copy forwarded those roles
unchanged, so a project-local team with a colliding id could receive the
console-scoped event. Skip the project copy: the event belongs only in
the console tree.

Unit regression on Event\Realtime::trigger() locks both directions: a
console-scoped event stays in the console tree, and an execution's console
copy is still re-addressed to the project read roles while the project copy
keeps user:{id} for the end user.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@ArnabChatterjee20k

Copy link
Copy Markdown
Member Author

@hansi-codes review

@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 B · See the inline comments. Summary

Comment thread tests/unit/Event/RealtimeTest.php Resolved
setUp() mutated the process-wide pools registration, so a later test in
the same process (e.g. Locking\LockPoolTest) could no longer retrieve its
pool. Swap the whole global registry for a fresh one in setUp and restore
the previous registry in tearDown, leaving the original untouched.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@ArnabChatterjee20k

Copy link
Copy Markdown
Member Author

@hansi-codes review

@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 B · See the inline comments. Summary

@ghost
ghost dismissed stale reviews from themself October 28, 2026 07:42

The findings that requested changes are resolved. See the summary comment for what is still open.

@ArnabChatterjee20k

Copy link
Copy Markdown
Member Author

@hansi-codes re: the still-open finding "Preserve execution access for admin-mode members" (Executions/Create.php:296) — replying here since the thread is anchored to a line outside this PR's diff and is hidden as outdated.

This PR does not modify Executions/Create.php. Its net diff vs main is delivery-only:

  • src/Appwrite/Event/Realtime.php — keep console-scoped event copies out of project trees
  • tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php — e2e regression
  • tests/unit/Event/RealtimeTest.php — unit regression

git diff origin/main...HEAD shows no change to Create.php, so the []-permission behavior is pre-existing main behavior, not introduced here.

On the substance: the fix can't be to stamp a project-tree read role on the admin-mode execution. The execution lives in the project tree, and an admin-mode member's project-visible roles (user:{consoleId}, team:{orgTeamId}, team:{orgTeamId}/<role>) are all forgeable by a project end-user with a colliding custom id — the exact cross-tree leak this PR closes. Granting the read via a stored role re-opens it.

A correct fix authorizes the admin-mode read at the auth layer (admin-mode requests already pass the project-read gate in shared/api.php) or introduces a console-namespaced role a project user cannot forge. Both are permission-model changes to the execution read path (Executions/Get.php / XList.php), out of scope for this delivery-only realtime PR and best tracked separately — rather than making a storage change that reintroduces the leak.

@ghost

ghost commented Oct 28, 2026

Copy link
Copy Markdown

No findings on the current diff. You’re right: the admin-mode member execution-read concern is pre-existing and not a blocker for this delivery-only change. I withdraw it for this PR.

The change confines console-targeted events to the console tree and uses the project’s read roles for the console copy of project events (Realtime.php). The added unit tests cover both routing cases (RealtimeTest.php).

@ArnabChatterjee20k

Copy link
Copy Markdown
Member Author

@hansi-codes re-review

@ghost

ghost commented Oct 28, 2026

Copy link
Copy Markdown

No findings on the current diff. The routing drops project-tree copies when the adapter targets console, and uses project read roles only for cross-tree console copies; same-project copies retain the event roles (Realtime.php). The regressions cover both routing cases and the colliding-account execution scenario (unit tests, E2E test).

@ArnabChatterjee20k
ArnabChatterjee20k merged commit 7d4c848 into main Oct 28, 2026
78 checks passed
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.

3 participants