tests: the restart and logs groups, the nine-group checklist and the fixture clean-up (#25)
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
// M3 slice 10 (#25): the "logs" acceptance group (DESIGN 11.3, 13 M3 group 9).
|
||||
//
|
||||
// Every key and every session value the test creates is grepped for in the server's whole log
|
||||
// afterwards. The log is the real one the binary wrote to stdout (tools/withserver.mjs appends
|
||||
// `<stateDir>/server.log`), across both runs, including the `gm-key --new` rotation: a secret from
|
||||
// the first run must not resurface in the second. Session *hashes* are checked too (they are in the
|
||||
// DB, they must never be in a log line either).
|
||||
//
|
||||
// The second test is the negative control: a `-d:leakfault` build with `LEAK_FAULT=keylog` writes a
|
||||
// raw key to stdout, bypassing eventlog, and the same grep must find it. Without that, a log that
|
||||
// was never captured, or a grep that matches nothing, would pass silently.
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import crypto from 'node:crypto'
|
||||
import fs from 'node:fs'
|
||||
import { createPlayer, join, openWs, rawWs, sleep } from './lib.mjs'
|
||||
import { gmKeyOf, makeStateDir, openAs, parseGmKey, runGmKey, startServer,
|
||||
stopServer } from './withserver.mjs'
|
||||
|
||||
const sha256 = s => crypto.createHash('sha256').update(s).digest('hex')
|
||||
|
||||
function assertClean(log, secrets, label) {
|
||||
for (const { what, value } of secrets) {
|
||||
assert.ok(value && value.length >= 8, `${label}: ${what} is a real secret`)
|
||||
assert.equal(log.includes(value), false,
|
||||
`${label}: the ${what} (${value}) appears in the log`)
|
||||
}
|
||||
}
|
||||
|
||||
test('logs: no key, session cookie or session hash reaches the log', async () => {
|
||||
const stateDir = makeStateDir()
|
||||
const secrets = []
|
||||
const note = (what, value) => secrets.push({ what, value })
|
||||
|
||||
const gmKey1 = gmKeyOf(stateDir)
|
||||
note('first GM key', gmKey1)
|
||||
let s = await startServer({ stateDir })
|
||||
try {
|
||||
// The GM signs in, creates two players (one named on two devices), reads a key, rotates a key,
|
||||
// and removes the other player: every path that handles a secret.
|
||||
const g = await openAs(s.origin, gmKey1)
|
||||
note('GM session', g.cookie)
|
||||
const alice = await createPlayer(g.ws)
|
||||
note('Alice key', alice.key)
|
||||
const bob = await createPlayer(g.ws)
|
||||
note('Bob key', bob.key)
|
||||
assert.equal((await g.ws.request({ t: 'key.show', id: alice.id })).key, alice.key)
|
||||
const shown = await g.ws.request({ t: 'key.show', id: alice.id })
|
||||
note('shown key', shown.key)
|
||||
|
||||
const aCookie = await join(s.origin, alice.key)
|
||||
assert.equal(aCookie.status, 303)
|
||||
note('Alice session 1', aCookie.cookie)
|
||||
const aDevice = await join(s.origin, alice.key)
|
||||
assert.equal(aDevice.status, 303, 'one key, several devices')
|
||||
note('Alice session 2', aDevice.cookie)
|
||||
// Named through the product path; its own sign-in is another session.
|
||||
const named = await openWs(s.origin, aCookie.cookie)
|
||||
await named.waitText('hello')
|
||||
assert.equal((await named.request({ t: 'player.setname', name: 'Alice' })).ok, true)
|
||||
await named.waitText('roster')
|
||||
named.close()
|
||||
|
||||
const replaced = await g.ws.request({ t: 'key.replace', id: alice.id })
|
||||
assert.equal(replaced.ok, true, JSON.stringify(replaced))
|
||||
note('replacement key', replaced.key)
|
||||
assert.notEqual(replaced.key, alice.key)
|
||||
assert.equal((await join(s.origin, alice.key)).status, 401, 'the old key is dead')
|
||||
const a2 = await join(s.origin, replaced.key)
|
||||
assert.equal(a2.status, 303)
|
||||
note('Alice session 3 (new key)', a2.cookie)
|
||||
|
||||
const bobConn = await openAs(s.origin, bob.key)
|
||||
await bobConn.ws.waitText('hello')
|
||||
note('Bob session', bobConn.cookie)
|
||||
assert.equal((await g.ws.request({ t: 'player.remove', id: bob.id })).ok, true)
|
||||
await bobConn.ws.closed
|
||||
|
||||
// Requests that refuse a secret still must not log it.
|
||||
const wrongKey = 'WRONG-WRONG-WRONG-WRONG-0000'
|
||||
note('wrong key', wrongKey)
|
||||
assert.equal((await join(s.origin, wrongKey)).status, 401)
|
||||
const tile = await fetch(`${s.origin}/tiles/1/v1/0_0.png`)
|
||||
assert.equal(tile.status, 403)
|
||||
const mismatch = await rawWs(s.origin, { cookie: a2.cookie, proto: 2 })
|
||||
await mismatch.closed
|
||||
const g2 = await openAs(s.origin, gmKey1)
|
||||
g2.ws.sendText('this is not json')
|
||||
await g2.ws.waitText('error')
|
||||
assert.equal(g2.ws.messages.find(m => m.t === 'error').code, 'malformed')
|
||||
g2.ws.close(); g.ws.close()
|
||||
} finally {
|
||||
await stopServer(s)
|
||||
}
|
||||
|
||||
// Rotate the GM key with the CLI (the server must be stopped: the instance lock), then serve
|
||||
// again with the new key: the first key is dead but both must stay out of the log.
|
||||
const rotated = runGmKey(stateDir, ['--new'])
|
||||
assert.equal(rotated.status, 0, rotated.stderr)
|
||||
const gmKey2 = parseGmKey(rotated.stdout)
|
||||
note('rotated GM key', gmKey2)
|
||||
assert.notEqual(gmKey2, gmKey1)
|
||||
s = await startServer({ stateDir })
|
||||
try {
|
||||
const g = await openAs(s.origin, gmKey2)
|
||||
note('GM session after rotation', g.cookie)
|
||||
assert.equal((await join(s.origin, gmKey1)).status, 401, 'the old GM key is dead')
|
||||
assert.equal((await join(s.origin, gmKey2)).status, 303)
|
||||
g.ws.close()
|
||||
} finally {
|
||||
await stopServer(s)
|
||||
}
|
||||
|
||||
const log = fs.readFileSync(`${stateDir}/server.log`, 'utf8')
|
||||
assert.ok(log.length > 0, 'the product wrote a log')
|
||||
for (const event of ['server_start', 'join_ok', 'key_created', 'key_replaced', 'player_removed']) {
|
||||
assert.ok(log.includes(event), `the log holds the ${event} path (so the grep is over real lines)`)
|
||||
}
|
||||
assertClean(log, secrets, 'clean run')
|
||||
// The session token itself is never stored, but its hash is: neither may be logged.
|
||||
for (const { what, value } of secrets.filter(x => x.what.includes('session'))) {
|
||||
const token = value.split('=')[1]
|
||||
assert.equal(log.includes(sha256(token)), false, `${what}: the session hash is not logged`)
|
||||
}
|
||||
fs.rmSync(stateDir, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
test('logs: the negative control finds a raw key when the server leaks one', async () => {
|
||||
// Same grep, same capture: a fault build prints the key to stdout without eventlog. If this test
|
||||
// passes, the clean run above is meaningful; if the grep breaks, this one fails.
|
||||
const stateDir = makeStateDir()
|
||||
const gmKey = gmKeyOf(stateDir)
|
||||
const s = await startServer({ stateDir, defines: ['leakfault'],
|
||||
env: { LEAK_FAULT: 'keylog' } })
|
||||
try {
|
||||
assert.equal((await join(s.origin, gmKey)).status, 303)
|
||||
await sleep(100)
|
||||
const log = s.log()
|
||||
assert.ok(log.includes(gmKey), `the leaked key must be visible to the grep; log:\n${log}`)
|
||||
} finally {
|
||||
await stopServer(s)
|
||||
fs.rmSync(stateDir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user