Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
security(remote/4d): bind peer key + session into MutualApproval, brand the type
enes73cec3e47af4
diff --git a/src/main/remote/mutual-approval-core.test.ts b/src/main/remote/mutual-approval-core.test.tsindex 4e3ede4aa..07ffc18b6 100644--- a/src/main/remote/mutual-approval-core.test.ts+++ b/src/main/remote/mutual-approval-core.test.ts@@ -36,7 +36,7 @@ describe('mutual SAS agreement', () => {describe('both ends must confirm', () => {it('is not approved until BOTH local and remote confirm', () => {- let s = emptyMutualApproval()+ let s = emptyMutualApproval('peerKey', 'session-1')expect(isMutuallyApproved(s)).toBe(false)s = confirmLocal(s)expect(isMutuallyApproved(s)).toBe(false) // only I confirmed@@ -45,39 +45,68 @@ describe('both ends must confirm', () => {})it('order does not matter (remote-first)', () => {- let s = confirmRemote(emptyMutualApproval())+ let s = confirmRemote(emptyMutualApproval('peerKey', 'session-1'))expect(isMutuallyApproved(s)).toBe(false)s = confirmLocal(s)expect(isMutuallyApproved(s)).toBe(true)})it('confirming the SAME side twice never approves (no self-satisfying handshake)', () => {- const localTwice = confirmLocal(confirmLocal(emptyMutualApproval()))+ const empty = emptyMutualApproval('peerKey', 'session-1')+ const localTwice = confirmLocal(confirmLocal(empty))expect(isMutuallyApproved(localTwice)).toBe(false)- const remoteTwice = confirmRemote(confirmRemote(emptyMutualApproval()))+ const remoteTwice = confirmRemote(confirmRemote(empty))expect(isMutuallyApproved(remoteTwice)).toBe(false)})})describe('pin-both-ends', () => {it('recordApproval pins the peer key only once mutually approved', () => {- const half = confirmLocal(emptyMutualApproval())- const notYet = recordApproval({ pubkeys: [] }, 'peerKey', half)+ const half = confirmLocal(emptyMutualApproval('peerKey', 'session-1'))+ const notYet = recordApproval({ pubkeys: [] }, half)expect(isPinned(notYet, 'peerKey')).toBe(false) // half-confirmed → no pinconst full = confirmRemote(half)- const pinned = recordApproval({ pubkeys: [] }, 'peerKey', full)+ const pinned = recordApproval({ pubkeys: [] }, full)expect(isPinned(pinned, 'peerKey')).toBe(true)})it('both ends end up pinning the OTHER (symmetric)', () => {const hostPub = 'HOST_PUB'const clientPub = 'CLIENT_PUB'- const done = confirmRemote(confirmLocal(emptyMutualApproval()))- // Host pins the client; client pins the host — each against its own store.- const hostStore = recordApproval({ pubkeys: [] }, clientPub, done)- const clientStore = recordApproval({ pubkeys: [] }, hostPub, done)+ // Host's state is bound to the CLIENT's key; the client's state is bound to the HOST's key.+ const hostSide = confirmRemote(confirmLocal(emptyMutualApproval(clientPub, 'session-1')))+ const clientSide = confirmRemote(confirmLocal(emptyMutualApproval(hostPub, 'session-1')))+ // Each pins the key carried by its own state, against its own store.+ const hostStore = recordApproval({ pubkeys: [] }, hostSide)+ const clientStore = recordApproval({ pubkeys: [] }, clientSide)expect(isPinned(hostStore, clientPub)).toBe(true)expect(isPinned(clientStore, hostPub)).toBe(true)})++ // Misuse vector #2 (pin-the-wrong-key): the key pinned is the one BOUND INTO the state at+ // construction, and there is no other key argument, so a state bound to key A can never pin key B —+ // no matter what other keys are in play.+ it('recordApproval pins the key CARRIED BY the state, never any other key', () => {+ const keyA = 'KEY_A'+ const keyB = 'KEY_B'+ const approved = confirmRemote(confirmLocal(emptyMutualApproval(keyA, 'session-1')))+ const store = recordApproval({ pubkeys: [] }, approved)+ expect(isPinned(store, keyA)).toBe(true) // the bound key is pinned+ expect(isPinned(store, keyB)).toBe(false) // a different key can never be pinned+ expect(store.pubkeys).toEqual([keyA]) // and NOTHING else+ })++ // Branded-type tripwire: a hand-forged approval struct must NOT typecheck. Without the `__brand`+ // on MutualApproval this line would compile cleanly and the @ts-expect-error would itself become an+ // error ("unused '@ts-expect-error'"), failing `npm run typecheck` — which is exactly how this+ // proves the brand is load-bearing. Guarded by a false condition so it never runs.+ it('a hand-forged approval struct is rejected by the compiler', () => {+ if (false as boolean) {+ const forged = { localConfirmed: true, remoteConfirmed: true, peerKeyB64: 'evil', sessionId: 'x' }+ // @ts-expect-error — a plain object is not a MutualApproval; only emptyMutualApproval() can mint one.+ recordApproval({ pubkeys: [] }, forged)+ }+ expect(true).toBe(true)+ })})diff --git a/src/main/remote/mutual-approval-core.ts b/src/main/remote/mutual-approval-core.tsindex ba1984d18..ece719178 100644--- a/src/main/remote/mutual-approval-core.ts+++ b/src/main/remote/mutual-approval-core.ts@@ -8,26 +8,66 @@// EACH end independently pins the other's stable box public key. A man-in-the-middle on the relay// terminates two DIFFERENT ECDH exchanges, so the two humans read out different digits and refuse —// which is exactly why neither confirmation alone may unlock the session.+//+// SECURITY — this module is a correct latch ONLY if 4c (host-service / client-service) honours two+// obligations it cannot express in this pure file. Both are security-FATAL if broken, because pinning+// a key grants the peer SHELL ACCESS on this machine:+//+// (a) `confirmRemote` MUST be driven ONLY by a frame received over the ENCRYPTED, session-keyed+// channel (the box under deriveSessionKey), never from a plaintext/relay-visible message. The+// relay is untrusted: if the peer's "I confirmed" signal can be forged or replayed by a relay+// MITM, mutual approval silently degrades back to ONE-WAY — the attacker supplies the remote+// confirmation the second human never gave, and a single local confirm then pins the attacker.+// Only a frame that decrypts under THIS session's key proves the real peer sent it.+//+// (b) There must be EXACTLY ONE `MutualApproval` per pairing attempt, created via+// `emptyMutualApproval(peerKeyB64, sessionId)` from THIS session's ECDH peer key (the same key+// whose shared secret produced the SAS the humans compared). Reusing a state across attempts,+// or seeding it with a key other than this session's, re-opens the "confirm-for-a-different-+// session" / "pin-the-wrong-key" holes this module's binding was built to close.+//+// This file makes the SAFE usage the ONLY compilable usage — the peer key + session identity are+// bound INTO the state at construction and the branded type has no other constructor — but it cannot+// reach across the seam to enforce (a) and (b). 4c owns those.import { sasFromSharedKey } from './e2ee'import { pinDevice, type ApprovedDevices } from './approved-devices-core'-export interface MutualApproval {+/**+ * Approval state for ONE pairing attempt, BOUND at creation to the peer + session it approves.+ *+ * Opaque/branded: the `__brand` field has no runtime value and cannot be written by hand, so the+ * ONLY way to obtain a `MutualApproval` is `emptyMutualApproval()`, and the only way to mutate one is+ * `confirmLocal`/`confirmRemote`. A hand-forged `{ localConfirmed: true, remoteConfirmed: true, … }`+ * is a COMPILE ERROR. This turns "safe if every caller is disciplined" into "safe by construction":+ * you cannot fabricate an already-approved state, nor move a confirmation onto a different pairing,+ * because every state carries the one peer key + session it belongs to.+ */+export type MutualApproval = {/** This human confirmed the SAS on THIS device. */- localConfirmed: boolean- /** The peer signalled (over the channel) that its human confirmed too. */- remoteConfirmed: boolean-}+ readonly localConfirmed: boolean+ /** The peer signalled (over the ENCRYPTED, session-keyed channel — see obligation (a)) that its human confirmed too. */+ readonly remoteConfirmed: boolean+ /** The peer's stable box public key (base64) from THIS session's ECDH — the ONLY key this state can ever pin. */+ readonly peerKeyB64: string+ /** Identity of the pairing attempt (session id or shared-key fingerprint) this state is bound to. */+ readonly sessionId: string+} & { readonly __brand: unique symbol }-export function emptyMutualApproval(): MutualApproval {- return { localConfirmed: false, remoteConfirmed: false }+/**+ * The sole constructor. Binds the peer's box public key + the session identity INTO the state, so a+ * confirmation can never be moved to a different pairing and `recordApproval` can only ever pin THIS+ * peer's key. Starts unconfirmed on both sides.+ */+export function emptyMutualApproval(peerKeyB64: string, sessionId: string): MutualApproval {+ return { localConfirmed: false, remoteConfirmed: false, peerKeyB64, sessionId } as MutualApproval}export function confirmLocal(s: MutualApproval): MutualApproval {- return s.localConfirmed ? s : { ...s, localConfirmed: true }+ return s.localConfirmed ? s : ({ ...s, localConfirmed: true } as MutualApproval)}export function confirmRemote(s: MutualApproval): MutualApproval {- return s.remoteConfirmed ? s : { ...s, remoteConfirmed: true }+ return s.remoteConfirmed ? s : ({ ...s, remoteConfirmed: true } as MutualApproval)}/**@@ -47,14 +87,12 @@ export function mutualSas(shared: Uint8Array): string {/*** Pin the peer's stable box public key locally — but ONLY once the SAS is mutually confirmed, so a- * half-finished handshake never leaves a pinned identity. Idempotent (pinDevice is). Each end calls- * this against its OWN store with the OTHER end's key, so both ends pin.+ * half-finished handshake never leaves a pinned identity. The key pinned is the one CARRIED BY the+ * state (`s.peerKeyB64`, bound at construction): there is no separate key argument, so a caller cannot+ * pin a key different from the one the two humans confirmed. Idempotent (pinDevice is). Each end calls+ * this against its OWN store; the state each end holds carries the OTHER end's key, so both ends pin.*/-export function recordApproval(- store: ApprovedDevices,- peerKeyB64: string,- s: MutualApproval-): ApprovedDevices {+export function recordApproval(store: ApprovedDevices, s: MutualApproval): ApprovedDevices {if (!isMutuallyApproved(s)) return store- return pinDevice(store, peerKeyB64)+ return pinDevice(store, s.peerKeyB64)}