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.
This commit is contained in:
@@ -63,8 +63,12 @@ function refresh(refreshToken) {
|
|||||||
}
|
}
|
||||||
function logout(refreshToken) { if (refreshToken) Tokens.del.run(String(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) {
|
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 <idToken> (or ?auth= for old clients) -> req.user / req.uid
|
// express middleware: Authorization: Bearer <idToken> (or ?auth= for old clients) -> req.user / req.uid
|
||||||
function middleware(req, _res, next) {
|
function middleware(req, _res, next) {
|
||||||
|
|||||||
@@ -14,7 +14,10 @@ function createRunner(opt = {}) {
|
|||||||
let n = 0;
|
let n = 0;
|
||||||
while (now >= next && n < 10) {
|
while (now >= next && n < 10) {
|
||||||
const t0 = performance.now();
|
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;
|
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`); }
|
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++;
|
next += STEP; n++;
|
||||||
|
|||||||
@@ -17,6 +17,28 @@ test('socket must authenticate with hello first', async () => {
|
|||||||
assert.equal((await bad.closed).code, 4003);
|
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 () => {
|
test('old protocol version is told to reload', async () => {
|
||||||
const a = await S.account();
|
const a = await S.account();
|
||||||
const c = await S.client(a.idToken, { v: 0, expectFail: true });
|
const c = await S.client(a.idToken, { v: 0, expectFail: true });
|
||||||
|
|||||||
@@ -36,14 +36,14 @@ function attach(server, { lobby }) {
|
|||||||
clearTimeout(helloTimer);
|
clearTimeout(helloTimer);
|
||||||
user = u;
|
user = u;
|
||||||
send({ t: 'welcome', uid: u.uid, name: u.name, serverTime: Date.now(), v: RC.Proto.VERSION });
|
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;
|
return;
|
||||||
}
|
}
|
||||||
if (msg.t === 'hello') 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', () => {});
|
ws.on('error', () => {});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user