Skip to content

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.

feat(remote): mutual SAS + pin-both-ends approval state (pure)

enes515709693322
diff --git a/src/main/remote/mutual-approval-core.test.ts b/src/main/remote/mutual-approval-core.test.ts
new file mode 100644
index 000000000..4e3ede4aa
--- /dev/null
+++ b/src/main/remote/mutual-approval-core.test.ts
@@ -0,0 +1,83 @@
+import { describe, it, expect } from 'vitest'
+import {
+ emptyMutualApproval,
+ confirmLocal,
+ confirmRemote,
+ isMutuallyApproved,
+ mutualSas,
+ recordApproval
+} from './mutual-approval-core'
+import { genKeyPair, deriveSharedKey, publicKeyToB64 } from './e2ee'
+import { isPinned } from './approved-devices-core'
+
+describe('mutual SAS agreement', () => {
+ it('both ends derive the SAME 6-digit code from their shared key', () => {
+ const host = genKeyPair()
+ const client = genKeyPair()
+ const hostShared = deriveSharedKey(publicKeyToB64(client.publicKey), host.secretKey)
+ const clientShared = deriveSharedKey(publicKeyToB64(host.publicKey), client.secretKey)
+ const a = mutualSas(hostShared)
+ const b = mutualSas(clientShared)
+ expect(a).toBe(b)
+ expect(a).toMatch(/^\d{3} \d{3}$/)
+ })
+
+ it('a man-in-the-middle (different shared key) shows a DIFFERENT code', () => {
+ const host = genKeyPair()
+ const client = genKeyPair()
+ const mitm = genKeyPair()
+ // Host actually talks to the MITM; the client actually talks to the MITM too.
+ const hostShared = deriveSharedKey(publicKeyToB64(mitm.publicKey), host.secretKey)
+ const clientShared = deriveSharedKey(publicKeyToB64(mitm.publicKey), client.secretKey)
+ // The two humans read out different digits -> they refuse to confirm.
+ expect(mutualSas(hostShared)).not.toBe(mutualSas(clientShared))
+ })
+})
+
+describe('both ends must confirm', () => {
+ it('is not approved until BOTH local and remote confirm', () => {
+ let s = emptyMutualApproval()
+ expect(isMutuallyApproved(s)).toBe(false)
+ s = confirmLocal(s)
+ expect(isMutuallyApproved(s)).toBe(false) // only I confirmed
+ s = confirmRemote(s)
+ expect(isMutuallyApproved(s)).toBe(true) // now both
+ })
+
+ it('order does not matter (remote-first)', () => {
+ let s = confirmRemote(emptyMutualApproval())
+ 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()))
+ expect(isMutuallyApproved(localTwice)).toBe(false)
+ const remoteTwice = confirmRemote(confirmRemote(emptyMutualApproval()))
+ 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)
+ expect(isPinned(notYet, 'peerKey')).toBe(false) // half-confirmed → no pin
+
+ const full = confirmRemote(half)
+ const pinned = recordApproval({ pubkeys: [] }, 'peerKey', 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)
+ expect(isPinned(hostStore, clientPub)).toBe(true)
+ expect(isPinned(clientStore, hostPub)).toBe(true)
+ })
+})
diff --git a/src/main/remote/mutual-approval-core.ts b/src/main/remote/mutual-approval-core.ts
new file mode 100644
index 000000000..ba1984d18
--- /dev/null
+++ b/src/main/remote/mutual-approval-core.ts
@@ -0,0 +1,60 @@
+// Pure state for SYMMETRIC pairing approval: both ends verify the same 6-digit SAS out of band and
+// both pin the other's key. Today approval is one-way (only the host pins the client). This adds the
+// second confirmation direction + the pin-both-ends bookkeeping as pure logic — no sockets, no
+// Electron — so it unit-tests directly and 4c can wire it into either end's handshake.
+//
+// The SAS digits themselves are already identical on both ends (both derive from the same ECDH shared
+// key via sasFromSharedKey); "mutual" means BOTH humans must confirm before the session opens, and
+// 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.
+import { sasFromSharedKey } from './e2ee'
+import { pinDevice, type ApprovedDevices } from './approved-devices-core'
+
+export interface MutualApproval {
+ /** This human confirmed the SAS on THIS device. */
+ localConfirmed: boolean
+ /** The peer signalled (over the channel) that its human confirmed too. */
+ remoteConfirmed: boolean
+}
+
+export function emptyMutualApproval(): MutualApproval {
+ return { localConfirmed: false, remoteConfirmed: false }
+}
+
+export function confirmLocal(s: MutualApproval): MutualApproval {
+ return s.localConfirmed ? s : { ...s, localConfirmed: true }
+}
+
+export function confirmRemote(s: MutualApproval): MutualApproval {
+ return s.remoteConfirmed ? s : { ...s, remoteConfirmed: true }
+}
+
+/**
+ * Approved only when BOTH ends have confirmed the SAS. There is deliberately no other way to reach
+ * this state: the two confirmations live on separate fields, each set by its own one-way transition,
+ * so re-playing one side's confirmation (calling confirmLocal/confirmRemote twice) can never stand in
+ * for the other's.
+ */
+export function isMutuallyApproved(s: MutualApproval): boolean {
+ return s.localConfirmed && s.remoteConfirmed
+}
+
+/** The 6-digit code ("NNN NNN") BOTH ends compute + confirm. Thin alias so 4c reads clearly. */
+export function mutualSas(shared: Uint8Array): string {
+ return sasFromSharedKey(shared)
+}
+
+/**
+ * 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.
+ */
+export function recordApproval(
+ store: ApprovedDevices,
+ peerKeyB64: string,
+ s: MutualApproval
+): ApprovedDevices {
+ if (!isMutuallyApproved(s)) return store
+ return pinDevice(store, peerKeyB64)
+}