Skip to content

fix(server): authenticate websocket upgrades via session cookie - #21

Open
Antisophy wants to merge 1 commit into
CyberShadow:masterfrom
Antisophy:fix/websocket-cookie-auth
Open

fix(server): authenticate websocket upgrades via session cookie#21
Antisophy wants to merge 1 commit into
CyberShadow:masterfrom
Antisophy:fix/websocket-cookie-auth

Conversation

@Antisophy

@Antisophy Antisophy commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

WebKit on old iOS Safari never attaches the basic-auth Authorization header to WebSocket upgrade requests, so /ws got a 401 on every attempt and the UI hung at "Connecting" forever.

Generate a random token from /dev/urandom when the transport's auth credentials are configured, hand it to clients as an HttpOnly session cookie on the first authenticated page response, and accept that cookie in checkAuth as an alternative to basic auth. Browsers do attach cookies to WebSocket handshakes, so the upgrade authenticates after one authenticated page load. No frontend changes needed.

The spec drives an authenticated page load, then replays the handshake condition directly: an upgrade carrying only the session cookie is accepted, and one carrying nothing stays rejected.

WebKit on old iOS Safari never attaches the basic-auth Authorization
header to WebSocket upgrade requests, so /ws got a 401 on every attempt
and the UI hung at "Connecting" forever.

Generate a random token from /dev/urandom when the transport's auth
credentials are configured, hand it to clients as an HttpOnly session
cookie on the first authenticated page response, and accept that cookie
in checkAuth as an alternative to basic auth. Browsers do attach
cookies to WebSocket handshakes, so the upgrade authenticates after one
authenticated page load. No frontend changes needed.

The spec drives an authenticated page load, then replays the handshake
condition directly: an upgrade carrying only the session cookie is
accepted, and one carrying nothing stays rejected.
@CyberShadow

Copy link
Copy Markdown
Owner

The HTTP basic mechanism has a few flaws (incl. that a cached client will fail to connect as the credentials dialog will not show), but cookie based sessions need more consideration than this - e.g. a proper implementation likely involves switching to a classical login form, and more importantly, a new logout button in the UI to invalidate the session server side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants