Skip to content

core: stop users/me from erasing session data saved by concurrent requests - #26790

Merged
dominic-r merged 3 commits into
core/user-switch-keep-sessionfrom
dominic/core-session-expiry-no-clobber
Oct 8, 2026
Merged

dominic-r merged 3 commits into
core/user-switch-keep-sessionfrom
dominic/core-session-expiry-no-clobber

Conversation

@dominic-r

@dominic-r dominic-r commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

Details

This targets #26765's branch. It replaces that PR's fix with one for the cause of the flaky 102-user-switching Playwright test, and keeps the original login behavior.

What was actually going wrong

GET /api/v3/core/users/me/ set session.modified = True on every call so the session's expiry would slide (commit f28509608b). The session middleware then saved the whole session it had loaded when the request started. When the user interface's users/me call overlapped POST /api/v3/core/users/switch/ ("Add another user"), users/me wrote back its older copy and erased the flow plan that the switch request had just saved. The flow executor then planned a fresh authentication flow without the add-user marker, and login() replaced the first account's session. That is the intermittent "first account gets signed out".

Problems fixed

  1. The race itself. users/me now calls SessionStore.extend_expiry(). When that is the only change to the session, SessionMiddleware updates just expires and last_used with a single UPDATE instead of rewriting session_data. A plan, SESSION_KEY_ADD_USER, or anything else a concurrent request saves is no longer lost to users/me. The cookie is refreshed exactly as before, and a session deleted mid-request still raises SessionInterrupted.
  2. Login semantics changed by core: keep the current login when adding a user after a stale session save #26765. core: keep the current login when adding a user after a stale session save #26765 made every login on a brand with user switching behave like "Add another user". A different user signing in through the login page, a recovery or enrollment flow, or a source login would keep the previous account's session alive in the browser instead of replacing it. On a shared machine that leaves the previous person signed in. This PR restores main's rule: only explicit add-user and switch plans keep the current login.
  3. The internal-user check skipped by core: keep the current login when adding a user after a stale session save #26765. "Only internal users can be added to user switching" only runs when the add-user marker is present. core: keep the current login when adding a user after a stale session save #26765's fallback added accounts without the marker, so an external user signing in kept an internal user's session alive, which is the case that check exists to prevent. Reverting the fallback closes that.
  4. A test that didn't reproduce the race. core: keep the current login when adding a user after a stale session save #26765's test replaced the plan by hand, which is the same as an ordinary login-page login, so it pinned the new semantics rather than the concurrency bug. test_add_user_survives_concurrent_user_me now runs the add-user request while a users/me request has already loaded the session, then completes the login and checks both accounts stay live.

Tests

  • test_add_user_survives_concurrent_user_me fails without the fix (KeyError: 'authentik/flows/plan', the plan was wiped) and passes with it.
  • test_plain_login_replaces_session_with_switching_enabled fails with core: keep the current login when adding a user after a stale session save #26765's stage.py and passes on this branch, so the brand-wide fallback can't return unnoticed.
  • test_user_me_extends_expiry checks that users/me still moves expires forward and refreshes the session cookie.
  • authentik/core, authentik/root, authentik/stages/user_login, and authentik/flows: 583 passed. The one local failure is test_locales needing the msgattrib binary, which is unrelated.

Not covered

Any other request that really modifies the session while the switch request is in flight can still write back a stale copy, for example BoundSessionMiddleware recording a new IP, or / popping the add-user key in another tab. Those are rare. users/me was the frequent writer in this flow. A general fix would need per-key session writes and is out of scope here.

🤖 Generated with Claude Code

dominic-r and others added 3 commits October 8, 2026 14:18
…ches

Treating every login on a brand with user switching as an add-user login
changed what signing in as someone else means and skipped the
internal-user check for added accounts. The plan losing its marker is
fixed at its cause instead.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
users/me marked the whole session modified to slide its expiry, so it
saved the session data it loaded at the start of the request. A request
that saved the session in the meantime, like starting "Add another
user", lost its changes, and the flow then logged in without the
add-user marker and signed out the first account.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@dominic-r
dominic-r requested a review from a team as a code owner October 8, 2026 18:28
@dominic-r dominic-r self-assigned this Oct 8, 2026
@dominic-r
dominic-r merged commit 0bbcf57 into core/user-switch-keep-session Oct 8, 2026
12 of 13 checks passed
@dominic-r
dominic-r deleted the dominic/core-session-expiry-no-clobber branch October 8, 2026 18:30
@netlify

netlify Bot commented Oct 8, 2026

Copy link
Copy Markdown

✅ Deploy Preview for authentik-docs ready!

Name Link
🔨 Latest commit 6b254a0
🔍 Latest deploy log https://app.netlify.com/projects/authentik-docs/deploys/6ac7e0b24dbc690008b89729
😎 Deploy Preview https://deploy-preview-26790--authentik-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

GirlBossRush added a commit that referenced this pull request Oct 14, 2026
… save (#26765)

* core: keep the current login when adding a user after a stale session save

The add-user marker lives in the session's flow plan. A concurrent request that
saved an older copy of the session could drop it, so the login stage treated the
new login as a plain one and login() deleted the previous user's session.

When the brand has a user switch flow, logging in as another user now always
keeps the current login as a switch target.

* core: stop users/me from erasing session data saved by concurrent requests (#26790)

* stages/user_login: only keep the current login for explicit user switches

Treating every login on a brand with user switching as an add-user login
changed what signing in as someone else means and skipped the
internal-user check for added accounts. The plan losing its marker is
fixed at its cause instead.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* core: extend session expiry from users/me without rewriting session data

users/me marked the whole session modified to slide its expiry, so it
saved the session data it loaded at the start of the request. A request
that saved the session in the meantime, like starting "Add another
user", lost its changes, and the flow then logged in without the
add-user marker and signed out the first account.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* core: pin that a plain login replaces the session with user switching enabled

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>

* core: read expiry_extended directly in the session middleware (#26791)

authentik's SessionStore is the only session engine, so the getattr
fallback never applies. It only hid that save_expiry() right below is
called without one.

Co-authored-by: Claude Opus 5.5 <[email protected]>

* stages/user_login: save only the last IP when a bound session changes IP

The binding middleware set LAST_IP as a session key, which marked the
session as modified. The full save at the end of the request then wrote
back the session data loaded at its start, erasing anything a concurrent
request saved in the meantime, like the user switching flow plan.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant