From 3dc770e34aedfa57d5f19d6a234a67cf216f4ed3 Mon Sep 17 00:00:00 2001 From: Benjamin Oezcan Date: Thu, 19 Nov 2026 10:02:51 +0100 Subject: [PATCH] fix(server): tokens of deleted accounts are refused; a failing message or fight can no longer crash the server After a database reset an old (still signed) token made economy.load insert a save for a missing user; the FOREIGN KEY error escaped the WebSocket handler and took the process down in a restart loop. --- server/auth.js | 6 +++++- server/match/runner.js | 5 ++++- server/test/lobby.test.js | 22 ++++++++++++++++++++++ server/ws/hub.js | 6 +++--- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/server/auth.js b/server/auth.js index 8625db2..0c20b20 100644 --- a/server/auth.js +++ b/server/auth.js @@ -63,8 +63,12 @@ function refresh(refreshToken) { } function logout(refreshToken) { if (refreshToken) Tokens.del.run(String(refreshToken)); } +// a token only counts while its account exists (deleted account / database reset -> the client must log in again) function verify(token) { - try { const d = jwt.verify(String(token || ''), SECRET); return { uid: d.uid, name: d.name, guest: !!d.guest, admin: d.role === 'admin' }; } catch (e) { return null; } + let d; + try { d = jwt.verify(String(token || ''), SECRET); } catch (e) { return null; } + const u = Users.byId.get(d.uid); + return u ? { uid: u.uid, name: u.name, guest: !!u.guest, admin: u.role === 'admin' } : null; } // express middleware: Authorization: Bearer (or ?auth= for old clients) -> req.user / req.uid function middleware(req, _res, next) { diff --git a/server/match/runner.js b/server/match/runner.js index aacee77..0b995e0 100644 --- a/server/match/runner.js +++ b/server/match/runner.js @@ -14,7 +14,10 @@ function createRunner(opt = {}) { let n = 0; while (now >= next && n < 10) { const t0 = performance.now(); - for (const room of rooms.values()) room.tick(); + for (const room of rooms.values()) { + // one broken fight must never stop the others (or the server): end it as a draw and log it + try { room.tick(); } catch (e) { console.error('[match] room', room.id, 'crashed', e); try { room.finish({ winner: -1, reason: 'time' }); } catch (e2) { rooms.delete(room.id); } } + } const ms = performance.now() - t0; if (ms > 8 && Date.now() - slowLogged > 10000) { slowLogged = Date.now(); console.warn(`[match] slow tick ${ms.toFixed(1)} ms for ${rooms.size} rooms`); } next += STEP; n++; diff --git a/server/test/lobby.test.js b/server/test/lobby.test.js index ef2441f..c8903a6 100644 --- a/server/test/lobby.test.js +++ b/server/test/lobby.test.js @@ -17,6 +17,28 @@ test('socket must authenticate with hello first', async () => { assert.equal((await bad.closed).code, 4003); }); +test('a token of a deleted account is refused - and the server keeps running', async () => { + const a = await S.account(); + await S.json('DELETE', '/api/me', undefined, a.idToken); + const c = await S.client(a.idToken, { expectFail: true }); + assert.equal((await c.next('error')).code, 'UNAUTH'); + assert.equal((await S.json('GET', '/api/online', undefined, a.idToken)).status, 401); + assert.equal((await S.json('GET', '/healthz')).status, 200); +}); + +test('a failing message handler answers with an error instead of crashing', async () => { + const a = await S.account(); + const c = await S.client(a.idToken); + const orig = S.lobby.handle; + S.lobby.handle = () => { throw new Error('boom'); }; + try { + c.send({ t: 'room', room: 'DE' }); + assert.equal((await c.next('error')).code, 'SERVER_ERROR'); + } finally { S.lobby.handle = orig; } + assert.equal((await S.json('GET', '/healthz')).status, 200); + c.close(); +}); + test('old protocol version is told to reload', async () => { const a = await S.account(); const c = await S.client(a.idToken, { v: 0, expectFail: true }); diff --git a/server/ws/hub.js b/server/ws/hub.js index 988f9a2..6524230 100644 --- a/server/ws/hub.js +++ b/server/ws/hub.js @@ -36,14 +36,14 @@ function attach(server, { lobby }) { clearTimeout(helloTimer); user = u; send({ t: 'welcome', uid: u.uid, name: u.name, serverTime: Date.now(), v: RC.Proto.VERSION }); - lobby.join({ uid: u.uid, name: u.name, admin: u.admin, send }); + try { lobby.join({ uid: u.uid, name: u.name, admin: u.admin, send }); } catch (e) { console.error('[ws] join failed', e); bye(1011, 'SERVER_ERROR'); } return; } if (msg.t === 'hello') return; - lobby.handle(user.uid, msg); + try { lobby.handle(user.uid, msg); } catch (e) { console.error('[ws] handler failed for', msg.t, e); send({ t: 'error', code: 'SERVER_ERROR' }); } }); - ws.on('close', () => { clearTimeout(helloTimer); if (user) lobby.leave(user.uid, send); }); + ws.on('close', () => { clearTimeout(helloTimer); if (user) { try { lobby.leave(user.uid, send); } catch (e) { console.error('[ws] leave failed', e); } } }); ws.on('error', () => {}); });