fix: disconnect a disabled user's open LiveViews #54
No reviewers
Labels
No labels
bug
critical
ebook-branch
high
low
main
medium
release-blocker
security
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
jfro/fuzzy_catalog!54
Loading…
Reference in a new issue
No description provided.
Delete branch "jtk/disconnect-disabled-users"
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?
Disabling a user deleted their session tokens, but LiveViews they already had open stayed connected, because
log_in_usernever put:live_socket_idin the session. With nothing in the session, no disconnect broadcast could ever reach them.Change
UserAuth.put_token_in_session/2now also sets:live_socket_idto"users_sessions:#{Base.url_encode64(token)}", the topic Phoenix's socket listens on. This covers every login path (password, magic link, OIDC, session reissue), since they all go throughcreate_or_extend_session.UserAuth.disconnect_sessions/1takes the deletedUserTokens, broadcasts"disconnect"for each session token, and logs how many it disconnected and for which user ID.Accounts.update_user_by_admin/2now returns{:ok, {user, expired_tokens}}, the same shape asupdate_user_password/2. Disabling returns the deleted tokens; other changes return[].AdminUsersLivepasses them todisconnect_sessions/1. The broadcast lives in the web layer, soAccountsdoesn't call the endpoint.log_out_user/1already broadcast on:live_socket_id, so logging out now disconnects too.Tests
log_in_userstores the right:live_socket_id.disconnect_sessions/1broadcasts on each session topic and skips non-session tokens.AdminUsersLive: disabling a user broadcasts disconnect on their topic; changing their role doesn't. This test failed before the fix.update_user_by_adminreturn shape.mix precommitpasses: 542 tests, 0 failures.Fixes #36
Note for release: sessions created before this deploys have no
:live_socket_id, so their open LiveViews won't get the disconnect until the user logs in again or the session token is reissued.log_in_user never put :live_socket_id in the session, so nothing could broadcast a disconnect to a user's LiveViews. Store it next to the session token, add UserAuth.disconnect_sessions/1, and call it with the deleted tokens when an admin disables a user (and on password change). update_user_by_admin/2 now returns {user, expired_tokens}, like update_user_password/2. Fixes #36Review from Nye (approved)
Checked: topic matches what Phoenix.LiveView.Socket reads from session "live_socket_id" and what log_out_user already broadcasts on; every login path goes through create_or_extend_session; reissue leaves the old token in the DB so disable still reaches the old topic; disabled-user mount fails since verify_session_token_query requires status active; no update_user_by_admin callers left on the old shape. Password-change disconnect in this PR is fine.
Nits (non-blocking):