Skip to content

fix: harden account claims, user credential responses and console realtime - #14976

Merged
lohanidamodar merged 9 commits into
mainfrom
fix/account-claim-and-realtime-isolation
Oct 28, 2026
Merged

lohanidamodar merged 9 commits into
mainfrom
fix/account-claim-and-realtime-isolation

Conversation

@lohanidamodar

@lohanidamodar lohanidamodar commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Hardening for account claims, user credential responses and console realtime fan-out.

Account claims

  • User::revokeCredentials also removes the account's console account keys, and triggers a credentialsRevoked hook so an edition can revoke credentials it keeps in its own collections.
  • Proving ownership of an unverified email now revokes credentials the registrant left behind on every claim path, not only OAuth and ID token sessions: magic URL / email OTP sessions, password recovery (link and OTP), and accepting a team invite while signed out. Explicit email verification is unchanged, since the account's own session requests it.

User credentials

  • Callers that hold users.read without users.write (a read-only console role, or a delegated OAuth2 token) get password hashes, hash options and provider tokens blanked, using the existing credential model flag. Standard API keys are unchanged because user migrations read hashes with users.read.

Realtime

  • When a project event is also published to the console namespace, the console copy now goes to the project's read roles instead of roles that only mean something inside the project.
  • Function and site deployment events go to the project's read roles instead of every organization member, matching migrations.
  • Execution logs and errors are kept out of realtime payloads, as they already are over HTTP for non-key callers. Webhooks still carry them.

Test Plan

  • Unit: MessagingTest::testFromPayloadDeploymentsReachOnlyProjectReaders (functions and sites).
  • E2E: testCreateMagicURLSessionRevokesUnverifiedAccountCredentials, testUpdateRecoveryRevokesUnverifiedAccountCredentials, testUpdateMembershipStatusRevokesUnverifiedAccountCredentials.
  • Pint, PHPStan and Rector on the changed files.

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?

Claiming an unverified account through a magic URL or email OTP session,
password recovery, or accepting an invite while signed out now revokes
the credentials the registrant left behind, as OAuth and ID token claims
already did. Revocation also drops console account keys.
A session or delegated token that can read users but not write them no
longer receives password hashes, hash options or provider tokens.
Standard API keys keep them, since user migrations read hashes.
…read roles

Console copies of execution updates now go to those who can read the
project instead of the execution's project-user roles, deployment events
follow the project's read roles instead of the whole organization, and
execution logs and errors stay out of realtime payloads as they do over
HTTP for non-key callers.
@luxor-code-review

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

Copy link
Copy Markdown

Review complete. 🟠 2 high · 🟡 1 medium

💬 Inline comments (2)

  • 🟠 Non-standard key types bypass credential-hiding gate — api.php:383
  • 🟡 Recovery revokes on stale unlocked emailVerification read — account.php:4654
📍 Findings outside the diff (1) — 🟠 1 high — defects on lines GitHub can't attach comments to

🟠 High — Email-verification confirm endpoints skip credential revocation · account.php:5464 · unchanged line

// app/controllers/api/account.php
5464	        $profile = $dbForProject->updateDocument('users', $profile->getId(), new Document(['emailVerification' => true]));

The PR adds User::revokeCredentials on every path where a party proves control of an unverified email, but PUT /v1/account/verifications/email and PUT /v1/account/verifications/email/otp set emailVerification = true without revoking anything (app/controllers/api/account.php:5464 and 6448). An attacker who registered the victim's email keeps their session, password, keys, and MFA after the real owner confirms the verification — the takeover window this PR closes everywhere else stays open on the most user-visible path.


The change adds User::revokeCredentials($dbForProject, $user, $project) to every flow where a party proves control of an unverified email — id-token exchange, recovery completion, invite acceptance — with console-only cleanup of account API keys, and hardens the claim paths with locked re-reads. It also swaps team-role matching for project read permissions on deployment realtime events so individually granted console users receive them, and gates credential redaction behind users.write scope in the shared API hook.

Files Change
app/controllers/api/account.php, src/Appwrite/Platform/Modules/Account/Http/Account/Sessions/IdToken/Create.php, src/Appwrite/Platform/Modules/Teams/Http/Memberships/Status/Update.php, src/Appwrite/Utopia/Database/Documents/User.php Thread a $project param through revokeCredentials, call it on email-claim and invite-accept paths, and delete console keys rows for the user.
app/controllers/shared/api.php Blank user credential fields unless the key holds users.write (OAuth2 keys exempt).
src/Appwrite/Event/Realtime.php, src/Appwrite/Messaging/Adapter/Realtime.php Publish console-namespace events with project read-permission roles instead of raw team roles, fixing cross-project role collisions and deployment-event delivery.
src/Appwrite/Platform/Workers/Functions.php Mark execution event payload fields logs/errors as sensitive.
tests/e2e/..., tests/unit/... E2E coverage for revocation on id-token/invite paths and unit tests for messaging redaction.

Reviewed commit: ef8b4c4

@ghost

ghost commented Oct 27, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

This PR revokes credentials left on unverified accounts across email-claim and recovery flows, including console account keys and edition-specific credentials. It also limits sensitive user credential fields for read-only callers and adjusts realtime event permissions and payloads for console consumers, project readers, and execution logs. E2E and unit tests cover the affected claim, authorization, and realtime behavior.

Latest changes: The latest commits update the remaining console-tail membership setup to pass the member object expected by its helper and change the sites test to sign in before invite acceptance, since signed-out acceptance now revokes credentials.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 17
File Change
app/controllers/api/account.php Revoke credentials on applicable unverified-email claim and recovery flows.
app/controllers/shared/api.php Blank sensitive user credential fields for callers without user-write access.
src/Appwrite/Event/Realtime.php Route console copies of project events to project reader roles.
src/Appwrite/Messaging/Adapter/Realtime.php Restrict function and site deployment events to project readers.
src/Appwrite/Platform/Modules/Account/Http/Account/Sessions/IdToken/Create.php Pass project context when revoking credentials during an ID-token claim.
src/Appwrite/Platform/Modules/Teams/Http/Memberships/Status/Update.php Revoke credentials when a signed-out invite acceptance claims an unverified account.
src/Appwrite/Platform/Workers/Functions.php Exclude execution logs and errors from realtime payloads.
src/Appwrite/Utopia/Database/Documents/User.php Remove console account keys and invoke the edition credential-revocation hook.
tests/e2e/Services/Account/AccountCustomClientTest.php Cover credential revocation in account-claim flows.
tests/e2e/Services/Migrations/MigrationsBase.php Verify realtime omits execution logs while the API still exposes them.
tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php Adjust invite acceptance setup to retain an authenticated session.
tests/e2e/Services/Realtime/RealtimeConsoleMigrationsTest.php Adjust invite acceptance setup to retain an authenticated session.
tests/e2e/Services/Realtime/RealtimeConsoleTailTest.php Adjust membership setup to pass the member object to the helper.
tests/e2e/Services/Sites/SitesCustomServerTest.php Sign in before accepting the invite in the scoped membership scenario.
tests/e2e/Services/Teams/TeamsCustomClientTest.php Cover invite acceptance, project access, and credential revocation.
tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php Adjust invite acceptance setup for the credential-claim behavior.
tests/unit/Messaging/MessagingTest.php Test that function and site deployment events reach only project readers.

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

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

@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 closes an account-takeover window by revoking attacker-held credentials whenever an unverified email is claimed, and fixes console realtime role scoping for individually granted users.

Key findings

  • 🟠 Email-verification confirm endpoints skip credential revocation — account.php:5464
  • 🟠 Non-standard key types bypass credential-hiding gate — api.php:383
  • 🟡 Recovery revokes on stale unlocked emailVerification read — account.php:4654

Comment thread app/controllers/shared/api.php Outdated
Comment on lines +383 to +386
$isStandardKey = ! empty($apiKey) && $apiKey->getType() !== API_KEY_OAUTH2;
if (\in_array('users.read', $scopes, true) && ! \in_array('users.write', $scopes, true) && ! $isStandardKey) {
$response->setHideCredentials(true);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 security · high

Non-standard key types bypass credential-hiding gate

The exemption is $apiKey->getType() !== API_KEY_OAUTH2, but the comment scopes it to standard keys (app/controllers/shared/api.php:383). API_KEY_EPHEMERAL, API_KEY_ORGANIZATION, API_KEY_ACCOUNT, and legacy keys are all exempted too, so a users.read-only key of those types still receives unhidden credential fields (passwordHash, hashOptions, MFA secrets) where sessions, OAuth2 keys, and JWTs are now blanked. This weakens the new gate to nearly a no-op for programmatic keys.

📋 Prompt for AI Agents

In app/controllers/shared/api.php at lines 382-386, replace $isStandardKey = ! empty($apiKey) && $apiKey->getType() !== API_KEY_OAUTH2; with an allowlist of key types that legitimately read user hashes (e.g. in_array($apiKey->getType(), [API_KEY_STANDARD], true)), so ephemeral and other non-standard key types with users.read-only scopes no longer bypass $response->setHideCredentials(true). Update the comment on line 382 to match the actual exemption list.

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.

Intentional. Every non-OAuth2 key is minted by someone who already holds keys authority and could grant users.write on it, so blanking would not take the credentials away from them, and Appwrite-to-Appwrite user migrations read hashes with a users.read key. OAuth2 tokens are the exception because they delegate a person's role, which may be read-only. I reworded the comment to say this.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The updated comment and variable rename ($isMintedKey) improve clarity, but the underlying logic is unchanged — the exemption is still $apiKey->getType() !== API_KEY_OAUTH2, which means API_KEY_EPHEMERAL, API_KEY_ORGANIZATION, API_KEY_ACCOUNT, and legacy key types all remain exempt.

The 'minter holds keys authority' rationale applies to standard app keys, but ephemeral keys (short-lived, scoped) and organization/account keys may be issued to contexts where users.write was never in scope and the minter never intended to grant credential access. The fix the finding requested is an allowlist — e.g. in_array($apiKey->getType(), [API_KEY_STANDARD], true) — so only key types with a legitimate migration use-case bypass the gate, rather than exempting everything that isn't OAuth2. The current diff does not implement that change.

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.

Each of those types traces back to someone who already holds users.write, so an allowlist would not narrow anything:

  • Ephemeral keys are minted by the platform for function and site executions, with scopes from the resource's config. Only functions.write / sites.write can set those, which in the role config means developer or above, and those roles carry users.write.
  • Organization keys need organization.keys.write (owner or developer).
  • Account keys are refused on every project except console (Account API keys can only be used with the console project), so they never reach a project's users or credentials.

Analyst and editor hold none of these minting scopes, which is why OAuth2 tokens (which delegate the holder's own role) are the only key path left that needs the gate.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The rationale is detailed and worth considering, but it hinges on invariants enforced elsewhere in the codebase — specifically that every non-OAuth2 key type is always minted by a principal who already holds 'users.write'. That is a runtime guarantee, not something the gate itself enforces. If any of those paths (ephemeral scope config, org key issuance, account key project check) ever changes or has an edge case, the gate silently fails open for credential fields.

The allowlist approach ('in_array($apiKey->getType(), [API_KEY_STANDARD], true)') would make the gate's safety self-contained and not reliant on those external invariants holding forever. The current diff still uses the denylist ('!== API_KEY_OAUTH2'), so the finding remains unaddressed in the code as shown.

Comment thread app/controllers/api/account.php Resolved

@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

@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/account-claim-and-realtime-isolation (after).

Metric Before After Change
🚀 Requests/sec 209.43 213.9 ⚪ +2.1%
⏱️ Latency P50 80.33 ms 80.8 ms ⚪ +0.6%
⏱️ Latency P95 195.46 ms 191.73 ms ⚪ -1.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 80.8 191.73 13,680 213.9 -3.73
Account 156.39 301.87 720 11.97 +12.9
TablesDB 78.13 144.15 7,440 119.38 +0.78
Storage 75.05 175.98 3,600 59.22 -20.27
Functions 114.63 229.88 1,920 32.31 +5.41

Top API waits (after)

API request Max wait (ms)
account.name.update 477.2
functions.create 381.75
tablesdb.rows.list 374.54
account.prefs.update 346.95
functions.variables.update 314.68

@lohanidamodar

Copy link
Copy Markdown
Member Author

On the out-of-diff finding about PUT /account/verifications/email and its OTP variant: leaving these alone is deliberate. The account's own session requests that token, so revoking at confirmation would remove the password and sessions of every ordinary sign-up the moment it verifies. The claim paths changed here are the ones a person who never held the account can start: magic URL / email OTP, recovery, and invite acceptance while signed out.

@lohanidamodar
lohanidamodar merged commit 2dc0efb into main Oct 28, 2026
67 checks passed
@lohanidamodar
lohanidamodar deleted the fix/account-claim-and-realtime-isolation branch October 28, 2026 06:20
ArnabChatterjee20k added a commit that referenced this pull request Oct 28, 2026
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]>
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