Skip to content

(fix): handle non-JSON Google OAuth2 responses - #13968

Merged
ChiragAgg5k merged 2 commits into
mainfrom
fix/google-oauth2-null-user
Sep 29, 2026
Merged

ChiragAgg5k merged 2 commits into
mainfrom
fix/google-oauth2-null-user

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

Fixes Sentry CLOUD-3QWQ (https://appwrite.sentry.io/issues/7713484379/): TypeError: Cannot assign null to property Appwrite\Auth\OAuth2\Google::$user of type array on /v1/account/sessions/oauth2/:provider/redirect (162 events).

Cause

OAuth2::request() returns an empty string when curl fails (HTTP code 0), and Google can also return a non-JSON body. json_decode() then returns null, which cannot be assigned to the typed array $user / array $tokens properties.

Fix

  • getUser() falls back to [], so getUserID() returns '' and the redirect takes its existing USER_MISSING_ID failure redirect instead of a 500.
  • getTokens() and refreshTokens() throw the OAuth2 provider exception when the response isn't JSON or has no access_token. The redirect's existing catch turns that into the provider-failure redirect, and a failed session refresh no longer overwrites the stored provider credentials with an empty access token.

Many other providers use the same $this->user = json_decode(...) pattern and can hit this too; this PR only fixes Google, the one reported.

…ON response

A failed or empty userinfo/token response makes json_decode return null, which cannot be assigned to the typed array properties and crashes the OAuth2 redirect with a TypeError. Falling back to [] lets the controller reach its existing USER_MISSING_ID failure redirect.
@ghost

ghost commented Sep 29, 2026 •

Copy link
Copy Markdown

🔵 Tier A · Mergeable after minor fixes

The refresh credential-loss issue is fixed, but the previously reported missing regression coverage remains open.

This PR makes Google OAuth2 user-info decoding fall back to an empty array when the response is not JSON. The latest commit also validates token-exchange and refresh responses, raising a provider exception rather than accepting a missing access token.

Latest changes: The newest commit adds a shared token-response parser and uses it for both Google token exchange and refresh.

Verdict New comments Fixed Still open
✅ Approved 0 1 1
📂 Walkthrough · 1
File Change
src/Appwrite/Auth/OAuth2/Google.php Falls back to an empty user on undecodable user info and rejects token responses without an access token.
⏳ Still open from earlier reviews · 1
✅ Fixed since the last review · 1
  • Do not persist empty credentials after a failed refresh · src/Appwrite/Auth/OAuth2/Google.php:101

Reviewed the commits since 1f72af4 · 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 B · 1 blocking finding to address. Summary

Comment thread src/Appwrite/Auth/OAuth2/Google.php Resolved
Comment thread src/Appwrite/Auth/OAuth2/Google.php Resolved
@greptile-apps

greptile-apps Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Critical risk] Changes how Google OAuth2 token responses are validated.

The PR appears safe to merge.

Summary

The PR handles invalid Google OAuth token responses and non-JSON userinfo responses.

  • Token exchange and refresh now reject responses without an access token.
  • Non-JSON userinfo responses produce an empty user, allowing the existing missing-ID failure path.
  • Both previous findings were fixed by the token-response rejection and their threads are resolved.

Reviews (2) · Last reviewed commit: "(fix): fail Google token exchange and re..."

Comment thread src/Appwrite/Auth/OAuth2/Google.php Resolved
Comment thread src/Appwrite/Auth/OAuth2/Google.php Resolved
@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/google-oauth2-null-user (after).

Metric Before After Change
🚀 Requests/sec 183.07 197.89 🟢 +8.1%
⏱️ Latency P50 92.5 ms 88.1 ms ⚪ -4.7%
⏱️ Latency P95 224.84 ms 203.26 ms 🟢 -9.6%
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.1 203.26 12,540 197.89 -21.58
Account 165.02 314.13 660 10.91 -28.62
TablesDB 83.63 162.18 6,820 109.19 -10.86
Storage 84.9 178.31 3,300 54.8 -27.56
Functions 129 243.77 1,760 29.92 -18.75

Top API waits (after)

API request Max wait (ms)
account.prefs.update 448.14
functions.variables.update 426.26
functions.create 410.75
account.name.update 406.99
storage.buckets.create 404.87

Defaulting a malformed token response to [] let a failed refresh overwrite the session's stored provider credentials with an empty access token, and let a failed code exchange continue to the userinfo call outside the controller's OAuth2 catch. Throwing the provider exception instead keeps the session untouched on refresh and routes the redirect to its provider-failure path.

@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 A · Looks good to merge. Summary

@ChiragAgg5k
ChiragAgg5k merged commit bc0c817 into main Sep 29, 2026
63 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the fix/google-oauth2-null-user branch September 29, 2026 08:47
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