Skip to content

fix(account): bind the OAuth2 state to the user who started the flow - #15029

Merged
ChiragAgg5k merged 3 commits into
mainfrom
fix/oauth2-state-binding
Oct 29, 2026
Merged

ChiragAgg5k merged 3 commits into
mainfrom
fix/oauth2-state-binding

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

The OAuth2 callback accepted any nonce cookie matching the state, and a site sharing the parent domain can set that cookie for another browser. The state now carries an HMAC of the nonce and the user who started the flow, and the callback checks it against its own user, so a flow started elsewhere cannot attach its identity to the signed-in account.

Rollout grace: a flow started on a pod without this change has no binding in its state. A guest callback accepts such a state, so console and project OAuth2 logins in flight during a deploy (the nonce lives up to an hour) still finish; the binding protects nothing for guests anyway, since every guest gets the same one. A callback from a signed-in browser (identity linking) still requires a matching binding, and any binding present is always verified. testCreateOAuth2SessionRejectsPlantedNonceCookie covers both: a state stripped of its binding is refused for the signed-in browser and completes for a guest holding the nonce.

Lint, PHPStan and the touched unit tests passed locally before rebasing on main; e2e runs in CI.

Related: appwrite-labs/cloud#6666

The OAuth2 callback accepted any nonce cookie matching the state, and
a site sharing the parent domain can set that cookie for another
browser. The state now carries an HMAC of the nonce and the user who
started the flow, and the callback checks it against its own user, so
a flow started elsewhere cannot attach its identity to the signed-in
account.
@luxor-code-review

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

Copy link
Copy Markdown

Review complete. No issues found — approved ✅.


This PR hardens the OAuth2 session and token flows against the planted-nonce-cookie attack: the flow start now computes hash_hmac('sha256', nonce:user, key) and carries it in state as binding, and the callback rejects any request whose binding does not match the currently authenticated user, on top of the existing nonce-cookie check. An e2e test proves a signed-in browser carrying a nonce planted by another browser is redirected to failure with user_oauth2_state_invalid and gains no identity.

Files Change
app/controllers/api/account.php Adds the user injection and HMAC binding field to both OAuth2 flows and enforces it at callback verification.
tests/e2e/Services/Account/AccountCustomClientTest.php Adds testCreateOAuth2SessionRejectsPlantedNonceCookie covering the planted-cookie replay rejection end to end.

One minor note below threshold: the binding falls back to an empty HMAC key when _APP_OPENSSL_KEY_V1 is unset, silently losing the protection instead of failing; a reviewer comment (not a blocker) recommends requiring the key. Note: one specialist scan (OAuth binding domain) produced no output; its focus areas were covered by the security, API, and sweep scans.

Reviewed commit: aae0468

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

ghost commented Oct 27, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

OAuth2 session and token flows now include an HMAC binding between the nonce and the initiating user, which the callback validates alongside the nonce cookie. Guest callbacks can also complete legacy flows whose state predates the binding. Account E2E coverage checks planted-cookie callbacks and legacy-state behavior.

Latest changes: The newest commit allows guest callbacks to finish unbound legacy states and adds E2E checks that signed-in callbacks still reject those states.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 2
File Change
app/controllers/api/account.php Adds user-bound HMACs to OAuth2 session and token states, validates the binding in callbacks, and supports unbound legacy states for guests.
tests/e2e/Services/Account/AccountCustomClientTest.php Covers rejection of planted nonce cookies and verifies legacy unbound callbacks fail for signed-in users but complete for guests.

Reviewed the commits since aae0468 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

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

ghost
ghost previously requested changes Oct 27, 2026

@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 app/controllers/api/account.php Resolved
@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/oauth2-state-binding (after).

Metric Before After Change
🚀 Requests/sec 188.83 199.9 🟢 +5.9%
⏱️ Latency P50 90.33 ms 86.87 ms ⚪ -3.8%
⏱️ Latency P95 212.09 ms 202.72 ms ⚪ -4.4%
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 86.87 202.72 12,540 199.9 -9.37
Account 165.47 306.34 660 11.23 +0.88
TablesDB 83.54 161.43 6,820 111.71 +0.63
Storage 81.13 190.75 3,300 55.62 -17.54
Functions 121.08 231.59 1,760 30.38 -28.51

Top API waits (after)

API request Max wait (ms)
account.name.update 453.45
account.prefs.update 394.79
tablesdb.rows.create 393.23
functions.create 387.22
tablesdb.rows.decrement 360.88

@ghost
ghost dismissed their stale review October 27, 2026 16:05

The findings that requested changes are resolved. See the summary comment for what is still open.

Flows started on a pod without the state binding and finished on one
with it were refused, so every console and project OAuth2 login in
flight during a rollout failed for up to the nonce's hour.

A guest callback gains nothing from the binding, since every guest
shares the same one, so it now accepts a state without it. A callback
from a signed-in browser still needs a matching binding, and any
binding present is still checked.

@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

@ChiragAgg5k
ChiragAgg5k merged commit 258bec9 into main Oct 29, 2026
35 of 36 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the fix/oauth2-state-binding branch October 29, 2026 10:34
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