File size: 9,490 Bytes
b855ced cce9364 b855ced | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | // Operator tools: the admin socket and command line.
import { spawn, spawnSync } from 'node:child_process';
import { mkdtempSync, rmSync, statSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import assert from 'node:assert/strict';
import { withUrl } from './test-client.js';
const PORT = 21000 + Math.floor(Math.random() * 1000);
const Client = withUrl(`ws://127.0.0.1:${PORT}/_taper/gateway/v1`);
const dir = mkdtempSync(join(tmpdir(), 'taper-admin-'));
const socket = join(dir, 'admin.sock');
const admin = (...args) => spawnSync(process.execPath, ['admin.js', '--socket', socket, ...args], { encoding: 'utf8' });
const server = spawn(process.execPath, ['index.js'], {
env: { ...process.env, PORT: String(PORT), TAPER_REGISTER_POW_BITS: '8', TAPER_ADMIN_SOCKET: socket },
stdio: ['ignore', 'pipe', 'pipe'],
});
let log = '';
server.stderr.on('data', (d) => { log += d; });
server.stdout.on('data', (d) => { log += d; });
await new Promise((resolve) => { const t = setInterval(() => { if (log.includes('Admin socket ready') && log.includes('Taper gateway')) { clearInterval(t); resolve(); } }, 50); });
const results = [];
const test = async (name, fn) => {
try { await fn(); results.push(['PASS', name]); } catch (e) { results.push(['FAIL', name, e.message || String(e.stack ?? e)]); }
};
try {
const alice = await Client.connect();
const aliceAuth = await alice.register('alice', 'alice-alice-alice');
await alice.identify(aliceAuth.access_token);
await Client.connect().then((c) => c.register('bob', 'bob-bob-bob-bob'));
await test('the socket is private to the operator', () => {
assert.equal(statSync(socket).mode & 0o777, 0o600);
});
await test('lists accounts, with no message data', () => {
const r = admin('users');
assert.equal(r.status, 0, r.stderr);
assert.match(r.stdout, /@alice:localhost/);
assert.match(r.stdout, /@bob:localhost/);
});
await test('suspending signs the account out and stops login; unsuspending restores it', async () => {
assert.equal(admin('suspend', 'alice').status, 0);
assert.equal((await alice.closed).code, 4004);
const c = await Client.connect();
await assert.rejects(c.login('alice', 'alice-alice-alice'), /suspended/);
assert.match(admin('users').stdout, /@alice:localhost.*SUSPENDED/);
assert.equal(admin('unsuspend', 'alice').status, 0);
assert.match((await c.login('alice', 'alice-alice-alice')).access_token, /^tp_/);
});
await test('a login started before a suspension cannot finish after it', async () => {
const c = await Client.connect();
const held = await c.reauth('bob', 'bob-bob-bob-bob');
assert.equal(admin('suspend', 'bob').status, 0);
const r = await c.request('t.opaque.login.finish', { login_id: held.login_id, ke3: held.ke3 });
assert.equal(r.ok, false);
assert.equal(admin('unsuspend', 'bob').status, 0);
});
await test('registration can be closed and opened while the server runs', async () => {
assert.equal(admin('limits', 'registration_open', 'false').status, 0);
await assert.rejects((await Client.connect()).register('carol', 'carol-carol-carol'), /Registration is closed/);
assert.equal(admin('limits', 'registration_open', 'true').status, 0);
assert.equal((await (await Client.connect()).register('carol', 'carol-carol-carol')).user_id, '@carol:localhost');
});
await test('limits change live, and bad values are refused', async () => {
assert.equal(admin('limits', 'register_pow_bits', '10').status, 0);
assert.equal(JSON.parse(admin('limits').stdout).register_pow_bits, 10);
for (const bad of [['register_pow_bits', '99'], ['rate_burst', '0'], ['nonsense', '1'], ['rate_per_sec', '"fast"']]) {
const r = admin('limits', ...bad);
assert.equal(r.status, 1, bad.join(' '));
}
assert.equal(JSON.parse(admin('limits').stdout).register_pow_bits, 10);
// A tighter rate limit applies to new connections at once.
assert.equal(admin('limits', 'rate_burst', '3').status, 0);
const c = await Client.connect();
const answers = await Promise.all(Array.from({ length: 8 }, () => c.request('t.nonexistent', {})));
assert.ok(answers.some((r) => r.error?.errcode === 'T_LIMIT_EXCEEDED'), 'no request was rate limited');
assert.equal(admin('limits', 'rate_burst', '20').status, 0);
});
await test('revoke signs every device out but keeps the account', async () => {
const c = await Client.connect();
const auth = await c.login('bob', 'bob-bob-bob-bob');
await c.identify(auth.access_token);
assert.equal(admin('revoke', 'bob').status, 0);
assert.equal((await c.closed).code, 4004);
assert.match((await (await Client.connect()).login('bob', 'bob-bob-bob-bob')).access_token, /^tp_/);
});
await test('unlock clears the security keys of someone who lost them, and signs them out', async () => {
const { SoftKey } = await import('./softkey.js');
const origin = `http://127.0.0.1:${PORT}`;
const WS = (await import('ws')).default;
const c = await Client.connectTo(`ws://127.0.0.1:${PORT}/_taper/gateway/v1`, { open: (u) => new WS(u, { origin }) });
const auth = await c.register('dora', 'dora-dora-dora-dora');
await c.identify(auth.access_token);
const key = new SoftKey();
const start = await c.call('t.webauthn.register.start', { auth: await c.reauth('dora', 'dora-dora-dora-dora') });
await c.call('t.webauthn.register.finish', { challenge_id: start.challenge_id, credential: key.create(start.options, origin) });
assert.match(admin('users').stdout, /@dora:localhost.*1 key/);
assert.equal(admin('unlock', 'dora').status, 0);
assert.equal((await c.closed).code, 4004);
assert.doesNotMatch(admin('users').stdout, /@dora:localhost.*key/);
assert.match((await (await Client.connect()).login('dora', 'dora-dora-dora-dora')).access_token, /^tp_/);
});
await test('delete removes the account, reserves the name, and the log never names anyone', async () => {
assert.equal(admin('delete', '@carol:localhost').status, 0);
assert.doesNotMatch(admin('users').stdout, /carol/);
await assert.rejects((await Client.connect()).register('carol', 'something-else-1'), /T_USER_IN_USE/);
assert.match(log, /admin: delete/);
assert.doesNotMatch(log, /alice|bob|carol/);
});
await test('unknown commands, unknown users and other servers are refused', () => {
assert.equal(admin('explode').status, 1);
assert.match(admin('suspend', 'nobody').stderr, /No such account/);
assert.match(admin('suspend', '@alice:elsewhere.example').stderr, /not an account on this server/);
});
await test('stats are counters only', () => {
const s = JSON.parse(admin('stats').stdout);
assert.deepEqual(Object.keys(s).sort(), ['limits', 'rooms', 'sessions', 'suspended', 'users']);
});
await test('invite-only mode requires a single-use invite token from admin.js invite', async () => {
assert.equal(admin('limits', 'invite_only', 'true').status, 0);
await assert.rejects((await Client.connect()).register('erin', 'erin-erin-erin-erin'), /Missing 'invite_token'/);
const inv = JSON.parse(admin('invite', '24').stdout);
assert.match(inv.invite_token, /^ti_/);
const { register } = await import('../client/opaque.js');
const c1 = await Client.connect();
const ok = await register((m, b) => c1.call(m, b), c1.hello.body.server_name, 'erin', 'erin-erin-erin-erin', { inviteToken: inv.invite_token });
assert.equal(ok.user_id, '@erin:localhost');
// Reusing the same single-use invite token is refused.
const c2 = await Client.connect();
await assert.rejects(
register((m, b) => c2.call(m, b), c2.hello.body.server_name, 'frank', 'frank-frank-frank', { inviteToken: inv.invite_token }),
/Invalid, used, or expired invite/,
);
assert.equal(admin('limits', 'invite_only', 'false').status, 0);
await c1.close();
await c2.close();
});
await test('users can list active devices and revoke a single stolen device without changing password', async () => {
const d1 = await Client.connect();
const a1 = await d1.login('bob', 'bob-bob-bob-bob');
await d1.identify(a1.access_token);
const d2 = await Client.connect();
const a2 = await d2.login('bob', 'bob-bob-bob-bob');
await d2.identify(a2.access_token);
const { devices } = await d1.call('t.devices.list', {});
assert.ok(devices.some((d) => d.device_id === a1.device_id && d.current));
assert.ok(devices.some((d) => d.device_id === a2.device_id && !d.current));
// Revoking needs fresh password proof.
assert.equal((await d1.request('t.devices.delete', { device_id: a2.device_id })).error?.needs, 'password');
await d1.call('t.devices.delete', { device_id: a2.device_id, auth: await d1.reauth('bob', 'bob-bob-bob-bob') });
assert.equal((await d2.closed).code, 4004);
// Current device stays signed in!
assert.equal((await d1.call('t.whoami', {})).device_id, a1.device_id);
await d1.close();
});
} finally {
server.kill('SIGTERM');
await new Promise((r) => server.once('exit', r));
rmSync(dir, { recursive: true, force: true });
}
let failed = 0;
for (const [status, name, detail] of results) {
console.log(`${status} ${name}${detail ? `\n ${detail}` : ''}`);
if (status === 'FAIL') failed++;
}
console.log(`\n${results.length - failed}/${results.length} passed`);
process.exit(failed ? 1 : 0);
|