Skip to content

fix(graphql): keep the caller's identity across fields of one request - #14820

Merged
ChiragAgg5k merged 1 commit into
mainfrom
fix/graphql-request-identity
Oct 22, 2026
Merged

ChiragAgg5k merged 1 commit into
mainfrom
fix/graphql-request-identity

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

What

GraphQL fields in one request share the request's user and authorization resources. The magic URL, email and phone token endpoints and the recovery and recovery OTP endpoints copied the recipient into the injected user and added the recipient's role to the shared authorization. Inside one GraphQL operation, later fields then ran with that identity.

  • The five endpoints now load the recipient into a local $recipient document, no longer inject user, and create their tokens under $authorization->skip() instead of granting the recipient's (or a placeholder's) role.
  • Resolvers::resolve() runs each field inside $authorization->withRoles($authorization->getRoles(), ...), so roles a field adds end with that field. The api init hook still derives roles from the request's user for every field.
  • The resolver lock is a per-request resolverLock resource instead of a static map entry that each resolver removed in its finally. Removing it there let resolvers that started later get a new lock while another coroutine still waited on the old one. The lock now lives for the whole request, so resolvers stay serialized.

Compatibility

Audit entries for token and recovery issuance from an unauthenticated request are now recorded with the guest actor rather than the recipient. The resource (user/{id}) is unchanged. Recovery events already set the event user explicitly, so webhook and realtime payloads are unchanged.

Tests

  • AccountTest::testCreateEmailTokenKeepsCallerIdentity (GraphQL e2e): a guest mutation issues an email token for an existing account and then calls accountUpdateName. The second field must fail, and the account's name must stay unchanged. Without the fix, the second field succeeds as the recipient.
  • The lock change has no e2e test: whether the race happens depends on coroutine scheduling inside one request, so a test can't trigger it reliably, and Resolvers runs Swoole coroutines, which unit tests must not do. The change removes the per-resolver unset, so the lock object can no longer be replaced during a request.
  • Pint, PHPStan (host config) and Rector pass on the touched files. E2E runs in CI.

Related: appwrite-labs/cloud#6666

Token and recovery issuance loaded the recipient into the request-scoped user and granted its role on the shared authorization. GraphQL fields of one request share those resources, so later fields saw the recipient. Load the recipient into a local document and create the token under skip instead.

Scope the roles a field grants to that field, and keep one resolver lock per request so it is never dropped while other resolvers wait on it.
@luxor-code-review

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

Copy link
Copy Markdown

Review complete. No issues found — approved ✅.


This PR fixes cross-identity role leakage when issuing tokens and hardens GraphQL request-scoped state. Token endpoints no longer grant the caller extra roles mid-request or mutate the injected user document; instead a local recipient document is built and token creation runs under an authorization skip. Resolvers now snapshot and restore roles around each field execution so grants made by one field cannot persist into sibling fields, and the per-request lock moves from a static map into the DI container as a request-scoped resource.

Files Change
app/controllers/api/account.php Token/recovery/verification endpoints use a local $recipient document and create tokens under authorization->skip, removing addRole calls on the caller's identity
src/Appwrite/GraphQL/Resolvers.php Wraps $utopia->execute in a withRoles snapshot and fetches a request-scoped resolverLock from context instead of a static map
app/init/resources/request.php Registers the new resolverLock request resource
tests/e2e/Services/GraphQL/AccountTest.php Adds e2e test locking in caller-identity behavior for email token creation

Reviewed commit: acfc742

@ChiragAgg5k ChiragAgg5k self-assigned this Oct 22, 2026
@ghost

ghost commented Oct 22, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The recipient identity no longer leaks between fields, and the resolver lock is retained for the full request lifetime.

The account token and recovery endpoints now keep recipient documents local and perform token persistence without granting recipient roles to the request authorization. GraphQL resolver execution now restores roles after each field and uses a per-request lock, with an end-to-end regression test for caller identity across mutation fields.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 4
File Change
app/controllers/api/account.php Loads token and recovery recipients into local documents and isolates token persistence from shared request roles.
app/init/resources/request.php Registers a resolver lock resource scoped to each HTTP request.
src/Appwrite/GraphQL/Resolvers.php Serializes resolvers with the request lock and restores authorization roles after each field.
tests/e2e/Services/GraphQL/AccountTest.php Adds an end-to-end regression test confirming token issuance does not authenticate a later field as the recipient.

Reviewed acfc742 · 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

@github-actions

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/graphql-request-identity (after).

Metric Before After Change
🚀 Requests/sec 172.64 191.16 🟢 +10.7%
⏱️ Latency P50 98.46 ms 88.53 ms 🟢 -10.1%
⏱️ Latency P95 235.96 ms 215.83 ms 🟢 -8.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.53 215.83 12,198 191.16 -20.13
Account 165.39 342.92 642 10.65 -10.63
TablesDB 84.92 161.58 6,634 106.69 -19.56
Storage 82.78 196.59 3,210 53.25 -46.9
Functions 126.29 256.26 1,712 29.13 -14.25

Top API waits (after)

API request Max wait (ms)
account.name.update 486.62
account.prefs.update 440.89
storage.files.create 416.41
storage.buckets.create 400.14
functions.variables.create 399.13

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

@ChiragAgg5k
ChiragAgg5k merged commit f3e4c08 into main Oct 22, 2026
69 checks passed
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
The magic URL, email and phone token endpoints and the recovery endpoint copied the recipient into the shared user resource and added its role to the shared authorization, so later fields in the same GraphQL operation ran as the recipient. They now use a local recipient and create tokens under authorization skip. Each resolver restores the roles it started with, and the resolver lock is a per-request resource. 1.9 has no recovery OTP endpoint and no Authorization::withRoles, so roles are saved and restored around the field.

(cherry picked from commit f3e4c08)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
The magic URL, email and phone token endpoints and the recovery endpoint copied the recipient into the shared user resource and added its role to the shared authorization, so later fields in the same GraphQL operation ran as the recipient. They now use a local recipient and create tokens under authorization skip. Each resolver restores the roles it started with, and the resolver lock is a per-request resource. 1.9 has no recovery OTP endpoint and no Authorization::withRoles, so roles are saved and restored around the field.

(cherry picked from commit f3e4c08)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
The magic URL, email and phone token endpoints and the recovery endpoint copied the recipient into the shared user resource and added its role to the shared authorization, so later fields in the same GraphQL operation ran as the recipient. They now use a local recipient and create tokens under authorization skip. Each resolver restores the roles it started with, and the resolver lock is a per-request resource. 1.9 has no recovery OTP endpoint and no Authorization::withRoles, so roles are saved and restored around the field.

(cherry picked from commit f3e4c08)
Tapraj-Pathak pushed a commit to Tapraj-Pathak/appwrite that referenced this pull request Oct 24, 2026
…dentity

fix(graphql): keep the caller's identity across fields of one request

(cherry picked from commit f3e4c08)
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