The tile gate no longer takes dbLock or runs a SQL session JOIN per tile: a published in-memory view (server/sessions.nim) holds token hashes -> player/role and one explored bitmap per player. Sessions load from the DB at startup and are updated by join, key replacement and player removal in the same lock as the write; refreshed cells publish before the ack. The gate is one snapshot read (parse + session + bit test + redirect) over scalar data only, so a tile burst is answered while a mutation holds dbLock. Evidence: tests/tsessions.nim pins the cache/bitmap lifecycle and gate rule; tiles.test adds the lock-held burst and the revocation/restart seam; auth.test adds the LEAK_FAULT=sessioncache negative control (a stale entry makes the auth group fail); mutation c5 keeps the cache drop load-bearing. Explored wire and persistence untouched. Bench (devbox run bench, in-process): tile gate old 0.006 ms -> new 0.001 ms per request; incremental explored mark 0.001 ms; snapshot build 2.0 ms at 262144 cells (startup only).
275 lines
12 KiB
JavaScript
275 lines
12 KiB
JavaScript
// M3 slice 2 (#17): getting in. Keys, sessions, cookies, the Origin check, connection caps and
|
|
// revocation, plus the `fff-vtt gm-key` command. Black-box through the real binary (withserver).
|
|
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import fs from 'node:fs'
|
|
import { gmKeyOf, makeStateDir, parseGmKey, runGmKey, startServer, stopServer,
|
|
withGmServer, signIn, openAs } from './withserver.mjs'
|
|
import { createPlayer, join, openWs, rawUpgrade } from './lib.mjs'
|
|
|
|
|
|
test('POST /join: cookie flags, and every refusal is a bare status', async () => {
|
|
await withGmServer(async (s, gmKey) => {
|
|
const ok = await join(s.origin, gmKey)
|
|
assert.equal(ok.status, 303)
|
|
assert.equal(ok.location, '/')
|
|
assert.match(ok.setCookie, /^fff_session=[0-9a-f]{64}; HttpOnly; Secure; SameSite=Strict; Path=\/$/)
|
|
assert.equal(ok.body, '')
|
|
assert.equal(ok.headers.get('referrer-policy'), 'no-referrer')
|
|
|
|
// comparison ignores case, dashes and spaces
|
|
for (const variant of [gmKey.toLowerCase(), gmKey.replaceAll('-', ' '), gmKey.replaceAll('-', '')]) {
|
|
const r = await join(s.origin, variant)
|
|
assert.equal(r.status, 303, `variant refused: ${variant}`)
|
|
assert.notEqual(r.cookie, ok.cookie, 'each sign-in issues a fresh session')
|
|
}
|
|
|
|
const wrong = await join(s.origin, 'AAAA-BBBB-CCCC-DDDD-EEEE')
|
|
assert.equal(wrong.status, 401)
|
|
assert.equal(wrong.setCookie, null)
|
|
assert.equal(wrong.body, '')
|
|
|
|
const missing = await fetch(`${s.origin}/join`, {
|
|
method: 'POST',
|
|
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
body: 'other=x',
|
|
})
|
|
assert.equal(missing.status, 400)
|
|
assert.equal(missing.headers.get('set-cookie'), null)
|
|
|
|
const wrongType = await fetch(`${s.origin}/join`, {
|
|
method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ key: gmKey }),
|
|
})
|
|
assert.equal(wrongType.status, 400)
|
|
|
|
const get = await fetch(`${s.origin}/join`)
|
|
assert.equal(get.status, 405)
|
|
|
|
const huge = await join(s.origin, 'A'.repeat(1100))
|
|
assert.equal(huge.status, 400)
|
|
})
|
|
})
|
|
|
|
test('no session, an unknown session, or a foreign Origin never upgrades', async () => {
|
|
await withGmServer(async (s, gmKey) => {
|
|
const { cookie } = await signIn(s.origin, gmKey)
|
|
assert.equal((await rawUpgrade(s.origin)).status, 401) // no cookie
|
|
assert.equal((await rawUpgrade(s.origin, { cookie: 'fff_session=' + '0'.repeat(64) })).status, 401)
|
|
assert.equal((await rawUpgrade(s.origin, { cookie, originHeader: 'https://evil.example' })).status, 403)
|
|
assert.equal((await rawUpgrade(s.origin, { cookie, originHeader: null })).status, 403) // missing Origin
|
|
assert.equal((await rawUpgrade(s.origin, { cookie })).status, 101)
|
|
// Origin is checked before the session: no cookie + bad origin is still 403 (T7 first line)
|
|
assert.equal((await rawUpgrade(s.origin, { originHeader: 'https://evil.example' })).status, 403)
|
|
})
|
|
})
|
|
|
|
test('one key on several devices: two cookies, one player, one per-player cap', async () => {
|
|
await withGmServer(async (s, gmKey) => {
|
|
const a = await signIn(s.origin, gmKey)
|
|
const b = await signIn(s.origin, gmKey)
|
|
assert.notEqual(a.cookie, b.cookie)
|
|
const wsA = await openWs(s.origin, a.cookie)
|
|
const wsB = await openWs(s.origin, b.cookie)
|
|
// three more on device A fill the player's four slots (two sessions count as one player)
|
|
const extra = []
|
|
for (let i = 0; i < 2; i++) extra.push(await openWs(s.origin, a.cookie))
|
|
assert.equal(wsA.closeInfo, null)
|
|
assert.equal(wsB.closeInfo, null)
|
|
// the fifth connection of the same player (device B) evicts the oldest (wsA)
|
|
const fifth = await openWs(s.origin, b.cookie)
|
|
const evicted = await wsA.closed
|
|
assert.equal(evicted.code, 1005) // mummy 0.4.8 sends an empty close frame (DESIGN 6.5, #18)
|
|
// ... so the reason rides the last app message before the close (FIXED, #18)
|
|
const last = wsA.messages[wsA.messages.length - 1]
|
|
assert.equal(last.t, 'closing')
|
|
assert.equal(last.reason, 'evicted')
|
|
assert.equal(last.code, 4002)
|
|
assert.equal(typeof last.n, 'number')
|
|
assert.equal(fifth.closeInfo, null)
|
|
assert.equal(wsB.closeInfo, null)
|
|
for (const w of [wsB, ...extra, fifth]) w.close()
|
|
})
|
|
})
|
|
|
|
test('the 17th connection is refused with 503 and Retry-After: 5', async () => {
|
|
await withGmServer(async (s, gmKey) => {
|
|
const gm = await openAs(s.origin, gmKey)
|
|
const players = []
|
|
for (let i = 0; i < 4; i++) players.push(await openAs(s.origin, (await createPlayer(gm.ws)).key))
|
|
// 1 GM + 4 players; fill to 16 without breaching a per-player cap (4+4+4+3+GM)
|
|
const conns = [gm.ws]
|
|
for (let i = 0; i < 4; i++) {
|
|
conns.push(players[i].ws)
|
|
for (let j = 0; j < (i === 3 ? 2 : 3); j++) conns.push(await openWs(s.origin, players[i].cookie))
|
|
}
|
|
assert.equal(conns.length, 16)
|
|
const refused = await rawUpgrade(s.origin, { cookie: players[0].cookie })
|
|
assert.equal(refused.status, 503)
|
|
assert.equal(refused.headers['retry-after'], '5')
|
|
for (const w of conns) w.close()
|
|
})
|
|
})
|
|
|
|
test('key.replace: old key and old sessions die, live connections close, the new key works', async () => {
|
|
await withGmServer(async (s, gmKey) => {
|
|
const gm = await openAs(s.origin, gmKey)
|
|
const player = await createPlayer(gm.ws)
|
|
const first = await openAs(s.origin, player.key)
|
|
const second = await openAs(s.origin, player.key)
|
|
const ack = await gm.ws.request({ t: 'key.replace', id: player.id })
|
|
assert.equal(ack.ok, true)
|
|
assert.match(ack.key, /^[A-Z2-9]{4}(-[A-Z2-9]{4}){4}$/)
|
|
assert.notEqual(ack.key, player.key)
|
|
// both live connections of that player are closed, the GM stays
|
|
await first.ws.closed
|
|
await second.ws.closed
|
|
for (const ws of [first.ws, second.ws]) {
|
|
const last = ws.messages[ws.messages.length - 1]
|
|
assert.equal(last.t, 'closing')
|
|
assert.equal(last.reason, 'revoked')
|
|
assert.equal(last.code, 4001)
|
|
assert.equal(ws.closeInfo.code, 1005) // the close frame itself carries no code
|
|
}
|
|
assert.equal(gm.ws.closeInfo, null)
|
|
// the old key and the old cookies are dead; the new key signs in
|
|
assert.equal((await join(s.origin, player.key)).status, 401)
|
|
assert.equal((await rawUpgrade(s.origin, { cookie: first.cookie })).status, 401)
|
|
assert.equal((await rawUpgrade(s.origin, { cookie: second.cookie })).status, 401)
|
|
const fresh = await openAs(s.origin, ack.key)
|
|
assert.equal(fresh.ws.closeInfo, null)
|
|
fresh.ws.close(); gm.ws.close()
|
|
})
|
|
})
|
|
|
|
// #65: the negative control for the in-memory session cache. `LEAK_FAULT=sessioncache` skips the
|
|
// cache drop on key.replace, so the old cookie still upgrades (101 instead of the clean build's
|
|
// 401). It proves the replacement test above is load-bearing: without cache invalidation the auth
|
|
// group fails. The fault build contains the hook; the shipped binary cannot.
|
|
test('fault sessioncache: a revoked session kept alive fails the authentication group', async () => {
|
|
const stateDir = makeStateDir()
|
|
const gmKey = gmKeyOf(stateDir)
|
|
const s = await startServer({ stateDir, defines: ['leakfault'],
|
|
env: { LEAK_FAULT: 'sessioncache' } })
|
|
try {
|
|
const gm = await openAs(s.origin, gmKey)
|
|
const player = await createPlayer(gm.ws)
|
|
const session = await openAs(s.origin, player.key)
|
|
assert.equal((await rawUpgrade(s.origin, { cookie: session.cookie })).status, 101)
|
|
assert.equal((await gm.ws.request({ t: 'key.replace', id: player.id })).ok, true)
|
|
await session.ws.closed
|
|
const status = (await rawUpgrade(s.origin, { cookie: session.cookie })).status
|
|
assert.equal(status, 101, 'the fault keeps the revoked session in the cache')
|
|
assert.notEqual(status, 401, 'a stale cache is exactly what the auth group refuses')
|
|
gm.ws.close()
|
|
} finally {
|
|
await stopServer(s)
|
|
fs.rmSync(stateDir, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
test('player.remove: key, sessions and connections gone; slot freed; the GM is not removable', async () => {
|
|
await withGmServer(async (s, gmKey) => {
|
|
const gm = await openAs(s.origin, gmKey)
|
|
const player = await createPlayer(gm.ws)
|
|
const session = await openAs(s.origin, player.key)
|
|
const ack = await gm.ws.request({ t: 'player.remove', id: player.id })
|
|
assert.equal(ack.ok, true)
|
|
await session.ws.closed
|
|
assert.equal(session.ws.messages[session.ws.messages.length - 1].reason, 'revoked')
|
|
assert.equal((await join(s.origin, player.key)).status, 401)
|
|
assert.equal((await rawUpgrade(s.origin, { cookie: session.cookie })).status, 401)
|
|
assert.equal((await gm.ws.request({ t: 'player.remove', id: player.id })).code, 'not-found')
|
|
const gmId = 1 // the GM is player 1 on a fresh DB
|
|
assert.equal((await gm.ws.request({ t: 'player.remove', id: gmId })).code, 'not-allowed')
|
|
assert.equal((await gm.ws.request({ t: 'key.replace', id: gmId })).code, 'not-allowed')
|
|
|
|
// the cap counts unused keys; removing a player frees the slot
|
|
const keys = []
|
|
for (let i = 0; i < 6; i++) keys.push(await createPlayer(gm.ws))
|
|
assert.equal((await gm.ws.request({ t: 'key.create' })).code, 'cap-reached')
|
|
await gm.ws.request({ t: 'player.remove', id: keys[0].id })
|
|
const replacement = await gm.ws.request({ t: 'key.create' })
|
|
assert.equal(replacement.ok, true)
|
|
assert.equal(typeof replacement.id, 'number')
|
|
gm.ws.close()
|
|
})
|
|
})
|
|
|
|
test('a player connection cannot use the GM intents', async () => {
|
|
await withGmServer(async (s, gmKey) => {
|
|
const gm = await openAs(s.origin, gmKey)
|
|
const player = await createPlayer(gm.ws)
|
|
const p = await openAs(s.origin, player.key)
|
|
assert.equal((await p.ws.request({ t: 'key.create' })).code, 'not-allowed')
|
|
assert.equal((await p.ws.request({ t: 'key.replace', id: 1 })).code, 'not-allowed')
|
|
assert.equal((await p.ws.request({ t: 'player.remove', id: player.id })).code, 'not-allowed')
|
|
p.ws.close(); gm.ws.close()
|
|
})
|
|
})
|
|
|
|
test('gm-key: same key on re-run (also while the server runs); --new refuses while it runs and rotates after', async () => {
|
|
const stateDir = makeStateDir()
|
|
try {
|
|
const key1 = gmKeyOf(stateDir)
|
|
assert.equal(gmKeyOf(stateDir), key1, 're-run must print the same key')
|
|
|
|
let s = await startServer({ stateDir })
|
|
try {
|
|
assert.equal(gmKeyOf(stateDir), key1, 'read-only print works while the server runs')
|
|
const before = await join(s.origin, key1)
|
|
assert.equal(before.status, 303)
|
|
const gm = await openWs(s.origin, before.cookie)
|
|
const refused = runGmKey(stateDir, ['--new'])
|
|
assert.equal(refused.status, 3)
|
|
assert.match(refused.stderr, /running|lock/i)
|
|
assert.equal(gmKeyOf(stateDir), key1, 'a refused --new changed nothing')
|
|
assert.equal(gm.closeInfo, null, 'the refusal did not touch live sessions')
|
|
gm.close()
|
|
} finally {
|
|
await stopServer(s)
|
|
}
|
|
|
|
const rotated = runGmKey(stateDir, ['--new'])
|
|
assert.equal(rotated.status, 0, rotated.stderr)
|
|
const key2 = parseGmKey(rotated.stdout)
|
|
assert.notEqual(key2, key1)
|
|
assert.equal(gmKeyOf(stateDir), key2)
|
|
|
|
s = await startServer({ stateDir })
|
|
try {
|
|
assert.equal((await join(s.origin, key1)).status, 401, 'the old GM key is dead')
|
|
assert.equal((await join(s.origin, key2)).status, 303)
|
|
} finally {
|
|
await stopServer(s)
|
|
}
|
|
} finally {
|
|
fs.rmSync(stateDir, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
test('no key or session token ever reaches the server log', async () => {
|
|
await withGmServer(async (s, gmKey) => {
|
|
const secrets = [gmKey]
|
|
const gm = await openAs(s.origin, gmKey)
|
|
secrets.push(gm.cookie ?? '')
|
|
const player = await createPlayer(gm.ws)
|
|
secrets.push(player.key)
|
|
const session = await openAs(s.origin, player.key)
|
|
secrets.push(session.cookie.split('=')[1])
|
|
const replaced = await gm.ws.request({ t: 'key.replace', id: player.id })
|
|
await session.ws.closed
|
|
secrets.push(replaced.key)
|
|
const again = await openAs(s.origin, replaced.key)
|
|
secrets.push(again.cookie.split('=')[1])
|
|
again.ws.close(); gm.ws.close()
|
|
// give the server a moment to flush its lines
|
|
await new Promise(r => setTimeout(r, 50))
|
|
const log = s.log()
|
|
for (const secret of secrets) {
|
|
if (!secret) continue
|
|
assert.ok(!log.includes(secret), `secret leaked into the log: ${secret}`)
|
|
assert.ok(!log.includes(secret.replaceAll('-', '')), 'secret (normalized) leaked into the log')
|
|
}
|
|
})
|
|
})
|