security: move session token to handshake auth and rotate on resume

The anonymous session token is a bearer credential that was sent in the
Socket.IO handshake query string, where it leaks into proxy/access/referer
logs and is easily observed. Anyone holding it becomes that user (credits,
payout address, pending payments).

- Client now sends the token in the handshake `auth` payload instead of the
  query string. Server reads `auth` first and falls back to the query string
  for backward compatibility during rollout.
- The token is rotated on every successful resume, so a leaked/observed
  token is invalidated the next time the legitimate owner reconnects. The
  client already persists the token returned in `session_resumed`.

Adds a security test asserting resume returns a new token and the old token
no longer resumes. Full suite: 12 suites / 116 tests.
This commit is contained in:
jwinterm
2026-05-31 09:29:20 -04:00
parent d07967c3d1
commit 223aa75871
5 changed files with 59 additions and 4 deletions
+5 -1
View File
@@ -172,7 +172,11 @@ class ConnectionHandler {
_getResumeToken(socket) {
try {
return socket.handshake.query?.resumeToken || null;
// Prefer the handshake `auth` payload (not logged by proxies); fall back to the
// query string for backward compatibility with older clients during rollout.
return socket.handshake.auth?.resumeToken
|| socket.handshake.query?.resumeToken
|| null;
} catch (_) {
return null;
}
+15 -1
View File
@@ -53,10 +53,24 @@ class SessionManager {
}
}
// SECURITY: Rotate the token on every resume. The session token is a bearer
// credential — rotating it limits the window in which a leaked/observed token is
// usable (a leaked token is invalidated the next time the real owner reconnects).
// The client persists the returned token from the `session_resumed` event.
let issuedToken = resumeToken;
try {
issuedToken = await this.rotateToken(user.id);
user.anon_token = issuedToken;
this.sessions.set(socketId, user);
} catch (rotErr) {
// If rotation fails, keep the existing token rather than locking the user out.
console.error('[SessionManager] Token rotation failed, keeping existing token:', rotErr.message);
}
if (this.debugManager.CONSOLE_LOGGING) console.log(`[SessionManager] Resumed session for user ${user.id}`);
return {
resumed: true,
token: resumeToken,
token: issuedToken,
user: {
...user,
socket_id: socketId