Fix logout not clearing prefixed NextAuth cookies over HTTPS #220
No reviewers
Labels
No labels
backend
bug
CI
dependencies
design
documentation
duplicate
enhancement
frontend
github_actions
good first issue
help wanted
invalid
javascript
question
refactoring
security
tooling
UX
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
NBIS/sda-download-ui!220
Loading…
Reference in a new issue
No description provided.
Delete branch "bugfix/clear-sucure-cookies"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Follow-up fix to #199 .
Problem
When the app runs over HTTPS, pressing Logout does not clear the nextauth session from the browser. Over HTTP (dev), the same code worked correctly because they are not attributed as secure.
Root cause
cookies().delete(name)emitsSet-Cookie: <name>=; Max-Age=0; Path=/without theSecureattribute. Browsers reject anySet-Cookiefor a__Host-or__Secure-prefixed cookie that isn't markedSecure- including deletion headers. The deletion is silently dropped and the cookie stays.The same bug affected
updateServerToken's fail-closed branch (when access-token verification proves the session is invalid): the cookie wasn't actually being cleared in HTTPS environments.Fix implemented
Replace
store.delete(name)inserverToken.tswith an explicit expiredstore.set(...)that carriesSecurefor prefixed cookie names, so the browser accepts the deletion.Changes:
expireCookiehelper that writes an expired cookie with the correct attributes (Securewhen the name is prefixed,Path=/,HttpOnly,SameSite=Lax,Max-Age=0).clearServerTokenand the fail-closed branch ofupdateServerTokennow useexpireCookieinstead ofstore.delete.store.set(name, "", { maxAge: 0, secure: true, ... })for prefixed names, andmaxAge: 0(without requiringsecure) for non-prefixed names.Testing
The bugfix is deployed on the demo site. Go there to test:
I'll start a review...
This looks good to me, I've tested it on the demo site and seen it working. 💯