Skip to content

fix(teams): hold the last-owner rule for every caller - #15153

Merged
ChiragAgg5k merged 3 commits into
mainfrom
fix/last-owner-any-caller
Nov 1, 2026
Merged

ChiragAgg5k merged 3 commits into
mainfrom
fix/last-owner-any-caller

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

The console "at least one owner" rule was tied to the caller, not the target membership:

$isCurrentUserAnOwner = $user->getSequence() === $membership->getAttribute('userInternalId');

An API key's user has no sequence, so a key could demote or delete the only owner. The count also included pending owner invites, so the sole confirmed owner could leave while an invite was unanswered.

Now, for console teams, PATCH/DELETE /v1/teams/:teamId/memberships/:membershipId:

lock team row (FOR UPDATE) ─┐
write (update / delete)     ├─ one transaction → 0 owners? throw + rollback
recount confirmed owners    ─┘
  • checks the target (confirmed owner losing owner), whoever calls
  • counts confirm = true owners only
  • locks the team row first, so concurrent demotions of different owners serialize
  • recounts after the write, in the same transaction, so unauthorized callers still get 401 first

Test Plan

  • TeamsConsoleClientTest::testPendingOwnerInviteDoesNotCountAsAnOwner: sole owner + pending owner invite → demote and leave both 400, owner keeps owner, invite still revocable. Old code returns 200 here.
  • Existing last-owner and permission tests in TeamsConsoleClientTest / TeamsBase unchanged.

Related PRs and Issues

None.

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

Related: appwrite-labs/cloud#6666

…wner's session

The console last-owner check compared the caller with the target membership, so an API key (whose user has no sequence) could demote or remove the only owner. It also counted pending owner invites, letting the sole confirmed owner leave while an invite was unanswered.

Check the target membership instead, count confirmed owners only, and recount after the write in the same transaction so the refused change rolls back as a whole.
@ghost

ghost commented Nov 1, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

For console teams, membership role updates and deletions now preserve at least one confirmed owner regardless of caller, counting only confirmed owner memberships and serializing guarded changes with a team-row lock. The E2E coverage verifies that pending invitations do not satisfy the owner rule and that concurrent owner removals leave one confirmed owner.

Latest changes: The latest commits add an E2E race between an owner's demotion and another owner's departure, and relax the persisted-role assertion to check that the owner role remains present.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 3
File Change
src/Appwrite/Platform/Modules/Teams/Http/Memberships/Delete.php Guards removal of a confirmed owner with a locked, transactional recount of confirmed owners.
src/Appwrite/Platform/Modules/Teams/Http/Memberships/Update.php Guards demotion of a confirmed owner with a locked, transactional recount of confirmed owners.
tests/e2e/Services/Teams/TeamsConsoleClientTest.php Covers pending owner invitations and concurrent owner removals, asserting a confirmed owner remains.

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

@github-actions

github-actions Bot commented Nov 1, 2026

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.

@luxor-code-review luxor-code-review Bot 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.

This PR strengthens team membership guards so the last confirmed owner of a team cannot be deleted or demoted, wrapping the membership write plus the owner-count check in a transaction on console projects, and adds e2e coverage.

Key findings

  • 🟡 Last-owner guard races: count is not serialized across requests — Delete.php:105

ghost
ghost previously requested changes Nov 1, 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

Two requests demoting or removing different owners each counted the other as still present and both committed. Locking the team row first makes the second wait and count the first's change.
@github-actions

github-actions Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/last-owner-any-caller (after).

Metric Before After Change
🚀 Requests/sec 173.64 192.27 🟢 +10.7%
⏱️ Latency P50 97.62 ms 89.82 ms 🟢 -8%
⏱️ Latency P95 233.04 ms 208.16 ms 🟢 -10.7%
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 89.82 208.16 12,255 192.27 -24.89
Account 169.92 324.03 645 10.67 -25.86
TablesDB 87.01 164.73 6,665 107.94 -9.74
Storage 83.26 193.85 3,225 53.69 -17.74
Functions 124.1 238.25 1,720 29.37 -107.16

Top API waits (after)

API request Max wait (ms)
tablesdb.rows.create 453.22
account.name.update 451.06
tablesdb.rows.delete 445.25
account.prefs.update 424.48
functions.create 422.66

@ghost
ghost dismissed their stale review November 1, 2026 09:07

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 · Looks good to merge. Summary

The creator's membership carries the team's default roles alongside owner, so assert owner is kept rather than the exact list.

@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

@ChiragAgg5k
ChiragAgg5k merged commit 47ede83 into main Nov 1, 2026
34 checks passed
@luxor-code-review

luxor-code-review Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Review complete. 🟡 1 medium

💬 Inline comments (1)

  • 🟡 Last-owner guard races: count is not serialized across requests — Delete.php:105
🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 Guard flags computed from pre-transaction membership snapshot (Update.php:103) — $isConfirmedOwner and $isDemotingConfirmedOwner are computed from the membership document fetched before the transaction starts (Update.php:103-106; the fetch is at Update.php:76, and the same pattern appears in Delete.php:91-92).

The change hardens the Teams membership Delete and Update endpoints against removing the team's last confirmed owner, previously only blocked for self-deletion. Both actions now run the membership mutation followed by an owner-count check inside a transaction, throwing a prohibition exception to roll back when no owner would remain, with the console-only e2e suite extended accordingly.

Files Change
src/Appwrite/Platform/Modules/Teams/Http/Memberships/Delete.php Adds a transaction-wrapped last-owner guard after deleting a membership, throwing when zero confirmed owners remain.
src/Appwrite/Platform/Modules/Teams/Http/Memberships/Update.php Adds the same transaction-wrapped guard for role changes, preventing demotion of the last confirmed owner.
tests/e2e/Services/Teams/TeamsConsoleClientTest.php Adds console-client e2e assertions for deleting and demoting the last owner.

Reviewed commit: 31ab407

@ChiragAgg5k ChiragAgg5k self-assigned this Nov 2, 2026
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