Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

refactor: allow multiple browser pairings per setup link by removing one-time nonce consumption

0xken1530480464233c
diff --git a/cli/src/cli.ts b/cli/src/cli.ts
index 8948d1664..919e1f726 100644
--- a/cli/src/cli.ts
+++ b/cli/src/cli.ts
@@ -155,7 +155,7 @@ Machine:
harness update update to the latest build now (it also self-updates in the background)
Browser end-to-end encryption:
- harness browser-link print a one-time setup link for this browser
+ harness browser-link print a reusable 7-day setup link for browsers
harness pair <code> pair a BROWSER (code shown on the machine page)
harness pairings list paired clients
harness unpair <#|fp> unpair one browser (by list number or fingerprint)
@@ -2120,7 +2120,7 @@ async function pairCommand(code: string | undefined): Promise<void> {
process.exit(1)
}
-/** `adapter browser-link` — print a one-time browser setup link for the current machine. */
+/** `harness browser-link` — print a reusable 7-day browser setup link for the current machine. */
async function browserLinkCommand(): Promise<void> {
const token = readSavedToken()
if (!token) { console.error('\n ✗ This computer is not joined. Run: harness join <token>\n'); process.exit(1) }
@@ -2140,8 +2140,9 @@ async function browserLinkCommand(): Promise<void> {
const setup = createSetupToken(agentId)
url = setupBrowserLink(agentId, setup.token)
}
- console.log('\n Browser setup link (valid for 7 days):\n')
+ console.log('\n Reusable browser setup link (valid for 7 days):\n')
console.log(` ${url}\n`)
+ console.log(' Anyone with this link can pair a browser until it expires. Keep it private.\n')
if (!readPid()) console.log(' Start the adapter with `harness join` if the browser cannot connect.\n')
process.exit(0)
}
diff --git a/cli/src/hookServer.ts b/cli/src/hookServer.ts
index d9f72c8b6..c5f411d14 100644
--- a/cli/src/hookServer.ts
+++ b/cli/src/hookServer.ts
@@ -304,8 +304,8 @@ export function startHookServer(
return
}
- // `adapter browser-link` → mint a one-time setup token using the running daemon's E2EE manager
- // so the nonce is immediately consumable by this process.
+ // `harness browser-link` → mint a reusable 7-day setup token using the running daemon's E2EE
+ // identity. The signed token is self-contained, so it remains valid across daemon restarts.
if (req.method === 'POST' && url === '/api/e2ee/setup-link') {
if (!localOk) { json(403, { error: 'FORBIDDEN' }); return }
if (!handlers.onSetupLink) { json(503, { error: 'UNAVAILABLE' }); return }
diff --git a/cli/src/lib/e2ee/manager.test.ts b/cli/src/lib/e2ee/manager.test.ts
index c987eca35..805d0469c 100644
--- a/cli/src/lib/e2ee/manager.test.ts
+++ b/cli/src/lib/e2ee/manager.test.ts
@@ -147,7 +147,7 @@ describe('E2eeManager pairing', () => {
expect(h.mgr.hasSession(conn)).toBe(true)
})
- it('setup-link claim rejects token reuse', () => {
+ it('one setup link pairs multiple browsers and establishes an E2EE session for each', () => {
const h = machine()
const web1 = new WebPeer()
const web2 = new WebPeer()
@@ -172,9 +172,17 @@ describe('E2eeManager pairing', () => {
sig: C.b64e(C.setupClaimSig(web2.identity.priv, AGENT, setup.token, web2.identity.pub)),
},
})
- const result = h.lastFor('setup-b', 'e2e_setup_claim_result')!.payload as Record<string, unknown>
- expect(result.error).toBe('UNKNOWN_TOKEN')
- expect(h.mgr.listPaired().length).toBe(1)
+ expect(h.lastFor('setup-a', 'e2e_setup_claim_result')!.payload).toMatchObject({ ok: true })
+ expect(h.lastFor('setup-b', 'e2e_setup_claim_result')!.payload).toMatchObject({ ok: true })
+ expect(h.mgr.listPaired().length).toBe(2)
+
+ const adapterPub = C.b64d(C.verifySetupToken(setup.token)!.payload.pub)
+ h.mgr.handleFrame('setup-a', web1.hello())
+ web1.onWelcome(h.lastFor('setup-a', 'e2e_welcome')!, adapterPub)
+ h.mgr.handleFrame('setup-b', web2.hello())
+ web2.onWelcome(h.lastFor('setup-b', 'e2e_welcome')!, adapterPub)
+ expect(h.mgr.hasSession('setup-a')).toBe(true)
+ expect(h.mgr.hasSession('setup-b')).toBe(true)
})
it('completes a full pairing, pins the browser, and establishes a session + group key', async () => {
diff --git a/cli/src/lib/e2ee/manager.ts b/cli/src/lib/e2ee/manager.ts
index 801eb13b9..fbc6f2b82 100644
--- a/cli/src/lib/e2ee/manager.ts
+++ b/cli/src/lib/e2ee/manager.ts
@@ -411,12 +411,12 @@ export class E2eeManager {
this.deps.sendTo(connId, { type: 'e2e_setup_claim_result', payload: { requestId, error } })
}
if (!token || !identityPub || !sig) { fail('BAD_CLAIM'); return true }
- const consumed = this.store.consumeSetupToken(token, this.deps.machineId)
- if (!consumed.ok) { fail(consumed.error); return true }
+ const validated = this.store.validateSetupToken(token, this.deps.machineId)
+ if (!validated.ok) { fail(validated.error); return true }
const pub = C.b64d(identityPub)
if (!C.setupClaimVerify(pub, this.deps.machineId, token, C.b64d(sig))) { fail('BAD_SIG'); return true }
this.store.addPaired(identityPub, label, this.now(), 'web')
- this.deps.sendTo(connId, { type: 'e2e_setup_claim_result', payload: { requestId, ok: true, fingerprint: consumed.fingerprint } })
+ this.deps.sendTo(connId, { type: 'e2e_setup_claim_result', payload: { requestId, ok: true, fingerprint: validated.fingerprint } })
return true
}
diff --git a/cli/src/lib/e2ee/setup-link.spec.ts b/cli/src/lib/e2ee/setup-link.spec.ts
index b10d029e8..596d00176 100644
--- a/cli/src/lib/e2ee/setup-link.spec.ts
+++ b/cli/src/lib/e2ee/setup-link.spec.ts
@@ -1,5 +1,5 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
-import { mkdtempSync, rmSync } from 'fs'
+import { mkdtempSync, rmSync, writeFileSync } from 'fs'
import { tmpdir } from 'os'
import { join } from 'path'
@@ -26,18 +26,51 @@ async function loadE2ee() {
}
describe('E2EE setup links', () => {
- it('creates one-time setup tokens bound to the adapter identity', async () => {
+ it('keeps signed setup tokens reusable without persisted nonce state', async () => {
const { C, E2eeStore } = await loadE2ee()
const store = new E2eeStore()
store.init()
const setup = store.createSetupToken(AGENT)
expect(C.verifySetupToken(setup.token, Date.now(), AGENT)?.payload.machineId).toBe(AGENT)
- expect(store.consumeSetupToken(setup.token, AGENT)).toMatchObject({ ok: true, fingerprint: setup.fingerprint })
- expect(store.consumeSetupToken(setup.token, AGENT)).toEqual({ ok: false, error: 'UNKNOWN_TOKEN' })
+ expect(store.validateSetupToken(setup.token, AGENT)).toMatchObject({ ok: true, fingerprint: setup.fingerprint })
+ expect(store.validateSetupToken(setup.token, AGENT)).toMatchObject({ ok: true, fingerprint: setup.fingerprint })
+
+ // Simulate a pre-upgrade build that already consumed this token and persisted an empty nonce list.
+ // A fresh post-upgrade store must still accept the signed, unexpired link.
+ writeFileSync(join(dataDir, 'e2e', 'setup-nonces.json'), '[]')
+ const afterUpgrade = new E2eeStore()
+ afterUpgrade.init()
+ expect(afterUpgrade.validateSetupToken(setup.token, AGENT)).toMatchObject({ ok: true, fingerprint: setup.fingerprint })
})
- it('auto-pairs a browser identity from a valid setup claim and rejects replay', async () => {
+ it('keeps every generated link valid until its signed expiry', async () => {
+ const { C, E2eeStore } = await loadE2ee()
+ const store = new E2eeStore()
+ store.init()
+ const first = store.createSetupToken(AGENT)
+ const second = store.createSetupToken(AGENT)
+ const expired = store.createSetupToken(AGENT, -1)
+ const foreign = C.newIdentity()
+ const foreignToken = C.encodeSetupToken({
+ v: 1,
+ typ: 'adapter-e2ee-setup',
+ pub: C.b64e(foreign.pub),
+ nonce: 'foreign-adapter',
+ exp: Date.now() + 60_000,
+ machineId: AGENT,
+ }, foreign.priv)
+ const tampered = `${first.token.slice(0, -1)}${first.token.endsWith('A') ? 'B' : 'A'}`
+
+ expect(store.validateSetupToken(first.token, AGENT).ok).toBe(true)
+ expect(store.validateSetupToken(second.token, AGENT).ok).toBe(true)
+ expect(store.validateSetupToken(expired.token, AGENT)).toEqual({ ok: false, error: 'BAD_TOKEN' })
+ expect(store.validateSetupToken(first.token, '00000000000000000000000000000000')).toEqual({ ok: false, error: 'BAD_TOKEN' })
+ expect(store.validateSetupToken(foreignToken, AGENT)).toEqual({ ok: false, error: 'WRONG_ADAPTER' })
+ expect(store.validateSetupToken(tampered, AGENT)).toEqual({ ok: false, error: 'BAD_TOKEN' })
+ })
+
+ it('auto-pairs multiple browser identities from the same setup link', async () => {
const { C, E2eeManager } = await loadE2ee()
const sent: Array<{ connId: string; frame: Record<string, unknown> }> = []
const mgr = new E2eeManager({
@@ -73,8 +106,12 @@ describe('E2EE setup links', () => {
sig: C.b64e(C.setupClaimSig(web2.priv, AGENT, setup.token, web2.pub)),
},
})
- expect(sent.at(-1)?.frame.payload).toMatchObject({ requestId: 'r2', error: 'UNKNOWN_TOKEN' })
- expect(mgr.listPaired()).toHaveLength(1)
+ expect(sent.at(-1)?.frame.payload).toMatchObject({ requestId: 'r2', ok: true, fingerprint: setup.fingerprint })
+ expect(mgr.listPaired()).toHaveLength(2)
+ expect(mgr.listPaired().map((p) => p.fingerprint)).toEqual(expect.arrayContaining([
+ C.fingerprint(web1.pub),
+ C.fingerprint(web2.pub),
+ ]))
})
})
diff --git a/cli/src/lib/e2ee/store.ts b/cli/src/lib/e2ee/store.ts
index d77462f63..7fb2d82b8 100644
--- a/cli/src/lib/e2ee/store.ts
+++ b/cli/src/lib/e2ee/store.ts
@@ -12,7 +12,6 @@ import { newIdentity, newPairId, b64e, b64d, fingerprint, encodeSetupToken, veri
const DIR = join(env.ADAPTER_DATA_DIR, 'e2e')
const IDENTITY_FILE = join(DIR, 'identity.json')
const PAIRED_FILE = join(DIR, 'paired.json')
-const SETUP_FILE = join(DIR, 'setup-nonces.json')
const SETUP_TTL_MS = 7 * 24 * 60 * 60 * 1000
export interface PairedClient {
@@ -21,12 +20,6 @@ export interface PairedClient {
pairedAt: number
role: PairRole // old records without this field are treated as web
}
-interface PendingSetupNonce {
- nonce: string
- expiresAt: number
- machineId?: string
-}
-
function writeSecure(file: string, data: unknown): void {
mkdirSync(DIR, { recursive: true })
writeFileSync(file, JSON.stringify(data, null, 2), { mode: 0o600 })
@@ -35,7 +28,6 @@ function writeSecure(file: string, data: unknown): void {
export class E2eeStore {
private identity: Identity | null = null
private paired = new Map<string, PairedClient>() // key = identityPub (base64)
- private setupNonces = new Map<string, PendingSetupNonce>() // key = nonce
/** Load or create the computer identity keypair; load the paired set. Idempotent. */
init(): Identity {
@@ -54,13 +46,6 @@ export class E2eeStore {
if (p?.identityPub) this.paired.set(p.identityPub, { ...p, role: p.role === 'device' ? 'device' : 'web' })
}
} catch { /* none yet */ }
- try {
- const arr = JSON.parse(readFileSync(SETUP_FILE, 'utf-8')) as PendingSetupNonce[]
- const now = Date.now()
- for (const p of arr) {
- if (p?.nonce && p.expiresAt > now) this.setupNonces.set(p.nonce, p)
- }
- } catch { /* none yet */ }
return this.identity
}
@@ -109,34 +94,17 @@ export class E2eeStore {
const nonce = b64e(newPairId()).replace(/=+$/g, '')
const expiresAt = Date.now() + ttlMs
const payload = { v: 1 as const, typ: 'adapter-e2ee-setup' as const, pub: b64e(id.pub), nonce, exp: expiresAt, ...(machineId ? { machineId } : {}) }
- this.setupNonces.set(nonce, { nonce, expiresAt, ...(machineId ? { machineId } : {}) })
- this.writeSetupNonces()
return { token: encodeSetupToken(payload, id.priv), expiresAt, fingerprint: this.fingerprint() }
}
- consumeSetupToken(token: string, expectedMachineId?: string): { ok: true; fingerprint: string } | { ok: false; error: 'BAD_TOKEN' | 'WRONG_ADAPTER' | 'UNKNOWN_TOKEN' | 'EXPIRED' | 'WRONG_MACHINE' } {
+ /** Validate a reusable setup link. The signed token is the capability: successful claims never
+ * consume server-side state, so the same unexpired link can pair multiple browser identities —
+ * including a link whose nonce was consumed by an older one-time build before an upgrade. */
+ validateSetupToken(token: string, expectedMachineId?: string): { ok: true; fingerprint: string } | { ok: false; error: 'BAD_TOKEN' | 'WRONG_ADAPTER' } {
const id = this.getIdentity()
const verified = verifySetupToken(token, Date.now(), expectedMachineId)
if (!verified) return { ok: false, error: 'BAD_TOKEN' }
if (b64e(id.pub) !== verified.payload.pub) return { ok: false, error: 'WRONG_ADAPTER' }
- const pending = this.setupNonces.get(verified.payload.nonce)
- if (!pending) return { ok: false, error: 'UNKNOWN_TOKEN' }
- if (pending.expiresAt < Date.now() || verified.payload.exp < Date.now()) {
- this.setupNonces.delete(verified.payload.nonce)
- this.writeSetupNonces()
- return { ok: false, error: 'EXPIRED' }
- }
- if (expectedMachineId && pending.machineId && pending.machineId !== expectedMachineId) return { ok: false, error: 'WRONG_MACHINE' }
- this.setupNonces.delete(verified.payload.nonce)
- this.writeSetupNonces()
return { ok: true, fingerprint: this.fingerprint() }
}
-
- private writeSetupNonces(): void {
- const now = Date.now()
- for (const [nonce, p] of [...this.setupNonces.entries()]) {
- if (p.expiresAt <= now) this.setupNonces.delete(nonce)
- }
- writeSecure(SETUP_FILE, [...this.setupNonces.values()])
- }
}
diff --git a/cli/src/webui.ts b/cli/src/webui.ts
index 7827735e7..9ddc90eef 100644
--- a/cli/src/webui.ts
+++ b/cli/src/webui.ts
@@ -154,7 +154,7 @@ function buildSetupApproveModal(s){
document.getElementById('pending').innerHTML =
'<div class="modal"><div class="modalCard">'
+ '<div class="mTitle">Set up browser encryption?</div>'
- + '<div class="mSub">This will create a 7-day setup link for the Machine browser you just opened. Approve only if you started this flow.</div>'
+ + '<div class="mSub">This creates a reusable 7-day setup link. Anyone with the link can pair a browser until it expires, so keep it private.</div>'
+ '<div class="mCodeLabel">machine</div><div class="mFp"><span class="fp">'+esc((s&&s.fingerprint)||'—')+'</span></div>'
+ '<div class="mCodeLabel">destination</div><div class="mFp">'+esc(machineUrl)+'</div>'
+ '<div class="mBtns"><button class="btn" onclick="closeSetupModal()">Cancel</button><button id="setupApproveBtn" class="btn primary" onclick="approveBrowserSetup()">Approve</button></div>'