From 24a9e43cad0412b458fced358349a9b4295396c2 Mon Sep 17 00:00:00 2001 From: Logan Johnson Date: Wed, 16 Sep 2026 16:35:11 -0400 Subject: [PATCH] fix(beehive): verify durable registration completion Signed-off-by: Logan Johnson --- beehive/src/manager-controller.ts | 4 +++- beehive/src/manager-credential-child.ts | 8 +++++-- beehive/src/manager-credential.ts | 6 ++++- beehive/test/manager-credential.test.ts | 31 +++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 beehive/test/manager-credential.test.ts diff --git a/beehive/src/manager-controller.ts b/beehive/src/manager-controller.ts index 657ca14e86..ac8b5f25d2 100644 --- a/beehive/src/manager-controller.ts +++ b/beehive/src/manager-controller.ts @@ -375,7 +375,9 @@ A Stop result is not a recent host report that confirms the agent is stopped.` } } runtime ??= this.saveAgentConfiguration(v); const { profile, profileState } = this.profilePreview; - await this.credential({ action: 'register-agent', directory: this.hostDirectory, secret: v.secret, profile: { profile, profileState }, runtimeId:runtime.id, expectedRevision:readSettings(this.hostDirectory).revision },abort.signal); check(); + await this.credential({ action: 'register-agent', directory: this.hostDirectory, secret: v.secret, publicKey:key, profile: { profile, profileState }, runtimeId:runtime.id, expectedRevision:readSettings(this.hostDirectory).revision },abort.signal); check(); + const committed = readSettings(this.hostDirectory); + if (!committed.agents.some(agent => agent.publicKey === key)) throw plain('Agent registration completion was not verified. Inspect saved registration before retrying.'); this.profilePreview = undefined; if (!pending) this.status = 'Agent registered. Configuration saved.'; else { diff --git a/beehive/src/manager-credential-child.ts b/beehive/src/manager-credential-child.ts index 48a8c9934f..c9e93790bc 100644 --- a/beehive/src/manager-credential-child.ts +++ b/beehive/src/manager-credential-child.ts @@ -26,8 +26,12 @@ process.once('message', async (input: any) => { process.send?.({ ok: true }); } else if (input.action === 'register-agent') { const secret = agentNsec(input.secret); - registerAgent(input.directory, secret, systemCredentials, input.profile, input.runtimeId, input.expectedRevision); - process.send?.({ ok: true }); + const registered = registerAgent(input.directory, secret, systemCredentials, input.profile, input.runtimeId, input.expectedRevision); + const committed = readSettings(input.directory); + if (!committed.agents.some(agent => agent.publicKey === registered.publicKey)) throw Error('Registration commit missing'); + // Public completion receipt only. The controller must not infer durable + // registration from a helper process merely accepting the request. + process.send?.({ ok: true, registration: { publicKey: registered.publicKey, settingsRevision: committed.revision } }); } else if (input.action === 'add-openai') { addOpenAI(input.directory,input.name,input.secret,providerCredentials()); process.send?.({ ok: true }); diff --git a/beehive/src/manager-credential.ts b/beehive/src/manager-credential.ts index f7be96b98e..54a79813a0 100644 --- a/beehive/src/manager-credential.ts +++ b/beehive/src/manager-credential.ts @@ -25,7 +25,11 @@ export function managerCredential(input: object, signal: AbortSignal, helper = n clearTimeout(timer); signal.removeEventListener('abort', cancel); if (request.action === 'signin-buzz' && (code !== 0 || failed || signal.aborted || !result?.ok)) reject(Error(buzzOwnerKeyErrors[code === 0 && !failed && !signal.aborted ? result?.reason : 'access'] ?? buzzOwnerKeyErrors.access)); else if (code !== 0 || failed || signal.aborted || !result?.ok) reject(Error('Could not complete key access. It may have been cancelled, timed out, denied, or unavailable. The key may not match. Changes may already be saved. Inspect before you try again. No key was reset or saved as plain text.')); - else resolve(result); + else if (request.action === 'register-agent' && + (!result.registration || result.registration.publicKey !== request.publicKey || + !Number.isSafeInteger(result.registration.settingsRevision))) { + reject(Error('Agent registration completion was not verified. Inspect saved registration before retrying.')); + } else resolve(result); }); child.send(input, error => { if (error) cancel(); }); }); diff --git a/beehive/test/manager-credential.test.ts b/beehive/test/manager-credential.test.ts new file mode 100644 index 0000000000..7b2e028a76 --- /dev/null +++ b/beehive/test/manager-credential.test.ts @@ -0,0 +1,31 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { mkdtempSync, writeFileSync } from 'node:fs'; +import { join } from 'node:path'; +import { tmpdir } from 'node:os'; +import { pathToFileURL } from 'node:url'; +import { managerCredential } from '../src/manager-credential.ts'; + +const key = 'a'.repeat(64); +function helper(source: string) { + const path = join(mkdtempSync(join(tmpdir(), 'beehive-receipt-')), 'helper.mjs'); + writeFileSync(path, source, { mode: 0o600 }); + return pathToFileURL(path); +} + +test('registration helper completion requires a matching durable public receipt', async () => { + const signal = new AbortController().signal; + await assert.rejects( + managerCredential({ action: 'register-agent', publicKey: key }, signal, + helper("process.once('message',()=>{process.send({ok:true});process.disconnect()})")), + /completion was not verified/, + ); + await assert.rejects( + managerCredential({ action: 'register-agent', publicKey: key }, signal, + helper("process.once('message',()=>{process.send({ok:true,registration:{publicKey:'b'.repeat(64),settingsRevision:2}});process.disconnect()})")), + /completion was not verified/, + ); + const result = await managerCredential({ action: 'register-agent', publicKey: key }, signal, + helper(`process.once('message',()=>{process.send({ok:true,registration:{publicKey:'${key}',settingsRevision:2}});process.disconnect()})`)); + assert.deepEqual(result.registration, { publicKey: key, settingsRevision: 2 }); +});