A socket joins its user's room, and an admin's the admin room, once at
connect time, so logging out or revoking a user's sessions left their open
sockets receiving notifications and the log stream. Each socket is now
recorded against the login session that opened it, and removing a session
disconnects those sockets on whichever worker holds them.
The main server takes one disconnect handler, so it moves next to the
connect handler and runs the activity cleanup as well.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`update_user` cleared only the caller's session, so an admin resetting a
compromised account left every attacker session on it live. It now revokes the
sessions of the account the change was applied to, keyed on the username the
account held before the update, and still clears the caller's own session when
they are that account. `set_user_new_password` already did this; the update
endpoint was the gap.
A revocation could also be undone by any request already in flight: the session
middleware rewrote its copy unconditionally on the way out. An existing session
now refreshes with `SET ... XX`, so a record deleted mid-request is not
recreated, and the response clears the cookie instead.
`clear_user_sessions` also builds its delete keys from decoded members. The
configured clients pass `decode_responses=True`, so this changes nothing in
production, but the key is now right for any client, and the whole set goes in
one `delete` rather than a call per session.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NouZquWDxNBqxYyu2E2GF8
Logging in failed once with a CSRF error and succeeded on retry.
`_csrf_tokens_match` validates the user id embedded in the token against
the current request's user. A browser holding a token bound to user N,
whose server-side session is gone (container restart, session expiry,
Redis flush), makes an anonymous login POST. N != None, so the request is
rejected. The rejection response then rotates the cookie, which is why the
second attempt works.
Keep the double-submit check and the cookie/header user agreement
unconditionally, and enforce the caller binding only when there is a
caller. An anonymous request cannot be forged this way regardless: an
attacker cannot read the cookie cross-origin, so it cannot produce the
matching header.
This is the mirror of #3420, which fixed the authenticated direction by
rotating the cookie on safe requests. That path is untouched here, and
test_authenticated_user_binding_still_enforced pins it.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
ASGI spec only allows headers on the http.response.start message;
appending Set-Cookie to body messages is out-of-spec and may break on
some servers. Early-return for non-start messages.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>