Repository navigation
Description
Problem
#14012 made the OAuth2 nonce cookie check mandatory for both OAuth2 flows. In the token flow, when an app's server starts sign-in (the SSR pattern in our docs: node-appwrite createOAuth2Token, then redirect), the cookie never reaches the user's browser. Every sign-in then fails with user_oauth2_state_invalid, and production SSR projects have been failing since the deploy.
Background, reproduction and production evidence: https://claude.ai/artifact/SMowSFFyS9mdNnff827m5w
Approach
Go back to what the token flow supported before Sep 30: no mandatory check. Add state and PKCE as optional parameters that developers can use if they want login-CSRF protection. The check lives in the developer's own code (comparing the state, or sending the PKCE verifier), so no project setting is needed. Keep #14012's cookie check on the session flow, where Appwrite creates the session itself before the app can verify anything.
Plan
1. Token flow: remove the mandatory cookie check
app/controllers/api/account.php, at the redirect callback (the nonce check around line 1706): run the existing a_oauth2_<projectId> check only when empty($state['token']), so only for the session flow.
2. Optional state (RFC 6749 §4.1.1)
- New optional
stateparam oncreateOAuth2TokenandcreateOAuth2Session, capped at 256 characters. The whole provider state is validated asText(2048)at the callback, and the app's value travels inside it. - Store it inside the provider
stateunder its own key, separate from Appwrite's internal nonce. - Return it unchanged as
state=on the success and failure redirect URLs (RFC 6749 §4.1.2 and §4.1.2.1). - The app stores the value in its own cookie and compares it on its success route. A forged callback carries the attacker's value, so the comparison fails in the app.
- Document the value requirements from RFC 9700 §4.7.1: unguessable, single use, and tied to the user's browser (for example, stored in the app's cookie). The value isn't secret; its strength comes from that comparison.
- The name is
state, notnonce. In OpenID Connect,nonceis returned inside the signed ID token and prevents token replay, which is a different mechanism.
// /login
const state = randomString();
cookies.set('oauth_state', state, { httpOnly: true, secure: true, sameSite: 'lax' });
redirect(await account.createOAuth2Token({ provider, state, success, failure }));
// /success?userId&secret&state
if (url.searchParams.get('state') !== cookies.get('oauth_state')) throw new Error('OAuth2 state mismatch');
cookies.delete('oauth_state');
await account.createSession({ userId, secret });3. Optional PKCE (RFC 7636) for the token flow
- New optional
codeChallengeandcodeChallengeMethodparams oncreateOAuth2Token. OnlyS256is accepted (RFC 9700 §2.1.1). - The challenge is carried in
stateand stored on the token created at the callback. This needs a new attribute on thetokenscollection, plus a migration for existing projects. - New optional
codeVerifierparam oncreateSession. The rules follow the downgrade protection in RFC 9700 §4.8:
| Token has a challenge | codeVerifier sent |
Result |
|---|---|---|
| no | no | session created (current behaviour) |
| yes | yes, S256(verifier) matches |
session created |
| yes | no | rejected |
| no | yes | rejected |
| yes | yes, no match | rejected |
- A new error type for a verifier that is missing or doesn't match.
4. Session flow: keep the cookie check
createOAuth2Session keeps #14012's mandatory cookie check. Appwrite sets the session at its own callback, so a check done by the app would come too late. The method is browser-only (the spec lists it for client and console), so the check never breaks server-started flows.
5. Explicit identity linking
Today the callback links the provider identity to whoever the browser is signed in as. Without the state cookie (the token flow, since #14157), anyone who sends their own callback link to a signed-in user can attach their identity to that account, as was possible before #14012.
- Add an explicit link action that only a signed-in user can start, with their session (browser cookie, or
setSessionon a server SDK). - Record each pending flow server-side, keyed by a random
stateid: intent (signinorlink), the user to link, expiry. Consume it once at the callback. - Link only when the flow record says
link, never because of who is signed in when the callback lands. - The same record holds the PKCE challenge from step 3.
- Prior art: Supabase
linkIdentity(/user/identities/authorize, called with the user's token), FirebaselinkWithRedirect.
Tests
tests/e2e/Services/Account/AccountCustomClientTest.php:
- Regression: start a token flow, then call the callback without the
a_oauth2_cookie. Expect a redirect to the success URL withuserIdandsecret. This fails onmaintoday. - Session flow: the existing tests (around lines 3040 and 3062) still expect
user_oauth2_state_invalidwithout the cookie. - State: with
stateset, the success and failure URLs carry it unchanged. Without it, they don't add the param. A value over 256 characters is rejected. - PKCE: one test for each row of the table above.
SDKs and docs
- The new params get generated into the client, console and server SDKs.
- Update the SSR auth docs to show the
stateand PKCE patterns as recommended practice.
Rollout
Merge to CE, then bump appwrite/server-ce in appwrite-labs/cloud once CE CI is green. Step 1 alone fixes the outage, so it can ship first if steps 2 and 3 need more review.
Open questions
- Should
pkcealso be accepted oncreateOAuth2Session? It has no redemption step, so it would have no effect there. - Final param names:
codeChallenge,codeChallengeMethod,codeVerifier.
Out of scope
- Server SDK fix (return the start URL instead of fetching it).
- Spec
Locationheader: Declare the Location header on OAuth2 redirect responses in the OpenAPI spec #14155.
Activity
[-]Add OAuth2 nonce project policy (off by default)[/-][+]Restore token-flow OAuth2 for SSR and add optional nonce and PKCE[/+][-]Restore token-flow OAuth2 for SSR and add optional nonce and PKCE[/-][+]Restore token-flow OAuth2 for SSR and add optional state and PKCE[/+]revert(account): keep the token flow's behaviour from before #14012
ChiragAgg5k commentedon Oct 7, 2026
Released in cloud 1.50.140 (contains appwrite-labs/cloud#6349).