{"record":{"id":"b11d19a676a5f9c1","repo":"toeverything/AFFiNE","slug":"invalid-oauth-callback-state","errorCode":"invalid_oauth_callback_state","errorMessage":"Invalid callback state parameter.","messagePattern":"Invalid callback state parameter\\.","errorType":"exception","errorClass":"InvalidOauthCallbackState","httpStatus":400,"severity":"error","filePath":"packages/backend/server/src/plugins/oauth/service.ts","lineNumber":103,"sourceCode":"  }\n\n  async preflight(input: {\n    provider: OAuthProviderName;\n    redirectUri?: string;\n    client: string;\n    clientNonce: string;\n    clientVersion?: string;\n    callbackUrl: string;\n    redirectBaseUrl: string;\n    redirectAllowedOrigins: string[];\n    redirectTrustedDomains: string[];\n  }) {\n    return await this.call<{ url: string }>({\n      action: 'oauth_preflight',\n      ...input,\n    });\n  }\n\n  async callback(input: {\n    code: string;\n    state: string;\n    clientNonce?: string;\n    issue: SessionIssueInput;\n  }) {\n    return await this.call<NativeOAuthCallback>({\n      action: 'oauth_callback',\n      ...input,\n    });\n  }\n\n  private async call<T>(input: Record<string, unknown>) {\n    try {\n      return await this.runtime.executeAuthSessionCommandV1<T>(input);\n    } catch (error) {\n      const message = String(error);\n      if (message.includes('unknown_oauth_provider')) {","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/toeverything/AFFiNE/blob/d897bb3d84099e54a6b3c0bd5f4265f8aa87d190/packages/backend/server/src/plugins/oauth/service.ts#L85-L121","documentation":"InvalidOauthCallbackState thrown at packages/backend/server/src/plugins/oauth/service.ts:96 when the state posted to /api/oauth/callback is not, after unwrapping, a 36-character UUID. AFFiNE's preflight returns a JSON envelope {state, client, provider, pkce} whose inner state is a 36-char UUID stored server-side; verifyCallback unwraps envelopes longer than 36 chars via OAuthStateEnvelopeSchema and then requires exactly 36 chars (isValidState).","triggerScenarios":"POST /api/oauth/callback with a state that is empty, truncated, double-encoded, a raw non-envelope string, or an envelope whose inner state is not the 36-char UUID; client echoing the IdP's query-string state through a URL-decode that corrupts it; tampered or fabricated state.","commonSituations":"Client sends only the inner state UUID when using the envelope flow incorrectly, or re-serializes the envelope (key order/quotes) so JSON.parse or schema parse fails and the raw >36 string is then rejected; mobile deep-link handoff losing part of the state; test scripts posting arbitrary state.","solutions":["Send back the exact state string returned by POST /api/oauth/preflight (the full JSON envelope) as the state field of /api/oauth/callback.","Do not re-encode or truncate the state between preflight and callback; treat it as an opaque string.","Log the received stateStr length — anything other than 36 after envelope unwrap fails.","If writing a custom client, run preflight, keep the envelope, and post {code, state, client_nonce} unchanged."],"exampleFix":"// before (custom client)\nawait fetch('/api/oauth/callback', { method: 'POST', body: JSON.stringify({ code, state: stateUuidOnly }) });\n\n// after\nconst { url } = await (await fetch('/api/oauth/preflight', { method: 'POST', body: JSON.stringify({ provider, client, client_nonce }) })).json();\n// ...user completes login, IdP redirects with code + state (the envelope)...\nawait fetch('/api/oauth/callback', { method: 'POST', body: JSON.stringify({ code, state: envelopeFromIdpRedirect, client_nonce }) });","handlingStrategy":"validation","validationCode":"// Client-side guard before posting the callback\nfunction isValidStateEnvelope(stateStr: unknown): boolean {\n  if (typeof stateStr !== 'string' || stateStr.length === 0) return false;\n  try {\n    const parsed = JSON.parse(stateStr);\n    return typeof parsed.state === 'string' && parsed.state.length === 36;\n  } catch {\n    return stateStr.length === 36;\n  }\n}\nif (!isValidStateEnvelope(stateFromIdpRedirect)) throw new Error('state corrupted in redirect');","typeGuard":null,"tryCatchPattern":"try {\n  const res = await fetch('/api/oauth/callback', { method: 'POST', body: JSON.stringify({ code, state, client_nonce }) });\n} catch (err) {\n  if (err.code === 'invalid_oauth_callback_state') {\n    // restart the flow from /api/oauth/preflight with a fresh state\n  }\n}","preventionTips":["Treat the preflight state as an opaque string; never re-serialize or trim it.","Carry the state through the IdP redirect without URL double-encoding.","If the state looks corrupted, silently restart login instead of posting garbage."],"tags":["oauth","state-parameter","callback","authentication"],"backgroundTag":"oauth-invalid-callback-state","analyzedSha":"d897bb3d84099e54a6b3c0bd5f4265f8aa87d190","analyzedAt":"2026-08-18T21:16:52.546Z","contentChangedAt":"2026-08-18T21:16:52.546Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}