P1-1: the organizations branch lets the instance owner's own session through to the two Triage paths; everyone else still gets this Console's unknown /api/ answer. P1-2: each start clears turns/, so the runner's numbered turn files do not collide on restart. P2-1: on exit, a message the session took and never marked read leaves the queue with a host line. P2-2: member, signed-out, organizations-Console and org-selected route tests. P3-1: bus-start cards carry --dev-local-owner on a dev local owner Console. P3-2: a failed start writes a host line; the queue index is guarded. P3-3: one shared refresh, a unique temp name per write, four organizations read at a time. P3-4: the restart path waits for stop() inside its two-second exit. P3-5: ANTHROPIC_API_KEY no longer passes from the route.
263 lines
19 KiB
JavaScript
263 lines
19 KiB
JavaScript
// #1873 triage-host: the Triage routes over the real HTTP boundary. Only the instance owner's own
|
|
// session reaches them; every other caller gets exactly what an unknown route gives that caller.
|
|
// The write also needs --send and the same-origin checks. No body, path or exception text leaves.
|
|
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { request } from 'node:http';
|
|
import { mkdtempSync, existsSync, readFileSync } from 'node:fs';
|
|
import { join } from 'node:path';
|
|
import { startServer } from '../src/serve.mjs';
|
|
import { createDevOwner } from '../src/dev-owner.mjs';
|
|
import { consoleAccess } from '../src/access.mjs';
|
|
import { createSsoSession } from './helpers/sso-session.mjs';
|
|
import { NO_ROUTE_MESSAGE, capMessage, triageView, triageHTTP, SNAPSHOT_MAX_AGE_MS } from '../src/triage-http.mjs';
|
|
import { createTriageHost, TriageError } from '../src/triage-host.mjs';
|
|
|
|
const LEAK = '/home/owner/.config/mosaic-dev/secret-token-0123456789';
|
|
|
|
function fakeTriage({ message } = {}) {
|
|
const calls = [];
|
|
return {
|
|
calls,
|
|
snapshotAt: () => null,
|
|
refresh: async () => { calls.push(['refresh']); return null; },
|
|
state: async () => ({ route: 'set', running: false, lastExit: null, snapshot: { snapshotVersion: 1 }, transcript: [{ at: 't', from: 'owner', id: 'owner-1', text: 'hi' }] }),
|
|
message: async text => { calls.push(['message', text]); return message ? message(text) : { id: 'owner-abc', at: '2026-11-16T00:00:00.000Z' }; },
|
|
};
|
|
}
|
|
|
|
const send = (port, path, { method = 'GET', headers = {}, body } = {}) => new Promise((resolve, reject) => {
|
|
const req = request({ host: '127.0.0.1', port, path, method, headers }, res => {
|
|
let text = ''; res.on('data', c => { text += c; }); res.on('end', () => resolve({ status: res.statusCode, text, json: (() => { try { return JSON.parse(text); } catch { return null; } })() }));
|
|
});
|
|
req.on('error', reject); req.end(body === undefined ? undefined : typeof body === 'string' ? body : JSON.stringify(body));
|
|
});
|
|
|
|
async function devServer(t, options = {}) {
|
|
const logs = [];
|
|
const s = await startServer({ port: 0, board: 'http://127.0.0.1:9', sso: createDevOwner(), send: async () => ({}), log: line => logs.push(line), ...options });
|
|
t.after(() => new Promise(r => s.close(r)));
|
|
const port = s.address().port;
|
|
const headers = { host: `127.0.0.1:${port}`, origin: `http://127.0.0.1:${port}`, 'sec-fetch-site': 'same-origin', 'content-type': 'application/json' };
|
|
return { port, logs, headers };
|
|
}
|
|
|
|
// The same request on an unknown path next to the Triage one: status and body must match.
|
|
async function sameAsUnknown(port, path, method, headers, body, label) {
|
|
const unknown = path.replace('/api/triage', '/api/triagz');
|
|
const [a, b] = [await send(port, path, { method, headers, body }), await send(port, unknown, { method, headers, body })];
|
|
assert.equal(a.status, b.status, `${label} ${method} ${path}: status`);
|
|
assert.equal(a.text, b.text, `${label} ${method} ${path}: body`);
|
|
}
|
|
const EVERY = [['/api/triage', 'GET'], ['/api/triage', 'POST'], ['/api/triage/message', 'GET'], ['/api/triage/message', 'POST']];
|
|
|
|
test('the dev local owner reads the drawer state and sends a message; the config names the drawer', async t => {
|
|
const triage = fakeTriage();
|
|
const f = await devServer(t, { triage });
|
|
const read = await send(f.port, '/api/triage', { headers: { host: f.headers.host } });
|
|
assert.equal(read.status, 200);
|
|
assert.deepEqual(read.json, { route: 'set', overCapUntil: null, running: false, lastExit: null, snapshot: { snapshotVersion: 1 }, transcript: [{ at: 't', from: 'owner', id: 'owner-1', text: 'hi' }], send: true });
|
|
const sent = await send(f.port, '/api/triage/message', { method: 'POST', headers: f.headers, body: { text: 'why is alpha down?' } });
|
|
assert.equal(sent.status, 202);
|
|
assert.deepEqual(sent.json, { id: 'owner-abc', at: '2026-11-16T00:00:00.000Z' });
|
|
assert.deepEqual(triage.calls.filter(c => c[0] === 'message'), [['message', 'why is alpha down?']]);
|
|
const config = await send(f.port, '/api/config', { headers: { host: f.headers.host } });
|
|
assert.equal(config.json.triage, true);
|
|
assert.equal(config.json.triageSend, true);
|
|
assert.equal(f.logs.join('\n').includes('why is alpha down'), false, 'no message text in a log line');
|
|
for (const line of f.logs.filter(l => l.includes('/api/triage'))) assert.match(line, /^\S+ (GET|POST) \/api\/triage(\/message)? \d{3}$/);
|
|
// A wrong method on either route.
|
|
assert.equal((await send(f.port, '/api/triage', { method: 'POST', headers: f.headers, body: {} })).status, 405);
|
|
assert.equal((await send(f.port, '/api/triage/message', { headers: { host: f.headers.host } })).status, 405);
|
|
});
|
|
|
|
test('the write is refused without --send, from a foreign Origin, and without same-origin Fetch Metadata', async t => {
|
|
const triage = fakeTriage();
|
|
const readOnly = await devServer(t, { triage, send: null });
|
|
const r = await send(readOnly.port, '/api/triage/message', { method: 'POST', headers: readOnly.headers, body: { text: 'hi' } });
|
|
assert.equal(r.status, 403); assert.deepEqual(r.json, { error: 'read-only' });
|
|
assert.equal((await send(readOnly.port, '/api/triage', { headers: { host: readOnly.headers.host } })).json.send, false);
|
|
assert.equal((await send(readOnly.port, '/api/config', { headers: { host: readOnly.headers.host } })).json.triageSend, false);
|
|
const f = await devServer(t, { triage });
|
|
assert.equal((await send(f.port, '/api/triage/message', { method: 'POST', headers: { ...f.headers, origin: 'http://evil.example' }, body: { text: 'hi' } })).status, 403);
|
|
const crossSite = await send(f.port, '/api/triage/message', { method: 'POST', headers: { ...f.headers, 'sec-fetch-site': 'cross-site' }, body: { text: 'hi' } });
|
|
assert.deepEqual([crossSite.status, crossSite.json], [403, { error: 'cross-site request refused' }]);
|
|
const noMeta = { ...f.headers }; delete noMeta['sec-fetch-site'];
|
|
const missing = await send(f.port, '/api/triage/message', { method: 'POST', headers: noMeta, body: { text: 'hi' } });
|
|
assert.deepEqual([missing.status, missing.json], [403, { error: 'cross-site request refused' }]);
|
|
assert.deepEqual(triage.calls.filter(c => c[0] === 'message'), []);
|
|
});
|
|
|
|
test('a malformed body is refused before the host sees it; the host refusals map to fixed answers with no leaked text', async t => {
|
|
let next = null;
|
|
const triage = fakeTriage({ message: () => { throw next; } });
|
|
const f = await devServer(t, { triage });
|
|
const post = (body, headers = f.headers) => send(f.port, '/api/triage/message', { method: 'POST', headers, body });
|
|
for (const body of [{ text: 'hi', extra: 1 }, { text: 5 }, {}, [], '{"text":', `{"text":"${'x'.repeat(17000)}"}`])
|
|
assert.deepEqual((await post(body)).json, { error: 'invalid-request', message: 'the request is not a Triage message' }, JSON.stringify(body).slice(0, 40));
|
|
assert.equal((await post({ text: 'hi' }, { ...f.headers, 'content-type': 'text/plain' })).status, 400);
|
|
assert.deepEqual(triage.calls.filter(c => c[0] === 'message'), []);
|
|
next = Object.assign(new TriageError('triage-over-cap', LEAK), { until: '2026-11-17T00:00:00.000Z' });
|
|
const cap = await post({ text: 'hi' });
|
|
assert.deepEqual([cap.status, cap.json], [429, { error: 'triage-over-cap', message: 'Triage is over its daily token cap and starts again after the time shown', until: '2026-11-17T00:00:00.000Z' }]);
|
|
next = new TriageError('triage-no-route', LEAK);
|
|
const none = await post({ text: 'hi' });
|
|
assert.deepEqual([none.status, none.json], [409, { error: 'triage-no-route', message: NO_ROUTE_MESSAGE }]);
|
|
next = new TriageError('triage-message-too-long', LEAK);
|
|
assert.equal((await post({ text: 'hi' })).status, 400);
|
|
next = new TriageError('triage-busy', LEAK);
|
|
assert.equal((await post({ text: 'hi' })).status, 429);
|
|
next = Object.assign(new Error(LEAK), { code: 'EACCES' });
|
|
const odd = await post({ text: 'hi' });
|
|
assert.deepEqual([odd.status, odd.json], [503, { error: 'outcome-unknown', message: 'Triage did not answer' }]);
|
|
for (const r of [cap, none, odd]) assert.equal(r.text.includes('/home/'), false);
|
|
assert.equal(f.logs.join('\n').includes('/home/'), false);
|
|
});
|
|
|
|
test('a signed-out caller and a granted user get the unknown-route answer on both routes', async t => {
|
|
const access = { accessVersion: 1, revision: 1,
|
|
users: [{ id: 'alice', home: 'local', subjects: [{ provider: 'writer', sub: 'alice-sub' }] }],
|
|
grants: [{ id: 'g-1', user: 'alice', business: 'demo', projects: ['*'], instances: ['*'], level: 'collaborator' }] };
|
|
const auth = await createSsoSession(t, { host: 'console.example', users: consoleAccess({ load: () => access, business: () => 'demo' }) });
|
|
const triage = fakeTriage();
|
|
const s = await startServer({ port: 0, sso: auth.sso, send: async () => ({}), triage, userOptions: () => ({}), log: () => {} });
|
|
t.after(() => new Promise(r => s.close(r)));
|
|
const port = s.address().port;
|
|
const owner = auth.writeHeaders(await auth.signIn(port));
|
|
assert.equal((await send(port, '/api/triage', { headers: owner })).status, 200, 'the SSO root owner reaches the read');
|
|
assert.equal((await send(port, '/api/triage/message', { method: 'POST', headers: owner, body: { text: 'hi' } })).status, 202, 'and the write');
|
|
const before = triage.calls.length;
|
|
const signedOut = auth.writeHeaders(null);
|
|
const alice = auth.writeHeaders(await auth.signIn(port, { sub: 'alice-sub' }));
|
|
for (const [path, method] of EVERY) {
|
|
await sameAsUnknown(port, path, method, signedOut, method === 'POST' ? { text: 'hi' } : undefined, 'signed out');
|
|
await sameAsUnknown(port, path, method, alice, method === 'POST' ? { text: 'hi' } : undefined, 'granted user');
|
|
}
|
|
assert.equal((await send(port, '/api/triage', { headers: signedOut })).status, 401);
|
|
assert.equal(Object.hasOwn((await send(port, '/api/config', { headers: alice })).json, 'triage'), false);
|
|
assert.equal(triage.calls.length, before, 'the host saw nothing from them');
|
|
});
|
|
|
|
test('on an organizations Console the instance owner reaches both routes; a member, a signed-out caller and an org-selected path get the unknown answer', async t => {
|
|
const triage = fakeTriage();
|
|
const organizations = { list: async () => [], resolve: async id => (id === 'x' ? { busHealthy: true } : null) };
|
|
const owner = await devServer(t, { triage, organizations });
|
|
const read = await send(owner.port, '/api/triage', { headers: { host: owner.headers.host } });
|
|
assert.equal(read.status, 200, 'the owner reads');
|
|
assert.equal(read.json.route, 'set');
|
|
assert.equal((await send(owner.port, '/api/triage/message', { method: 'POST', headers: owner.headers, body: { text: 'hi' } })).status, 202, 'and writes');
|
|
assert.equal((await send(owner.port, '/api/config', { headers: { host: owner.headers.host } })).json.triage, true);
|
|
const seen = triage.calls.length;
|
|
// With an organization selected the paths are that organization's, and Triage is in none.
|
|
for (const [path, method] of EVERY) {
|
|
const orgPath = path.replace('/api/', '/api/orgs/x/');
|
|
await sameAsUnknown(owner.port, orgPath, method, owner.headers, method === 'POST' ? { text: 'hi' } : undefined, 'org selected');
|
|
}
|
|
const member = await devServer(t, { triage, organizations, sso: { ...createDevOwner(), session: () => ({ name: 'Bob', rootOwner: false, instanceRole: 'member' }) } });
|
|
const signedOut = await devServer(t, { triage, organizations, sso: { ...createDevOwner(), session: () => null } });
|
|
for (const [label, f] of [['member', member], ['signed out', signedOut]]) {
|
|
for (const [path, method] of EVERY) await sameAsUnknown(f.port, path, method, f.headers, method === 'POST' ? { text: 'hi' } : undefined, label);
|
|
}
|
|
assert.equal(Object.hasOwn((await send(member.port, '/api/config', { headers: { host: member.headers.host } })).json, 'triage'), false);
|
|
assert.equal(triage.calls.length, seen, 'the host saw nothing from them');
|
|
});
|
|
|
|
test('without organizations a member session gets the unknown answer and no drawer in the config', async t => {
|
|
const triage = fakeTriage();
|
|
const member = await devServer(t, { triage, sso: { ...createDevOwner(), session: () => ({ name: 'Bob', rootOwner: false, instanceRole: 'member' }) } });
|
|
for (const [path, method] of EVERY) await sameAsUnknown(member.port, path, method, member.headers, method === 'POST' ? { text: 'hi' } : undefined, 'member');
|
|
assert.equal((await send(member.port, '/api/triage', { headers: { host: member.headers.host } })).status, 404);
|
|
assert.equal(Object.hasOwn((await send(member.port, '/api/config', { headers: { host: member.headers.host } })).json, 'triage'), false);
|
|
assert.deepEqual(triage.calls, []);
|
|
});
|
|
|
|
test('without a host, on a loopback Host under --sso, and on the tester listener the routes are unknown', async t => {
|
|
const none = await devServer(t);
|
|
for (const [path, method] of EVERY) await sameAsUnknown(none.port, path, method, none.headers, method === 'POST' ? { text: 'hi' } : undefined, 'no host');
|
|
assert.equal(Object.hasOwn((await send(none.port, '/api/config', { headers: { host: none.headers.host } })).json, 'triage'), false);
|
|
const auth = await createSsoSession(t, { host: 'console.example' });
|
|
const triage = fakeTriage();
|
|
const s = await startServer({ port: 0, sso: auth.sso, send: async () => ({}), triage, log: () => {} });
|
|
t.after(() => new Promise(r => s.close(r)));
|
|
const port = s.address().port;
|
|
const cookie = await auth.signIn(port);
|
|
const loop = { host: `127.0.0.1:${port}`, cookie, origin: `http://127.0.0.1:${port}`, 'sec-fetch-site': 'same-origin', 'content-type': 'application/json' };
|
|
for (const [path, method] of EVERY) await sameAsUnknown(port, path, method, loop, method === 'POST' ? { text: 'hi' } : undefined, 'loopback Host');
|
|
const { startTesterSocket } = await import('../src/tester-socket.mjs');
|
|
const { testerRequest } = await import('../../cli/src/tester-http.mjs');
|
|
const root = mkdtempSync(join(process.env.TMPDIR, 'th-'));
|
|
const ts = await startTesterSocket({ dataRoot: root, triage, log: () => {} });
|
|
t.after(() => new Promise(r => ts.close(r)));
|
|
for (const [path, method] of EVERY) {
|
|
const body = method === 'POST' ? { text: 'hi' } : undefined;
|
|
const a = await testerRequest({ dataRoot: root, path, method, body });
|
|
const b = await testerRequest({ dataRoot: root, path: path.replace('/api/triage', '/api/triagz'), method, body });
|
|
assert.equal(a.status, b.status, `tester ${method} ${path}`);
|
|
}
|
|
assert.deepEqual(triage.calls, []);
|
|
});
|
|
|
|
test('the read refreshes a missing or stale snapshot and keeps a fresh one', async () => {
|
|
let at = null, clock = 1_000_000;
|
|
const calls = [];
|
|
const triage = { snapshotAt: () => at, refresh: async () => { calls.push(clock); at = clock; }, state: async () => ({ route: 'not set', running: false, lastExit: null, snapshot: null, transcript: [] }) };
|
|
const view = await triageView(triage, { send: true, now: () => clock });
|
|
assert.deepEqual(view, { route: 'not set', routeMessage: NO_ROUTE_MESSAGE, overCapUntil: null, running: false, lastExit: null, snapshot: null, transcript: [], send: true });
|
|
clock += SNAPSHOT_MAX_AGE_MS - 1; await triageView(triage, { now: () => clock });
|
|
clock += 2; await triageView(triage, { now: () => clock });
|
|
assert.deepEqual(calls, [1_000_000, 1_000_000 + SNAPSHOT_MAX_AGE_MS + 1]);
|
|
});
|
|
|
|
test('over the cap: the read names the time in plain words and the write answers 429 with it', async t => {
|
|
const dataRoot = join(mkdtempSync(join(process.env.TMPDIR, 'th-')), 'data');
|
|
const route = { admit: async () => ({ ok: false, code: 'triage-over-cap', until: '2026-11-17T00:00:00.000Z' }) };
|
|
const host = createTriageHost({ dataRoot, route, readSnapshot: async () => null, now: () => new Date('2026-11-16T20:00:00.000Z'), bwrap: '/nonexistent/bwrap' });
|
|
const f = await devServer(t, { triage: host });
|
|
const before = await send(f.port, '/api/triage', { headers: { host: f.headers.host } });
|
|
assert.deepEqual([before.json.route, before.json.overCapUntil, Object.hasOwn(before.json, 'capMessage')], ['set', null, false]);
|
|
const sent = await send(f.port, '/api/triage/message', { method: 'POST', headers: f.headers, body: { text: 'hello' } });
|
|
assert.deepEqual([sent.status, sent.json.error, sent.json.until], [429, 'triage-over-cap', '2026-11-17T00:00:00.000Z']);
|
|
const after = await send(f.port, '/api/triage', { headers: { host: f.headers.host } });
|
|
assert.equal(after.json.overCapUntil, '2026-11-17T00:00:00.000Z');
|
|
assert.equal(after.json.capMessage, capMessage('2026-11-17T00:00:00.000Z'));
|
|
assert.equal(host.running, false);
|
|
});
|
|
|
|
test('with the real host and no route: the read says so in plain words, the write refuses, nothing launches or is written', async t => {
|
|
const dataRoot = join(mkdtempSync(join(process.env.TMPDIR, 'th-')), 'data');
|
|
const host = createTriageHost({ dataRoot, readSnapshot: async () => ({ snapshotVersion: 1, at: '2026-11-16T00:00:00.000Z' }), bwrap: '/nonexistent/bwrap' });
|
|
const f = await devServer(t, { triage: host });
|
|
const read = await send(f.port, '/api/triage', { headers: { host: f.headers.host } });
|
|
assert.equal(read.status, 200);
|
|
assert.equal(read.json.route, 'not set');
|
|
assert.equal(read.json.routeMessage, NO_ROUTE_MESSAGE);
|
|
assert.deepEqual(read.json.snapshot, { snapshotVersion: 1, at: '2026-11-16T00:00:00.000Z' });
|
|
const sent = await send(f.port, '/api/triage/message', { method: 'POST', headers: f.headers, body: { text: 'hello' } });
|
|
assert.deepEqual([sent.status, sent.json.error], [409, 'triage-no-route']);
|
|
assert.equal(host.running, false);
|
|
assert.equal(existsSync(host.paths.transcript), false, 'a refused message is not written');
|
|
assert.equal(JSON.parse(readFileSync(join(host.paths.health, 'snapshot.json'), 'utf8')).snapshotVersion, 1, 'the read wrote the snapshot');
|
|
});
|
|
|
|
test('the route module keeps its own write checks when called alone: --send, one exact Origin, one same-origin Fetch Metadata', async () => {
|
|
const triage = fakeTriage();
|
|
const call = async (headers, { send = true } = {}) => {
|
|
const distinct = Object.fromEntries(Object.entries(headers).map(([k, v]) => [k, [v].flat()]));
|
|
const req = { method: 'POST', headers: Object.fromEntries(Object.entries(headers).map(([k, v]) => [k, [v].flat()[0]])), headersDistinct: distinct,
|
|
async *[Symbol.asyncIterator]() { yield Buffer.from('{"text":"hi"}'); } };
|
|
let status = null, body = '';
|
|
const res = { writeHead: s => { status = s; }, end: b => { body = b; } };
|
|
await triageHTTP(req, res, { path: '/api/triage/message', triage, send, origin: 'https://console.example' });
|
|
return [status, JSON.parse(body).error ?? 'ok'];
|
|
};
|
|
const good = { origin: 'https://console.example', 'sec-fetch-site': 'same-origin', 'content-type': 'application/json' };
|
|
assert.deepEqual(await call(good, { send: false }), [403, 'read-only']);
|
|
assert.deepEqual(await call({ ...good, origin: 'https://evil.example' }), [403, 'cross-origin request refused']);
|
|
assert.deepEqual(await call({ ...good, origin: ['https://console.example', 'https://console.example'] }), [403, 'cross-origin request refused']);
|
|
const { origin, ...noOrigin } = good;
|
|
assert.deepEqual(await call(noOrigin), [403, 'cross-origin request refused']);
|
|
assert.deepEqual(await call({ ...good, 'sec-fetch-site': ['same-origin', 'same-origin'] }), [403, 'cross-site request refused']);
|
|
assert.deepEqual(triage.calls, []);
|
|
assert.deepEqual(await call(good), [202, 'ok']);
|
|
});
|