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
+33
View File
@@ -71,6 +71,9 @@ function createMockDb() {
const addrIdx = params.findIndex((_, i) => text.includes(`payout_address = $${i + 1}`));
if (addrIdx >= 0) user.payout_address = params[addrIdx];
}
if (text.includes('anon_token =')) {
user.anon_token = params[0];
}
}
return { rows: user ? [user] : [] };
}
@@ -239,6 +242,36 @@ describe('SQL Injection Prevention', () => {
}
});
test('should rotate the session token on resume (bearer-token replay protection)', async () => {
// Create a session to obtain a real token.
const created = await sessionManager.resumeOrCreate({
socketId: 'rotate-socket-1',
ipAddress: '127.0.0.1',
resumeToken: null
});
expect(created.resumed).toBe(false);
const firstToken = created.token;
expect(firstToken).toBeTruthy();
// Resume with that token — should succeed and hand back a DIFFERENT token.
const resumed = await sessionManager.resumeOrCreate({
socketId: 'rotate-socket-2',
ipAddress: '127.0.0.1',
resumeToken: firstToken
});
expect(resumed.resumed).toBe(true);
expect(resumed.token).toBeTruthy();
expect(resumed.token).not.toBe(firstToken); // rotated
// The OLD token must no longer resume (it was invalidated).
const replay = await sessionManager.resumeOrCreate({
socketId: 'rotate-socket-3',
ipAddress: '127.0.0.1',
resumeToken: firstToken
});
expect(replay.resumed).toBe(false);
});
test('should safely handle malicious socket ID', async () => {
const maliciousSocketId = "'; DROP TABLE users; --";