fix: persist session cookie to disk to prevent PWA logout#23746
Open
fix: persist session cookie to disk to prevent PWA logout#23746
Conversation
The session cookie (coder_session_token) was created without Expires or MaxAge, making it a transient session cookie that only lives in memory. In a regular browser this works fine because the browser process rarely fully terminates. However, the /agents PWA runs with display: standalone in its own isolated browser process. When mobile OSes kill that process (e.g. user swipes the app away), in-memory cookies are purged — forcing an unexpected re-login even though the server-side API key is still valid. Setting MaxAge on the cookie to match the API key's LifetimeSeconds makes it a persistent cookie (written to disk) that survives process kills. This is safe because: - The server already validates ExpiresAt on every request - The server already refreshes expiry on activity - The security model is unchanged; MaxAge just controls disk persistence
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
/agentsPWA logs users out frequently when the app is backgrounded or swiped away, even though the normal site doesn't.Root Cause
The
coder_session_tokencookie was created withoutExpiresorMaxAge, making it a transient session cookie that only lives in memory:display: standalone): Runs in its own isolated browser process. When mobile OSes kill that process (user swipes the app away), in-memory cookies are purged — forcing re-login even though the server-side API key is still valid (default 24h, refreshed on activity).Fix
Set
MaxAgeon the cookie to match the API key'sLifetimeSeconds, making it a persistent cookie written to disk.This is safe because:
ExpiresAton every requestDisableSessionExpiryRefreshis set)MaxAgeonly controls whether the browser writes the cookie to disk vs. keeping it in memoryAnalysis details
Cookie creation path
coderd/apikey.go:createAPIKey()→ used by login, OAuth flows, and workspace app token issuance.Session lifetime flow
apikey.Generate()setsLifetimeSecondsfromSessions.DefaultDuration(default 24h)ValidateAPIKey()inhttpmw/apikey.gorefreshesExpiresAtin the DB whenExpiresAt - now <= lifetime - 1hWhy the normal site isn't affected
Regular browser tabs share a long-lived browser process. Session cookies persist across tab closes and most browsers restore them on restart. The standalone PWA gets its own WebKit/Chromium instance that the OS can terminate independently.