Skip to content

fix: resolve the current session under JWT auth - #13900

Merged
Meldiron merged 9 commits into
appwrite:mainfrom
jaysomani:fix/jwt-session-null-mfa-current-resolution
Sep 29, 2026
Merged

Meldiron merged 9 commits into
appwrite:mainfrom
jaysomani:fix/jwt-session-null-mfa-current-resolution

Conversation

@jaysomani

@jaysomani jaysomani commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Resolves the current session under JWT auth. The session resource (app/init/resources/request.php) and the 'current' lookups in account.php only checked the cookie secret. They ignored the sessionId that the JWT already carries and that request.php already validates with sessionActive(). Under JWT, session was null and 'current' matched nothing.

Bugs covered by the new E2E tests

Every test fails on main and passes with this PR.

Test Expected On main Impact
testMFAFactorCountUnderJWT 401 user_more_factors_required 200 MFA bypass. shared/api.php guards the factor check with if ($session && ...). A JWT minted from a password-only session before MFA was enabled keeps working for its lifetime (15 min by default, 60 max), while that session's cookie is blocked.
testUpdatePasswordInvalidatesOtherSessionsUnderJWT Other sessions deleted Other sessions kept Security. Session invalidation on password change (on by default) is skipped when the password is changed with a JWT.
testMFARecencyCheckUnderJWT 401 user_challenge_required 500 Fails closed, no bypass. mfaProtected requires a non-null Document $session, so this is a TypeError. The same applies to the other routes that inject session: MFA update, challenge completion and authenticator verification.
testGetSessionCurrentUnderJWT 200, the JWT's session 404 'current' keyword unusable with JWT
testUpdateSessionCurrentUnderJWT 200, the JWT's session 404 Same
testDeleteSessionCurrentUnderJWT 204, deletes the JWT's session only 404, nothing deleted Same. Passing an explicit session ID already works.
testListSessionsCurrentUnderJWT JWT's session has current: true No session marked current Cosmetic
testCreatePushTargetReplacesRotatedTokenUnderJWT Rotated token replaces the session's target A second live target is created Duplicate push deliveries

Fix

  • session resource (app/init/resources/request.php): when the cookie secret doesn't resolve a session and an x-appwrite-jwt header is present, fall back to the JWT's sessionId. It is only used when the JWT's userId matches the resolved user and the session is still active. That condition matters when a JWT and an account API key are combined, where the resolved user may not come from the JWT.
  • Account routes now use the session resource instead of calling sessionVerify() on the cookie:
    • GET /account/sessions: current flag
    • GET, PATCH and DELETE /account/sessions/current
    • PATCH /account/password: invalidation of the other sessions
    • POST /account/targets/push: linking the target to the session and rotating tokens

Impersonation behaves as before: session belongs to the impersonator, so it never matches the target's sessions.

Behavior changes

  • JWT requests are now subject to the MFA factor check. This includes JWTs minted server-side with users.createJWT for a session with too few factors. Those now get user_more_factors_required, as a cookie request on the same session would.
  • DELETE /account/sessions/current with a JWT deletes the JWT's session, which ends that JWT as well. Session cookies are only cleared when the request came with one.
  • MFA-protected routes, MFA update, challenge completion and authenticator verification now work with a JWT instead of returning 500.

Left unchanged: POST /account/jwts (JWTs can't create JWTs), the OAuth2 redirect (browser cookie flow) and function executions (the JWT is already forwarded as-is).

Test Plan

  • AccountCustomClientTest --filter UnderJWT: 8/8 fail on main, 8/8 pass with this PR
  • Full Account (119 tests) and Users (58 tests) E2E suites pass locally
  • Pint and PHPStan pass on the changed files

Related PRs and Issues

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

Under JWT authentication, \ resolves to null via sessionVerify()
because JWT auth never populates \'s cookie secret. This silently
bypasses checks in 4 places in production today:

1. MFA recency check — JWT requests bypass MFA recency enforcement
2. MFA factor-count check — JWT requests bypass factor count enforcement
3. deleteSession('current') — returns 404, wrong session resolved
4. getSession('current') — returns 404, wrong session resolved

Tests assert expected behavior — all currently fail, confirming the bug.
Fix will thread JWT-resolved session ID through from request.php auth
resolution so all 4 spots benefit from a single change.

Draft PR — tests only
@greptile-apps

greptile-apps Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Critical risk] Changes how the system identifies the current user session under JWT authentication.

The PR appears safe to merge; no new actionable issue or outstanding previous finding remains.

Summary

The PR resolves the active session for JWT-authenticated requests, uses it in account session and password operations, and adds JWT regression coverage, including a session-deletion webhook test.

  • The latest change separates the deletion event’s current and countryName fields from cookie clearing.
  • The new webhook test checks that a JWT-authenticated deletion reports the deleted session as current.

Reviews (8) · Last reviewed commit: "fix: mark the JWT's deleted session as c..."

- testMFAFactorCountBypassedUnderJWT: create JWT from session B before
  enabling MFA so JWT creation is not blocked by the factor-count check
- testMFARecencyCheckFailsUnderJWT: rename from Bypassed — request errors
  with TypeError 500 because session resolves to null under JWT

Addresses Greptile P1 and P2 findings.
@jaysomani
jaysomani force-pushed the fix/jwt-session-null-mfa-current-resolution branch from 41dd0b2 to 9b55e3e Compare September 25, 2026 02:42
@jaysomani
jaysomani marked this pull request as ready for review September 25, 2026 02:43
@ghost

ghost commented Sep 29, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The previously reported event-payload issue is fixed, and the incremental changes introduce no identified defects.

This PR resolves the request session from an active JWT when no cookie session is available, then uses it for account session, password, and push-target operations. It adds E2E coverage for those JWT flows. The latest commits also make a JWT-authenticated deletion of the current session report current: true in its webhook payload and test that event.

Latest changes: The newest commits update the session-delete event payload's current-session flag and add a JWT webhook regression test.

Verdict New comments Fixed Still open
💬 Commented 0 1 0

Note

@jaysomani does not have write access to this repository, so Hansi does not approve automatically. A maintainer can review and approve.

📂 Walkthrough · 4
File Change
app/init/resources/request.php Falls back to the active JWT session when resolving the request session.
app/controllers/api/account.php Uses the resolved session across account routes and marks JWT current-session deletions in event payloads.
tests/e2e/Services/Account/AccountCustomClientTest.php Covers JWT authentication across MFA, password changes, sessions, and push targets.
tests/e2e/Services/ProjectWebhooks/WebhooksCustomClientTest.php Checks the webhook payload for deletion of the JWT's current session.
✅ Fixed since the last review · 1
  • Mark JWT's deleted current session as current in the event · app/controllers/api/account.php:807

Reviewed the commits since 54cb9e3 · 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 · See the inline comments. Summary

Meldiron and others added 2 commits September 29, 2026 11:36
Rename the JWT session tests to describe expected behavior, share JWT and
session setup through helpers, and add coverage for updating the current
session, marking the current session in the sessions list, and
invalidating other sessions on password change under JWT.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@Meldiron Meldiron changed the title test: add failing E2E tests proving JWT session null bug test: cover session resolution under JWT auth Sep 29, 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 · See the inline comments. Summary

Meldiron and others added 3 commits September 29, 2026 11:55
The session resource only resolved from the cookie secret, so JWT requests
had no session. MFA factor enforcement was skipped, MFA-protected routes
crashed, and 'current' session lookups, current-session flags, password
change invalidation and push target rotation all missed the JWT's session.
Fall back to the JWT's session ID and use the session resource in those
account routes.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ution

fix: resolve the current session under JWT auth
@Meldiron Meldiron changed the title test: cover session resolution under JWT auth fix: resolve the current session under JWT auth Sep 29, 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 A · See the inline comments. Summary

Comment thread app/controllers/api/account.php Resolved
@Meldiron
Meldiron merged commit 3188119 into appwrite:main Sep 29, 2026
95 of 97 checks passed
Meldiron pushed a commit that referenced this pull request Sep 29, 2026
Adds optional bool param current (default true) to DELETE /account/sessions.
When false, all sessions except the calling session are deleted, allowing
sign-out everywhere while staying signed in on the current device.

Uses the session resource from #13900 so JWT auth works correctly for
both cookie and JWT authenticated callers.

X-Fallback-Cookies header is now only sent when current=true to avoid
wiping the caller's stored session cookie when signing out other sessions.

E2E tests cover cookie and JWT auth paths with three-session setup
per spec: other two sessions get 401, calling session stays 200,
listSessions returns exactly one session marked current.
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.

2 participants