Skip to content

Restore token-flow OAuth2 for SSR and add optional state and PKCE #14156

Closed

Description

@ChiragAgg5k

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 state param on createOAuth2Token and createOAuth2Session, capped at 256 characters. The whole provider state is validated as Text(2048) at the callback, and the app's value travels inside it.
  • Store it inside the provider state under 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, not nonce. In OpenID Connect, nonce is 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 codeChallenge and codeChallengeMethod params on createOAuth2Token. Only S256 is accepted (RFC 9700 §2.1.1).
  • The challenge is carried in state and stored on the token created at the callback. This needs a new attribute on the tokens collection, plus a migration for existing projects.
  • New optional codeVerifier param on createSession. 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 setSession on a server SDK).
  • Record each pending flow server-side, keyed by a random state id: intent (signin or link), 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), Firebase linkWithRedirect.

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 with userId and secret. This fails on main today.
  • Session flow: the existing tests (around lines 3040 and 3062) still expect user_oauth2_state_invalid without the cookie.
  • State: with state set, 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 state and 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 pkce also be accepted on createOAuth2Session? It has no redemption step, so it would have no effect there.
  • Final param names: codeChallenge, codeChallengeMethod, codeVerifier.

Out of scope

Activity

  1. self-assigned this
    on Oct 6, 2026
  2. added
    product / auth
    product / console
    product / databases
    product / migrations
    product / vcs
    on Oct 6, 2026
  3. changed the title [-]Add OAuth2 nonce project policy (off by default)[/-] [+]Restore token-flow OAuth2 for SSR and add optional nonce and PKCE[/+] on Oct 6, 2026
  4. changed the title [-]Restore token-flow OAuth2 for SSR and add optional nonce and PKCE[/-] [+]Restore token-flow OAuth2 for SSR and add optional state and PKCE[/+] on Oct 6, 2026
  5. ChiragAgg5k commented on Oct 7, 2026

    @ChiragAgg5k
    MemberAuthor

    Released in cloud 1.50.140 (contains appwrite-labs/cloud#6349).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

product / authproduct / consoleproduct / databasesproduct / migrationsproduct / vcs

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions