diff --git a/packages/bus/src/definition-scan.mjs b/packages/bus/src/definition-scan.mjs new file mode 100644 index 000000000..bf7f6f826 --- /dev/null +++ b/packages/bus/src/definition-scan.mjs @@ -0,0 +1,77 @@ +// #1873 T1 follow-up (org-defscan): the live credential store's held-secret check on an org +// definition's text. A definition is role text written into the organization file; launch hands its +// instructions to an agent as the contract. A file edited by hand, or while the bus was down, never +// went through the writer's check, so bus start and every launch run this one. +// +// The answer names where a held credential sits, never the text there. A key that itself holds a +// credential is named by its parent ("personality key"). Not imported by broker.mjs (the sandbox +// loads that file); runtime.mjs is its only caller. +import { BusError } from './broker.mjs'; + +export const DEFINITION_HOLDS_CREDENTIAL = 'definition-holds-credential'; +// A definition name the scan quotes: the organization loader's id pattern. Anything else is "*". +const NAME = /^[a-z0-9][a-z0-9-]{0,63}$/; +// A path segment the scan quotes; anything else becomes "*" (the hit is still reported). +const SEGMENT = /^[A-Za-z0-9_-]{1,64}$/; +const MAX_DEPTH = 32; + +const isClean = (assertClean, value) => { + try { + assertClean(value); + return true; + } catch { + return false; + } +}; + +// The first place in `entry` whose text the store holds, as a path ("instructions", +// "personality.traits[1]", "role key"), or null when the entry is clean. A hit the walk can't place +// (a value JSON.stringify sees and the walk doesn't) is "(entry)": the check fails closed. +export function heldPath(entry, assertClean) { + if (isClean(assertClean, entry)) return null; + const join = (path, key) => { + const segment = SEGMENT.test(key) && isClean(assertClean, key) ? key : '*'; + return path ? `${path}.${segment}` : segment; + }; + const walk = (value, path, depth) => { + if (typeof value === 'string') return isClean(assertClean, value) ? null : path || '(entry)'; + if (value === null || typeof value !== 'object' || depth > MAX_DEPTH) return null; + if (Array.isArray(value)) { + for (let i = 0; i < value.length; i++) { + const hit = walk(value[i], `${path}[${i}]`, depth + 1); + if (hit) return hit; + } + return null; + } + for (const [key, child] of Object.entries(value)) { + if (!isClean(assertClean, key)) return path ? `${path} key` : 'key'; + const hit = walk(child, join(path, key), depth + 1); + if (hit) return hit; + } + return null; + }; + return walk(entry, '', 0) ?? '(entry)'; +} + +// The refusal for one definition. `name` is the definition's name without "org:". +export function definitionRefusal(name, path) { + const quoted = typeof name === 'string' && NAME.test(name) ? name : '*'; + return new BusError(DEFINITION_HOLDS_CREDENTIAL, `definitions.${quoted} ${path} holds a credential this bus holds`); +} + +// Every entry of a `definitions` map; throws the refusal for the first that holds a credential. +// `prefix` is stripped from each name ("org:" for a business's resolved definitions). +export function scanDefinitions(definitions, assertClean, { prefix = '' } = {}) { + if (definitions === undefined) return; // an organization with no definitions key + if (definitions === null || typeof definitions !== 'object' || Array.isArray(definitions)) { + if (!isClean(assertClean, definitions)) throw new BusError(DEFINITION_HOLDS_CREDENTIAL, 'definitions holds a credential this bus holds'); + return; + } + for (const [key, entry] of Object.entries(definitions)) { + if (prefix && !key.startsWith(prefix)) continue; + const name = key.slice(prefix.length); + if (!isClean(assertClean, key)) throw definitionRefusal('*', 'key'); + const path = heldPath(entry, assertClean); + if (path !== null) throw definitionRefusal(name, path); + } +} diff --git a/packages/bus/src/process.mjs b/packages/bus/src/process.mjs index 1f181d337..a7e6949ac 100644 --- a/packages/bus/src/process.mjs +++ b/packages/bus/src/process.mjs @@ -2,6 +2,7 @@ // never command arguments, stdout or service-token-bearing environment variables. import { startBroker } from './runtime.mjs'; import { BusError } from './broker.mjs'; +import { DEFINITION_HOLDS_CREDENTIAL } from './definition-scan.mjs'; const LAUNCH_OPS = new Set([ 'identity', 'authorizeLaunch', @@ -9,6 +10,7 @@ const LAUNCH_OPS = new Set([ 'endLaunch', 'recoverLaunch', 'credentialStatus', + 'definitionScan', 'modelPreflight', 'modelAdmission', 'modelStall', @@ -90,6 +92,8 @@ function launchOp(m) { if (m.op === 'request.chief') return runtime.requestChief(m.business); if (m.op === 'request.admission') return runtime.admission({ business: m.business, project: m.project, projects: m.projects, instance: m.instance, revision: m.revision, thread: m.thread, session: m.session }); if (m.op === 'credentialStatus') return runtime.credentialStatus(m.business, m.role); + // #1873 T1 follow-up (org-defscan): the launcher's held-secret check of an org: definition. + if (m.op === 'definitionScan') return runtime.definitionScan({ business: m.business, definition: m.definition, entry: m.entry }); if (m.op === 'modelAdmission') return runtime.modelAdmission(m); if (m.op === 'modelPreflight') return runtime.modelPreflight(m); if (m.op === 'modelStall') return runtime.modelStall(m); @@ -163,7 +167,9 @@ if (!process.send) { } process.send({ ok: true, path: runtime.path, launches: runtime.launches, readers: runtime.readers }); } catch (e) { - process.send?.(tag(message, { ok: false, error: e instanceof BusError ? e.code : 'startup-refused' }), () => + // org-defscan: a held credential in a definition says which definition and key, never the text. + const reason = e instanceof BusError && e.code === DEFINITION_HOLDS_CREDENTIAL && typeof e.reason === 'string' ? { reason: e.reason } : {}; + process.send?.(tag(message, { ok: false, error: e instanceof BusError ? e.code : 'startup-refused', ...reason }), () => close(2), ); } diff --git a/packages/bus/src/refusals.mjs b/packages/bus/src/refusals.mjs index 07c0b45b7..2b9d9860a 100644 --- a/packages/bus/src/refusals.mjs +++ b/packages/bus/src/refusals.mjs @@ -90,6 +90,11 @@ group( "Role configuration and protected instance controls", "Read the permitted configuration/status and ask the owner or configured chief to repair it through the supported path. Do not edit policy to bypass the refusal." ); +group( + "definition-holds-credential", + "R1: no held credential in an organization definition's role text", + "Ask the organization owner to remove the credential from the named definition and key, then restart the bus or relaunch the agent. Do not copy the text anywhere to inspect it." +); group( "field-writer task-already-archived task-archived task-conflict task-create-conflict task-create-uncertain task-not-archived task-not-found task-position-full task-project-refused task-scope-required task-subject-required", "Role task ownership, allowed fields and current task state", diff --git a/packages/bus/src/runtime.mjs b/packages/bus/src/runtime.mjs index d607f68f1..7cf591772 100644 --- a/packages/bus/src/runtime.mjs +++ b/packages/bus/src/runtime.mjs @@ -38,6 +38,8 @@ import { ModelKeys, modelOrganization, stopModelRuns, MODEL_STOP_TIMEOUT_MS } fr import { instanceSettings } from '../../organization/src/instance-settings.mjs'; import { organizationWriter, OrganizationError, setReplyLimits } from '../../organization/src/organization.mjs'; import { definitionVerbs, DEFINITION_BUS_VERBS } from '../../organization/src/definition-verbs.mjs'; +import { ORG_PREFIX } from '../../organization/src/definitions.mjs'; +import { heldPath, scanDefinitions } from './definition-scan.mjs'; import { ROLE_RIGHTS } from '../../organization/src/org-rights.mjs'; import { getProjectRemote } from '../../organization/src/project-remotes.mjs'; import { credentialVerbs } from './credential-verbs.mjs'; @@ -387,6 +389,16 @@ export async function startBroker({ mkdirSync(storeRoot, { recursive: true, mode: 0o700 }); credentials = new Credentials({ references, repoRoots: protectedRoots, dataRoot, storeRoot, held, modelAgents: proxy ? Object.entries(businesses).flatMap(([id, b]) => Object.keys(b.roles).map(role => id + '/' + role)) : [] }); + // #1873 T1 follow-up (org-defscan): no organization definition's text holds a credential this store + // holds. The boot copy's org: definitions first, then every entry of the file itself, named or not. + // A hit refuses the start; the refusal names the definition and key, never the text. + const clean = value => credentials.assertClean(value); + for (const [id, b] of Object.entries(businesses)) { + scanDefinitions(b.definitions, clean, { prefix: ORG_PREFIX }); + if (!organization) continue; + const raw = organizationWriter({ dir: organization.dir, rolesDir: organization.rolesDir, dataRoot: instanceRoot ?? dataRoot }).readRaw(id); + if (raw.readable) scanDefinitions(raw.document.definitions, clean); + } if (proxy) { const inventory = modelRunInventory(dataRoot, modelRuns, credentials); modelKeys = new ModelKeys({ credentials, client: modelProxyClient, configDir: org ? instanceConfigDir : organization.dir, org: org?.id ?? null, lock: proxyLock, @@ -1148,6 +1160,20 @@ export async function startBroker({ .filter((c) => c.instance === business + '/' + role) .map(({ service, date, state, keyId, issuedAt }) => ({ service, date, state, ...(service === 'model' ? { keyId, issuedAt } : {}) })); } + // #1873 T1 follow-up (org-defscan): the launcher's check of the org: definition it is about to + // launch, as it read it from the organization file. Answers where a held credential sits, or null; + // never the text. A malformed ask refuses, so the launch refuses. + function definitionScan({ business, definition, entry } = {}) { + if (typeof business !== 'string' || !Object.hasOwn(businesses, business) || typeof definition !== 'string' + || !definition.startsWith(ORG_PREFIX) || definition.length > 128 || entry === null || typeof entry !== 'object' || Array.isArray(entry)) + throw new BusError('invalid-request'); + let size; + try { size = JSON.stringify(entry).length; } catch { throw new BusError('invalid-request'); } + if (size > 4 * 1024 * 1024) throw new BusError('invalid-request'); + const clean = value => credentials.assertClean(value); + try { clean(definition); } catch { return { held: 'name' }; } + return { held: heldPath(entry, clean) }; + } // C2-03a: each library entry with a sync user fetches once at start, then every 60 s. A pass // still running skips the tick. Only codes are logged, never git's output. const timed = Object.values(businesses).flatMap(business => Object.entries(business.projects ?? {}) @@ -1321,6 +1347,7 @@ export async function startBroker({ sessionClearStart: (r) => broker.sessionClearStart(r), refuse: (cap, code, subject) => broker.refuse(cap, code, subject), credentialStatus, + definitionScan, launches: bound, readers: readCaps, async close() { diff --git a/packages/bus/tests/definition-scan.test.mjs b/packages/bus/tests/definition-scan.test.mjs new file mode 100644 index 000000000..8e025135d --- /dev/null +++ b/packages/bus/tests/definition-scan.test.mjs @@ -0,0 +1,125 @@ +// #1873 T1 follow-up (org-defscan): bus start runs the live credential store's held-secret check on +// every organization definition, and the launch check asks the bus the same question. A hit names +// the definition and the key, never the text there or the credential's name. +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { readdirSync, readFileSync, writeFileSync } from 'node:fs'; +import { join } from 'node:path'; +import { BusError } from '../src/broker.mjs'; +import { REFUSALS } from '../src/refusals.mjs'; +import { DEFINITION_HOLDS_CREDENTIAL, heldPath, scanDefinitions } from '../src/definition-scan.mjs'; +import { startBroker } from '../src/runtime.mjs'; +import { bootConfig, loadSystem } from '../../cli/src/config.mjs'; +import { fixture, tmp } from '../../cli/tests/helpers.mjs'; +import { entryFromTemplate, readTemplate } from '../../organization/src/definitions.mjs'; +import { REPO_ROLES, writeJson } from '../../organization/tests/helpers.mjs'; + +// The fixture bus holds SENTINEL as coder's gitea token. +const SENTINEL = 'sk-test-NOT-A-SECRET-1234'; +const UNHELD = 'sk-test-UNHELD-LOOKS-LIKE-A-TOKEN-0000'; + +function files(dir) { + return readdirSync(dir, { withFileTypes: true }).flatMap((e) => e.isDirectory() ? files(join(dir, e.name)) : e.isFile() ? [join(dir, e.name)] : []); +} + +// An organization whose coder names org:builder, a copy of the coder template; `edit` changes the +// builder entry before the file is written. +function org(t, edit = (entry) => entry) { + const root = tmp(t, 'defscan-'); + const builder = edit(entryFromTemplate(readTemplate(REPO_ROLES, 'coder'))); + const f = fixture(root, 'acme', (doc) => ({ ...doc, definitions: { builder }, roles: { ...doc.roles, coder: { ...doc.roles.coder, definition: 'org:builder' } } })); + writeFileSync(f.doc.roles.coder.credentials.gitea.file, SENTINEL); + const file = join(root, 'config/businesses/acme.json'); + const boot = () => bootConfig({ system: loadSystem({ env: f.env }), businessId: 'acme', env: f.env }); + const start = (b = boot()) => startBroker({ dataRoot: f.dataRoot, businesses: b.businesses, organization: b.organization }); + return { root, f, file, boot, start }; +} + +function refusedWithout(e, reason) { + assert.ok(e instanceof BusError, String(e)); + assert.equal(e.code, DEFINITION_HOLDS_CREDENTIAL); + assert.equal(e.reason, reason); + for (const text of [e.message, e.reason, String(e.stack), JSON.stringify(e)]) { + assert.ok(!text.includes(SENTINEL), 'the refusal never carries the value'); + assert.ok(!/gitea/.test(text), 'the refusal never names the credential'); + } + return true; +} + +// The start's outcome: the refusal, or "started" after closing a bus that should not have started. +const outcome = (start) => start.then(async (bus) => { await bus.close(); return 'started'; }, (e) => e); + +function nothingWritten(root) { + for (const path of files(root)) { + if (path.endsWith('.token') || path.endsWith('acme.json')) continue; + assert.ok(!readFileSync(path).includes(SENTINEL), path); + } +} + +test('org-defscan: the refusal code is in the TI refusal table with a next step', () => { + const entry = REFUSALS[DEFINITION_HOLDS_CREDENTIAL]; + assert.ok(entry, 'definition-holds-credential is a known refusal'); + assert.match(entry.rail, /^R\d+:/); + assert.match(entry.nextStep, /definition/); +}); + +test('org-defscan: a held credential in a definition\'s instructions refuses bus start, naming definition and key only', async (t) => { + const o = org(t, (entry) => ({ ...entry, instructions: `${entry.instructions}\nThe forge token is ${SENTINEL}.\n` })); + refusedWithout(await outcome(o.start()), 'definitions.builder instructions holds a credential this bus holds'); + nothingWritten(o.root); +}); + +test('org-defscan: a held credential in a personality field refuses bus start the same way', async (t) => { + const o = org(t, (entry) => ({ ...entry, personality: { summary: `Careful. Key ${SENTINEL}.`, traits: ['plain'] } })); + refusedWithout(await outcome(o.start()), 'definitions.builder personality.summary holds a credential this bus holds'); + const p = org(t, (entry) => ({ ...entry, personality: { summary: 'Careful.', traits: ['plain', `x${SENTINEL}`] } })); + refusedWithout(await outcome(p.start()), 'definitions.builder personality.traits[1] holds a credential this bus holds'); + nothingWritten(o.root); + nothingWritten(p.root); +}); + +test('org-defscan: a definition in the file but not in the boot copy is checked too', async (t) => { + const o = org(t); + const boot = o.boot(); + // Written after the load and before the bus starts: no instance names it, so only the raw read sees it. + const doc = JSON.parse(readFileSync(o.file, 'utf8')); + doc.definitions.spare = { ...doc.definitions.builder, instructions: `Spare: ${SENTINEL}\n` }; + writeJson(o.file, doc); + refusedWithout(await outcome(o.start(boot)), 'definitions.spare instructions holds a credential this bus holds'); +}); + +test('org-defscan: a clean file starts, and token-looking text the store does not hold starts', async (t) => { + for (const edit of [(entry) => entry, (entry) => ({ ...entry, instructions: `${entry.instructions}\nExample: ${UNHELD}\n`, personality: { summary: `Says ${UNHELD}.` } })]) { + const o = org(t, edit); + const bus = await o.start(); + try { + // The launch check's question: a clean entry answers null, a held one names the key only. + const entry = o.boot().businesses.acme.definitions['org:builder']; + assert.deepEqual(bus.definitionScan({ business: 'acme', definition: 'org:builder', entry }), { held: null }); + const hit = bus.definitionScan({ business: 'acme', definition: 'org:builder', entry: { ...entry, instructions: `x ${SENTINEL}` } }); + assert.deepEqual(hit, { held: 'instructions' }); + // A malformed question refuses rather than answering clean. + for (const bad of [{}, { business: 'acme', definition: 'builder', entry }, { business: 'other', definition: 'org:builder', entry }, + { business: 'acme', definition: 'org:builder', entry: null }, { business: 'acme', definition: 'org:builder', entry: [entry] }]) + assert.throws(() => bus.definitionScan(bad), (e) => e.code === 'invalid-request'); + } finally { + await bus.close(); + } + } +}); + +test('org-defscan: heldPath names the first place, a key by its parent, and never the text', () => { + const held = 'HELD-VALUE-77'; + const assertClean = (value) => { if ((JSON.stringify(value) ?? '').includes(held)) throw new BusError('credential-leak'); }; + assert.equal(heldPath({ instructions: 'fine', personality: { traits: ['a', 'b'] } }, assertClean), null); + assert.equal(heldPath({ instructions: 'fine', personality: { traits: ['a', `b${held}`] } }, assertClean), 'personality.traits[1]'); + assert.equal(heldPath({ role: { tools: [held] } }, assertClean), 'role.tools[0]'); + assert.equal(heldPath({ personality: { [held]: 'x' } }, assertClean), 'personality key'); + assert.equal(heldPath({ [`${held}`]: 'x' }, assertClean), 'key'); + assert.throws(() => scanDefinitions({ 'org:a': { instructions: 'ok' }, 'org:b': { instructions: held } }, assertClean, { prefix: 'org:' }), + (e) => e.code === DEFINITION_HOLDS_CREDENTIAL && e.reason === 'definitions.b instructions holds a credential this bus holds'); + // A name outside the loader's pattern is not quoted. + assert.throws(() => scanDefinitions({ 'Bad Name': { instructions: held } }, assertClean), (e) => e.reason === 'definitions.* instructions holds a credential this bus holds'); + assert.throws(() => scanDefinitions({ [held]: { instructions: 'ok' } }, assertClean), (e) => e.reason === 'definitions.* key holds a credential this bus holds' && !e.reason.includes(held)); + assert.doesNotThrow(() => scanDefinitions(undefined, assertClean)); +}); diff --git a/packages/cli/src/boot-status.mjs b/packages/cli/src/boot-status.mjs index 67cdf40d6..97931a0e5 100644 --- a/packages/cli/src/boot-status.mjs +++ b/packages/cli/src/boot-status.mjs @@ -37,9 +37,10 @@ function write(dataRoot, status, event) { appendFileSync(bootEventsFile(dataRoot), `${JSON.stringify(event)}\n`, { mode: 0o600 }); } -// A start refused: keeps the time of the last good load from the status it replaces. -export function recordBootRefused(dataRoot, { business, file, reason, at = new Date().toISOString() }) { - const prior = readBootStatus(dataRoot); +// A start refused: keeps the time of the last good load from the status it replaces. `prior` is the +// status from before this start, for a refusal that comes after the start recorded its load (org-defscan: +// the broker's held-secret check on the definitions). +export function recordBootRefused(dataRoot, { business, file, reason, at = new Date().toISOString(), prior = readBootStatus(dataRoot) }) { const lastGood = prior?.ok ? prior.at : prior?.lastGood ?? null; const text = String(reason).slice(0, REASON_MAX); write(dataRoot, { ok: false, at, business, file, reason: text, lastGood }, { event: "bus.boot.refused", at, business, file, reason: text }); diff --git a/packages/cli/src/cli.mjs b/packages/cli/src/cli.mjs index 0d314951c..314ff713f 100644 --- a/packages/cli/src/cli.mjs +++ b/packages/cli/src/cli.mjs @@ -651,9 +651,14 @@ async function bus(rest, io, deps) { throw e; } if (instance) boot.consoleOwners = [...(boot.consoleOwners ?? []), DEV_OWNER]; + const priorBoot = readBootStatus(org.root); try { recordBootOk(org.root, { business: businessId, file }); } catch { /* status is advisory */ } const binding = readNotifyConfig(org.root, businessId); - const host = await startHost({ boot, business: businessId, notifier: binding ? { binding } : null, refuseStale: true, options: { devLocalOwner: Boolean(instance), pm: parsed.flags.has("--pm"), noLaunch: !launches } }); + const host = await startHost({ boot, business: businessId, notifier: binding ? { binding } : null, refuseStale: true, options: { devLocalOwner: Boolean(instance), pm: parsed.flags.has("--pm"), noLaunch: !launches } }).catch((e) => { + // org-defscan: the broker refused a definition that holds a credential it holds; the load was not good after all. + if (e.code === "definition-holds-credential") try { recordBootRefused(org.root, { business: businessId, file, reason: e.message, prior: priorBoot }); } catch { /* the refusal still throws */ } + throw e; + }); try { // A dev fixture serves only the organizations that use it; their launches carry no provider credential. const orgDoc = instance ? JSON.parse(readFileSync(file, "utf8")) : undefined; diff --git a/packages/cli/src/host.mjs b/packages/cli/src/host.mjs index 62bc7d3be..8286713af 100644 --- a/packages/cli/src/host.mjs +++ b/packages/cli/src/host.mjs @@ -228,7 +228,10 @@ export async function startHost({ boot, business, notifier = null, options = nul if (ready?.ok !== true) { await ended(broker, 5000); release(); - throw new CliError(`broker refused to start: ${typeof ready?.error === "string" ? ready.error : "startup-refused"}`, 3); + const code = typeof ready?.error === "string" ? ready.error : "startup-refused"; + // org-defscan: the broker's reason names the definition and key that hold a credential, never the text. + const why = code === "definition-holds-credential" && typeof ready?.reason === "string" ? `: ${ready.reason.slice(0, 300)}` : ""; + throw Object.assign(new CliError(`broker refused to start: ${code}${why}`, 3), code === "definition-holds-credential" ? { code } : {}); } // Every step from here to host.json shares one failure path: it stops the diff --git a/packages/cli/src/launcher.mjs b/packages/cli/src/launcher.mjs index 7b82d65ee..78c04091c 100644 --- a/packages/cli/src/launcher.mjs +++ b/packages/cli/src/launcher.mjs @@ -413,6 +413,12 @@ export function createLauncher({ const primary = routeOf("primary"); const fallback = hasFallback(resolved) ? routeOf("fallback") : null; if (!resolved.contract && typeof resolved.instructions !== "string") throw new Refusal("no-role-contract", `role ${resolved.definition} has no contract file`); + // org-defscan (#1873): an org: definition, as this check just read it, passes the bus's held-secret check before a contract exists. + if (String(resolved.definition).startsWith("org:")) { + const { held } = (await host.op({ op: "definitionScan", business: b.id, definition: resolved.definition, entry: b.definitions?.[resolved.definition] })) ?? {}; + if (typeof held === "string") throw new Refusal("definition-holds-credential", `${instance}'s definition ${resolved.definition} holds a credential in ${held}`); + if (held !== null) throw new Refusal("organization-refused", `the bus did not check ${instance}'s definition ${resolved.definition}`); + } // Row 64 (#1557): every skill on the role's list is enabled and readable before a run exists. try { readSkills(resolved.skills, skillsDir); diff --git a/packages/cli/tests/definition-scan.test.mjs b/packages/cli/tests/definition-scan.test.mjs new file mode 100644 index 000000000..238a0e05c --- /dev/null +++ b/packages/cli/tests/definition-scan.test.mjs @@ -0,0 +1,129 @@ +// #1873 T1 follow-up (org-defscan): a definition that holds a credential the bus holds refuses at +// launch (the launch check asks the bus before a contract exists) and at `mosaic bus start`, and the +// value reaches no refusal, record or log. +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'; +import { join } from 'node:path'; +import { spawnSync } from 'node:child_process'; +import { fileURLToPath } from 'node:url'; +import { createLauncher } from '../src/launcher.mjs'; +import { bootConfig, loadSystem } from '../src/config.mjs'; +import { readBootStatus, bootEventsFile } from '../src/boot-status.mjs'; +import { hostFile } from '../src/host.mjs'; +import { startBroker } from '../../bus/src/runtime.mjs'; +import { initializeControl } from '../../processing-control/src/state.mjs'; +import { launchLogFile } from '../../seat/src/session.mjs'; +import { entryFromTemplate, readTemplate } from '../../organization/src/definitions.mjs'; +import { reserveOrg, activateOrg } from '../../organization/src/org-registry.mjs'; +import { REPO_ROLES, writeJson } from '../../organization/tests/helpers.mjs'; +import { fixture, notifyConfig, tmp, withoutHostCredentials } from './helpers.mjs'; + +// The fixture bus holds SENTINEL as coder's gitea token. +const SENTINEL = 'sk-test-NOT-A-SECRET-1234'; +const CLI = fileURLToPath(new URL('../src/cli.mjs', import.meta.url)); + +function files(dir) { + if (!existsSync(dir)) return []; + return readdirSync(dir, { withFileTypes: true }).flatMap((e) => e.isDirectory() ? files(join(dir, e.name)) : e.isFile() ? [join(dir, e.name)] : []); +} +const withBuilder = (builder) => (doc) => ({ ...doc, definitions: { builder }, roles: { ...doc.roles, coder: { ...doc.roles.coder, definition: 'org:builder' } } }); + +test('org-defscan: a file edited after start to hold a held credential refuses the next launch of an instance naming it; a template launch still works', async (t) => { + const root = tmp(t); + const f = fixture(root, 'acme', withBuilder(entryFromTemplate(readTemplate(REPO_ROLES, 'coder')))); + const file = join(root, 'config/businesses/acme.json'); + writeFileSync(f.doc.roles.coder.credentials.gitea.file, SENTINEL); + withoutHostCredentials(f.env, { piKey: true }); + initializeControl(f.dataRoot); + const boot = bootConfig({ system: loadSystem({ env: f.env }), businessId: 'acme', env: f.env }); + const bus = await startBroker({ dataRoot: f.dataRoot, businesses: boot.businesses, organization: boot.organization }); + t.after(() => bus.close()); + // The real host's op path for the scan, over a structured clone as IPC carries it; the fixture + // stops at authorization, so "reached authorizeLaunch" is "would have launched". + const ops = []; + const host = { business: 'acme', beforeClose: () => {}, op: async (x) => { + ops.push(x.op); + if (x.op === 'identity') return { business: 'acme', role: 'pm' }; + if (x.op === 'mayLaunch') return { allow: true }; + if (x.op === 'authorizeLaunch') throw Object.assign(Error('fixture authorization stop'), { code: 'fixture-policy' }); + if (x.op === 'definitionScan') return structuredClone(bus.definitionScan(structuredClone({ business: x.business, definition: x.definition, entry: x.entry }))); + return {}; + } }; + const launcher = createLauncher({ host, system: JSON.parse(readFileSync(f.config, 'utf8')), env: f.env, bwrap: '/usr/bin/true', log: () => {} }); + t.after(() => launcher.close?.()); + + // Clean: the scan passes and the launch reaches authorization. + assert.equal((await launcher.fromSession('fixture-cap', 'coder')).error, 'fixture-policy'); + assert.ok(ops.includes('definitionScan')); + + // Edited by hand while the bus runs. + const doc = JSON.parse(readFileSync(file, 'utf8')); + doc.definitions.builder.instructions += `\nThe forge token is ${SENTINEL}.\n`; + writeJson(file, doc); + ops.length = 0; + assert.deepEqual(await launcher.fromSession('fixture-cap', 'coder'), { ok: false, error: 'definition-holds-credential' }); + assert.ok(!ops.includes('authorizeLaunch'), 'nothing launches'); + const event = JSON.parse(readFileSync(launchLogFile(f.dataRoot, 'acme'), 'utf8').trim().split('\n').at(-1)); + assert.equal(event.code, 'definition-holds-credential'); + assert.equal(event.instance, 'coder'); + assert.match(event.why, /coder's definition org:builder holds a credential in instructions/); + + // A personality field is checked the same way. + doc.definitions.builder.instructions = doc.definitions.builder.instructions.replace(SENTINEL, 'REDACTED'); + doc.definitions.builder.personality = { summary: 'Plain.', voice: `Signs off with ${SENTINEL}` }; + writeJson(file, doc); + assert.deepEqual(await launcher.fromSession('fixture-cap', 'coder'), { ok: false, error: 'definition-holds-credential' }); + assert.match(JSON.parse(readFileSync(launchLogFile(f.dataRoot, 'acme'), 'utf8').trim().split('\n').at(-1)).why, /in personality\.voice$/); + + // An instance on a shipped template launches as before, and asks no scan. + ops.length = 0; + assert.equal((await launcher.fromSession('fixture-cap', 'reviewer')).error, 'fixture-policy'); + assert.ok(!ops.includes('definitionScan')); + + // The value reached no record the bus or the launcher wrote. + for (const path of files(f.dataRoot)) assert.ok(!readFileSync(path).includes(SENTINEL), path); +}); + +// A bootstrapped instance with an active organization whose file names org:builder. +async function orgFixture(t, builder) { + const root = tmp(t, 'o-'); + const f = fixture(root, 'acme', withBuilder(builder)); + const configDir = join(root, '.config', 'mosaic-boot'); + const env = { ...f.env, HOME: root, MOSAIC_CONFIG: join(configDir, 'config.json') }; + const bootstrap = spawnSync(join(process.cwd(), 'scripts/bootstrap.sh'), ['--instance', 'boot'], { env, encoding: 'utf8' }); + assert.equal(bootstrap.status, 0, bootstrap.stderr); + mkdirSync(join(configDir, 'businesses'), { recursive: true, mode: 0o700 }); + const file = join(configDir, 'businesses', 'acme.json'); + writeFileSync(file, readFileSync(join(root, 'config/businesses/acme.json')), { mode: 0o600 }); + writeFileSync(f.doc.roles.coder.credentials.gitea.file, SENTINEL); + const dataRoot = join(root, '.mosaic-boot'); + const ctx = { configDir, dataRoot, instance: 'boot' }; + const org = await reserveOrg(ctx, 'acme'); + await activateOrg(ctx, 'acme'); + // A notifier config, so a start that passes the load goes on to start the broker. + notifyConfig(org.root, 'acme', null); + return { env, file, dataRoot, orgRoot: org.root }; +} + +test('org-defscan: mosaic bus start refuses a held credential in a definition, records why, and the value is nowhere', async (t) => { + const builder = entryFromTemplate(readTemplate(REPO_ROLES, 'coder')); + const o = await orgFixture(t, { ...builder, instructions: `${builder.instructions}\nThe forge token is ${SENTINEL}.\n` }); + const run = (...args) => spawnSync(process.execPath, [CLI, ...args], { env: o.env, encoding: 'utf8', timeout: 60000 }); + const r = run('bus', 'start', 'acme', '--detach', '--no-launch'); + // Should a start get through (it must not), stop it before the fixture's directory goes. + if (r.status === 0 || existsSync(hostFile(o.orgRoot))) run('bus', 'stop', 'acme'); + assert.equal(r.status, 3, r.stderr); + assert.ok(!existsSync(join(o.orgRoot, 'bus', 'broker.sock')), 'no socket'); + const s = readBootStatus(o.orgRoot); + assert.equal(s.ok, false); + assert.equal(s.business, 'acme'); + assert.match(s.reason, /definition-holds-credential: definitions\.builder instructions holds a credential this bus holds/); + assert.equal(s.lastGood, null, 'the load that the broker then refused is not a good load'); + const events = readFileSync(bootEventsFile(o.orgRoot), 'utf8').trim().split('\n').map(JSON.parse); + assert.equal(events.at(-1).event, 'bus.boot.refused'); + const status = run('bus', 'status', 'acme', '--json'); + assert.equal(JSON.parse(status.stdout).boot.ok, false); + for (const text of [r.stdout, r.stderr, status.stdout, status.stderr]) assert.ok(!text.includes(SENTINEL)); + for (const path of files(o.dataRoot)) assert.ok(!readFileSync(path).includes(SENTINEL), path); +}); diff --git a/packages/cli/tests/org-definitions.test.mjs b/packages/cli/tests/org-definitions.test.mjs index 1f2f6371b..d29c96739 100644 --- a/packages/cli/tests/org-definitions.test.mjs +++ b/packages/cli/tests/org-definitions.test.mjs @@ -21,6 +21,7 @@ const host = { business: 'acme', beforeClose: () => {}, op: async (x) => { if (x.op === 'identity') return { business: 'acme', role: 'pm' }; if (x.op === 'mayLaunch') return { allow: true }; if (x.op === 'authorizeLaunch') throw stop(); + if (x.op === 'definitionScan') return { held: null }; return {}; } };